aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnton Tananaev <anton.tananaev@gmail.com>2015-10-28 15:43:25 +1300
committerAnton Tananaev <anton.tananaev@gmail.com>2015-10-28 15:43:25 +1300
commit4610c2245dea9178ff46e7eea63b9fffd99f852d (patch)
tree84f4da60d273b84932d9483272415886793a62d6
parent1b31eda6bfa623dd866de5139b001caca8d69e54 (diff)
downloadtrackermap-server-4610c2245dea9178ff46e7eea63b9fffd99f852d.tar.gz
trackermap-server-4610c2245dea9178ff46e7eea63b9fffd99f852d.tar.bz2
trackermap-server-4610c2245dea9178ff46e7eea63b9fffd99f852d.zip
Refactor Xirgo protocol decoder
-rw-r--r--src/org/traccar/protocol/XirgoProtocolDecoder.java98
-rw-r--r--test/org/traccar/protocol/XirgoProtocolDecoderTest.java3
2 files changed, 43 insertions, 58 deletions
diff --git a/src/org/traccar/protocol/XirgoProtocolDecoder.java b/src/org/traccar/protocol/XirgoProtocolDecoder.java
index 400d3c76c..0f6ad50ba 100644
--- a/src/org/traccar/protocol/XirgoProtocolDecoder.java
+++ b/src/org/traccar/protocol/XirgoProtocolDecoder.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.helper.UnitsConverter;
import org.traccar.model.Event;
import org.traccar.model.Position;
@@ -32,79 +32,63 @@ public class XirgoProtocolDecoder extends BaseProtocolDecoder {
super(protocol);
}
- private static final Pattern PATTERN = Pattern.compile(
- "\\$\\$" +
- "(\\d+)," + // IMEI
- "(\\d+)," + // Event
- "(\\d{4})/(\\d{2})/(\\d{2})," + // Date
- "(\\d{2}):(\\d{2}):(\\d{2})," + // Time
- "(-?\\d+\\.?\\d*)," + // Latitude
- "(-?\\d+\\.?\\d*)," + // Longitude
- "(-?\\d+\\.?\\d*)," + // Altitude
- "(\\d+\\.?\\d*)," + // Speed
- "(\\d+\\.?\\d*)," + // Course
- "(\\d+)," + // Satellites
- "(\\d+\\.?\\d*)," + // HDOP
- "(\\d+\\.\\d+)," + // Battery
- "(\\d+)," + // GSM
- "(\\d+\\.?\\d*)," + // Odometer
- "(\\d+)," + // GPS
- ".*");
+ private static final Pattern PATTERN = new PatternBuilder()
+ .text("$$")
+ .number("(d+),") // imei
+ .number("(d+),") // event
+ .number("(dddd)/(dd)/(dd),") // date
+ .number("(dd):(dd):(dd),") // time
+ .number("(-?d+.?d*),") // latitude
+ .number("(-?d+.?d*),") // longitude
+ .number("(-?d+.?d*),") // altitude
+ .number("(d+.?d*),") // speed
+ .number("(d+.?d*),") // course
+ .number("(d+),") // satellites
+ .number("(d+.?d*),") // hdop
+ .number("(d+.d+),") // battery
+ .number("(d+),") // gsm
+ .number("(d+.?d*),") // odometer
+ .number("(d+),") // gps
+ .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;
-
- // Get device by IMEI
- if (!identify(parser.group(index++), channel, remoteAddress)) {
+ if (!identify(parser.next(), channel, remoteAddress)) {
return null;
}
position.setDeviceId(getDeviceId());
- position.set(Event.KEY_EVENT, parser.group(index++));
+ position.set(Event.KEY_EVENT, parser.next());
- // Date
- Calendar time = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
- time.clear();
- time.set(Calendar.YEAR, 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());
- // Location
- position.setLatitude(Double.parseDouble(parser.group(index++)));
- position.setLongitude(Double.parseDouble(parser.group(index++)));
- position.setAltitude(Double.parseDouble(parser.group(index++)));
- position.setSpeed(UnitsConverter.knotsFromMph(Double.parseDouble(parser.group(index++))));
- position.setCourse(Double.parseDouble(parser.group(index++)));
+ position.setLatitude(parser.nextDouble());
+ position.setLongitude(parser.nextDouble());
+ position.setAltitude(parser.nextDouble());
+ position.setSpeed(UnitsConverter.knotsFromMph(parser.nextDouble()));
+ position.setCourse(parser.nextDouble());
- // Additional data
- position.set(Event.KEY_SATELLITES, parser.group(index++));
- position.set(Event.KEY_HDOP, parser.group(index++));
- position.set(Event.KEY_BATTERY, parser.group(index++));
- position.set(Event.KEY_GSM, parser.group(index++));
- position.set(Event.KEY_ODOMETER, parser.group(index++));
+ position.set(Event.KEY_SATELLITES, parser.next());
+ position.set(Event.KEY_HDOP, parser.next());
+ position.set(Event.KEY_BATTERY, parser.next());
+ position.set(Event.KEY_GSM, parser.next());
+ position.set(Event.KEY_ODOMETER, parser.next());
- // Validity
- position.setValid(Integer.parseInt(parser.group(index++)) == 1);
+ position.setValid(parser.nextInt() == 1);
return position;
}
diff --git a/test/org/traccar/protocol/XirgoProtocolDecoderTest.java b/test/org/traccar/protocol/XirgoProtocolDecoderTest.java
index b7b2ace73..8bad0ff6a 100644
--- a/test/org/traccar/protocol/XirgoProtocolDecoderTest.java
+++ b/test/org/traccar/protocol/XirgoProtocolDecoderTest.java
@@ -11,7 +11,8 @@ public class XirgoProtocolDecoderTest extends ProtocolDecoderTest {
XirgoProtocolDecoder decoder = new XirgoProtocolDecoder(new XirgoProtocol());
verifyPosition(decoder, text(
- "$$354660046140722,6001,2013/01/22,15:36:18,25.80907,-80.32531,7.1,19,165.2,11,0.8,11.1,17,1,1,3.9,2##"));
+ "$$354660046140722,6001,2013/01/22,15:36:18,25.80907,-80.32531,7.1,19,165.2,11,0.8,11.1,17,1,1,3.9,2##"),
+ position("2013-01-22 15:36:18.000", true, 25.80907, -80.32531));
verifyPosition(decoder, text(
"$$357207059646786,4003,2015/05/19,15:54:56,-20.21422,-70.14927,37.5,1.8,0.0,11,0.8,12.9,31,297,1,0,0.0,0.0,0,1,1,1##"));