aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAnton Tananaev <anton.tananaev@gmail.com>2015-11-05 11:56:32 +1300
committerAnton Tananaev <anton.tananaev@gmail.com>2015-11-05 11:56:32 +1300
commit8598581c0bdfc7e5ca842da7363e107232943849 (patch)
treeb152efa0e56cf2f116db7d0a6a198e9ae8c5b645 /src
parent3b3f7dddcb9a7ac43651cacc7cf05247a4f7d82b (diff)
downloadtrackermap-server-8598581c0bdfc7e5ca842da7363e107232943849.tar.gz
trackermap-server-8598581c0bdfc7e5ca842da7363e107232943849.tar.bz2
trackermap-server-8598581c0bdfc7e5ca842da7363e107232943849.zip
Refactor Haicom protocol decoder
Diffstat (limited to 'src')
-rw-r--r--src/org/traccar/protocol/GpsMarkerProtocolDecoder.java4
-rw-r--r--src/org/traccar/protocol/HaicomProtocolDecoder.java138
2 files changed, 64 insertions, 78 deletions
diff --git a/src/org/traccar/protocol/GpsMarkerProtocolDecoder.java b/src/org/traccar/protocol/GpsMarkerProtocolDecoder.java
index e0f8d529e..9313ad099 100644
--- a/src/org/traccar/protocol/GpsMarkerProtocolDecoder.java
+++ b/src/org/traccar/protocol/GpsMarkerProtocolDecoder.java
@@ -38,9 +38,9 @@ public class GpsMarkerProtocolDecoder extends BaseProtocolDecoder {
.number("(d{15})") // imei
.number("T(dd)(dd)(dd)") // date
.number("(dd)(dd)(dd)?") // time
- .number("([NS])")
+ .expression("([NS])")
.number("(dd)(dd)(dddd)") // latitude
- .number("([EW])")
+ .expression("([EW])")
.number("(ddd)(dd)(dddd)") // longitude
.number("(ddd)") // speed
.number("(ddd)") // course
diff --git a/src/org/traccar/protocol/HaicomProtocolDecoder.java b/src/org/traccar/protocol/HaicomProtocolDecoder.java
index 42d806728..d187a4a63 100644
--- a/src/org/traccar/protocol/HaicomProtocolDecoder.java
+++ b/src/org/traccar/protocol/HaicomProtocolDecoder.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2014 Anton Tananaev (anton.tananaev@gmail.com)
+ * Copyright 2014 - 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,93 +32,78 @@ public class HaicomProtocolDecoder extends BaseProtocolDecoder {
super(protocol);
}
- private static final Pattern PATTERN = Pattern.compile(
- "\\$GPRS" +
- "(\\d+)," + // IMEI
- "([^,]+)," + // Version
- "(\\d{2})(\\d{2})(\\d{2})," + // Date
- "(\\d{2})(\\d{2})(\\d{2})," + // Time
- "(\\d)" + // Flags
- "(\\d{2})(\\d{5})" + // Latitude (DDMMMMM)
- "(\\d{3})(\\d{5})," + // Longitude (DDDMMMMM)
- "(\\d+)," + // Speed
- "(\\d+)," + // Course
- "(\\d+)," + // Status
- "(\\d+)?," + // GPRS counting value
- "(\\d+)?," + // GPS power saving counting value
- "(\\d+)," + // Switch status
- "(\\d+)" + // Relay status
- "(?:[LH]{2})?" + // Power status
- "#V(\\d+).*"); // Battery
+ private static final Pattern PATTERN = new PatternBuilder()
+ .text("$GPRS")
+ .number("(d+),") // IMEI
+ .expression("([^,]+),") // Version
+ .number("(dd)(dd)(dd),") // Date
+ .number("(dd)(dd)(dd),") // Time
+ .number("(d)") // Flags
+ .number("(dd)(d{5})") // Latitude (DDMMMMM)
+ .number("(ddd)(d{5}),") // Longitude (DDDMMMMM)
+ .number("(d+),") // Speed
+ .number("(d+),") // Course
+ .number("(d+),") // Status
+ .number("(d+)?,") // GPRS counting value
+ .number("(d+)?,") // GPS power saving counting value
+ .number("(d+),") // Switch status
+ .number("(d+)") // Relay status
+ .expression("(?:[LH]{2})?") // Power status
+ .number("#V(d+)") // Battery
+ .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());
- // Firmware version
- position.set(Event.KEY_VERSION, parser.group(index++));
-
- // Date
- 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());
-
- // Validity
- int flags = Integer.parseInt(parser.group(index++));
- position.setValid((flags & 0x1) != 0);
-
- // Latitude
- Double latitude = Double.parseDouble(parser.group(index++));
- latitude += Double.parseDouble(parser.group(index++)) / 60000;
- if ((flags & 0x4) == 0) latitude = -latitude;
- position.setLatitude(latitude);
-
- // Longitude
- Double longitude = Double.parseDouble(parser.group(index++));
- longitude += Double.parseDouble(parser.group(index++)) / 60000;
- if ((flags & 0x2) == 0) longitude = -longitude;
- position.setLongitude(longitude);
-
- // Speed
- position.setSpeed(Double.parseDouble(parser.group(index++)) / 10);
-
- // Course
- position.setCourse(Double.parseDouble(parser.group(index++)) / 10);
-
- // Additional data
- position.set(Event.KEY_STATUS, parser.group(index++));
- position.set(Event.KEY_GSM, parser.group(index++));
- position.set(Event.KEY_GPS, parser.group(index++));
- position.set(Event.KEY_INPUT, parser.group(index++));
- position.set(Event.KEY_OUTPUT, parser.group(index++));
- position.set(Event.KEY_BATTERY, Double.parseDouble(parser.group(index++)) / 10);
+ position.set(Event.KEY_VERSION, parser.next());
+
+ DateBuilder dateBuilder = new DateBuilder()
+ .setDate(parser.nextInt(), parser.nextInt(), parser.nextInt())
+ .setTime(parser.nextInt(), parser.nextInt(), parser.nextInt());
+ position.setTime(dateBuilder.getDate());
+
+ int flags = parser.nextInt();
+
+ position.setValid(BitUtil.check(flags, 0));
+
+ double latitude = parser.nextDouble() + parser.nextDouble() / 60000;
+ if (BitUtil.check(flags, 2)) {
+ position.setLatitude(latitude);
+ } else {
+ position.setLatitude(-latitude);
+ }
+
+ double longitude = parser.nextDouble() + parser.nextDouble() / 60000;
+ if (BitUtil.check(flags, 1)) {
+ position.setLongitude(longitude);
+ } else {
+ position.setLongitude(-longitude);
+ }
+
+ position.setSpeed(parser.nextDouble() / 10);
+ position.setCourse(parser.nextDouble() / 10);
+
+ position.set(Event.KEY_STATUS, parser.next());
+ position.set(Event.KEY_GSM, parser.next());
+ position.set(Event.KEY_GPS, parser.next());
+ position.set(Event.KEY_INPUT, parser.next());
+ position.set(Event.KEY_OUTPUT, parser.next());
+ position.set(Event.KEY_BATTERY, parser.nextDouble() / 10);
return position;
}