From 18f418e96868669861b2a18eaa38c13b93acfdb2 Mon Sep 17 00:00:00 2001 From: David George Date: Tue, 11 Aug 2015 12:53:15 +0200 Subject: [PATCH] Fix issue #37with power playback and #67 GPX extensions --- .../model/ant/DummySpeedCadenceListener.java | 14 +++++-- src/com/wattzap/utils/GpxImporter.java | 39 ++++++++++++++----- 2 files changed, 39 insertions(+), 14 deletions(-) diff --git a/src/com/wattzap/model/ant/DummySpeedCadenceListener.java b/src/com/wattzap/model/ant/DummySpeedCadenceListener.java index 66b2b41..e8a4b8a 100644 --- a/src/com/wattzap/model/ant/DummySpeedCadenceListener.java +++ b/src/com/wattzap/model/ant/DummySpeedCadenceListener.java @@ -52,7 +52,6 @@ public class DummySpeedCadenceListener extends Thread implements int resistance; Power power; boolean virtualPower; - private double levels = 1; private static Logger logger = LogManager .getLogger("DummySpeedCadenceListener"); @@ -89,6 +88,7 @@ public void run() { // if ant disabled always use this calculation if ((virtualPower || !UserPreferences.INSTANCE.isAntEnabled()) && routeData != null) { + if (routeData.routeType() == RouteReader.SLOPE) { p = routeData.getPoint(distance); @@ -122,11 +122,16 @@ public void run() { // what would be our real speed for those watts - // show // in odo + double realSpeed = power.getRealSpeed(mass, p.getGradient() / 100, powerWatts); speed = (realSpeed * 3600) / 1000; } } else { + // here we are a power file + // TODO For power files we just want to play at normal + // speed. OK we no there is no ANT here. + p = routeData.getPoint(distance); // power comes from video (gradient) // powerWatts = (int) ((p.getGradient()) + @@ -150,15 +155,15 @@ public void run() { t.setVirtualSpeed(bd.intValue()); t.setResistance(UserPreferences.INSTANCE .getResistance()); - // speed = p.getSpeed(); // FIXME what about RLV + if (routeData.getExtension().equals("pwr")) { + speed = p.getSpeed(); + } } } } else { speed = power.getRealSpeed(mass, 0, powerWatts) * 3.6; } - // t.setHeartRate((int) (110 + (powerWatts * 60 / 400))); - // t.setCadence((int) (60 + ((powerWatts * 40 / 300)))); t.setPower(powerWatts); if (routeData != null) { @@ -181,6 +186,7 @@ public void run() { t.setSpeed(speed); t.setDistanceMeters(distance * 1000); t.setTime(System.currentTimeMillis()); + MessageBus.INSTANCE.send(Messages.SPEED, t); // d = s * t diff --git a/src/com/wattzap/utils/GpxImporter.java b/src/com/wattzap/utils/GpxImporter.java index 361c2b9..db2238e 100644 --- a/src/com/wattzap/utils/GpxImporter.java +++ b/src/com/wattzap/utils/GpxImporter.java @@ -12,7 +12,7 @@ * * You should have received a copy of the GNU General Public License * along with Wattzap. If not, see . -*/ + */ package com.wattzap.utils; import java.text.ParseException; @@ -43,7 +43,7 @@ */ public class GpxImporter extends DefaultHandler { State currentState = State.UNDEFINED; - StringBuilder buffer; + StringBuilder buffer = new StringBuilder(); // GPX files have two data formats private static final SimpleDateFormat msdateFormat = new SimpleDateFormat( "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); @@ -69,7 +69,8 @@ public GpxImporter() { currentState = State.UNDEFINED; data = new ArrayList(); distance = 0; - tzOffset = Calendar.getInstance().getTimeZone().getRawOffset() + Calendar.getInstance().getTimeZone().getDSTSavings(); + tzOffset = Calendar.getInstance().getTimeZone().getRawOffset() + + Calendar.getInstance().getTimeZone().getDSTSavings(); } public void startElement(String uri, String name, String qName, @@ -82,12 +83,18 @@ public void startElement(String uri, String name, String qName, point.setLatitude(Double.parseDouble(atts.getValue("lat"))); point.setLongitude(Double.parseDouble(atts.getValue("lon"))); } - } else { + } else if (currentState == State.TRKPT) { + if ("extensions".equalsIgnoreCase(name)) { + currentState = State.EXTENSION; + } + } + if (buffer.length() > 0) { buffer = new StringBuilder(); } } public void endElement(String uri, String name, String qName) { + if (currentState == State.TRKPT) { if ("time".equalsIgnoreCase(name)) { try { @@ -95,21 +102,20 @@ public void endElement(String uri, String name, String qName) { if (t.length() == 20) { // there are two different formats of time stamp Date d = timestampFormatter.parse(t); - point.setTime(d.getTime()+tzOffset); + point.setTime(d.getTime() + tzOffset); } else if (t.length() == 24) { Date d = msdateFormat.parse(t); - point.setTime(d.getTime()+tzOffset); + point.setTime(d.getTime() + tzOffset); } } catch (ParseException e) { logger.error(e + " " + buffer); } - } - if ("ele".equalsIgnoreCase(name)) { + } else if ("ele".equalsIgnoreCase(name)) { point.setElevation(gAve.add(Double.parseDouble(buffer .toString()))); - } else if ("trkpt".equalsIgnoreCase(name)) { + index++; int current = data.size(); if (current > 0) { @@ -173,6 +179,19 @@ public void endElement(String uri, String name, String qName) { data.add(point); currentState = State.UNDEFINED; } + } else if (currentState == State.EXTENSION) { + // System.out.println(name); + if ("extensions".equalsIgnoreCase(name)) { + currentState = State.TRKPT; + } else if ("atemp".equalsIgnoreCase(name)) { + System.out.println("temp " + buffer); + } else if ("hr".equalsIgnoreCase(name)) { + point.setHeartRate(Integer.parseInt(buffer + .toString())); + } else if ("cad".equalsIgnoreCase(name)) { + point.setCadence(Integer.parseInt(buffer + .toString())); + } } } @@ -184,6 +203,6 @@ public void characters(char ch[], int start, int length) { } public enum State { - TRKPT, UNDEFINED + TRKPT, EXTENSION, UNDEFINED } }