aboutsummaryrefslogtreecommitdiff
path: root/src/org/traccar
diff options
context:
space:
mode:
authorAnton Tananaev <anton.tananaev@gmail.com>2015-11-04 12:01:08 +1300
committerAnton Tananaev <anton.tananaev@gmail.com>2015-11-04 12:01:08 +1300
commite7b586030222ed2bae3f9f2f3f86486aeabc7794 (patch)
tree7e52721ed355907dd6c58b636ed288e200900fbc /src/org/traccar
parent2bafdf66c05b9af276fa6b6f23497b580de83259 (diff)
downloadtraccar-server-e7b586030222ed2bae3f9f2f3f86486aeabc7794.tar.gz
traccar-server-e7b586030222ed2bae3f9f2f3f86486aeabc7794.tar.bz2
traccar-server-e7b586030222ed2bae3f9f2f3f86486aeabc7794.zip
Refactor EasyTrack protocol decoder
Diffstat (limited to 'src/org/traccar')
-rw-r--r--src/org/traccar/protocol/EasyTrackProtocolDecoder.java137
1 files changed, 57 insertions, 80 deletions
diff --git a/src/org/traccar/protocol/EasyTrackProtocolDecoder.java b/src/org/traccar/protocol/EasyTrackProtocolDecoder.java
index 21553fa44..352fc97dc 100644
--- a/src/org/traccar/protocol/EasyTrackProtocolDecoder.java
+++ b/src/org/traccar/protocol/EasyTrackProtocolDecoder.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2013 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,13 @@
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.BitUtil;
+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,105 +32,81 @@ public class EasyTrackProtocolDecoder extends BaseProtocolDecoder {
super(protocol);
}
- private static final Pattern PATTERN = Pattern.compile(
- "\\*..," + // Manufacturer
- "(\\d+)," + // IMEI
- "([^,]{2})," + // Command
- "([AV])," + // Validity
- "(\\p{XDigit}{2})" + // Year
- "(\\p{XDigit}{2})" + // Month
- "(\\p{XDigit}{2})," + // Day
- "(\\p{XDigit}{2})" + // Hours
- "(\\p{XDigit}{2})" + // Minutes
- "(\\p{XDigit}{2})," + // Seconds
- "(\\p{XDigit})" +
- "(\\p{XDigit}{7})," + // Latitude
- "(\\p{XDigit})" +
- "(\\p{XDigit}{7})," + // Longitude
- "(\\p{XDigit}{4})," + // Speed
- "(\\p{XDigit}{4})," + // Course
- "(\\p{XDigit}{8})," + // Status
- "(\\p{XDigit}+)," + // Signal
- "(\\d+)," + // Power
- "(\\p{XDigit}{4})," + // Oil
- "(\\p{XDigit}+),?" + // Odometer
- "(\\d+)?" + // Altitude
- ".*");
+ private static final Pattern PATTERN = new PatternBuilder()
+ .text("*").expression("..,") // manufacturer
+ .number("(d+),") // imei
+ .expression("([^,]{2}),") // command
+ .expression("([AV]),") // validity
+ .number("(xx)") // year
+ .number("(xx)") // month
+ .number("(xx),") // day
+ .number("(xx)") // hours
+ .number("(xx)") // minutes
+ .number("(xx),") // seconds
+ .number("(x)")
+ .number("(x{7}),") // latitude
+ .number("(x)")
+ .number("(x{7}),") // longitude
+ .number("(x{4}),") // speed
+ .number("(x{4}),") // course
+ .number("(x{8}),") // status
+ .number("(x+),") // signal
+ .number("(d+),") // power
+ .number("(x{4}),") // oil
+ .number("(x+),?") // odometer
+ .number("(d+)?") // altitude
+ .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)) {
+ if (!identify(parser.next(), channel)) {
return null;
}
position.setDeviceId(getDeviceId());
- // Command
- position.set("command", parser.group(index++));
-
- // Validity
- position.setValid(parser.group(index++).compareTo("A") == 0);
-
- // Date
- Calendar time = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
- time.clear();
- time.set(Calendar.YEAR, 2000 + Integer.parseInt(parser.group(index++), 16));
- time.set(Calendar.MONTH, Integer.parseInt(parser.group(index++), 16) - 1);
- time.set(Calendar.DAY_OF_MONTH, Integer.parseInt(parser.group(index++), 16));
- time.set(Calendar.HOUR_OF_DAY, Integer.parseInt(parser.group(index++), 16));
- time.set(Calendar.MINUTE, Integer.parseInt(parser.group(index++), 16));
- time.set(Calendar.SECOND, Integer.parseInt(parser.group(index++), 16));
- position.setTime(time.getTime());
+ position.set("command", parser.next());
- // Location
- int hemisphere = parser.group(index++).equals("8") ? -1 : 1;
- position.setLatitude(
- hemisphere * Integer.parseInt(parser.group(index++), 16) / 600000.0);
+ position.setValid(parser.next().equals("A"));
- hemisphere = parser.group(index++).equals("8") ? -1 : 1;
- position.setLongitude(
- hemisphere * Integer.parseInt(parser.group(index++), 16) / 600000.0);
+ DateBuilder dateBuilder = new DateBuilder()
+ .setDate(parser.nextInt(16), parser.nextInt(16), parser.nextInt(16))
+ .setTime(parser.nextInt(16), parser.nextInt(16), parser.nextInt(16));
+ position.setTime(dateBuilder.getDate());
- position.setSpeed(Integer.parseInt(parser.group(index++), 16) / 100.0);
- position.setCourse(Integer.parseInt(parser.group(index++), 16) / 100.0);
-
- // Status
- position.set(Event.KEY_STATUS, parser.group(index++));
+ if (BitUtil.check(parser.nextInt(16), 7)) {
+ position.setLatitude(-parser.nextInt(16) / 600000.0);
+ } else {
+ position.setLatitude(parser.nextInt(16) / 600000.0);
+ }
- // Signal
- position.set("signal", parser.group(index++));
+ if (BitUtil.check(parser.nextInt(16), 7)) {
+ position.setLongitude(-parser.nextInt(16) / 600000.0);
+ } else {
+ position.setLongitude(parser.nextInt(16) / 600000.0);
+ }
- // Power
- position.set(Event.KEY_POWER, Double.parseDouble(parser.group(index++)));
+ position.setSpeed(parser.nextInt(16) / 100.0);
+ position.setCourse(parser.nextInt(16) / 100.0);
- // Oil
- position.set("oil", Integer.parseInt(parser.group(index++), 16));
+ position.set(Event.KEY_STATUS, parser.next());
+ position.set("signal", parser.next());
+ position.set(Event.KEY_POWER, parser.nextDouble());
+ position.set("oil", parser.nextInt(16));
+ position.set(Event.KEY_ODOMETER, parser.nextInt(16));
- // Odometer
- position.set(Event.KEY_ODOMETER, Integer.parseInt(parser.group(index++), 16));
+ position.setAltitude(parser.nextDouble());
- // Altitude
- String altitude = parser.group(index++);
- if (altitude != null) {
- position.setAltitude(Double.parseDouble(altitude));
- }
return position;
}