diff options
author | Anton Tananaev <anton.tananaev@gmail.com> | 2016-01-21 16:48:09 +1300 |
---|---|---|
committer | Anton Tananaev <anton.tananaev@gmail.com> | 2016-01-21 16:48:09 +1300 |
commit | 39de5581b92a505a19cf1929a52bc562c48c3e2f (patch) | |
tree | 0924db688457d2cef28dbfa1b1d6328b19ba6207 | |
parent | e7c04ee2af5b2bdabc58647c11ec059281dccae0 (diff) | |
download | trackermap-server-39de5581b92a505a19cf1929a52bc562c48c3e2f.tar.gz trackermap-server-39de5581b92a505a19cf1929a52bc562c48c3e2f.tar.bz2 trackermap-server-39de5581b92a505a19cf1929a52bc562c48c3e2f.zip |
Add support for old GoSafe format
-rw-r--r-- | src/org/traccar/protocol/GoSafeProtocolDecoder.java | 73 | ||||
-rw-r--r-- | test/org/traccar/protocol/GoSafeProtocolDecoderTest.java | 4 |
2 files changed, 61 insertions, 16 deletions
diff --git a/src/org/traccar/protocol/GoSafeProtocolDecoder.java b/src/org/traccar/protocol/GoSafeProtocolDecoder.java index 810aee3e0..8be462ebf 100644 --- a/src/org/traccar/protocol/GoSafeProtocolDecoder.java +++ b/src/org/traccar/protocol/GoSafeProtocolDecoder.java @@ -41,7 +41,6 @@ public class GoSafeProtocolDecoder extends BaseProtocolDecoder { .number("(d+),") // imei .number("(dd)(dd)(dd)") // time .number("(dd)(dd)(dd),") // date - .optional(2) .expression("(.*)#?") // data .compile(); @@ -101,6 +100,23 @@ public class GoSafeProtocolDecoder extends BaseProtocolDecoder { .groupEnd("?") .compile(); + private static final Pattern PATTERN_OLD = new PatternBuilder() + .text("*GS") // header + .number("d+,") // protocol version + .number("(d+),") // imei + .text("GPS:") + .number("(dd)(dd)(dd);") // time + .number("d;").optional() // fix type + .expression("([AV]);") // validity + .number("([NS])(d+.d+);") // latitude + .number("([EW])(d+.d+);") // longitude + .number("(d+)?;") // speed + .number("(d+);") // course + .number("(d+.?d*)").optional() // hdop + .number("(dd)(dd)(dd)") // date + .any() + .compile(); + private Position decodePosition(Parser parser, Date time) { Position position = new Position(); @@ -148,7 +164,10 @@ public class GoSafeProtocolDecoder extends BaseProtocolDecoder { channel.write("1234"); } - Parser parser = new Parser(PATTERN, (String) msg); + String sentence = (String) msg; + Pattern pattern = sentence.startsWith("*GS02") ? PATTERN_OLD : PATTERN; + + Parser parser = new Parser(pattern, (String) msg); if (!parser.matches()) { return null; } @@ -157,21 +176,47 @@ public class GoSafeProtocolDecoder extends BaseProtocolDecoder { return null; } - Date time = null; - if (parser.hasNext(6)) { + if (pattern == PATTERN_OLD) { + + Position position = new Position(); + position.setProtocol(getProtocolName()); + position.setDeviceId(getDeviceId()); + DateBuilder dateBuilder = new DateBuilder() - .setTime(parser.nextInt(), parser.nextInt(), parser.nextInt()) - .setDateReverse(parser.nextInt(), parser.nextInt(), parser.nextInt()); - time = dateBuilder.getDate(); - } + .setTime(parser.nextInt(), parser.nextInt(), parser.nextInt()); - List<Position> positions = new LinkedList<>(); - Parser itemParser = new Parser(PATTERN_ITEM, parser.next()); - while (itemParser.find()) { - positions.add(decodePosition(itemParser, time)); - } + position.setValid(parser.next().equals("A")); + position.setLatitude(parser.nextCoordinate(Parser.CoordinateFormat.HEM_DEG)); + position.setLongitude(parser.nextCoordinate(Parser.CoordinateFormat.HEM_DEG)); + position.setSpeed(parser.nextDouble()); + position.setCourse(parser.nextDouble()); + + position.set(Event.KEY_HDOP, parser.next()); + + dateBuilder.setDateReverse(parser.nextInt(), parser.nextInt(), parser.nextInt()); + position.setTime(dateBuilder.getDate()); - return positions; + return position; + + } else { + + Date time = null; + if (parser.hasNext(6)) { + DateBuilder dateBuilder = new DateBuilder() + .setTime(parser.nextInt(), parser.nextInt(), parser.nextInt()) + .setDateReverse(parser.nextInt(), parser.nextInt(), parser.nextInt()); + time = dateBuilder.getDate(); + } + + List<Position> positions = new LinkedList<>(); + Parser itemParser = new Parser(PATTERN_ITEM, parser.next()); + while (itemParser.find()) { + positions.add(decodePosition(itemParser, time)); + } + + return positions; + + } } } diff --git a/test/org/traccar/protocol/GoSafeProtocolDecoderTest.java b/test/org/traccar/protocol/GoSafeProtocolDecoderTest.java index 8569fcae8..badc267ab 100644 --- a/test/org/traccar/protocol/GoSafeProtocolDecoderTest.java +++ b/test/org/traccar/protocol/GoSafeProtocolDecoderTest.java @@ -10,8 +10,8 @@ public class GoSafeProtocolDecoderTest extends ProtocolTest { GoSafeProtocolDecoder decoder = new GoSafeProtocolDecoder(new GoSafeProtocol()); - /*verifyPositions(decoder, text( - "*GS02,358696043774648,GPS:230040;A;S1.166829;E36.934287;0;0;170116,STT:20;0,MGR:32755204,ADC:0;11.2;1;28.3;2;4.1,GFS:0;0"));*/ + verifyPosition(decoder, text( + "*GS02,358696043774648,GPS:230040;A;S1.166829;E36.934287;0;0;170116,STT:20;0,MGR:32755204,ADC:0;11.2;1;28.3;2;4.1,GFS:0;0")); verifyNothing(decoder, text( "*GS02,358696043774648")); |