From f30f25164a3b9be08bad55f80481332b73d6e9e1 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Sat, 8 Oct 2016 12:49:17 +1300 Subject: Decode additional cGuard data (fix #2419) --- .../traccar/protocol/CguardProtocolDecoder.java | 93 ++++++++++++++++++---- .../protocol/CguardProtocolDecoderTest.java | 21 +++-- 2 files changed, 92 insertions(+), 22 deletions(-) diff --git a/src/org/traccar/protocol/CguardProtocolDecoder.java b/src/org/traccar/protocol/CguardProtocolDecoder.java index 1646363e5..6c528c559 100644 --- a/src/org/traccar/protocol/CguardProtocolDecoder.java +++ b/src/org/traccar/protocol/CguardProtocolDecoder.java @@ -33,7 +33,7 @@ public class CguardProtocolDecoder extends BaseProtocolDecoder { super(protocol); } - private static final Pattern PATTERN = new PatternBuilder() + private static final Pattern PATTERN_NV = new PatternBuilder() .text("NV:") .number("(dd)(dd)(dd) ") // date .number("(dd)(dd)(dd):") // time @@ -45,23 +45,16 @@ public class CguardProtocolDecoder extends BaseProtocolDecoder { .number("(?:NAN|(d+.?d*))") // altitude .compile(); - @Override - protected Object decode( - Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { - - String sentence = (String) msg; - - if (sentence.startsWith("ID:") || sentence.startsWith("IDRO:")) { - getDeviceSession(channel, remoteAddress, sentence.substring(sentence.indexOf(':') + 1)); - return null; - } + private static final Pattern PATTERN_BC = new PatternBuilder() + .text("BC:") + .number("(dd)(dd)(dd) ") // date + .number("(dd)(dd)(dd):") // time + .expression("(.+)") // data + .compile(); - DeviceSession deviceSession = getDeviceSession(channel, remoteAddress); - if (deviceSession == null) { - return null; - } + private Position decodePosition(DeviceSession deviceSession, String sentence) { - Parser parser = new Parser(PATTERN, (String) msg); + Parser parser = new Parser(PATTERN_NV, sentence); if (!parser.matches()) { return null; } @@ -88,4 +81,72 @@ public class CguardProtocolDecoder extends BaseProtocolDecoder { return position; } + private Position decodeStatus(DeviceSession deviceSession, String sentence) { + + Parser parser = new Parser(PATTERN_BC, sentence); + if (!parser.matches()) { + return null; + } + + Position position = new Position(); + position.setProtocol(getProtocolName()); + position.setDeviceId(deviceSession.getDeviceId()); + + DateBuilder dateBuilder = new DateBuilder() + .setDate(parser.nextInt(), parser.nextInt(), parser.nextInt()) + .setTime(parser.nextInt(), parser.nextInt(), parser.nextInt()); + + getLastLocation(position, dateBuilder.getDate()); + + String[] data = parser.next().split(":"); + for (int i = 0; i < data.length / 2; i++) { + String key = data[i * 2]; + String value = data[i * 2 + 1]; + switch (key) { + case "CSQ1": + position.set(Position.KEY_GSM, Integer.parseInt(value)); + break; + case "NSQ1": + position.set(Position.KEY_SATELLITES, Integer.parseInt(value)); + break; + case "BAT1": + position.set(Position.KEY_BATTERY, Integer.parseInt(value) + "%"); + break; + case "PWR1": + position.set(Position.KEY_POWER, Double.parseDouble(value)); + break; + default: + position.set(key.toLowerCase(), value); + break; + } + } + + return position; + } + + @Override + protected Object decode( + Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { + + String sentence = (String) msg; + + if (sentence.startsWith("ID:") || sentence.startsWith("IDRO:")) { + getDeviceSession(channel, remoteAddress, sentence.substring(sentence.indexOf(':') + 1)); + return null; + } + + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress); + if (deviceSession == null) { + return null; + } + + if (sentence.startsWith("NV:")) { + return decodePosition(deviceSession, sentence); + } else if (sentence.startsWith("BC:")) { + return decodeStatus(deviceSession, sentence); + } + + return null; + } + } diff --git a/test/org/traccar/protocol/CguardProtocolDecoderTest.java b/test/org/traccar/protocol/CguardProtocolDecoderTest.java index 49d037f8f..ff9ecb9be 100644 --- a/test/org/traccar/protocol/CguardProtocolDecoderTest.java +++ b/test/org/traccar/protocol/CguardProtocolDecoderTest.java @@ -13,16 +13,25 @@ public class CguardProtocolDecoderTest extends ProtocolTest { verifyNothing(decoder, text( "IDRO:354868050655283")); + verifyPosition(decoder, text( + "NV:161007 122043:55.812730:37.733689:3.62:NAN:244.05:143.4")); + + verifyPosition(decoder, text( + "NV:161007 122044:55.812732:37.733670:3.97:NAN:260.95:143.9")); + + verifyAttributes(decoder, text( + "BC:161007 122044:CSQ1:77:NSQ1:18:BAT1:100")); + verifyPosition(decoder, text( "NV:160711 044023:54.342907:48.582590:0:NAN:0:110.1")); verifyPosition(decoder, text( "NV:160711 044023:54.342907:-148.582590:0:NAN:0:110.1")); - verifyNothing(decoder, text( + verifyAttributes(decoder, text( "BC:160711 044023:CSQ1:48:NSQ1:7:NSQ2:1:BAT1:98:PWR1:11.7:CLG1:NAN")); - verifyNothing(decoder, text( + verifyAttributes(decoder, text( "BC:160711 044524:CSQ1:61:NSQ1:18:BAT1:98:PWR1:11.7:CLG1:NAN")); verifyNothing(decoder, text( @@ -31,19 +40,19 @@ public class CguardProtocolDecoderTest extends ProtocolTest { verifyPosition(decoder, text( "NV:160420 101902:55.799425:37.674033:0.94:NAN:213.59:156.6")); - verifyNothing(decoder, text( + verifyAttributes(decoder, text( "BC:160628 081024:CSQ1:32:NSQ1:10:BAT1:100")); - verifyNothing(decoder, text( + verifyAttributes(decoder, text( "BC:160628 081033:NSQ2:0")); verifyPosition(decoder, text( "NV:160630 151537:55.799913:37.674267:0.7:NAN:10.21:174.9")); - verifyNothing(decoder, text( + verifyAttributes(decoder, text( "BC:160630 153316:BAT1:76")); - verifyNothing(decoder, text( + verifyAttributes(decoder, text( "BC:160630 153543:NSQ2:0")); verifyNothing(decoder, text( -- cgit v1.2.3