Skip to content

Commit 4d806de

Browse files
committed
Support Jawbone activity distance with decimal
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.
1 parent ef61a48 commit 4d806de

File tree

4 files changed

+19
-5
lines changed

4 files changed

+19
-5
lines changed

java-shim-sdk/src/main/java/org/openmhealth/shim/common/mapper/JsonNodeMappingSupport.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import org.slf4j.Logger;
66
import org.slf4j.LoggerFactory;
77

8+
import java.math.BigDecimal;
89
import java.time.*;
910
import java.time.format.DateTimeFormatter;
1011
import java.time.format.DateTimeParseException;
@@ -83,6 +84,7 @@ public static String asRequiredString(JsonNode parentNode, String path) {
8384
}
8485

8586
// TODO add tests
87+
8688
/**
8789
* @param parentNode a parent node
8890
* @param path the path to a child node
@@ -108,6 +110,7 @@ public static Long asRequiredLong(JsonNode parentNode, String path) {
108110
}
109111

110112
// TODO add tests
113+
111114
/**
112115
* @param parentNode a parent node
113116
* @param path the path to a child node
@@ -397,6 +400,17 @@ public static Optional<Integer> asOptionalInteger(JsonNode parentNode, String pa
397400
return asOptionalValue(parentNode, path, JsonNode::isIntegralNumber, JsonNode::intValue);
398401
}
399402

403+
/**
404+
* @param parentNode a parent node
405+
* @param path the path to a child node
406+
* @return the value of the child node as a Big Decimal, or an empty optional if the child doesn't exist or if the
407+
* value of the child node isn't numeric
408+
*/
409+
public static Optional<BigDecimal> asOptionalBigDecimal(JsonNode parentNode, String path) {
410+
411+
return asOptionalValue(parentNode, path, JsonNode::isNumber, JsonNode::decimalValue);
412+
}
413+
400414
/**
401415
* @param parentNode a parent node
402416
* @param path the path to a child node

shim-server/src/main/java/org/openmhealth/shim/jawbone/mapper/JawbonePhysicalActivityDataPointMapper.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@
4646
* @author Emerson Farrugia
4747
* @author Danilo Bonilla
4848
* @author Chris Schaefbauer
49-
*
5049
* @see <a href="https://jawbone.com/up/developer/endpoints/workouts">API documentation</a>
5150
*/
5251
public class JawbonePhysicalActivityDataPointMapper extends JawboneDataPointMapper<PhysicalActivity> {
@@ -100,7 +99,8 @@ protected Optional<PhysicalActivity> getMeasure(JsonNode workoutNode) {
10099

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

103-
asOptionalInteger(workoutNode, "details.meters")
102+
103+
asOptionalBigDecimal(workoutNode, "details.meters")
104104
.ifPresent(distance -> builder.setDistance(new LengthUnitValue(METER, distance)));
105105

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

155-
switch (intensityValue) {
155+
switch ( intensityValue ) {
156156
case 1:
157157
return LIGHT;
158158
case 2:

shim-server/src/test/java/org/openmhealth/shim/jawbone/mapper/JawbonePhysicalActivityDataPointMapperUnitTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ public void asDataPointsShouldReturnCorrectMissingSensedDataPoints() {
101101
List<DataPoint<PhysicalActivity>> dataPoints = mapper.asDataPoints(singletonList(responseNode));
102102

103103
PhysicalActivity expectedPhysicalActivity = new PhysicalActivity.Builder("Bike")
104-
.setDistance(new LengthUnitValue(METER, 1188))
104+
.setDistance(new LengthUnitValue(METER, 6318.2688961))
105105
.setEffectiveTimeFrame(
106106
TimeInterval.ofEndDateTimeAndDuration(OffsetDateTime.parse("2015-04-29T16:07:07-04:00"),
107107
new DurationUnitValue(SECOND, 343)))

shim-server/src/test/resources/org/openmhealth/shim/jawbone/mapper/jawbone-workouts.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
"goal": 0,
4949
"intensity": null,
5050
"km": 1.188,
51-
"meters": 1188,
51+
"meters": 6318.2688961,
5252
"place_acc": 0,
5353
"steps": 0,
5454
"time": 343,

0 commit comments

Comments
 (0)