aboutsummaryrefslogtreecommitdiff
path: root/src/org
diff options
context:
space:
mode:
authorAnton Tananaev <anton.tananaev@gmail.com>2015-11-05 11:46:57 +1300
committerAnton Tananaev <anton.tananaev@gmail.com>2015-11-05 11:46:57 +1300
commit3b3f7dddcb9a7ac43651cacc7cf05247a4f7d82b (patch)
tree88136af1f7247d91252e4316f9627ce091249ea2 /src/org
parent344799f3be95dded17bf068c42c0e8776e82aa3f (diff)
downloadtraccar-server-3b3f7dddcb9a7ac43651cacc7cf05247a4f7d82b.tar.gz
traccar-server-3b3f7dddcb9a7ac43651cacc7cf05247a4f7d82b.tar.bz2
traccar-server-3b3f7dddcb9a7ac43651cacc7cf05247a4f7d82b.zip
Refactor GpsMarker protocol decoder
Diffstat (limited to 'src/org')
-rw-r--r--src/org/traccar/helper/Parser.java7
-rw-r--r--src/org/traccar/protocol/GpsMarkerProtocolDecoder.java119
2 files changed, 47 insertions, 79 deletions
diff --git a/src/org/traccar/helper/Parser.java b/src/org/traccar/helper/Parser.java
index bda7d6366..8d6dd1a64 100644
--- a/src/org/traccar/helper/Parser.java
+++ b/src/org/traccar/helper/Parser.java
@@ -90,6 +90,7 @@ public class Parser {
DEG_HEM,
DEG_MIN_HEM,
DEG_MIN_MIN_HEM,
+ HEM_DEG_MIN_MIN,
HEM_DEG,
HEM_DEG_MIN,
HEM_DEG_MIN_HEM
@@ -126,6 +127,12 @@ public class Parser {
hemisphere = next();
}
break;
+ case HEM_DEG_MIN_MIN:
+ hemisphere = next();
+ coordinate = nextInt();
+ coordinate += Double.parseDouble(next() + '.' + next()) / 60;
+ break;
+ case DEG_MIN_HEM:
default:
coordinate = nextInt();
coordinate += nextDouble() / 60;
diff --git a/src/org/traccar/protocol/GpsMarkerProtocolDecoder.java b/src/org/traccar/protocol/GpsMarkerProtocolDecoder.java
index c9d9b0cce..e0f8d529e 100644
--- a/src/org/traccar/protocol/GpsMarkerProtocolDecoder.java
+++ b/src/org/traccar/protocol/GpsMarkerProtocolDecoder.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.Event;
import org.traccar.model.Position;
@@ -31,99 +31,60 @@ public class GpsMarkerProtocolDecoder extends BaseProtocolDecoder {
super(protocol);
}
- private static final Pattern PATTERN = Pattern.compile(
- "\\$GM" +
- "\\d" + // Type
- "(?:\\p{XDigit}{2})?" + // Index
- "(\\d{15})" + // IMEI
- "T(\\d{2})(\\d{2})(\\d{2})" + // Date
- "(\\d{2})(\\d{2})(\\d{2})?" + // Time
- "([NS])" +
- "(\\d{2})(\\d{2}\\d{4})" + // Latitude
- "([EW])" +
- "(\\d{3})(\\d{2}\\d{4})" + // Longitude
- "(\\d{3})" + // Speed
- "(\\d{3})" + // Course
- "(\\d)" + // Satellites
- "(\\d{2})" + // Battery
- "(\\d)" + // Input
- "(\\d)" + // Output
- "(\\d{3})" + // Temperature
- ".*");
+ private static final Pattern PATTERN = new PatternBuilder()
+ .text("$GM")
+ .number("d") // type
+ .number("(?:xx)?") // index
+ .number("(d{15})") // imei
+ .number("T(dd)(dd)(dd)") // date
+ .number("(dd)(dd)(dd)?") // time
+ .number("([NS])")
+ .number("(dd)(dd)(dddd)") // latitude
+ .number("([EW])")
+ .number("(ddd)(dd)(dddd)") // longitude
+ .number("(ddd)") // speed
+ .number("(ddd)") // course
+ .number("(d)") // satellites
+ .number("(dd)") // battery
+ .number("(d)") // input
+ .number("(d)") // output
+ .number("(ddd)") // temperature
+ .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
- String imei = parser.group(index++);
- if (!identify(imei, channel, remoteAddress)) {
+ if (!identify(parser.next(), channel, remoteAddress)) {
return null;
}
position.setDeviceId(getDeviceId());
- // Date and Time
- Calendar time = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
- time.clear();
- 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++)));
- time.set(Calendar.HOUR_OF_DAY, Integer.parseInt(parser.group(index++)));
- time.set(Calendar.MINUTE, Integer.parseInt(parser.group(index++)));
- String seconds = parser.group(index++);
- if (seconds != null) {
- time.set(Calendar.SECOND, Integer.parseInt(seconds));
- }
- position.setTime(time.getTime());
+ DateBuilder dateBuilder = new DateBuilder()
+ .setDateReverse(parser.nextInt(), parser.nextInt(), parser.nextInt())
+ .setTime(parser.nextInt(), parser.nextInt(), parser.nextInt());
+ position.setTime(dateBuilder.getDate());
- // Validity
position.setValid(true);
-
- // Latitude
- String hemisphere = parser.group(index++);
- Double latitude = Double.parseDouble(parser.group(index++));
- latitude += Double.parseDouble(parser.group(index++)) / 600000;
- if (hemisphere.compareTo("S") == 0) {
- latitude = -latitude;
- }
- position.setLatitude(latitude);
-
- // Longitude
- hemisphere = parser.group(index++);
- Double longitude = Double.parseDouble(parser.group(index++));
- longitude += Double.parseDouble(parser.group(index++)) / 600000;
- if (hemisphere.compareTo("W") == 0) {
- longitude = -longitude;
- }
- position.setLongitude(longitude);
-
- // Speed
- position.setSpeed(Double.parseDouble(parser.group(index++)));
-
- // Course
- position.setCourse(Double.parseDouble(parser.group(index++)));
-
- // Additional data
- position.set(Event.KEY_SATELLITES, parser.group(index++));
- position.set(Event.KEY_BATTERY, parser.group(index++));
- position.set(Event.KEY_INPUT, parser.group(index++));
- position.set(Event.KEY_OUTPUT, parser.group(index++));
- position.set(Event.PREFIX_TEMP + 1, parser.group(index++));
+ position.setLatitude(parser.nextCoordinate(Parser.CoordinateFormat.HEM_DEG_MIN_MIN));
+ position.setLongitude(parser.nextCoordinate(Parser.CoordinateFormat.HEM_DEG_MIN_MIN));
+ position.setSpeed(parser.nextDouble());
+ position.setCourse(parser.nextDouble());
+
+ position.set(Event.KEY_SATELLITES, parser.next());
+ position.set(Event.KEY_BATTERY, parser.next());
+ position.set(Event.KEY_INPUT, parser.next());
+ position.set(Event.KEY_OUTPUT, parser.next());
+ position.set(Event.PREFIX_TEMP + 1, parser.next());
return position;
}