diff options
author | Anton Tananaev <anton.tananaev@gmail.com> | 2020-03-23 21:13:35 -0700 |
---|---|---|
committer | Anton Tananaev <anton.tananaev@gmail.com> | 2020-03-23 21:13:35 -0700 |
commit | f3d465abe7f255ec44e976caade642e4c2fd7598 (patch) | |
tree | d268a79d63b9d1f27b6e433fedc90de7b427b172 /src/main/java/org/traccar | |
parent | 2816e5ed49e6d237f2a4e48a98cd4a5f1a893246 (diff) | |
download | traccar-server-f3d465abe7f255ec44e976caade642e4c2fd7598.tar.gz traccar-server-f3d465abe7f255ec44e976caade642e4c2fd7598.tar.bz2 traccar-server-f3d465abe7f255ec44e976caade642e4c2fd7598.zip |
Support null values
Diffstat (limited to 'src/main/java/org/traccar')
-rw-r--r-- | src/main/java/org/traccar/protocol/SigfoxProtocolDecoder.java | 70 |
1 files changed, 47 insertions, 23 deletions
diff --git a/src/main/java/org/traccar/protocol/SigfoxProtocolDecoder.java b/src/main/java/org/traccar/protocol/SigfoxProtocolDecoder.java index c2c3d0fc3..5fc81085b 100644 --- a/src/main/java/org/traccar/protocol/SigfoxProtocolDecoder.java +++ b/src/main/java/org/traccar/protocol/SigfoxProtocolDecoder.java @@ -48,6 +48,31 @@ public class SigfoxProtocolDecoder extends BaseHttpProtocolDecoder { super(protocol); } + private boolean jsonContains(JsonObject json, String key) { + if (json.containsKey(key)) { + JsonValue value = json.get(key); + if (value.getValueType() == JsonValue.ValueType.STRING) { + return !((JsonString) value).getString().equals("null"); + + } else { + return true; + } + } + return false; + } + + private boolean getJsonBoolean(JsonObject json, String key) { + JsonValue value = json.get(key); + if (value != null) { + if (value.getValueType() == JsonValue.ValueType.STRING) { + return Boolean.parseBoolean(((JsonString) value).getString()); + } else { + return value.getValueType() == JsonValue.ValueType.TRUE; + } + } + return false; + } + private int getJsonInt(JsonObject json, String key) { JsonValue value = json.get(key); if (value != null) { @@ -99,46 +124,36 @@ public class SigfoxProtocolDecoder extends BaseHttpProtocolDecoder { Position position = new Position(getProtocolName()); position.setDeviceId(deviceSession.getDeviceId()); - if (json.containsKey("time")) { + if (jsonContains(json, "time")) { position.setTime(new Date(getJsonInt(json, "time") * 1000L)); - } else if (json.containsKey("positionTime")) { + } else if (jsonContains(json, "positionTime")) { position.setTime(new Date(getJsonInt(json, "positionTime") * 1000L)); } else { position.setTime(new Date()); } - if (json.containsKey("lastSeen")) { + if (jsonContains(json, "lastSeen")) { position.setDeviceTime(new Date(getJsonInt(json, "lastSeen") * 1000L)); } - if (json.containsKey("location") - || json.containsKey("lat") && json.containsKey("lng") && !json.containsKey("data") - || json.containsKey("latitude") && json.containsKey("longitude") && !json.containsKey("data")) { + if (jsonContains(json, "location") + || jsonContains(json, "lat") && jsonContains(json, "lng") && !jsonContains(json, "data") + || jsonContains(json, "latitude") && jsonContains(json, "longitude") && !jsonContains(json, "data")) { JsonObject location; - if (json.containsKey("location")) { + if (jsonContains(json, "location")) { location = json.getJsonObject("location"); } else { location = json; } position.setValid(true); - position.setLatitude(getJsonDouble(location, location.containsKey("lat") ? "lat" : "latitude")); - position.setLongitude(getJsonDouble(location, location.containsKey("lng") ? "lng" : "longitude")); - - if (location.containsKey("moving")) { - position.set(Position.KEY_MOTION, location.getBoolean("moving")); - } - if (location.containsKey("magStatus")) { - position.set(Position.KEY_BLOCKED, location.getBoolean("magStatus")); - } - if (location.containsKey("temperature")) { - position.set(Position.KEY_DEVICE_TEMP, location.getJsonNumber("temperature").doubleValue()); - } + position.setLatitude(getJsonDouble(location, jsonContains(location, "lat") ? "lat" : "latitude")); + position.setLongitude(getJsonDouble(location, jsonContains(location, "lng") ? "lng" : "longitude")); - } else { + } else if (jsonContains(json, "data") || jsonContains(json, "payload")) { - String data = json.getString(json.containsKey("data") ? "data" : "payload"); + String data = json.getString(jsonContains(json, "data") ? "data" : "payload"); ByteBuf buf = Unpooled.wrappedBuffer(DataConverter.parseHex(data)); try { int event = buf.readUnsignedByte(); @@ -229,10 +244,19 @@ public class SigfoxProtocolDecoder extends BaseHttpProtocolDecoder { getLastLocation(position, position.getDeviceTime()); } - if (json.containsKey("rssi")) { + if (jsonContains(json, "moving")) { + position.set(Position.KEY_MOTION, getJsonBoolean(json, "moving")); + } + if (jsonContains(json, "magStatus")) { + position.set(Position.KEY_BLOCKED, getJsonBoolean(json, "magStatus")); + } + if (jsonContains(json, "temperature")) { + position.set(Position.KEY_DEVICE_TEMP, getJsonDouble(json, "temperature")); + } + if (jsonContains(json, "rssi")) { position.set(Position.KEY_RSSI, getJsonDouble(json, "rssi")); } - if (json.containsKey("seqNumber")) { + if (jsonContains(json, "seqNumber")) { position.set(Position.KEY_INDEX, getJsonInt(json, "seqNumber")); } |