adjusted to change in Igtimi API for "latest" URL which now returns data in standard format

This commit is contained in:
Axel Uhl
2014-02-25 13:24:08 +01:00
parent dd584bf782
commit 72a6fee435
4 changed files with 33 additions and 63 deletions
@@ -16,12 +16,11 @@ import com.sap.sailing.domain.igtimiadapter.impl.FixFactory;
public class LatestDatumTest {
private final String sample1 = "{\"latest_data\":[{\"latest_datum\":{\"serial_number\":\"DD-EE-AAGA\",\"resource_id\":1012353,\"c1\":0.5556,\"timestamp\":1384091813000.0}},{\"latest_datum\":{\"serial_number\":\"DD-EE-AAHG\",\"resource_id\":1012356,\"c1\":0.3704,\"timestamp\":1384078160000.0}}]}";
private final String sample2 = "{\"latest_data\":[{\"latest_datum\":{\"serial_number\":\"DD-EE-AAHG\",\"resource_id\":1012356,\"c1\":4.0744,\"timestamp\":1384078157500.0}}]}";
private final String sample1 = "{\"DD-EE-AAGA\":{\"9\":{\"t\":[1.384091813E12],\"resource_id\":1012353,\"1\":[0.5556]}},\"DD-EE-AAHG\":{\"9\":{\"t\":[1.386186319E12],\"resource_id\":1012677,\"1\":[0.0]}}}";
@Test
public void testFixProductionFromSample1() throws ParseException {
Iterable<Fix> fixes = new FixFactory().createFixesFromLastDatum((JSONObject) new JSONParser().parse(sample1), Type.SOG);
Iterable<Fix> fixes = new FixFactory().createFixes((JSONObject) new JSONParser().parse(sample1));
assertEquals(2, Util.size(fixes));
Iterator<Fix> i = fixes.iterator();
Fix fix1 = i.next();
@@ -31,14 +30,4 @@ public class LatestDatumTest {
assertEquals("DD-EE-AAHG", fix2.getSensor().getDeviceSerialNumber());
assertEquals(Type.SOG, fix2.getType());
}
@Test
public void testFixProductionFromSample2() throws ParseException {
Iterable<Fix> fixes = new FixFactory().createFixesFromLastDatum((JSONObject) new JSONParser().parse(sample2), Type.AWS);
assertEquals(1, Util.size(fixes));
Iterator<Fix> i = fixes.iterator();
Fix fix1 = i.next();
assertEquals("DD-EE-AAHG", fix1.getSensor().getDeviceSerialNumber());
assertEquals(Type.AWS, fix1.getType());
}
}
@@ -15,6 +15,7 @@ import org.json.simple.JSONObject;
import com.sap.sailing.domain.common.TimePoint;
import com.sap.sailing.domain.common.impl.MillisecondsTimePoint;
import com.sap.sailing.domain.common.impl.Util;
import com.sap.sailing.domain.igtimiadapter.Sensor;
import com.sap.sailing.domain.igtimiadapter.datatypes.Fix;
import com.sap.sailing.domain.igtimiadapter.datatypes.Type;
@@ -27,60 +28,40 @@ public class FixFactory {
for (Entry<Object, Object> e : sensorsJson.entrySet()) {
String deviceSerialNumber = (String) e.getKey();
JSONObject typesJson = (JSONObject) e.getValue();
for (Entry<Object, Object> fixTypeAndFixesJson : typesJson.entrySet()) {
final String fixTypeAndOptionalSensorId = (String) fixTypeAndFixesJson.getKey();
final String[] fixTypeAndOptionalColonSeparatedSensorsSubId = fixTypeAndOptionalSensorId.split(":");
int fixType = Integer.valueOf(fixTypeAndOptionalColonSeparatedSensorsSubId[0]);
JSONObject fixesJson = (JSONObject) fixTypeAndFixesJson.getValue();
JSONArray timePointsMillis = (JSONArray) fixesJson.get("t");
int fixIndex = 0;
for (Object timePointMillis : timePointsMillis) {
TimePoint timePoint = new MillisecondsTimePoint(((Number) timePointMillis).longValue());
Map<Integer, Object> valuesPerSubindex = new HashMap<>();
int i=1;
JSONArray values;
while ((values=(JSONArray) fixesJson.get(""+i)) != null) {
valuesPerSubindex.put(i, (Number) values.get(fixIndex));
i++;
}
Sensor sensor = new SensorImpl(deviceSerialNumber,
fixTypeAndOptionalColonSeparatedSensorsSubId.length < 2 ? 0
: Long.valueOf(fixTypeAndOptionalColonSeparatedSensorsSubId[1]));
Fix fix = createFix(sensor, Type.valueOf(fixType), timePoint, valuesPerSubindex);
result.add(fix);
fixIndex++;
Util.addAll(createFixesForTypes(deviceSerialNumber, typesJson), result);
}
return result;
}
private Iterable<Fix> createFixesForTypes(String deviceSerialNumber, JSONObject typesJson) {
final List<Fix> result = new ArrayList<>();
for (Entry<Object, Object> fixTypeAndFixesJson : typesJson.entrySet()) {
final String fixTypeAndOptionalSensorId = (String) fixTypeAndFixesJson.getKey();
final String[] fixTypeAndOptionalColonSeparatedSensorsSubId = fixTypeAndOptionalSensorId.split(":");
int fixType = Integer.valueOf(fixTypeAndOptionalColonSeparatedSensorsSubId[0]);
JSONObject fixesJson = (JSONObject) fixTypeAndFixesJson.getValue();
JSONArray timePointsMillis = (JSONArray) fixesJson.get("t");
int fixIndex = 0;
for (Object timePointMillis : timePointsMillis) {
TimePoint timePoint = new MillisecondsTimePoint(((Number) timePointMillis).longValue());
Map<Integer, Object> valuesPerSubindex = new HashMap<>();
int i=1;
JSONArray values;
while ((values=(JSONArray) fixesJson.get(""+i)) != null) {
valuesPerSubindex.put(i, (Number) values.get(fixIndex));
i++;
}
Sensor sensor = new SensorImpl(deviceSerialNumber,
fixTypeAndOptionalColonSeparatedSensorsSubId.length < 2 ? 0
: Long.valueOf(fixTypeAndOptionalColonSeparatedSensorsSubId[1]));
Fix fix = createFix(sensor, Type.valueOf(fixType), timePoint, valuesPerSubindex);
result.add(fix);
fixIndex++;
}
}
return result;
}
/**
* @param lastDataJson expected to use "c1", "c2", ... instead of "1" and "2" for the sensor parameters, and
* "timestamp" instead of "t" for the timestamp. The values are not provided as arrays but as single values.
*/
public Iterable<Fix> createFixesFromLastDatum(JSONObject lastDataJson, Type type) {
List<Fix> result = new ArrayList<>();
JSONArray latestDataArray = (JSONArray) lastDataJson.get("latest_data");
for (Object d : latestDataArray) {
JSONObject latestDatum = (JSONObject) ((JSONObject) d).get("latest_datum");
String serialNumber = (String) latestDatum.get("serial_number");
// there is a field called "resource_id" but we're currently ignoring it because not all fixes have one and we currently
// don't have a use for it
Map<Integer, Object> valuesPerSubindex = new HashMap<>();
int i=1;
Object c_i;
while ((c_i=latestDatum.get("c"+i)) != null) {
valuesPerSubindex.put(i, c_i);
i++;
}
Sensor sensor = new SensorImpl(serialNumber, /* sensor ID */ 0); // Note: the sensor ID isn't obvious from neither the URL nor the response; using 0 as default
TimePoint timestamp = new MillisecondsTimePoint(((Number) latestDatum.get("timestamp")).longValue());
result.add(createFix(sensor, type, timestamp, valuesPerSubindex));
}
return result;
}
private Fix createFix(Sensor sensor, Type fixType, TimePoint timePoint, Map<Integer, Object> valuesPerSubindex) {
try {
Constructor<? extends Fix> constructor = fixType.getFixClass().getConstructor(TimePoint.class, Sensor.class, Map.class);
@@ -113,7 +113,7 @@ public class IgtimiConnectionImpl implements IgtimiConnection {
HttpClient client = connectionFactory.getHttpClient();
HttpGet getLatestData = new HttpGet(connectionFactory.getLatestDatumUrl(deviceSerialNumbers, type, account));
JSONObject latestDataJson = ConnectivityUtils.getJsonFromResponse(client.execute(getLatestData));
return new FixFactory().createFixesFromLastDatum(latestDataJson, type);
return new FixFactory().createFixes(latestDataJson);
}
@Override
@@ -48,7 +48,7 @@
<stringAttribute key="org.eclipse.jdt.launching.CLASSPATH_PROVIDER" value="com.google.gwt.eclipse.core.moduleClasspathProvider"/>
<booleanAttribute key="org.eclipse.jdt.launching.DEFAULT_CLASSPATH" value="false"/>
<stringAttribute key="org.eclipse.jdt.launching.MAIN_TYPE" value="com.google.gwt.dev.DevMode"/>
<stringAttribute key="org.eclipse.jdt.launching.PROGRAM_ARGUMENTS" value="-war &quot;${project_loc:com.sap.sailing.gwt.ui}&quot; -noserver -remoteUI &quot;${gwt_remote_ui_server_port}:${unique_id}&quot; -logLevel INFO -codeServerPort 9997 -startupUrl /gwt/AdminConsole.html com.sap.sailing.gwt.ui.AdminConsole -startupUrl /gwt/LeaderboardEditing.html com.sap.sailing.gwt.ui.LeaderboardEditing -startupUrl /gwt/UserManagement.html com.sap.sailing.gwt.ui.UserManagement -startupUrl /gwt/Leaderboard.html com.sap.sailing.gwt.ui.Leaderboard -startupUrl /gwt/Spectator.html com.sap.sailing.gwt.ui.Spectator -startupUrl /gwt/RaceBoard.html com.sap.sailing.gwt.ui.RaceBoard -startupUrl /gwt/TvView.html com.sap.sailing.gwt.ui.TvView -startupUrl /gwt/PolarSheets.html com.sap.sailing.gwt.ui.PolarSheets -startupUrl /gwt/RegattaOverview.html com.sap.sailing.gwt.ui.RegattaOverview -startupUrl /gwt/Simulator.html com.sap.sailing.gwt.ui.VideoPopup"/>
<stringAttribute key="org.eclipse.jdt.launching.PROGRAM_ARGUMENTS" value="-war &quot;${project_loc:com.sap.sailing.gwt.ui}&quot; -noserver -remoteUI &quot;${gwt_remote_ui_server_port}:${unique_id}&quot; -logLevel INFO -codeServerPort 9997 -startupUrl /gwt/AdminConsole.html com.sap.sailing.gwt.ui.AdminConsole -startupUrl /gwt/LeaderboardEditing.html com.sap.sailing.gwt.ui.LeaderboardEditing -startupUrl /gwt/UserManagement.html com.sap.sailing.gwt.ui.UserManagement -startupUrl /gwt/Leaderboard.html com.sap.sailing.gwt.ui.Leaderboard -startupUrl /gwt/Spectator.html com.sap.sailing.gwt.ui.Spectator -startupUrl /gwt/RaceBoard.html com.sap.sailing.gwt.ui.RaceBoard -startupUrl /gwt/TvView.html com.sap.sailing.gwt.ui.TvView -startupUrl /gwt/PolarSheets.html com.sap.sailing.gwt.ui.PolarSheets -startupUrl /gwt/RegattaOverview.html com.sap.sailing.gwt.ui.VideoPopup"/>
<stringAttribute key="org.eclipse.jdt.launching.PROJECT_ATTR" value="com.sap.sailing.gwt.ui"/>
<stringAttribute key="org.eclipse.jdt.launching.VM_ARGUMENTS" value="-Xmx2048m -XX:MaxPermSize=512m"/>
</launchConfiguration>