implement poc

bug5293
This commit is contained in:
Georg Herdt
2020-06-02 19:28:21 +02:00
parent 773c550164
commit 5e78d892c2
3 changed files with 32 additions and 3 deletions
@@ -16,6 +16,7 @@ public class FlatSmartphoneUuidAndGPSFixMovingJsonSerializer {
public static final String LON_DEG = "longitude";
public static final String LAT_DEG = "latitude";
public static final String TIME_MILLIS = "timestamp";
public static final String TIME_ISO = "timestamp-iso";
public static final String SPEED_M_PER_S = "speed";
public static final String BEARING_DEG = "course";
}
@@ -36,7 +36,14 @@ public class FlatSmartphoneUuidAndGPSFixMovingJsonDeserializerTest {
" \"longitude\" : 8.03456,\n" +
" \"speed\" : 5.1,\n" +
" \"course\" : 14.2,\n" +
" }\n" +
" },\n" +
" {\n" +
" \"timestamp-iso\" : \"2004-06-14T23:34:30Z\",\n" +
" \"latitude\" : -67.672456,\n" +
" \"longitude\" : -2.03456,\n" +
" \"speed\" : 19.3,\n" +
" \"course\" : 359.99,\n" +
" }\n" +
" ]\n" +
"}";
JsonDeserializer<Pair<UUID, List<GPSFixMoving>>> deserializer =
@@ -45,6 +52,6 @@ public class FlatSmartphoneUuidAndGPSFixMovingJsonDeserializerTest {
Pair<UUID, List<GPSFixMoving>> result =
deserializer.deserialize(Helpers.toJSONObjectSafe(JSONValue.parseWithException(json)));
assertThat("uuid", result.getA(), equalTo(UUID.fromString("af855a56-9726-4a9c-a77e-da955bd289bf")));
assertThat("number of fixes", result.getB().size(), equalTo(2));
assertThat("number of fixes", result.getB().size(), equalTo(3));
}
}
@@ -1,5 +1,7 @@
package com.sap.sailing.server.gateway.deserialization.impl;
import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
@@ -34,7 +36,7 @@ public class FlatSmartphoneUuidAndGPSFixMovingJsonDeserializer implements
JSONObject fixObject = Helpers.toJSONObjectSafe(jsonFixes.get(i));
double lonDeg = Double.parseDouble(fixObject.get(FlatSmartphoneUuidAndGPSFixMovingJsonSerializer.LON_DEG).toString());
double latDeg = Double.parseDouble(fixObject.get(FlatSmartphoneUuidAndGPSFixMovingJsonSerializer.LAT_DEG).toString());
long timeMillis = Long.parseLong(fixObject.get(FlatSmartphoneUuidAndGPSFixMovingJsonSerializer.TIME_MILLIS).toString());
long timeMillis = deserializeTimestamp(fixObject);
double speedMperS = Double.parseDouble(fixObject.get(FlatSmartphoneUuidAndGPSFixMovingJsonSerializer.SPEED_M_PER_S).toString());
double speedKnots = new MeterPerSecondSpeedImpl(speedMperS).getKnots();
double bearingDeg = Double.parseDouble(fixObject.get(FlatSmartphoneUuidAndGPSFixMovingJsonSerializer.BEARING_DEG).toString());
@@ -44,4 +46,23 @@ public class FlatSmartphoneUuidAndGPSFixMovingJsonDeserializer implements
return new Pair<UUID, List<GPSFixMoving>>(device, fixes);
}
private long deserializeTimestamp(JSONObject fixObject) throws JsonDeserializationException {
long timeMillis;
if (fixObject.get(FlatSmartphoneUuidAndGPSFixMovingJsonSerializer.TIME_MILLIS) != null) {
timeMillis = Long.parseLong(
fixObject.get(FlatSmartphoneUuidAndGPSFixMovingJsonSerializer.TIME_MILLIS).toString());
} else if (fixObject.get(FlatSmartphoneUuidAndGPSFixMovingJsonSerializer.TIME_ISO) != null) {
String strIsoTimestamp = fixObject.get(FlatSmartphoneUuidAndGPSFixMovingJsonSerializer.TIME_ISO)
.toString();
DateTimeFormatter timeFormatter = DateTimeFormatter.ISO_DATE_TIME;
OffsetDateTime offsetDateTime = OffsetDateTime.parse(strIsoTimestamp, timeFormatter);
timeMillis = offsetDateTime.toInstant().toEpochMilli();
} else {
throw new JsonDeserializationException("no timestamp field provided. Please provide one of these fields: "
+ FlatSmartphoneUuidAndGPSFixMovingJsonSerializer.TIME_MILLIS + ", "
+ FlatSmartphoneUuidAndGPSFixMovingJsonSerializer.TIME_ISO);
}
return timeMillis;
}
}