From ef7247060c22c060744580f825391673773703c5 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Wed, 9 Nov 2016 20:50:48 +1300 Subject: Support LBS for H02 protocol --- src/org/traccar/protocol/H02ProtocolDecoder.java | 65 ++++++++++++++++++++-- .../traccar/protocol/H02ProtocolDecoderTest.java | 14 ++++- 2 files changed, 74 insertions(+), 5 deletions(-) diff --git a/src/org/traccar/protocol/H02ProtocolDecoder.java b/src/org/traccar/protocol/H02ProtocolDecoder.java index e74f9e862..45a978571 100644 --- a/src/org/traccar/protocol/H02ProtocolDecoder.java +++ b/src/org/traccar/protocol/H02ProtocolDecoder.java @@ -170,6 +170,22 @@ public class H02ProtocolDecoder extends BaseProtocolDecoder { .any() .compile(); + private static final Pattern PATTERN_NBR = new PatternBuilder() + .text("*") + .expression("..,") // manufacturer + .number("(d+),") // imei + .text("NBR,") + .number("(dd)(dd)(dd),") // time + .number("(d+),") // mcc + .number("(d+),") // mnc + .number("d+,") // gsm delay time + .number("d+,") // count + .number("((?:d+,d+,d+,)+)") // cells + .number("(dd)(dd)(dd),") // date (ddmmyy) + .number("(x{8})") // status + .any() + .compile(); + private Position decodeText(String sentence, Channel channel, SocketAddress remoteAddress) { Parser parser = new Parser(PATTERN, sentence); @@ -177,13 +193,13 @@ public class H02ProtocolDecoder extends BaseProtocolDecoder { return null; } - Position position = new Position(); - position.setProtocol(getProtocolName()); - DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, parser.next()); if (deviceSession == null) { return null; } + + Position position = new Position(); + position.setProtocol(getProtocolName()); position.setDeviceId(deviceSession.getDeviceId()); DateBuilder dateBuilder = new DateBuilder(); @@ -224,6 +240,42 @@ public class H02ProtocolDecoder extends BaseProtocolDecoder { return position; } + private Position decodeLbs(String sentence, Channel channel, SocketAddress remoteAddress) { + + Parser parser = new Parser(PATTERN_NBR, sentence); + if (!parser.matches()) { + return null; + } + + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, parser.next()); + if (deviceSession == null) { + return null; + } + + Position position = new Position(); + position.setProtocol(getProtocolName()); + position.setDeviceId(deviceSession.getDeviceId()); + + DateBuilder dateBuilder = new DateBuilder() + .setTime(parser.nextInt(), parser.nextInt(), parser.nextInt()); + + position.set(Position.KEY_MCC, parser.nextInt()); + position.set(Position.KEY_MNC, parser.nextInt()); + + String[] cells = parser.next().split(","); // decode all in future + position.set(Position.KEY_LAC, Integer.parseInt(cells[0])); + position.set(Position.KEY_CID, Integer.parseInt(cells[1])); + position.set(Position.KEY_GSM, Integer.parseInt(cells[2])); + + dateBuilder.setDateReverse(parser.nextInt(), parser.nextInt(), parser.nextInt()); + + getLastLocation(position, dateBuilder.getDate()); + + processStatus(position, parser.nextLong(16)); + + return position; + } + @Override protected Object decode( Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { @@ -234,7 +286,12 @@ public class H02ProtocolDecoder extends BaseProtocolDecoder { // handle X mode? if (marker.equals("*")) { - return decodeText(buf.toString(StandardCharsets.US_ASCII), channel, remoteAddress); + String sentence = buf.toString(StandardCharsets.US_ASCII); + if (sentence.contains(",NBR,")) { + return decodeLbs(sentence, channel, remoteAddress); + } else { + return decodeText(sentence, channel, remoteAddress); + } } else if (marker.equals("$")) { return decodeBinary(buf, channel, remoteAddress); } diff --git a/test/org/traccar/protocol/H02ProtocolDecoderTest.java b/test/org/traccar/protocol/H02ProtocolDecoderTest.java index 88c76f66a..48fe408d8 100644 --- a/test/org/traccar/protocol/H02ProtocolDecoderTest.java +++ b/test/org/traccar/protocol/H02ProtocolDecoderTest.java @@ -10,6 +10,18 @@ public class H02ProtocolDecoderTest extends ProtocolTest { H02ProtocolDecoder decoder = new H02ProtocolDecoder(new H02Protocol()); + verifyAttributes(decoder, buffer( + "*HQ,1400046168,NBR,160169,460,0,1,4,9338,3692,150,9338,3691,145,9338,3690,140,9338,3692,139,180813,FFFFFBFF#")); + + verifyAttributes(decoder, buffer( + "*HQ,1600068860,NBR,120156,262,03,255,6,802,54702,46,802,5032,37,802,54782,30,802,5052,28,802,54712,12,802,5042,12,081116,FFFFFBFF#")); + + verifyAttributes(decoder, buffer( + "*HQ,1600068860,NBR,110326,262,03,255,6,802,23152,23,812,49449,14,802,35382,13,802,35402,11,812,56622,09,802,23132,04,081116,FFFFFBFF#")); + + verifyNothing(decoder, buffer( + "*HQ,1600068860,LINK,112137,20,8,67,0,0,081116,FFFFFBFF#")); + verifyNothing(decoder, buffer( "*HQ,355488020533263,V3,121536,65501,04,000152,014001,156,-64,000161,010642,138,,000152,014003,129,,000152,013973,126,,02E4,0,X,071116,FFFFFBFF#")); @@ -23,7 +35,7 @@ public class H02ProtocolDecoderTest extends ProtocolTest { "*HQ,4210051415,V1,164549,A,0956.3869,N,08406.7068,W,000.00,000,221215,FFFFFBFF,712,01,0,0,6#"), position("2015-12-22 16:45:49.000", true, 9.93978, -84.11178)); - verifyNothing(decoder, buffer( + verifyAttributes(decoder, buffer( "*HQ,1451316451,NBR,112315,724,10,2,2,215,2135,123,215,2131,121,011215,FFFFFFFF#")); verifyPosition(decoder, buffer( -- cgit v1.2.3