/* * Search the field with name fieldName in the list of fields. */ private Field getField(String fieldName, Collection<Field> fields) { for (Iterator<Field> iterator = fields.iterator(); iterator.hasNext(); ) { Field field = (Field) iterator.next(); if (fieldName.equals(field.getName())) return field; } return null; // not found }
/* * Convert a fit field coördinate to a GPX coördinate. It will only convert * fit fields with unit "semicircles". * * dms=semicircles*(180/2^31) */ public static BigDecimal semicircleToDms(Field field) { BigDecimal dms = null; if ((field != null) && ("semicircles".equals(field.getUnits()))) { dms = semicircleToDms(field.getLongValue()); } return dms; // 42.059009616 }
/** * Convert the fit time field to the gpx time format (standard UTC time). Will only convert "s" * seconds. * * @param time * @return */ public static String convertTime(Field time) { String result = null; if ((time != null) && ("s".equals(time.getUnits()))) { DateTime dateTime = new DateTime(time.getLongValue()); result = convertTime(dateTime.getDate()); } return result; }
@Override public void onMesg(Mesg mesg) { if ((mesg != null) && ("lap".equals(mesg.getName()))) { // System.out.println("got here"); } else if ((mesg != null) && ("record".equals(mesg.getName()))) { Telemetry point = new Telemetry(); // Search relevant info Collection<Field> fields = mesg.getFields(); for (Field f : fields) { f.getName(); } Field time = getField("timestamp", fields); Field cadence = getField("cadence", fields); if (cadence != null) { point.setCadence(cadence.getByteValue()); } Field hr = getField("heart_rate", fields); if (hr != null) { point.setHeartRate(hr.getIntegerValue()); } Field posLat = getField("position_lat", fields); Field posLon = getField("position_long", fields); // Convert semicircles latitude to decimals if (posLat != null) { BigDecimal posLatDec = semicircleToDms(posLat); point.setLatitude(posLatDec.doubleValue()); } if (posLon != null) { BigDecimal posLonDec = semicircleToDms(posLon); point.setLongitude(posLonDec.doubleValue()); } Field elevation = getField("altitude", fields); if (elevation != null) { point.setElevation(elevation.getDoubleValue()); } Field distance = getField("distance", fields); if (distance != null) { point.setDistance(distance.getDoubleValue()); } if (time != null) { point.setTime(time.getLongValue() * 1000 + OFFSET); } Field speed = getField("speed", fields); if (speed != null) { point.setSpeed(speed.getDoubleValue() * 3.6); } // use power from file Field power = getField("power", fields); if (power != null) { point.setPower(power.getShortValue()); if (point.getPower() > 0) { isPower = true; // contains power values } } if (last != null) { if (distance == null) { if (posLat == null || posLon == null) { // no latitude or longitude, drop // point return; } // calculate distance from GPS points double d = GPXReader.distance( point.getLatitude(), last.getLatitude(), point.getLongitude(), last.getLongitude(), point.getElevation(), last.getElevation()); if (d > 1000) { // large value, drop. return; } totalDistance += d; point.setDistance(totalDistance); } else if (point.getDistance() == last.getDistance()) { // no change to distance, drop point return; } if (speed == null) { // calculate speed, s = d / t // double speed = d * 3600 // / ((point.getTime() - adjust) - last.getTime()); } double gradient = (point.getElevation() - last.getElevation()) / (point.getDistance() - last.getDistance()); point.setGradient(gAve.add(gradient)); } else { // first time through point.setResistance(WorkoutData.FIT); } last = point; data.add(point); } }