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 ++++++++++++++++++---- 1 file changed, 77 insertions(+), 16 deletions(-) (limited to 'src/org') 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; + } + } -- cgit v1.2.3