diff options
-rw-r--r-- | src/main/java/org/traccar/protocol/SigfoxProtocolDecoder.java | 122 | ||||
-rw-r--r-- | src/test/java/org/traccar/protocol/SigfoxProtocolDecoderTest.java | 2 |
2 files changed, 68 insertions, 56 deletions
diff --git a/src/main/java/org/traccar/protocol/SigfoxProtocolDecoder.java b/src/main/java/org/traccar/protocol/SigfoxProtocolDecoder.java index 5fc81085b..8379fca82 100644 --- a/src/main/java/org/traccar/protocol/SigfoxProtocolDecoder.java +++ b/src/main/java/org/traccar/protocol/SigfoxProtocolDecoder.java @@ -151,10 +151,9 @@ public class SigfoxProtocolDecoder extends BaseHttpProtocolDecoder { position.setLatitude(getJsonDouble(location, jsonContains(location, "lat") ? "lat" : "latitude")); position.setLongitude(getJsonDouble(location, jsonContains(location, "lng") ? "lng" : "longitude")); - } else if (jsonContains(json, "data") || jsonContains(json, "payload")) { + } else if (jsonContains(json, "data")) { - String data = json.getString(jsonContains(json, "data") ? "data" : "payload"); - ByteBuf buf = Unpooled.wrappedBuffer(DataConverter.parseHex(data)); + ByteBuf buf = Unpooled.wrappedBuffer(DataConverter.parseHex(json.getString("data"))); try { int event = buf.readUnsignedByte(); if (event == 0x0f || event == 0x1f) { @@ -175,69 +174,80 @@ public class SigfoxProtocolDecoder extends BaseHttpProtocolDecoder { position.set(Position.KEY_BATTERY, (int) buf.readUnsignedByte()); - } else if (event >> 4 == 0) { + } else if (event >> 4 <= 3 && buf.writerIndex() == 12) { - position.setValid(true); - position.setLatitude(buf.readIntLE() * 0.0000001); - position.setLongitude(buf.readIntLE() * 0.0000001); - position.setCourse(buf.readUnsignedByte() * 2); - position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedByte())); + if (event >> 4 == 0) { + position.setValid(true); + position.setLatitude(buf.readIntLE() * 0.0000001); + position.setLongitude(buf.readIntLE() * 0.0000001); + position.setCourse(buf.readUnsignedByte() * 2); + position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedByte())); - position.set(Position.KEY_BATTERY, buf.readUnsignedByte() * 0.025); + position.set(Position.KEY_BATTERY, buf.readUnsignedByte() * 0.025); + } else { + return null; + } - } else { + } + } finally { + buf.release(); + } - position.set(Position.KEY_EVENT, event); - if (event == 0x22 || event == 0x62) { - position.set(Position.KEY_ALARM, Position.ALARM_SOS); - } + } else if (jsonContains(json, "payload")) { - while (buf.isReadable()) { - int type = buf.readUnsignedByte(); - switch (type) { - case 0x01: - position.setValid(true); - position.setLatitude(buf.readMedium()); - position.setLongitude(buf.readMedium()); - break; - case 0x02: - position.setValid(true); - position.setLatitude(buf.readFloat()); - position.setLongitude(buf.readFloat()); - break; - case 0x03: - position.set(Position.PREFIX_TEMP + 1, buf.readByte() * 0.5); - break; - case 0x04: - position.set(Position.KEY_BATTERY, buf.readUnsignedByte() * 0.1); - break; - case 0x05: - position.set(Position.KEY_BATTERY_LEVEL, buf.readUnsignedByte()); - break; - case 0x06: - String mac = ByteBufUtil.hexDump(buf.readSlice(6)).replaceAll("(..)", "$1:"); - position.setNetwork(new Network(WifiAccessPoint.from( - mac.substring(0, mac.length() - 1), buf.readUnsignedByte()))); - break; - case 0x07: - buf.skipBytes(10); // wifi extended - break; - case 0x08: - buf.skipBytes(6); // accelerometer - break; - case 0x09: - position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedByte())); - break; - default: - buf.readUnsignedByte(); // fence number - break; - } - } + ByteBuf buf = Unpooled.wrappedBuffer(DataConverter.parseHex(json.getString("payload"))); + try { + int event = buf.readUnsignedByte(); + position.set(Position.KEY_EVENT, event); + if (event == 0x22 || event == 0x62) { + position.set(Position.KEY_ALARM, Position.ALARM_SOS); + } + while (buf.isReadable()) { + int type = buf.readUnsignedByte(); + switch (type) { + case 0x01: + position.setValid(true); + position.setLatitude(buf.readMedium()); + position.setLongitude(buf.readMedium()); + break; + case 0x02: + position.setValid(true); + position.setLatitude(buf.readFloat()); + position.setLongitude(buf.readFloat()); + break; + case 0x03: + position.set(Position.PREFIX_TEMP + 1, buf.readByte() * 0.5); + break; + case 0x04: + position.set(Position.KEY_BATTERY, buf.readUnsignedByte() * 0.1); + break; + case 0x05: + position.set(Position.KEY_BATTERY_LEVEL, buf.readUnsignedByte()); + break; + case 0x06: + String mac = ByteBufUtil.hexDump(buf.readSlice(6)).replaceAll("(..)", "$1:"); + position.setNetwork(new Network(WifiAccessPoint.from( + mac.substring(0, mac.length() - 1), buf.readUnsignedByte()))); + break; + case 0x07: + buf.skipBytes(10); // wifi extended + break; + case 0x08: + buf.skipBytes(6); // accelerometer + break; + case 0x09: + position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedByte())); + break; + default: + buf.readUnsignedByte(); // fence number + break; + } } } finally { buf.release(); } + } if (position.getLatitude() == 0 && position.getLongitude() == 0) { diff --git a/src/test/java/org/traccar/protocol/SigfoxProtocolDecoderTest.java b/src/test/java/org/traccar/protocol/SigfoxProtocolDecoderTest.java index 0ee34a4fc..e118100fb 100644 --- a/src/test/java/org/traccar/protocol/SigfoxProtocolDecoderTest.java +++ b/src/test/java/org/traccar/protocol/SigfoxProtocolDecoderTest.java @@ -12,6 +12,8 @@ public class SigfoxProtocolDecoderTest extends ProtocolTest { SigfoxProtocolDecoder decoder = new SigfoxProtocolDecoder(null); + verifyNull(decoder, request(HttpMethod.POST, "/", buffer("{ \"device\":\"BFE47E\", \"time\":1590497040, \"data\":\"10297eb01e621122070000be\", \"seqNumber\":8, \"deviceTypeId\":\"5ecb8bfac563d620cc9e6798\", \"ack\":false }"))); + 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\"}"))); |