aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAnton Tananaev <anton.tananaev@gmail.com>2015-10-21 20:53:20 +1300
committerAnton Tananaev <anton.tananaev@gmail.com>2015-10-21 20:53:20 +1300
commit122a26d2822f35b27b9cb0b217916d6b821d9654 (patch)
tree3f9d9240e7e8a078b0b116bbdf33205c62ef84a9 /src
parent6fc48063460a673815b182e0a8f3adf4db0fd08a (diff)
downloadtrackermap-server-122a26d2822f35b27b9cb0b217916d6b821d9654.tar.gz
trackermap-server-122a26d2822f35b27b9cb0b217916d6b821d9654.tar.bz2
trackermap-server-122a26d2822f35b27b9cb0b217916d6b821d9654.zip
Refactor TK102 and fix minor issues
Diffstat (limited to 'src')
-rw-r--r--src/org/traccar/helper/PatternBuilder.java2
-rw-r--r--src/org/traccar/protocol/SkypatrolProtocolDecoder.java66
-rw-r--r--src/org/traccar/protocol/Tk102ProtocolDecoder.java89
3 files changed, 53 insertions, 104 deletions
diff --git a/src/org/traccar/helper/PatternBuilder.java b/src/org/traccar/helper/PatternBuilder.java
index b68089b53..5092200be 100644
--- a/src/org/traccar/helper/PatternBuilder.java
+++ b/src/org/traccar/helper/PatternBuilder.java
@@ -43,7 +43,7 @@ public class PatternBuilder {
// TeXT
public PatternBuilder txt(String s) {
- pattern.append(s.replaceAll("([\\\\\\.\\[\\{\\(\\*\\+\\?\\^\\$\\|])", "\\\\$1"));
+ pattern.append(s.replaceAll("([\\\\\\.\\[\\{\\(\\)\\*\\+\\?\\^\\$\\|])", "\\\\$1"));
return this;
}
diff --git a/src/org/traccar/protocol/SkypatrolProtocolDecoder.java b/src/org/traccar/protocol/SkypatrolProtocolDecoder.java
index 0609e2094..231024731 100644
--- a/src/org/traccar/protocol/SkypatrolProtocolDecoder.java
+++ b/src/org/traccar/protocol/SkypatrolProtocolDecoder.java
@@ -17,13 +17,12 @@ package org.traccar.protocol;
import java.net.SocketAddress;
import java.nio.charset.Charset;
-import java.util.Calendar;
-import java.util.TimeZone;
import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.channel.Channel;
import org.traccar.BaseProtocolDecoder;
import org.traccar.Context;
import org.traccar.helper.BitUtil;
+import org.traccar.helper.DateBuilder;
import org.traccar.helper.Log;
import org.traccar.model.Event;
import org.traccar.model.Position;
@@ -56,11 +55,9 @@ public class SkypatrolProtocolDecoder extends BaseProtocolDecoder {
ChannelBuffer buf = (ChannelBuffer) msg;
- // Read header
int apiNumber = buf.readUnsignedShort();
int commandType = buf.readUnsignedByte();
- int messageType = buf.getUnsignedByte(buf.readerIndex()) >> 4;
- boolean needAck = (buf.readUnsignedByte() & 0xf) == 1;
+ int messageType = BitUtil.from(buf.readUnsignedByte(), 4);
long mask = defaultMask;
if (buf.readUnsignedByte() == 4) {
mask = buf.readUnsignedInt();
@@ -69,17 +66,14 @@ public class SkypatrolProtocolDecoder extends BaseProtocolDecoder {
// Binary position report
if (apiNumber == 5 && commandType == 2 && messageType == 1 && BitUtil.check(mask, 0)) {
- // Create new position
Position position = new Position();
position.setProtocol(getProtocolName());
- // Status code
if (BitUtil.check(mask, 1)) {
position.set(Event.KEY_STATUS, buf.readUnsignedInt());
}
- // Device id
- String id = null;
+ String id;
if (BitUtil.check(mask, 23)) {
id = buf.toString(buf.readerIndex(), 8, Charset.defaultCharset()).trim();
buf.skipBytes(8);
@@ -95,116 +89,92 @@ public class SkypatrolProtocolDecoder extends BaseProtocolDecoder {
}
position.setDeviceId(getDeviceId());
- // IO data
if (BitUtil.check(mask, 3)) {
- buf.readUnsignedShort();
+ buf.readUnsignedShort(); // io data
}
- // ADC 1
if (BitUtil.check(mask, 4)) {
- buf.readUnsignedShort();
+ buf.readUnsignedShort(); // adc 1
}
- // ADC 2
if (BitUtil.check(mask, 5)) {
- buf.readUnsignedShort();
+ buf.readUnsignedShort(); // adc 2
}
- // Function category
if (BitUtil.check(mask, 7)) {
- buf.readUnsignedByte();
+ buf.readUnsignedByte(); // function category
}
- Calendar time = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
- time.clear();
+ DateBuilder dateBuilder = new DateBuilder();
- // Date
if (BitUtil.check(mask, 8)) {
- time.set(Calendar.DAY_OF_MONTH, buf.readUnsignedByte());
- time.set(Calendar.MONTH, buf.readUnsignedByte() - 1);
- time.set(Calendar.YEAR, 2000 + buf.readUnsignedByte());
+ dateBuilder.setDateReverse(
+ buf.readUnsignedByte(), buf.readUnsignedByte(), buf.readUnsignedByte());
}
- // GPS status
if (BitUtil.check(mask, 9)) {
- position.setValid(buf.readUnsignedByte() == 1);
+ position.setValid(buf.readUnsignedByte() == 1); // gps status
}
- // Latitude
if (BitUtil.check(mask, 10)) {
position.setLatitude(convertCoordinate(buf.readUnsignedInt()));
}
- // Longitude
if (BitUtil.check(mask, 11)) {
position.setLongitude(convertCoordinate(buf.readUnsignedInt()));
}
- // Speed
if (BitUtil.check(mask, 12)) {
position.setSpeed(buf.readUnsignedShort() / 10.0);
}
- // Course
if (BitUtil.check(mask, 13)) {
position.setCourse(buf.readUnsignedShort() / 10.0);
}
- // Time
if (BitUtil.check(mask, 14)) {
- time.set(Calendar.HOUR_OF_DAY, buf.readUnsignedByte());
- time.set(Calendar.MINUTE, buf.readUnsignedByte());
- time.set(Calendar.SECOND, buf.readUnsignedByte());
+ dateBuilder.setTime(
+ buf.readUnsignedByte(), buf.readUnsignedByte(), buf.readUnsignedByte());
}
- position.setTime(time.getTime());
+ position.setTime(dateBuilder.getDate());
- // Altitude
if (BitUtil.check(mask, 15)) {
position.setAltitude(buf.readMedium());
}
- // Satellites
if (BitUtil.check(mask, 16)) {
position.set(Event.KEY_SATELLITES, buf.readUnsignedByte());
}
- // Battery percentage
if (BitUtil.check(mask, 17)) {
- buf.readUnsignedShort();
+ buf.readUnsignedShort(); // battery percentage
}
- // Trip odometer
if (BitUtil.check(mask, 20)) {
position.set("trip", buf.readUnsignedInt());
}
- // Odometer
if (BitUtil.check(mask, 21)) {
position.set(Event.KEY_ODOMETER, buf.readUnsignedInt());
}
- // Time of message generation
if (BitUtil.check(mask, 22)) {
- buf.skipBytes(6);
+ buf.skipBytes(6); // time of message generation
}
- // Battery level
if (BitUtil.check(mask, 24)) {
position.set(Event.KEY_POWER, buf.readUnsignedShort() / 1000.0);
}
- // GPS overspeed
if (BitUtil.check(mask, 25)) {
- buf.skipBytes(18);
+ buf.skipBytes(18); // gps overspeed
}
- // Cell information
if (BitUtil.check(mask, 26)) {
- buf.skipBytes(54);
+ buf.skipBytes(54); // cell information
}
- // Sequence number
if (BitUtil.check(mask, 28)) {
position.set(Event.KEY_INDEX, buf.readUnsignedShort());
}
diff --git a/src/org/traccar/protocol/Tk102ProtocolDecoder.java b/src/org/traccar/protocol/Tk102ProtocolDecoder.java
index 613513f89..b02a424e0 100644
--- a/src/org/traccar/protocol/Tk102ProtocolDecoder.java
+++ b/src/org/traccar/protocol/Tk102ProtocolDecoder.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.Position;
public class Tk102ProtocolDecoder extends BaseProtocolDecoder {
@@ -30,28 +30,33 @@ public class Tk102ProtocolDecoder extends BaseProtocolDecoder {
super(protocol);
}
- private static final Pattern PATTERN = Pattern.compile(
- "\\[.\\d{10}.\\(\\p{Upper}+" +
- "(\\d{2})(\\d{2})(\\d{2})" + // Time (HHMMSS)
- "([AV])" + // Validity
- "(\\d{2})(\\d{2}\\.\\d{4})" + // Latitude (DDMM.MMMM)
- "([NS])" +
- "(\\d{3})(\\d{2}\\.\\d{4})" + // Longitude (DDDMM.MMMM)
- "([EW])" +
- "(\\d{3}\\.\\d{3})" + // Speed
- "(\\d{2})(\\d{2})(\\d{2})" + // Date (DDMMYY)
- "\\d+.*\\)\\]?");
+ private static final Pattern PATTERN = new PatternBuilder()
+ .txt("[")
+ .xpr(".")
+ .num("d{10}")
+ .xpr(".")
+ .txt("(")
+ .xpr("[A-Z]+")
+ .num("(dd)(dd)(dd)") // Time (HHMMSS)
+ .xpr("([AV])") // Validity
+ .num("(dd)(dd.dddd)([NS])") // Latitude (DDMM.MMMM)
+ .num("(ddd)(dd.dddd)([EW])") // Longitude (DDDMM.MMMM)
+ .num("(ddd.ddd)") // Speed
+ .num("(dd)(dd)(dd)") // Date (DDMMYY)
+ .num("d+")
+ .any()
+ .txt(")")
+ .opt("]")
+ .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("[!")) {
- // Login
if (!identify(sentence.substring(14, 14 + 15), channel)) {
return null;
}
@@ -59,54 +64,28 @@ public class Tk102ProtocolDecoder extends BaseProtocolDecoder {
channel.write("[”0000000001" + sentence.substring(13) + "]");
}
- } else if (sentence.startsWith("[#")) {
-
- // TODO: EXIT Send response
-
} else if (hasDeviceId()) {
- 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;
-
- // 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++)));
-
- // 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;
- if (parser.group(index++).compareTo("W") == 0) longitude = -longitude;
- position.setLongitude(longitude);
-
- // Speed
- position.setSpeed(Double.parseDouble(parser.group(index++)));
-
- // 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 dateBuilder = new DateBuilder()
+ .setTime(parser.nextInt(), parser.nextInt(), parser.nextInt());
+
+ position.setValid(parser.next().equals("A"));
+ position.setLatitude(parser.nextCoordinate());
+ position.setLongitude(parser.nextCoordinate());
+ position.setSpeed(parser.nextDouble());
+
+ dateBuilder.setDateReverse(parser.nextInt(), parser.nextInt(), parser.nextInt());
+ position.setTime(dateBuilder.getDate());
+
return position;
}