From 0373efc4f558041c18274b3a227fb505e7560af3 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Wed, 27 May 2020 00:06:01 -0700 Subject: Improve Sigfox message handling --- .../traccar/protocol/SigfoxProtocolDecoder.java | 122 +++++++++++---------- 1 file changed, 66 insertions(+), 56 deletions(-) (limited to 'src/main/java/org/traccar') 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) { -- cgit v1.2.3