aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAnton Tananaev <anton.tananaev@gmail.com>2015-10-25 12:16:28 +1300
committerAnton Tananaev <anton.tananaev@gmail.com>2015-10-25 12:16:28 +1300
commit638f9d94bccad164103429dc5d0c7717d12f0fe6 (patch)
treefe6e6c36c795f950bc4ee55df6bba4f20f7268ba /src
parentf0be675bf27de3c1db5a8eb9e0df7bb82feee440 (diff)
downloadtrackermap-server-638f9d94bccad164103429dc5d0c7717d12f0fe6.tar.gz
trackermap-server-638f9d94bccad164103429dc5d0c7717d12f0fe6.tar.bz2
trackermap-server-638f9d94bccad164103429dc5d0c7717d12f0fe6.zip
Refactor Xexun protocol decoder
Diffstat (limited to 'src')
-rw-r--r--src/org/traccar/protocol/XexunProtocolDecoder.java86
1 files changed, 25 insertions, 61 deletions
diff --git a/src/org/traccar/protocol/XexunProtocolDecoder.java b/src/org/traccar/protocol/XexunProtocolDecoder.java
index 89ea954bc..7a9e880a8 100644
--- a/src/org/traccar/protocol/XexunProtocolDecoder.java
+++ b/src/org/traccar/protocol/XexunProtocolDecoder.java
@@ -22,6 +22,8 @@ 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;
@@ -67,8 +69,12 @@ public class XexunProtocolDecoder extends BaseProtocolDecoder {
protected Object decode(
Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
- Pattern pattern = full ? PATTERN_FULL : PATTERN_BASIC;
- Matcher parser = pattern.matcher((String) msg);
+ Pattern pattern = PATTERN_BASIC;
+ if (full) {
+ pattern = PATTERN_FULL;
+ }
+
+ Parser parser = new Parser(pattern, (String) msg);
if (!parser.matches()) {
return null;
}
@@ -76,79 +82,37 @@ public class XexunProtocolDecoder extends BaseProtocolDecoder {
Position position = new Position();
position.setProtocol(getProtocolName());
- Integer index = 1;
-
if (full) {
- position.set("serial", parser.group(index++));
- position.set("number", parser.group(index++));
+ position.set("serial", parser.next());
+ position.set("number", parser.next());
}
- // 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++)));
- time.set(Calendar.MILLISECOND, 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;
- String hemisphere = parser.group(index++);
- if (hemisphere != null && hemisphere.compareTo("W") == 0) {
- longitude = -longitude;
- }
- position.setLongitude(longitude);
+ DateBuilder dateBuilder = new DateBuilder()
+ .setTime(parser.nextInt(), parser.nextInt(), parser.nextInt(), parser.nextInt());
- // Speed
- position.setSpeed(Double.parseDouble(parser.group(index++)));
+ position.setValid(parser.next().equals("A"));
+ position.setLatitude(parser.nextCoordinate());
+ position.setLongitude(parser.nextCoordinate());
+ position.setSpeed(parser.nextDouble());
+ position.setCourse(parser.nextDouble());
- // Course
- String course = parser.group(index++);
- if (course != null) {
- position.setCourse(Double.parseDouble(course));
- }
-
- // 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.setDateReverse(parser.nextInt(), parser.nextInt(), parser.nextInt());
+ position.setTime(dateBuilder.getDate());
- // Signal
- position.set("signal", parser.group(index++));
+ position.set("signal", parser.next());
+ position.set(Event.KEY_ALARM, parser.next());
- // Alarm
- position.set(Event.KEY_ALARM, parser.group(index++));
-
- // Get device by IMEI
- if (!identify(parser.group(index++), channel)) {
+ if (!identify(parser.next(), channel)) {
return null;
}
position.setDeviceId(getDeviceId());
if (full) {
+ position.set(Event.KEY_SATELLITES, parser.next().replaceFirst("^0*(?![\\.$])", ""));
- // Satellites
- position.set(Event.KEY_SATELLITES, parser.group(index++).replaceFirst("^0*(?![\\.$])", ""));
-
- // Altitude
- String altitude = parser.group(index++);
- if (altitude != null) {
- position.setAltitude(Double.parseDouble(altitude));
- }
+ position.setAltitude(parser.nextDouble());
- // Power
- position.set(Event.KEY_POWER, Double.parseDouble(parser.group(index++)));
+ position.set(Event.KEY_POWER, parser.nextDouble());
}
return position;