aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnton Tananaev <anton.tananaev@gmail.com>2020-03-23 21:13:35 -0700
committerAnton Tananaev <anton.tananaev@gmail.com>2020-03-23 21:13:35 -0700
commitf3d465abe7f255ec44e976caade642e4c2fd7598 (patch)
treed268a79d63b9d1f27b6e433fedc90de7b427b172
parent2816e5ed49e6d237f2a4e48a98cd4a5f1a893246 (diff)
downloadtraccar-server-f3d465abe7f255ec44e976caade642e4c2fd7598.tar.gz
traccar-server-f3d465abe7f255ec44e976caade642e4c2fd7598.tar.bz2
traccar-server-f3d465abe7f255ec44e976caade642e4c2fd7598.zip
Support null values
-rw-r--r--src/main/java/org/traccar/protocol/SigfoxProtocolDecoder.java70
-rw-r--r--src/test/java/org/traccar/protocol/SigfoxProtocolDecoderTest.java6
2 files changed, 53 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"));
}
diff --git a/src/test/java/org/traccar/protocol/SigfoxProtocolDecoderTest.java b/src/test/java/org/traccar/protocol/SigfoxProtocolDecoderTest.java
index 2bf6276b3..0ee34a4fc 100644
--- a/src/test/java/org/traccar/protocol/SigfoxProtocolDecoderTest.java
+++ b/src/test/java/org/traccar/protocol/SigfoxProtocolDecoderTest.java
@@ -12,6 +12,12 @@ public class SigfoxProtocolDecoderTest extends ProtocolTest {
SigfoxProtocolDecoder decoder = new SigfoxProtocolDecoder(null);
+ verifyAttributes(decoder, request(HttpMethod.POST, "/",
+ buffer("{\"messageType\":\"accelerometer\",\"deviceId\":\"testdev001\",\"snr\":\"1234\",\"rssi\":\"-120.00\",\"station\":\"5678\",\"seqNum\":\"9123\",\"newPosition\":false,\"latitude\":\"null\",\"longitude\":\"null\",\"positionTime\":\"null\",\"moving\":true,\"magChange\":\"true\",\"magStatus\":\"true\",\"temperature\":\"7.5\",\"battery\":\"null\",\"batteryPercentage\":\"null\",\"lastSeen\":\"1582560425\",\"fwVersion\":\"null\",\"dlConfig\":\"null\",\"recievedPayload\":\"cb020051\"}")));
+
+ verifyAttributes(decoder, request(HttpMethod.POST, "/",
+ buffer("{\"messageType\":\"downlinkAcknowledgement\",\"deviceId\":\"testdev002\",\"snr\":\"1234\",\"rssi\":\"-120.00\",\"station\":\"5678\",\"seqNum\":\"9123\",\"newPosition\":false,\"latitude\":\"null\",\"longitude\":\"null\",\"positionTime\":\"null\",\"moving\":false,\"magChange\":\"true\",\"magStatus\":\"true\",\"temperature\":\"8.5\",\"battery\":\"3.6\",\"batteryPercentage\":\"100\",\"lastSeen\":\"1582560425\",\"fwVersion\":\"1.15\",\"dlConfig\":\"808c180202140216\",\"recievedPayload\":\"cf808c180202140216b4010f\"}")));
+
verifyPosition(decoder, request(HttpMethod.POST, "/",
buffer("{\"deviceId\":\"3377BC\",\"snr\":\"16.46\",\"rssi\":\"-123.00\",\"station\":\"-123.00\",\"seqNum\":\"3042\",\"newPosition\":true,\"latitude\":51.9189749,\"longitude\":-8.3979322,\"positionTime\":\"1582801850\",\"moving\":false,\"magChange\":false,\"magStatus\":false,\"temperature\":-2,\"battery\":\"null\",\"batteryPercentage\":\"null\",\"lastSeen\":\"1582801850\",\"fwVersion\":\"null\",\"dlConfig\":\"null\",\"recievedPayload\":\"09495a9085f5c94c\"}")));