diff options
author | Anton Tananaev <anton.tananaev@gmail.com> | 2015-11-03 12:41:03 +1300 |
---|---|---|
committer | Anton Tananaev <anton.tananaev@gmail.com> | 2015-11-03 12:41:03 +1300 |
commit | 15079797152451e6d04af6527ab4a38209dce1d7 (patch) | |
tree | 478eea8b7dfcd8a938a345b4e17c8cf0d8080ac9 /src | |
parent | c27a9280838a20a18df2fdabbf5de1db85cab39d (diff) | |
download | traccar-server-15079797152451e6d04af6527ab4a38209dce1d7.tar.gz traccar-server-15079797152451e6d04af6527ab4a38209dce1d7.tar.bz2 traccar-server-15079797152451e6d04af6527ab4a38209dce1d7.zip |
Refactor TR900 protocol decoder
Diffstat (limited to 'src')
-rw-r--r-- | src/org/traccar/protocol/Tr900ProtocolDecoder.java | 114 | ||||
-rw-r--r-- | src/org/traccar/protocol/TrackboxProtocolDecoder.java | 22 |
2 files changed, 54 insertions, 82 deletions
diff --git a/src/org/traccar/protocol/Tr900ProtocolDecoder.java b/src/org/traccar/protocol/Tr900ProtocolDecoder.java index f8d0a7a5c..b3500d552 100644 --- a/src/org/traccar/protocol/Tr900ProtocolDecoder.java +++ b/src/org/traccar/protocol/Tr900ProtocolDecoder.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,93 +31,65 @@ public class Tr900ProtocolDecoder extends BaseProtocolDecoder { super(protocol); } - private static final Pattern PATTERN = Pattern.compile( - ">(\\d+)," + // ID - "\\d+," + // Period - "(\\d)," + // Fix - "(\\d{2})(\\d{2})(\\d{2})," + // Date (YYMMDD) - "(\\d{2})(\\d{2})(\\d{2})," + // Time (HHMMSS) - "([EW])" + - "(\\d{3})(\\d{2}\\.\\d+)," + // Longitude (DDDMM.MMMM) - "([NS])" + - "(\\d{2})(\\d{2}\\.\\d+)," + // Latitude (DDMM.MMMM) - "[^,]*," + // Command - "(\\d+\\.?\\d*)," + // Speed - "(\\d+\\.?\\d*)," + // Course - "(\\d+)," + // GSM - "(\\d+)," + // Event - "(\\d+)-" + // ADC - "(\\d+)," + // Battery - "\\d+," + // Impulses - "(\\d+)," + // Input - "(\\d+)" + // Status - ".*(?:\r\n)?"); + private static final Pattern PATTERN = new PatternBuilder() + .number(">(d+),") // id + .number("d+,") // period + .number("(d),") // fix + .number("(dd)(dd)(dd),") // date (yymmdd) + .number("(dd)(dd)(dd),") // time + .expression("([EW])") + .number("(ddd)(dd.d+),") // longitude + .expression("([NS])") + .number("(dd)(dd.d+),") // latitude + .expression("[^,]*,") // command + .number("(d+.?d*),") // speed + .number("(d+.?d*),") // course + .number("(d+),") // gsm + .number("(d+),") // event + .number("(d+)-") // adc + .number("(d+),") // battery + .number("d+,") // impulses + .number("(d+),") // input + .number("(d+)") // 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; - - // 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; - // Identification - if (!identify(parser.group(index++), channel, remoteAddress)) { + if (!identify(parser.next(), channel, remoteAddress)) { return null; } position.setDeviceId(getDeviceId()); - // Validity - position.setValid(parser.group(index++).compareTo("1") == 0); - - // 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++)); - longitude += Double.parseDouble(parser.group(index++)) / 60; - if (hemisphere.compareTo("W") == 0) longitude = -longitude; - position.setLongitude(longitude); + position.setValid(parser.nextInt() == 1); - // Latitude - hemisphere = parser.group(index++); - Double latitude = Double.parseDouble(parser.group(index++)); - latitude += Double.parseDouble(parser.group(index++)) / 60; - if (hemisphere.compareTo("S") == 0) latitude = -latitude; - position.setLatitude(latitude); + DateBuilder dateBuilder = new DateBuilder() + .setDate(parser.nextInt(), parser.nextInt(), parser.nextInt()) + .setTime(parser.nextInt(), parser.nextInt(), parser.nextInt()); + position.setTime(dateBuilder.getDate()); - // Speed - position.setSpeed(Double.parseDouble(parser.group(index++))); + position.setLongitude(parser.nextCoordinate(Parser.CoordinateFormat.HEM_DEG_MIN)); + position.setLatitude(parser.nextCoordinate(Parser.CoordinateFormat.HEM_DEG_MIN)); + position.setSpeed(parser.nextDouble()); + position.setCourse(parser.nextDouble()); - // Course - position.setCourse(Double.parseDouble(parser.group(index++))); + position.set(Event.KEY_GSM, parser.next()); + position.set(Event.KEY_EVENT, parser.nextInt()); + position.set(Event.PREFIX_ADC + 1, parser.nextInt()); + position.set(Event.KEY_BATTERY, parser.nextInt()); + position.set(Event.KEY_INPUT, parser.next()); + position.set(Event.KEY_STATUS, parser.next()); - // Other - position.set(Event.KEY_GSM, parser.group(index++)); - position.set(Event.KEY_EVENT, Integer.parseInt(parser.group(index++))); - position.set(Event.PREFIX_ADC + 1, Integer.parseInt(parser.group(index++))); - position.set(Event.KEY_BATTERY, Integer.parseInt(parser.group(index++))); - position.set(Event.KEY_INPUT, parser.group(index++)); - position.set(Event.KEY_STATUS, parser.group(index++)); return position; } diff --git a/src/org/traccar/protocol/TrackboxProtocolDecoder.java b/src/org/traccar/protocol/TrackboxProtocolDecoder.java index 2cd2b98c2..bf4c4f34d 100644 --- a/src/org/traccar/protocol/TrackboxProtocolDecoder.java +++ b/src/org/traccar/protocol/TrackboxProtocolDecoder.java @@ -32,17 +32,17 @@ public class TrackboxProtocolDecoder extends BaseProtocolDecoder { } private static final Pattern PATTERN = new PatternBuilder() - .number("(dd)(dd)(dd).(ddd),") // Time - .number("(dd)(dd.dddd)([NS]),") // Latitude (DDMM.MMMM) - .number("(ddd)(dd.dddd)([EW]),") // Longitude (DDDMM.MMMM) - .number("(d+.d),") // HDOP - .number("(-?d+.?d*),") // Altitude - .number("(d),") // Fix Type - .number("(d+.d+),") // Course - .number("d+.d+,") // Speed (kph) - .number("(d+.d+),") // Speed (knots) - .number("(dd)(dd)(dd),") // Date - .number("(d+)") // Satellites + .number("(dd)(dd)(dd).(ddd),") // time + .number("(dd)(dd.dddd)([NS]),") // latitude + .number("(ddd)(dd.dddd)([EW]),") // longitude + .number("(d+.d),") // hdop + .number("(-?d+.?d*),") // altitude + .number("(d),") // fix type + .number("(d+.d+),") // course + .number("d+.d+,") // speed (kph) + .number("(d+.d+),") // speed (knots) + .number("(dd)(dd)(dd),") // date + .number("(d+)") // satellites .compile(); private void sendResponse(Channel channel) { |