diff options
author | Anton Tananaev <anton.tananaev@gmail.com> | 2015-10-29 15:20:22 +1300 |
---|---|---|
committer | Anton Tananaev <anton.tananaev@gmail.com> | 2015-10-29 15:20:22 +1300 |
commit | 9a7e9e1b5822351c2c7aec8d6f1fde5b637afaaf (patch) | |
tree | 6741cb83415100a08205a536d2c7ba972439205e | |
parent | 0d65586e0011f44d7597a0e332a0a1b83276607c (diff) | |
download | traccar-server-9a7e9e1b5822351c2c7aec8d6f1fde5b637afaaf.tar.gz traccar-server-9a7e9e1b5822351c2c7aec8d6f1fde5b637afaaf.tar.bz2 traccar-server-9a7e9e1b5822351c2c7aec8d6f1fde5b637afaaf.zip |
Refactor Sanav protocol decoder
-rw-r--r-- | src/org/traccar/protocol/SanavProtocolDecoder.java | 92 | ||||
-rw-r--r-- | src/org/traccar/protocol/TytanProtocolDecoder.java | 1 | ||||
-rw-r--r-- | test/org/traccar/protocol/SanavProtocolDecoderTest.java | 3 |
3 files changed, 34 insertions, 62 deletions
diff --git a/src/org/traccar/protocol/SanavProtocolDecoder.java b/src/org/traccar/protocol/SanavProtocolDecoder.java index 77af9b214..1411651d5 100644 --- a/src/org/traccar/protocol/SanavProtocolDecoder.java +++ b/src/org/traccar/protocol/SanavProtocolDecoder.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.Position; public class SanavProtocolDecoder extends BaseProtocolDecoder { @@ -30,81 +30,53 @@ public class SanavProtocolDecoder extends BaseProtocolDecoder { super(protocol); } - private static final Pattern PATTERN = Pattern.compile( - ".*imei[:=]" + - "(\\d+)" + // IMEI - "&?rmc[:=]\\$GPRMC," + - "(\\d{2})(\\d{2})(\\d{2}).(\\d+)," + // Time (HHMMSS.SSS) - "([AV])," + // Validity - "(\\d+)(\\d{2}\\.\\d+)," + // Latitude (DDMM.MMMM) - "([NS])," + - "(\\d+)(\\d{2}\\.\\d+)," + // Longitude (DDDMM.MMMM) - "([EW])," + - "(\\d+\\.\\d+)," + // Speed - "(\\d+\\.\\d+)?," + // Course - "(\\d{2})(\\d{2})(\\d{2})," + // Date (DDMMYY) - ".*"); + private static final Pattern PATTERN = new PatternBuilder() + .any() + .expression("imei[:=]") + .number("(d+)") // imei + .expression("&?rmc[:=]") + .text("$GPRMC,") + .number("(dd)(dd)(dd).(d+),") // time + .expression("([AV]),") // validity + .number("(d+)(dd.d+),") // latitude + .expression("([NS]),") + .number("(d+)(dd.d+),") // longitude + .expression("([EW]),") + .number("(d+.d+),") // speed + .number("(d+.d+)?,") // course + .number("(dd)(dd)(dd),") // date (ddmmyy) + .any() + .compile(); @Override protected Object decode( - Channel channel, SocketAddress remoteAddress, Object msg) - throws Exception { + Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { - // Parse message - String sentence = (String) msg; - 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)) { + if (!identify(parser.next(), channel)) { return null; } position.setDeviceId(getDeviceId()); - // Time - Calendar time = Calendar.getInstance(TimeZone.getTimeZone("UTC")); - time.clear(); - 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++))); - time.set(Calendar.MILLISECOND, Integer.parseInt(parser.group(index++))); + DateBuilder dateBuilder = new DateBuilder() + .setTime(parser.nextInt(), parser.nextInt(), parser.nextInt(), parser.nextInt()); - // Validity - position.setValid(parser.group(index++).compareTo("A") == 0); + position.setValid(parser.next().equals("A")); + position.setLatitude(parser.nextCoordinate()); + position.setLongitude(parser.nextCoordinate()); + position.setSpeed(parser.nextDouble()); + position.setCourse(parser.nextDouble()); - // Latitude - Double latitude = Double.parseDouble(parser.group(index++)); - latitude += Double.parseDouble(parser.group(index++)) / 60; - if (parser.group(index++).compareTo("S") == 0) latitude = -latitude; - position.setLatitude(latitude); + dateBuilder.setDateReverse(parser.nextInt(), parser.nextInt(), parser.nextInt()); + position.setTime(dateBuilder.getDate()); - // Longitude - Double longitude = Double.parseDouble(parser.group(index++)); - longitude += Double.parseDouble(parser.group(index++)) / 60; - if (parser.group(index++).compareTo("W") == 0) longitude = -longitude; - position.setLongitude(longitude); - - // Speed - position.setSpeed(Double.parseDouble(parser.group(index++))); - - // Course - String course = parser.group(index++); - if (course != null) { - position.setCourse(Double.parseDouble(course)); - } - - // Date - 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++))); - position.setTime(time.getTime()); return position; } diff --git a/src/org/traccar/protocol/TytanProtocolDecoder.java b/src/org/traccar/protocol/TytanProtocolDecoder.java index e8549d913..b0c1d243b 100644 --- a/src/org/traccar/protocol/TytanProtocolDecoder.java +++ b/src/org/traccar/protocol/TytanProtocolDecoder.java @@ -16,7 +16,6 @@ package org.traccar.protocol; import java.net.SocketAddress; -import java.nio.ByteOrder; import java.nio.charset.Charset; import java.util.Date; import java.util.LinkedHashSet; diff --git a/test/org/traccar/protocol/SanavProtocolDecoderTest.java b/test/org/traccar/protocol/SanavProtocolDecoderTest.java index d7168ff2d..cd254abdd 100644 --- a/test/org/traccar/protocol/SanavProtocolDecoderTest.java +++ b/test/org/traccar/protocol/SanavProtocolDecoderTest.java @@ -11,7 +11,8 @@ public class SanavProtocolDecoderTest extends ProtocolDecoderTest { SanavProtocolDecoder decoder = new SanavProtocolDecoder(new SanavProtocol()); verifyPosition(decoder, text( - "imei=352024028982787&rmc=$GPRMC,103048.000,A,4735.0399,N,01905.2895,E,0.00,0.00,171013,,*05,AUTO-4095mv")); + "imei=352024028982787&rmc=$GPRMC,103048.000,A,4735.0399,N,01905.2895,E,0.00,0.00,171013,,*05,AUTO-4095mv"), + position("2013-10-17 10:30:48.000", true, 47.58400, 19.08816)); verifyPosition(decoder, text( "imei:352024028980000rmc:$GPRMC,093604.354,A,4735.0862,N,01905.2146,E,0.00,0.00,171013,,*09,AUTO-4103mv")); |