aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAnton Tananaev <anton.tananaev@gmail.com>2019-11-21 20:54:16 -0800
committerAnton Tananaev <anton.tananaev@gmail.com>2019-11-21 20:54:16 -0800
commita87b9f3d9ba75b12d70a1d62640edcf64bd59072 (patch)
treefd4f5394719349bcf6d610920aaa5f311613cb31 /src
parent2bccfa2bff73a5e44503c3fae14c83b21008d339 (diff)
downloadtraccar-server-a87b9f3d9ba75b12d70a1d62640edcf64bd59072.tar.gz
traccar-server-a87b9f3d9ba75b12d70a1d62640edcf64bd59072.tar.bz2
traccar-server-a87b9f3d9ba75b12d70a1d62640edcf64bd59072.zip
More flexible JSON format
Diffstat (limited to 'src')
-rw-r--r--src/main/java/org/traccar/protocol/SigfoxProtocolDecoder.java47
-rw-r--r--src/test/java/org/traccar/protocol/SigfoxProtocolDecoderTest.java3
2 files changed, 43 insertions, 7 deletions
diff --git a/src/main/java/org/traccar/protocol/SigfoxProtocolDecoder.java b/src/main/java/org/traccar/protocol/SigfoxProtocolDecoder.java
index f9c79fb5b..e513edcc2 100644
--- a/src/main/java/org/traccar/protocol/SigfoxProtocolDecoder.java
+++ b/src/main/java/org/traccar/protocol/SigfoxProtocolDecoder.java
@@ -31,7 +31,10 @@ import org.traccar.model.Position;
import org.traccar.model.WifiAccessPoint;
import javax.json.Json;
+import javax.json.JsonNumber;
import javax.json.JsonObject;
+import javax.json.JsonString;
+import javax.json.JsonValue;
import java.io.StringReader;
import java.net.SocketAddress;
import java.net.URLDecoder;
@@ -44,6 +47,30 @@ public class SigfoxProtocolDecoder extends BaseHttpProtocolDecoder {
super(protocol);
}
+ private int getJsonInt(JsonObject json, String key) {
+ JsonValue value = json.get(key);
+ if (value != null) {
+ if (value.getValueType() == JsonValue.ValueType.NUMBER) {
+ return ((JsonNumber) value).intValue();
+ } else if (value.getValueType() == JsonValue.ValueType.STRING) {
+ return Integer.parseInt(((JsonString) value).getString());
+ }
+ }
+ return 0;
+ }
+
+ private double getJsonDouble(JsonObject json, String key) {
+ JsonValue value = json.get(key);
+ if (value != null) {
+ if (value.getValueType() == JsonValue.ValueType.NUMBER) {
+ return ((JsonNumber) value).doubleValue();
+ } else if (value.getValueType() == JsonValue.ValueType.STRING) {
+ return Double.parseDouble(((JsonString) value).getString());
+ }
+ }
+ return 0;
+ }
+
@Override
protected Object decode(
Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
@@ -65,18 +92,24 @@ public class SigfoxProtocolDecoder extends BaseHttpProtocolDecoder {
position.setDeviceId(deviceSession.getDeviceId());
if (json.containsKey("time")) {
- position.setTime(new Date(json.getInt("time") * 1000L));
+ position.setTime(new Date(getJsonInt(json, "time") * 1000L));
} else {
position.setTime(new Date());
}
- if (json.containsKey("location")) {
+ if (json.containsKey("location") || json.containsKey("lat") && json.containsKey("lng")) {
- JsonObject location = json.getJsonObject("location");
+
+ JsonObject location;
+ if (json.containsKey("location")) {
+ location = json.getJsonObject("location");
+ } else {
+ location = json;
+ }
position.setValid(true);
- position.setLatitude(location.getJsonNumber("lat").doubleValue());
- position.setLongitude(location.getJsonNumber("lng").doubleValue());
+ position.setLatitude(getJsonDouble(location, "lat"));
+ position.setLongitude(getJsonDouble(location, "lng"));
} else {
@@ -154,10 +187,10 @@ public class SigfoxProtocolDecoder extends BaseHttpProtocolDecoder {
}
if (json.containsKey("rssi")) {
- position.set(Position.KEY_RSSI, json.getJsonNumber("rssi").doubleValue());
+ position.set(Position.KEY_RSSI, getJsonDouble(json, "rssi"));
}
if (json.containsKey("seqNumber")) {
- position.set(Position.KEY_INDEX, json.getInt("seqNumber"));
+ position.set(Position.KEY_INDEX, getJsonInt(json, "seqNumber"));
}
sendResponse(channel, HttpResponseStatus.OK);
diff --git a/src/test/java/org/traccar/protocol/SigfoxProtocolDecoderTest.java b/src/test/java/org/traccar/protocol/SigfoxProtocolDecoderTest.java
index a2cb021ef..4ab343876 100644
--- a/src/test/java/org/traccar/protocol/SigfoxProtocolDecoderTest.java
+++ b/src/test/java/org/traccar/protocol/SigfoxProtocolDecoderTest.java
@@ -13,6 +13,9 @@ public class SigfoxProtocolDecoderTest extends ProtocolTest {
SigfoxProtocolDecoder decoder = new SigfoxProtocolDecoder(null);
verifyPosition(decoder, request(HttpMethod.POST, "/",
+ buffer("{ \"device\" : \"33827B\", \"data\" : \"1f03198e63807f08836402ff\", \"time\" : \"1574346702\", \"snr\" : \"8.82\", \"station\" : \"140A\", \"avgSnr\" : \"11.28\", \"lat\" : \"52.0\", \"lng\" : \"-8.0\", \"rssi\" : \"-141.00\", \"seqNumber\" : \"3662\"}")));
+
+ verifyPosition(decoder, request(HttpMethod.POST, "/",
buffer("{ \"device\": \"49F941\", \"location\": {\"lat\":19.48954345634299,\"lng\":-99.09340606338463,\"radius\":1983,\"source\":2,\"status\":1} }")));
verifyAttribute(decoder, request(HttpMethod.POST, "/",