aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnton Tananaev <anton.tananaev@gmail.com>2015-11-03 23:12:54 +1300
committerAnton Tananaev <anton.tananaev@gmail.com>2015-11-03 23:12:54 +1300
commitf8a3f9e444b0c3d489635fccdbf6f8939068bba5 (patch)
treeed10769e811cfe91e37c566044b7dc750335a38c
parent1e9edb291f05e9a8512437c76db81bdf19f3cc73 (diff)
downloadtraccar-server-f8a3f9e444b0c3d489635fccdbf6f8939068bba5.tar.gz
traccar-server-f8a3f9e444b0c3d489635fccdbf6f8939068bba5.tar.bz2
traccar-server-f8a3f9e444b0c3d489635fccdbf6f8939068bba5.zip
Refactor CarTrack protocol decoder
-rw-r--r--src/org/traccar/protocol/CarTrackProtocolDecoder.java128
-rw-r--r--test/org/traccar/protocol/CarTrackProtocolDecoderTest.java4
2 files changed, 50 insertions, 82 deletions
diff --git a/src/org/traccar/protocol/CarTrackProtocolDecoder.java b/src/org/traccar/protocol/CarTrackProtocolDecoder.java
index d9bf27973..762017d33 100644
--- a/src/org/traccar/protocol/CarTrackProtocolDecoder.java
+++ b/src/org/traccar/protocol/CarTrackProtocolDecoder.java
@@ -1,6 +1,6 @@
/*
- * Copyright 2014 Anton Tananaev (anton.tananaev@gmail.com)
- * Rohit
+ * Copyright 2014 - 2015 Anton Tananaev (anton.tananaev@gmail.com)
+ * Copyright 2014 Rohit
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -17,12 +17,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;
@@ -32,100 +32,63 @@ public class CarTrackProtocolDecoder extends BaseProtocolDecoder {
super(protocol);
}
- private static final Pattern PATTERN = Pattern.compile(
- "\\$\\$" + // Header
- "(\\d+)\\?*" + // Device ID
- "&A" +
- "(\\d{4})" + // Command - 2
- "&B" +
- "(\\d{2})(\\d{2})(\\d{2})\\.(\\d{3})," + // HHMMSS.DDD
- "([AV])," + // STATUS : A= Valid, V = Invalid
- "(\\d{2})(\\d{2}\\.\\d{4})," + // Lat : XXMM.DDDDD
- "([NS])," + // N/S
- "(\\d{3})(\\d{2}\\.\\d{4})," + // Long : YYYMM.DDDD
- "([EW])," + // E/W
- "(\\d+.\\d*)?," + // Speed in Knots
- "(\\d+.\\d*)?," + // Heading
- "(\\d{2})(\\d{2})(\\d{2})" + // DDMMYY
- ".*" +
- "&C(.*)" + // IO Port Data
- "&D(.*)" + // Mile Meter Data
- "&E(.*)" + // Alarm Data
- "(?:&Y)?(.*)"); // AD Input Data
+ private static final Pattern PATTERN = new PatternBuilder()
+ .text("$$") // header
+ .number("(d+)") // device id
+ .text("?").expression("*")
+ .text("&A")
+ .number("(dddd)") // command
+ .text("&B")
+ .number("(dd)(dd)(dd).(ddd),") // time
+ .expression("([AV]),") // validity
+ .number("(dd)(dd.dddd),") // latitude
+ .expression("([NS]),")
+ .number("(ddd)(dd.dddd),") // longitude
+ .expression("([EW]),")
+ .number("(d+.d*)?,") // speed
+ .number("(d+.d*)?,") // course
+ .number("(dd)(dd)(dd)") // date (ddmmyy)
+ .any()
+ .expression("&C([^&]*)") // io
+ .expression("&D([^&]*)") // odometer
+ .expression("&E([^&]*)") // alarm
+ .expression("&Y([^&]*)").optional() // adc
+ .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;
- // Get device by unique identifier
- if (!identify(parser.group(index++), channel)) {
+ if (!identify(parser.next(), channel)) {
return null;
}
position.setDeviceId(getDeviceId());
- // Command
- position.set("command", parser.group(index++));
-
- // 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++)));
-
- // Validity
- position.setValid(parser.group(index++).compareTo("A") == 0);
-
- // 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);
-
- // 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
- String speed = parser.group(index++);
- if (speed != null) {
- position.setSpeed(Double.parseDouble(speed));
- }
+ position.set("command", parser.next());
- // Course
- String course = parser.group(index++);
- if (course != null) {
- position.setCourse(Double.parseDouble(course));
- }
+ DateBuilder dateBuilder = new DateBuilder()
+ .setTime(parser.nextInt(), parser.nextInt(), parser.nextInt(), parser.nextInt());
- // 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());
+ position.setValid(parser.next().equals("A"));
+ position.setLatitude(parser.nextCoordinate());
+ position.setLongitude(parser.nextCoordinate());
+ position.setSpeed(parser.nextDouble());
+ position.setCourse(parser.nextDouble());
- // State
- position.set(Event.PREFIX_IO + 1, parser.group(index++));
+ dateBuilder.setDateReverse(parser.nextInt(), parser.nextInt(), parser.nextInt());
+ position.setTime(dateBuilder.getDate());
- // Odometer
- String odometer = parser.group(index++);
+ position.set(Event.PREFIX_IO + 1, parser.next());
+
+ String odometer = parser.next();
odometer = odometer.replace(":", "A");
odometer = odometer.replace(";", "B");
odometer = odometer.replace("<", "C");
@@ -134,8 +97,9 @@ public class CarTrackProtocolDecoder extends BaseProtocolDecoder {
odometer = odometer.replace("?", "F");
position.set(Event.KEY_ODOMETER, Integer.parseInt(odometer, 16));
- position.set(Event.KEY_ALARM, parser.group(index++));
- position.set("ad", parser.group(index++));
+ position.set(Event.KEY_ALARM, parser.next());
+ position.set(Event.PREFIX_ADC + 1, parser.next());
+
return position;
}
diff --git a/test/org/traccar/protocol/CarTrackProtocolDecoderTest.java b/test/org/traccar/protocol/CarTrackProtocolDecoderTest.java
index 6b26607d7..a23dea444 100644
--- a/test/org/traccar/protocol/CarTrackProtocolDecoderTest.java
+++ b/test/org/traccar/protocol/CarTrackProtocolDecoderTest.java
@@ -11,6 +11,10 @@ public class CarTrackProtocolDecoderTest extends ProtocolDecoderTest {
CarTrackProtocolDecoder decoder = new CarTrackProtocolDecoder(new CarTrackProtocol());
verifyPosition(decoder, text(
+ "$$2222234???????&A9955&B102904.000,A,2233.0655,N,11404.9440,E,0.00,,030109,,*17|6.3|&C0100000100&D000024?>&E10000000"),
+ position("2009-01-03 10:29:04.000", true, 22.55109, 114.08240));
+
+ verifyPosition(decoder, text(
"$$2222234???????&A9955&B102904.000,A,2233.0655,N,11404.9440,E,0.00,,030109,,*17|6.3|&C0100000100&D000024?>&E10000000&Y00100020"));
verifyPosition(decoder, text(