Skip to content

Commit

Permalink
Support Jawbone activity distance with decimal
Browse files Browse the repository at this point in the history
Jawbone documentation stated that the "meters" value in workouts was of type int, which was supported by all of the test data we had ever seen. However, it was brought to our attention that the value can contain a number with decimal values, which failed to map using an integer-based json mapper. We switched the mapper to use BigDecimal instead of integer as the parsing type, which handles both integer and double data at high precision.
  • Loading branch information
schaefba committed Oct 16, 2015
1 parent ef61a48 commit 4d806de
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.math.BigDecimal;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
Expand Down Expand Up @@ -83,6 +84,7 @@ public static String asRequiredString(JsonNode parentNode, String path) {
}

// TODO add tests

/**
* @param parentNode a parent node
* @param path the path to a child node
Expand All @@ -108,6 +110,7 @@ public static Long asRequiredLong(JsonNode parentNode, String path) {
}

// TODO add tests

/**
* @param parentNode a parent node
* @param path the path to a child node
Expand Down Expand Up @@ -397,6 +400,17 @@ public static Optional<Integer> asOptionalInteger(JsonNode parentNode, String pa
return asOptionalValue(parentNode, path, JsonNode::isIntegralNumber, JsonNode::intValue);
}

/**
* @param parentNode a parent node
* @param path the path to a child node
* @return the value of the child node as a Big Decimal, or an empty optional if the child doesn't exist or if the
* value of the child node isn't numeric
*/
public static Optional<BigDecimal> asOptionalBigDecimal(JsonNode parentNode, String path) {

return asOptionalValue(parentNode, path, JsonNode::isNumber, JsonNode::decimalValue);
}

/**
* @param parentNode a parent node
* @param path the path to a child node
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
* @author Emerson Farrugia
* @author Danilo Bonilla
* @author Chris Schaefbauer
*
* @see <a href="https://jawbone.com/up/developer/endpoints/workouts">API documentation</a>
*/
public class JawbonePhysicalActivityDataPointMapper extends JawboneDataPointMapper<PhysicalActivity> {
Expand Down Expand Up @@ -100,7 +99,8 @@ protected Optional<PhysicalActivity> getMeasure(JsonNode workoutNode) {

PhysicalActivity.Builder builder = new PhysicalActivity.Builder(activityName);

asOptionalInteger(workoutNode, "details.meters")

asOptionalBigDecimal(workoutNode, "details.meters")
.ifPresent(distance -> builder.setDistance(new LengthUnitValue(METER, distance)));

Optional<Long> endTimestamp = asOptionalLong(workoutNode, "time_completed");
Expand Down Expand Up @@ -152,7 +152,7 @@ public String getActivityName(@Nullable String title, @Nullable Integer workoutT
*/
public SelfReportedIntensity asSelfReportedIntensity(int intensityValue) {

switch (intensityValue) {
switch ( intensityValue ) {
case 1:
return LIGHT;
case 2:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public void asDataPointsShouldReturnCorrectMissingSensedDataPoints() {
List<DataPoint<PhysicalActivity>> dataPoints = mapper.asDataPoints(singletonList(responseNode));

PhysicalActivity expectedPhysicalActivity = new PhysicalActivity.Builder("Bike")
.setDistance(new LengthUnitValue(METER, 1188))
.setDistance(new LengthUnitValue(METER, 6318.2688961))
.setEffectiveTimeFrame(
TimeInterval.ofEndDateTimeAndDuration(OffsetDateTime.parse("2015-04-29T16:07:07-04:00"),
new DurationUnitValue(SECOND, 343)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
"goal": 0,
"intensity": null,
"km": 1.188,
"meters": 1188,
"meters": 6318.2688961,
"place_acc": 0,
"steps": 0,
"time": 343,
Expand Down

0 comments on commit 4d806de

Please sign in to comment.