From c10c4b985002d9b39213f6a4e33466212705f66d Mon Sep 17 00:00:00 2001 From: Oliver Schramm Date: Thu, 24 Jun 2021 19:39:18 +0200 Subject: Improve compatibility with megastek devices Megastek devices send some data which either is not fully compliant with the megastek protocol (by omitting some data) or which is (for our purposes) garbage data, which is only delimited with a single LF (0x0A). --- .../org/traccar/protocol/MegastekFrameDecoder.java | 3 ++ .../traccar/protocol/MegastekProtocolDecoder.java | 33 +++++++++++++--------- .../traccar/protocol/MegastekFrameDecoderTest.java | 4 +++ .../protocol/MegastekProtocolDecoderTest.java | 6 ++++ 4 files changed, 32 insertions(+), 14 deletions(-) diff --git a/src/main/java/org/traccar/protocol/MegastekFrameDecoder.java b/src/main/java/org/traccar/protocol/MegastekFrameDecoder.java index 347fa24b1..a4091436c 100644 --- a/src/main/java/org/traccar/protocol/MegastekFrameDecoder.java +++ b/src/main/java/org/traccar/protocol/MegastekFrameDecoder.java @@ -46,6 +46,9 @@ public class MegastekFrameDecoder extends BaseFrameDecoder { if (delimiter == -1) { delimiter = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) '!'); } + if (delimiter == -1) { + delimiter = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) '\n'); + } if (delimiter != -1) { ByteBuf result = buf.readRetainedSlice(delimiter - buf.readerIndex()); buf.skipBytes(1); diff --git a/src/main/java/org/traccar/protocol/MegastekProtocolDecoder.java b/src/main/java/org/traccar/protocol/MegastekProtocolDecoder.java index f0c0df8a6..2ce5b9dae 100644 --- a/src/main/java/org/traccar/protocol/MegastekProtocolDecoder.java +++ b/src/main/java/org/traccar/protocol/MegastekProtocolDecoder.java @@ -141,6 +141,9 @@ public class MegastekProtocolDecoder extends BaseProtocolDecoder { beginIndex = endIndex + 2; endIndex = sentence.indexOf('*', beginIndex) + 3; + if (beginIndex > endIndex) { + return null; + } location = sentence.substring(beginIndex, endIndex); beginIndex = endIndex + 1; @@ -243,13 +246,13 @@ public class MegastekProtocolDecoder extends BaseProtocolDecoder { .number("dd,") .number("(dd),") // satellites .number("dd,") - .number("(d+.d+),") // hdop + .number("(d+.d+)?,") // hdop .number("(d+.d+)?,") // speed .number("(d+.d+)?,") // course .number("(-?d+.d+)?,") // altitude .number("(d+.d+)?,") // odometer - .number("(d+),") // mcc - .number("(d+),") // mnc + .number("(d+)?,") // mcc + .number("(d+)?,") // mnc .number("(xxxx)?,") // lac .number("(x+)?,") // cid .number("(d+)?,") // gsm @@ -276,7 +279,7 @@ public class MegastekProtocolDecoder extends BaseProtocolDecoder { .groupEnd("?").text(",") .number("(d+)?,") // rfid .number("([01])(d)?").optional() // charge and belt status - .expression("[^,]*,").optional() + .expression("[^,]*,") .number("(d+)?,") // battery .expression("([^,]*)[,;]") // alert .any() @@ -318,17 +321,19 @@ public class MegastekProtocolDecoder extends BaseProtocolDecoder { position.set(Position.KEY_ODOMETER, parser.nextDouble(0) * 1000); } - int mcc = parser.nextInt(); - int mnc = parser.nextInt(); - Integer lac = parser.nextHexInt(); - Integer cid = parser.nextHexInt(); - Integer rssi = parser.nextInt(); - if (lac != null && cid != null) { - CellTower tower = CellTower.from(mcc, mnc, lac, cid); - if (rssi != null) { - tower.setSignalStrength(rssi); + if (parser.hasNext(5)) { + int mcc = parser.nextInt(); + int mnc = parser.nextInt(); + Integer lac = parser.nextHexInt(); + Integer cid = parser.nextHexInt(); + Integer rssi = parser.nextInt(); + if (lac != null && cid != null) { + CellTower tower = CellTower.from(mcc, mnc, lac, cid); + if (rssi != null) { + tower.setSignalStrength(rssi); + } + position.setNetwork(new Network(tower)); } - position.setNetwork(new Network(tower)); } if (parser.hasNext(5)) { diff --git a/src/test/java/org/traccar/protocol/MegastekFrameDecoderTest.java b/src/test/java/org/traccar/protocol/MegastekFrameDecoderTest.java index f1650b1b2..19e5cb0ab 100644 --- a/src/test/java/org/traccar/protocol/MegastekFrameDecoderTest.java +++ b/src/test/java/org/traccar/protocol/MegastekFrameDecoderTest.java @@ -22,6 +22,10 @@ public class MegastekFrameDecoderTest extends ProtocolTest { binary("53545832363034373520202020202020202020024f244750524d432c3133313131302e30302c562c2c2c2c2c2c2c3036303931332c2c2c4e2a37362c3232322c30312c383135412c443435352c31312c39372c303030302c303030312c302c54696d65723b3735"), decoder.decode(null, null, binary("53545832363034373520202020202020202020024f244750524d432c3133313131302e30302c562c2c2c2c2c2c2c3036303931332c2c2c4e2a37362c3232322c30312c383135412c443435352c31312c39372c303030302c303030312c302c54696d65723b37350d0a"))); + verifyFrame( + binary("636d643d6169643b757365723d2a2a2a3b7077643d2a2a2a3b6c61743d33382e35353239353338333b6c6f6e3d32312e30353337343530303b706163633d31303030"), + decoder.decode(null, null, binary("636d643d6169643b757365723d2a2a2a3b7077643d2a2a2a3b6c61743d33382e35353239353338333b6c6f6e3d32312e30353337343530303b706163633d313030300a"))); + } } diff --git a/src/test/java/org/traccar/protocol/MegastekProtocolDecoderTest.java b/src/test/java/org/traccar/protocol/MegastekProtocolDecoderTest.java index 3c8377b51..83d62e766 100644 --- a/src/test/java/org/traccar/protocol/MegastekProtocolDecoderTest.java +++ b/src/test/java/org/traccar/protocol/MegastekProtocolDecoderTest.java @@ -10,6 +10,12 @@ public class MegastekProtocolDecoderTest extends ProtocolTest { public void testDecode() throws Exception { var decoder = new MegastekProtocolDecoder(null); + + verifyPosition(decoder, text( + "$MGV002,860719020193193,,S,070521,160748,V,2255.09165,N,11404.01322,E,00,00,00,,,,,,,,,,,,,,,,,,,10,015,Restart;!")); + + verifyPosition(decoder, text( + "$MGV002,860719020193193,,R,070621,115717,V,2255.09165,N,11404.01322,E,00,00,00,99.9,,,,,460,07,262C,0F54,20,,,,,,,,,10,039,Timer;!")); verifyPosition(decoder, text( "0132$MGV002,869152024261561,,S,310818,133945,V,3814.35442,N,02144.50662,E,00,00,00,99.9,,,44.2,,202,10,,,13,0,0,0,0,90,,,,11,100,Timer;!")); -- cgit v1.2.3