From 39de5581b92a505a19cf1929a52bc562c48c3e2f Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Thu, 21 Jan 2016 16:48:09 +1300 Subject: Add support for old GoSafe format --- .../traccar/protocol/GoSafeProtocolDecoder.java | 73 +++++++++++++++++----- 1 file changed, 59 insertions(+), 14 deletions(-) (limited to 'src/org/traccar') 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 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 positions = new LinkedList<>(); + Parser itemParser = new Parser(PATTERN_ITEM, parser.next()); + while (itemParser.find()) { + positions.add(decodePosition(itemParser, time)); + } + + return positions; + + } } } -- cgit v1.2.3