aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/org/traccar/protocol/Xt013ProtocolDecoder.java104
-rw-r--r--test/org/traccar/protocol/Xt013ProtocolDecoderTest.java5
2 files changed, 48 insertions, 61 deletions
diff --git a/src/org/traccar/protocol/Xt013ProtocolDecoder.java b/src/org/traccar/protocol/Xt013ProtocolDecoder.java
index 3c3c06b3d..5ae9f8622 100644
--- a/src/org/traccar/protocol/Xt013ProtocolDecoder.java
+++ b/src/org/traccar/protocol/Xt013ProtocolDecoder.java
@@ -16,13 +16,12 @@
package org.traccar.protocol;
import java.net.SocketAddress;
-import java.text.ParseException;
-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.helper.UnitsConverter;
import org.traccar.model.Event;
import org.traccar.model.Position;
@@ -33,77 +32,64 @@ public class Xt013ProtocolDecoder extends BaseProtocolDecoder {
super(protocol);
}
- private static final Pattern PATTERN = Pattern.compile(
- "(?:HI,\\d+)?" +
- "TK," +
- "(\\d+)," + // IMEI
- "(\\d{2})(\\d{2})(\\d{2})" + // Date (YYMMDD)
- "(\\d{2})(\\d{2})(\\d{2})," + // Time (HHMMSS)
- "([+-]\\d+\\.\\d+)," + // Latitude
- "([+-]\\d+\\.\\d+)," + // Longitude
- "(\\d+)," + // Speed
- "(\\d+)," + // Course
- "\\d+," +
- "(\\d+)," + // Altitude
- "([FL])," + // GPS fix
- "\\d+," +
- "(\\d+)," + // GPS level
- "\\p{XDigit}+," +
- "\\p{XDigit}+," +
- "(\\d+)," + // GSM level
- "[^,]*," +
- "(\\d+\\.\\d+)," + // Battery
- "(\\d)," + // Charging
- ".*");
+ private static final Pattern PATTERN = new PatternBuilder()
+ .number("HI,d+").optional()
+ .text("TK,")
+ .number("(d+),") // imei
+ .number("(dd)(dd)(dd)") // date (yymmdd)
+ .number("(dd)(dd)(dd),") // time
+ .number("([+-]d+.d+),") // latitude
+ .number("([+-]d+.d+),") // longitude
+ .number("(d+),") // speed
+ .number("(d+),") // course
+ .number("d+,")
+ .number("(d+),") // altitude
+ .expression("([FL]),") // gps fix
+ .number("d+,")
+ .number("(d+),") // gps level
+ .number("x+,")
+ .number("x+,")
+ .number("(d+),") // gsm level
+ .expression("[^,]*,")
+ .number("(d+.d+),") // battery
+ .number("(d),") // charging
+ .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()) {
- throw new ParseException(null, 0);
+ return null;
}
- // Create new position
Position position = new Position();
position.setProtocol(getProtocolName());
- Integer index = 1;
-
- // Identify device
- 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.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());
+ DateBuilder dateBuilder = new DateBuilder()
+ .setDate(parser.nextInt(), parser.nextInt(), parser.nextInt())
+ .setTime(parser.nextInt(), parser.nextInt(), parser.nextInt());
+ position.setTime(dateBuilder.getDate());
+
+ position.setLatitude(parser.nextDouble());
+ position.setLongitude(parser.nextDouble());
+ position.setSpeed(UnitsConverter.knotsFromKph(parser.nextDouble()));
+ position.setCourse(parser.nextDouble());
+ position.setAltitude(parser.nextDouble());
+ position.setValid(parser.next().equals("F"));
- // Location
- position.setLatitude(Double.parseDouble(parser.group(index++)));
- position.setLongitude(Double.parseDouble(parser.group(index++)));
- position.setSpeed(UnitsConverter.knotsFromKph(Double.parseDouble(parser.group(index++))));
- position.setCourse(Double.parseDouble(parser.group(index++)));
- position.setAltitude(Double.parseDouble(parser.group(index++)));
- position.setValid(parser.group(index++).equals("F"));
+ position.set(Event.KEY_GPS, parser.next());
+ position.set(Event.KEY_GSM, parser.next());
+ position.set(Event.KEY_BATTERY, parser.next());
+ position.set(Event.KEY_CHARGE, parser.next());
- // Other
- position.set(Event.KEY_GPS, parser.group(index++));
- position.set(Event.KEY_GSM, parser.group(index++));
- position.set(Event.KEY_BATTERY, parser.group(index++));
- position.set(Event.KEY_CHARGE, parser.group(index++));
return position;
}
diff --git a/test/org/traccar/protocol/Xt013ProtocolDecoderTest.java b/test/org/traccar/protocol/Xt013ProtocolDecoderTest.java
index aafd597ac..569f2b8f2 100644
--- a/test/org/traccar/protocol/Xt013ProtocolDecoderTest.java
+++ b/test/org/traccar/protocol/Xt013ProtocolDecoderTest.java
@@ -11,8 +11,9 @@ public class Xt013ProtocolDecoderTest extends ProtocolDecoderTest {
Xt013ProtocolDecoder decoder = new Xt013ProtocolDecoder(new Xt013Protocol());
verifyPosition(decoder, text(
- "TK,862950021650364,150131090859,+53.267863,+5.767363,0,38,12,0,F,204,08,C94,336C,24,,4.09,1,,,,,,,,"));
-
+ "TK,862950021650364,150131090859,+53.267863,+5.767363,0,38,12,0,F,204,08,C94,336C,24,,4.09,1,,,,,,,,"),
+ position("2015-01-31 09:08:59.000", true, 53.26786, 5.76736));
+
verifyPosition(decoder, text(
"TK,862950021650364,150118113832,+53.267722,+5.767143,0,86,12,0,F,204,08,C94,336C,22,,4.21,1,,,,,,,,"));