aboutsummaryrefslogtreecommitdiff
path: root/src/org/traccar/protocol/Tk102ProtocolDecoder.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/org/traccar/protocol/Tk102ProtocolDecoder.java')
-rw-r--r--src/org/traccar/protocol/Tk102ProtocolDecoder.java89
1 files changed, 34 insertions, 55 deletions
diff --git a/src/org/traccar/protocol/Tk102ProtocolDecoder.java b/src/org/traccar/protocol/Tk102ProtocolDecoder.java
index 613513f89..b02a424e0 100644
--- a/src/org/traccar/protocol/Tk102ProtocolDecoder.java
+++ b/src/org/traccar/protocol/Tk102ProtocolDecoder.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.Position;
public class Tk102ProtocolDecoder extends BaseProtocolDecoder {
@@ -30,28 +30,33 @@ public class Tk102ProtocolDecoder extends BaseProtocolDecoder {
super(protocol);
}
- private static final Pattern PATTERN = Pattern.compile(
- "\\[.\\d{10}.\\(\\p{Upper}+" +
- "(\\d{2})(\\d{2})(\\d{2})" + // Time (HHMMSS)
- "([AV])" + // Validity
- "(\\d{2})(\\d{2}\\.\\d{4})" + // Latitude (DDMM.MMMM)
- "([NS])" +
- "(\\d{3})(\\d{2}\\.\\d{4})" + // Longitude (DDDMM.MMMM)
- "([EW])" +
- "(\\d{3}\\.\\d{3})" + // Speed
- "(\\d{2})(\\d{2})(\\d{2})" + // Date (DDMMYY)
- "\\d+.*\\)\\]?");
+ private static final Pattern PATTERN = new PatternBuilder()
+ .txt("[")
+ .xpr(".")
+ .num("d{10}")
+ .xpr(".")
+ .txt("(")
+ .xpr("[A-Z]+")
+ .num("(dd)(dd)(dd)") // Time (HHMMSS)
+ .xpr("([AV])") // Validity
+ .num("(dd)(dd.dddd)([NS])") // Latitude (DDMM.MMMM)
+ .num("(ddd)(dd.dddd)([EW])") // Longitude (DDDMM.MMMM)
+ .num("(ddd.ddd)") // Speed
+ .num("(dd)(dd)(dd)") // Date (DDMMYY)
+ .num("d+")
+ .any()
+ .txt(")")
+ .opt("]")
+ .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;
if (sentence.startsWith("[!")) {
- // Login
if (!identify(sentence.substring(14, 14 + 15), channel)) {
return null;
}
@@ -59,54 +64,28 @@ public class Tk102ProtocolDecoder extends BaseProtocolDecoder {
channel.write("[”0000000001" + sentence.substring(13) + "]");
}
- } else if (sentence.startsWith("[#")) {
-
- // TODO: EXIT Send response
-
} else if (hasDeviceId()) {
- Matcher parser = PATTERN.matcher(sentence);
+ Parser parser = new Parser(PATTERN, sentence);
if (!parser.matches()) {
return null;
}
- // Create new position
Position position = new Position();
position.setProtocol(getProtocolName());
position.setDeviceId(getDeviceId());
- Integer index = 1;
-
- // 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++)));
-
- // 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
- position.setSpeed(Double.parseDouble(parser.group(index++)));
-
- // 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());
+ DateBuilder dateBuilder = new DateBuilder()
+ .setTime(parser.nextInt(), parser.nextInt(), parser.nextInt());
+
+ position.setValid(parser.next().equals("A"));
+ position.setLatitude(parser.nextCoordinate());
+ position.setLongitude(parser.nextCoordinate());
+ position.setSpeed(parser.nextDouble());
+
+ dateBuilder.setDateReverse(parser.nextInt(), parser.nextInt(), parser.nextInt());
+ position.setTime(dateBuilder.getDate());
+
return position;
}