diff options
author | Anton Tananaev <anton.tananaev@gmail.com> | 2015-10-28 15:35:55 +1300 |
---|---|---|
committer | Anton Tananaev <anton.tananaev@gmail.com> | 2015-10-28 15:35:55 +1300 |
commit | 1b31eda6bfa623dd866de5139b001caca8d69e54 (patch) | |
tree | 9ef25214df2eae4a7e97eefe864a37980016c029 /src/org/traccar/protocol | |
parent | e60132dc040d2af3a5ad4f7ea8ee2e5fab7675ee (diff) | |
download | trackermap-server-1b31eda6bfa623dd866de5139b001caca8d69e54.tar.gz trackermap-server-1b31eda6bfa623dd866de5139b001caca8d69e54.tar.bz2 trackermap-server-1b31eda6bfa623dd866de5139b001caca8d69e54.zip |
Refactor YWT protocol decoder
Diffstat (limited to 'src/org/traccar/protocol')
-rw-r--r-- | src/org/traccar/protocol/YwtProtocolDecoder.java | 106 |
1 files changed, 38 insertions, 68 deletions
diff --git a/src/org/traccar/protocol/YwtProtocolDecoder.java b/src/org/traccar/protocol/YwtProtocolDecoder.java index 0801f45fe..574c5b877 100644 --- a/src/org/traccar/protocol/YwtProtocolDecoder.java +++ b/src/org/traccar/protocol/YwtProtocolDecoder.java @@ -16,12 +16,12 @@ package org.traccar.protocol; import java.net.SocketAddress; -import java.util.Calendar; -import java.util.TimeZone; -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.model.Event; import org.traccar.model.Position; @@ -31,28 +31,28 @@ public class YwtProtocolDecoder extends BaseProtocolDecoder { super(protocol); } - private static final Pattern PATTERN = Pattern.compile( - "%(..)," + // Type - "(\\d+):" + // Unit identifier - "\\d+," + // Subtype - "(\\d{2})(\\d{2})(\\d{2})" + // Date (YYMMDD) - "(\\d{2})(\\d{2})(\\d{2})," + // Time (HHMMSS) - "([EW])" + - "(\\d{3}\\.\\d{6})," + // Longitude (DDDMM.MMMM) - "([NS])" + - "(\\d{2}\\.\\d{6})," + // Latitude (DDMM.MMMM) - "(\\d+)?," + // Altitude - "(\\d+)," + // Speed - "(\\d+)," + // Course - "(\\d+)," + // Satellite - "([^,]+)," + // Report identifier - "([0-9a-fA-F\\-]+)" + // Status - ".*"); + private static final Pattern PATTERN = new PatternBuilder() + .expression("%(..),") // type + .number("(d+):") // unit identifier + .number("d+,") // subtype + .number("(dd)(dd)(dd)") // date (yymmdd) + .number("(dd)(dd)(dd),") // time + .expression("([EW])") + .number("(ddd.d{6}),") // longitude + .expression("([NS])") + .number("(dd.d{6}),") // latitude + .number("(d+)?,") // altitude + .number("(d+),") // speed + .number("(d+),") // course + .number("(d+),") // satellite + .expression("([^,]+),") // report identifier + .expression("([-0-9a-fA-F]+)") // status + .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; @@ -71,69 +71,39 @@ public class YwtProtocolDecoder extends BaseProtocolDecoder { return null; } - // Parse message - Matcher parser = PATTERN.matcher(sentence); + Parser parser = new Parser(PATTERN, sentence); if (!parser.matches()) { return null; } - // Create new position Position position = new Position(); position.setProtocol(getProtocolName()); - Integer index = 1; - String type = parser.group(index++); - // Device - if (!identify(parser.group(index++), channel)) { + String type = parser.next(); + + if (!identify(parser.next(), channel)) { return null; } position.setDeviceId(getDeviceId()); - // Time - Calendar time = Calendar.getInstance(TimeZone.getTimeZone("UTC")); - time.clear(); - time.set(Calendar.YEAR, 2000 + Integer.parseInt(parser.group(index++))); - time.set(Calendar.MONTH, Integer.parseInt(parser.group(index++)) - 1); - time.set(Calendar.DAY_OF_MONTH, 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()); - - // Longitude - String hemisphere = parser.group(index++); - Double longitude = Double.parseDouble(parser.group(index++)); - if (hemisphere.compareTo("W") == 0) longitude = -longitude; - position.setLongitude(longitude); - - // Latitude - hemisphere = parser.group(index++); - Double latitude = Double.parseDouble(parser.group(index++)); - if (hemisphere.compareTo("S") == 0) latitude = -latitude; - position.setLatitude(latitude); - - // Altitude - String altitude = parser.group(index++); - if (altitude != null) { - position.setAltitude(Double.parseDouble(altitude)); - } - - // Speed - position.setSpeed(Double.parseDouble(parser.group(index++))); + DateBuilder dateBuilder = new DateBuilder() + .setDate(parser.nextInt(), parser.nextInt(), parser.nextInt()) + .setTime(parser.nextInt(), parser.nextInt(), parser.nextInt()); + position.setTime(dateBuilder.getDate()); - // Course - position.setCourse(Double.parseDouble(parser.group(index++))); + position.setLongitude(parser.nextCoordinate(Parser.CoordinateFormat.HEM_DEG)); + position.setLatitude(parser.nextCoordinate(Parser.CoordinateFormat.HEM_DEG)); + position.setAltitude(parser.nextDouble()); + position.setSpeed(parser.nextDouble()); + position.setCourse(parser.nextDouble()); - // Satellites - int satellites = Integer.parseInt(parser.group(index++)); + int satellites = parser.nextInt(); position.setValid(satellites >= 3); position.set(Event.KEY_SATELLITES, satellites); - // Report identifier - String reportId = parser.group(index++); + String reportId = parser.next(); - // Status - position.set(Event.KEY_STATUS, parser.group(index++)); + position.set(Event.KEY_STATUS, parser.next()); // Send response if ((type.equals("KP") || type.equals("EP")) && channel != null) { |