aboutsummaryrefslogtreecommitdiff
path: root/src/org/traccar/protocol/MiniFinderProtocolDecoder.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/org/traccar/protocol/MiniFinderProtocolDecoder.java')
-rw-r--r--src/org/traccar/protocol/MiniFinderProtocolDecoder.java99
1 files changed, 48 insertions, 51 deletions
diff --git a/src/org/traccar/protocol/MiniFinderProtocolDecoder.java b/src/org/traccar/protocol/MiniFinderProtocolDecoder.java
index 0bb9aee60..5a5ef3964 100644
--- a/src/org/traccar/protocol/MiniFinderProtocolDecoder.java
+++ b/src/org/traccar/protocol/MiniFinderProtocolDecoder.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,80 +32,76 @@ public class MiniFinderProtocolDecoder extends BaseProtocolDecoder {
super(protocol);
}
- private static final Pattern PATTERN = Pattern.compile(
- "!D," +
- "(\\d+)/(\\d+)/(\\d+)," + // Date
- "(\\d+):(\\d+):(\\d+)," + // Time
- "(-?\\d+\\.\\d+)," + // Latitude
- "(-?\\d+\\.\\d+)," + // Longitude
- "(\\d+\\.?\\d*)," + // Speed
- "(\\d+\\.?\\d*)," + // Course
- "(\\p{XDigit}+)," + // Flags
- "(-?\\d+\\.\\d+)," + // Altitude
- "(\\d+)," + // Battery
- "(\\d+)," + // Satellites in use
- "(\\d+)," + // Satellites in view
- "0");
+ private static final Pattern PATTERN = new PatternBuilder()
+ .expression("![AD],")
+ .number("(d+)/(d+)/(d+),") // date
+ .number("(d+):(d+):(d+),") // time
+ .number("(-?d+.d+),") // latitude
+ .number("(-?d+.d+),") // longitude
+ .number("(d+.?d*),") // speed
+ .number("(d+.?d*),") // course
+ .groupBegin()
+ .number("(x+),") // flags
+ .number("(-?d+.d+),") // altitude
+ .number("(d+),") // battery
+ .number("(d+),") // satellites in use
+ .number("(d+),") // satellites in view
+ .text("0")
+ .or()
+ .any()
+ .groupEnd()
+ .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("!1")) {
- // Identification
identify(sentence.substring(3, sentence.length()), channel);
- } else if (sentence.startsWith("!D") && hasDeviceId()) {
+ } else if ((sentence.startsWith("!D") || sentence.startsWith("!A")) && hasDeviceId()) {
- // Location
- 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;
+ DateBuilder dateBuilder = new DateBuilder()
+ .setDateReverse(parser.nextInt(), parser.nextInt(), parser.nextInt())
+ .setTime(parser.nextInt(), parser.nextInt(), parser.nextInt());
+ position.setTime(dateBuilder.getDate());
- // 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++)));
- time.set(Calendar.SECOND, Integer.parseInt(parser.group(index++)));
- position.setTime(time.getTime());
+ position.setLatitude(parser.nextDouble());
+ position.setLongitude(parser.nextDouble());
+ position.setSpeed(parser.nextDouble());
- // Location
- position.setLatitude(Double.parseDouble(parser.group(index++)));
- position.setLongitude(Double.parseDouble(parser.group(index++)));
- position.setSpeed(Double.parseDouble(parser.group(index++)));
- position.setCourse(Double.parseDouble(parser.group(index++)));
+ position.setCourse(parser.nextDouble());
+ if (position.getCourse() > 360) {
+ position.setCourse(0);
+ }
+
+ if (parser.hasNext(5)) {
- // Flags
- String flags = parser.group(index++);
- position.set(Event.KEY_FLAGS, flags);
- position.setValid((Integer.parseInt(flags, 16) & 0x01) != 0);
+ int flags = parser.nextInt(16);
+ position.set(Event.KEY_FLAGS, flags);
+ position.setValid(BitUtil.check(flags, 0));
- // Altitude
- position.setAltitude(Double.parseDouble(parser.group(index++)));
+ position.setAltitude(parser.nextDouble());
- // Battery
- position.set(Event.KEY_BATTERY, parser.group(index++));
+ position.set(Event.KEY_BATTERY, parser.next());
+ position.set(Event.KEY_SATELLITES, parser.next());
- // Satellites
- position.set(Event.KEY_SATELLITES, parser.group(index++));
+ }
return position;
+
}
return null;