aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnton Tananaev <anton.tananaev@gmail.com>2015-11-03 12:41:03 +1300
committerAnton Tananaev <anton.tananaev@gmail.com>2015-11-03 12:41:03 +1300
commit15079797152451e6d04af6527ab4a38209dce1d7 (patch)
tree478eea8b7dfcd8a938a345b4e17c8cf0d8080ac9
parentc27a9280838a20a18df2fdabbf5de1db85cab39d (diff)
downloadtraccar-server-15079797152451e6d04af6527ab4a38209dce1d7.tar.gz
traccar-server-15079797152451e6d04af6527ab4a38209dce1d7.tar.bz2
traccar-server-15079797152451e6d04af6527ab4a38209dce1d7.zip
Refactor TR900 protocol decoder
-rw-r--r--src/org/traccar/protocol/Tr900ProtocolDecoder.java114
-rw-r--r--src/org/traccar/protocol/TrackboxProtocolDecoder.java22
-rw-r--r--test/org/traccar/protocol/Tr900ProtocolDecoderTest.java3
3 files changed, 56 insertions, 83 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) {
diff --git a/test/org/traccar/protocol/Tr900ProtocolDecoderTest.java b/test/org/traccar/protocol/Tr900ProtocolDecoderTest.java
index 627b2296f..ddcacba1c 100644
--- a/test/org/traccar/protocol/Tr900ProtocolDecoderTest.java
+++ b/test/org/traccar/protocol/Tr900ProtocolDecoderTest.java
@@ -11,7 +11,8 @@ public class Tr900ProtocolDecoderTest extends ProtocolDecoderTest {
Tr900ProtocolDecoder decoder = new Tr900ProtocolDecoder(new Tr900Protocol());
verifyPosition(decoder, text(
- ">00001001,4,1,150626,131252,W05830.2978,S3137.2783,,00,348,18,00,003-000,0,3,11111011*3b!"));
+ ">00001001,4,1,150626,131252,W05830.2978,S3137.2783,,00,348,18,00,003-000,0,3,11111011*3b!"),
+ position("2015-06-26 13:12:52.000", true, -31.62131, -58.50496));
verifyPosition(decoder, text(
">12345678,1,1,070201,144111,W05829.2613,S3435.2313,,00,034,25,00,126-000,0,3,11111111*2d!"));