aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnton Tananaev <anton.tananaev@gmail.com>2015-11-05 14:23:43 +1300
committerAnton Tananaev <anton.tananaev@gmail.com>2015-11-05 14:23:43 +1300
commit111ea9bbec5516c36939df8eec4f7ba3edb268bf (patch)
tree7e0dc71cbcfef82af7f41e406a0cf862ecbc648e
parente9aa53180dac2dd4a0ed585791959018804c4815 (diff)
downloadtraccar-server-111ea9bbec5516c36939df8eec4f7ba3edb268bf.tar.gz
traccar-server-111ea9bbec5516c36939df8eec4f7ba3edb268bf.tar.bz2
traccar-server-111ea9bbec5516c36939df8eec4f7ba3edb268bf.zip
Refacor Gotop protocol decoder
-rw-r--r--src/org/traccar/protocol/GotopProtocolDecoder.java87
-rw-r--r--test/org/traccar/protocol/GotopProtocolDecoderTest.java5
2 files changed, 34 insertions, 58 deletions
diff --git a/src/org/traccar/protocol/GotopProtocolDecoder.java b/src/org/traccar/protocol/GotopProtocolDecoder.java
index 16348cb7f..cfae1b0f6 100644
--- a/src/org/traccar/protocol/GotopProtocolDecoder.java
+++ b/src/org/traccar/protocol/GotopProtocolDecoder.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2013 - 2014 Anton Tananaev (anton.tananaev@gmail.com)
+ * Copyright 2013 - 2015 Anton Tananaev (anton.tananaev@gmail.com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -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,77 +32,52 @@ public class GotopProtocolDecoder extends BaseProtocolDecoder {
super(protocol);
}
- private static final Pattern PATTERN = Pattern.compile(
- "(\\d+)," + // IMEI
- "[^,]+," + // Type
- "([AV])," + // Validity
- "DATE:(\\d{2})(\\d{2})(\\d{2})," + // Date (YYMMDD)
- "TIME:(\\d{2})(\\d{2})(\\d{2})," + // Time (HHMMSS)
- "LAT:(\\d+.\\d+)([NS])," + // Latitude
- "LOT:(\\d+.\\d+)([EW])," + // Longitude
- "Speed:(\\d+.\\d+)," + // Speed
- "([^,]+)," + // Status
- "(\\d+)?" + // Course
- ".*");
+ private static final Pattern PATTERN = new PatternBuilder()
+ .number("(d+),") // imei
+ .expression("[^,]+,") // type
+ .expression("([AV]),") // validity
+ .number("DATE:(dd)(dd)(dd),") // date (yyddmm)
+ .number("TIME:(dd)(dd)(dd),") // time
+ .number("LAT:(d+.d+)([NS]),") // latitude
+ .number("LOT:(d+.d+)([EW]),") // longitude
+ .text("Speed:").number("(d+.d+),") // speed
+ .expression("([^,]+),") // status
+ .number("(d+)?") // course
+ .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);
- if (sentence.isEmpty() || !parser.matches()) {
+ 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)) {
+ if (!identify(parser.next(), channel)) {
return null;
}
position.setDeviceId(getDeviceId());
- // Validity
- position.setValid(parser.group(index++).compareTo("A") == 0);
+ position.setValid(parser.next().equals("A"));
- // 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());
- // Latitude
- Double latitude = Double.parseDouble(parser.group(index++));
- if (parser.group(index++).compareTo("S") == 0) latitude = -latitude;
- position.setLatitude(latitude);
+ position.setLatitude(parser.nextCoordinate(Parser.CoordinateFormat.DEG_HEM));
+ position.setLongitude(parser.nextCoordinate(Parser.CoordinateFormat.DEG_HEM));
+ position.setSpeed(UnitsConverter.knotsFromKph(parser.nextDouble()));
- // Longitude
- Double longitude = Double.parseDouble(parser.group(index++));
- if (parser.group(index++).compareTo("W") == 0) longitude = -longitude;
- position.setLongitude(longitude);
+ position.set(Event.KEY_STATUS, parser.next());
- // Speed
- position.setSpeed(UnitsConverter.knotsFromKph(Double.parseDouble(parser.group(index++))));
+ position.setCourse(parser.nextDouble());
- // Status
- position.set(Event.KEY_STATUS, parser.group(index++));
-
- // Course
- String course = parser.group(index++);
- if (course != null) {
- position.setCourse(Double.parseDouble(course));
- }
return position;
}
diff --git a/test/org/traccar/protocol/GotopProtocolDecoderTest.java b/test/org/traccar/protocol/GotopProtocolDecoderTest.java
index 21c32d86e..abc967076 100644
--- a/test/org/traccar/protocol/GotopProtocolDecoderTest.java
+++ b/test/org/traccar/protocol/GotopProtocolDecoderTest.java
@@ -20,8 +20,9 @@ public class GotopProtocolDecoderTest extends ProtocolDecoderTest {
"013226009991924,CMD-T,A,DATE:130802,TIME:153721,LAT:25.9757433S,LOT:028.1087816E,Speed:000.0,X-X-X-X-81-26,000,65501-00A0-4B8E"));
verifyPosition(decoder, text(
- "353327020115804,CMD-T,A,DATE:090329,TIME:223252,LAT:22.7634066N,LOT:114.3964783E,Speed:000.0,84-20,000"));
-
+ "353327020115804,CMD-T,A,DATE:090329,TIME:223252,LAT:22.7634066N,LOT:114.3964783E,Speed:000.0,84-20,000"),
+ position("2009-03-29 22:32:52.000", true, 22.76341, 114.39648));
+
verifyPosition(decoder, text(
"353327020115804,CMD-T,A,DATE:090329,TIME:223252,LAT:22.7634066N,LOT:114.3964783E,Speed:000.0,1-1-0-84-20,000"));