From 52f6ed7567fc11babf831401ac9744f9ebd7c735 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Tue, 27 Oct 2015 10:56:05 +1300 Subject: Refactor Visiontek protocol decoder --- src/org/traccar/helper/Parser.java | 2 +- .../traccar/protocol/VisiontekProtocolDecoder.java | 139 +++++++++------------ 2 files changed, 59 insertions(+), 82 deletions(-) (limited to 'src') diff --git a/src/org/traccar/helper/Parser.java b/src/org/traccar/helper/Parser.java index 9f24cacd0..f70fe2e89 100644 --- a/src/org/traccar/helper/Parser.java +++ b/src/org/traccar/helper/Parser.java @@ -101,7 +101,7 @@ public class Parser { switch (format) { case DEG_MIN_MIN_HEM: coordinate = nextInt(); - coordinate += (nextInt() + nextInt() / 1000.0) / 60; + coordinate += Double.parseDouble(next() + '.' + next()) / 60; hemisphere = next(); break; case HEM_DEG: diff --git a/src/org/traccar/protocol/VisiontekProtocolDecoder.java b/src/org/traccar/protocol/VisiontekProtocolDecoder.java index f974d08be..0dce4954d 100644 --- a/src/org/traccar/protocol/VisiontekProtocolDecoder.java +++ b/src/org/traccar/protocol/VisiontekProtocolDecoder.java @@ -22,6 +22,9 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; import org.jboss.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; +import org.traccar.helper.DateBuilder; +import org.traccar.helper.Parser; +import org.traccar.helper.PatternBuilder; import org.traccar.helper.UnitsConverter; import org.traccar.model.Event; import org.traccar.model.Position; @@ -32,107 +35,81 @@ public class VisiontekProtocolDecoder extends BaseProtocolDecoder { super(protocol); } - private static final Pattern PATTERN = Pattern.compile( - "\\$1," + - "([^,]+)," + // Identifier - "(?:(\\d+),)?" + // IMEI - "(\\d{2}),(\\d{2}),(\\d{2})," + // Date - "(\\d{2}),(\\d{2}),(\\d{2})," + // Time - "(\\d{2})(\\d{2}\\.?\\d{4})([NS])," + // Latitude - "(\\d{3})(\\d{2}\\.?\\d{4})([EW])," + // Longitude - "(\\d+\\.?\\d+)," + // Speed - "(\\d+)," + // Course - "(?:(\\d+)," + // Altitude - "(\\d+),)?" + // Satellites - "(\\d+)," + // Odometer - "(?:(\\d)," + // Ignition - "(\\d)," + // Input 1 - "(\\d)," + // Input 2 - "(\\d)," + // Immobilizer - "(\\d)," + // External Battery Status - "(\\d+),)?" + // GSM - "([AV]),?" + // Validity - "(\\d+)?" + // RFID - ".*"); + private static final Pattern PATTERN = new PatternBuilder() + .text("$1,") + .expression("([^,]+),") // identifier + .number("(d+),").optional() // imei + .number("(dd),(dd),(dd),") // date + .number("(dd),(dd),(dd),") // time + .number("(dd)(dd).?(dddd)") // latitude + .expression("([NS]),") + .number("(ddd)(dd).?(dddd)") // longitude + .expression("([EW]),") + .number("(d+.?d+),") // speed + .number("(d+),") // course + .groupBegin() + .number("(d+),") // altitude + .number("(d+),") // satellites + .groupEnd("?") + .number("(d+),") // odometer + .groupBegin() + .number("(d),") // ignition + .number("(d),") // input 1 + .number("(d),") // input 2 + .number("(d),") // immobilizer + .number("(d),") // external battery status + .number("(d+),") // gsm + .groupEnd("?") + .expression("([AV]),?") // validity + .number("(d+)?") // rfid + .any() + .compile(); @Override protected Object decode( - Channel channel, SocketAddress remoteAddress, Object msg) - throws Exception { + Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { - String sentence = (String) msg; - - // Parse message - Matcher parser = PATTERN.matcher(sentence); + Parser parser = new Parser(PATTERN, (String) msg); if (!parser.matches()) { return null; } - // Create new position Position position = new Position(); position.setProtocol(getProtocolName()); - Integer index = 1; - - // Device identification - String id = parser.group(index++); - String imei = parser.group(index++); + String id = parser.next(); + String imei = parser.next(); if (!identify(id, channel, null, false) && !identify(imei, channel)) { return null; } position.setDeviceId(getDeviceId()); - // Date - Calendar time = Calendar.getInstance(TimeZone.getTimeZone("UTC")); - time.clear(); - time.set(Calendar.DAY_OF_MONTH, Integer.parseInt(parser.group(index++))); - time.set(Calendar.MONTH, Integer.parseInt(parser.group(index++)) - 1); - time.set(Calendar.YEAR, 2000 + Integer.parseInt(parser.group(index++))); - time.set(Calendar.HOUR_OF_DAY, Integer.parseInt(parser.group(index++))); - time.set(Calendar.MINUTE, Integer.parseInt(parser.group(index++))); - time.set(Calendar.SECOND, Integer.parseInt(parser.group(index++))); - position.setTime(time.getTime()); - - // Latitude - Double latitude = Double.parseDouble(parser.group(index++)); - latitude += Double.parseDouble(parser.group(index++).replace(".", "")) / 600000; - if (parser.group(index++).compareTo("S") == 0) latitude = -latitude; - position.setLatitude(latitude); - - // Longitude - Double longitude = Double.parseDouble(parser.group(index++)); - longitude += Double.parseDouble(parser.group(index++).replace(".", "")) / 600000; - if (parser.group(index++).compareTo("W") == 0) longitude = -longitude; - position.setLongitude(longitude); - - // Speed + DateBuilder dateBuilder = new DateBuilder() + .setDateReverse(parser.nextInt(), parser.nextInt(), parser.nextInt()) + .setTime(parser.nextInt(), parser.nextInt(), parser.nextInt()); + position.setTime(dateBuilder.getDate()); + + position.setLatitude(parser.nextCoordinate(Parser.CoordinateFormat.DEG_MIN_MIN_HEM)); + position.setLongitude(parser.nextCoordinate(Parser.CoordinateFormat.DEG_MIN_MIN_HEM)); + position.setSpeed(UnitsConverter.knotsFromKph(Double.parseDouble( - parser.group(index++).replace(".", "")) / 10)); + parser.next().replace(".", "")) / 10)); - // Course - position.setCourse(Double.parseDouble(parser.group(index++))); + position.setCourse(parser.nextDouble()); + position.setAltitude(parser.nextDouble()); - // Altitude - String altitude = parser.group(index++); - if (altitude != null) { - position.setAltitude(Double.parseDouble(altitude)); - } + position.set(Event.KEY_SATELLITES, parser.next()); + position.set(Event.KEY_ODOMETER, parser.next()); + position.set(Event.KEY_IGNITION, parser.next()); + position.set(Event.PREFIX_IO + 1, parser.next()); + position.set(Event.PREFIX_IO + 2, parser.next()); + position.set("immobilizer", parser.next()); + position.set(Event.KEY_POWER, parser.next()); + position.set(Event.KEY_GSM, parser.next()); + + position.setValid(parser.next().equals("A")); - // Additional data - position.set(Event.KEY_SATELLITES, parser.group(index++)); - position.set(Event.KEY_ODOMETER, parser.group(index++)); - position.set(Event.KEY_IGNITION, parser.group(index++)); - position.set(Event.PREFIX_IO + 1, parser.group(index++)); - position.set(Event.PREFIX_IO + 2, parser.group(index++)); - position.set("immobilizer", parser.group(index++)); - position.set(Event.KEY_POWER, parser.group(index++)); - position.set(Event.KEY_GSM, parser.group(index++)); - - // Validity - position.setValid(parser.group(index++).compareTo("A") == 0); - - // RFID - position.set(Event.KEY_RFID, parser.group(index++)); + position.set(Event.KEY_RFID, parser.next()); return position; } -- cgit v1.2.3