aboutsummaryrefslogtreecommitdiff
path: root/src/org
diff options
context:
space:
mode:
authorAnton Tananaev <anton.tananaev@gmail.com>2015-11-04 17:34:27 +1300
committerAnton Tananaev <anton.tananaev@gmail.com>2015-11-04 17:34:27 +1300
commit722f69031f53bdab5a72e926f101ef4b0df1c828 (patch)
tree18122eb4d5a689b179d2ffad425e528766e6a517 /src/org
parentad55df29750fa9df1b37b9af34c2f744c4e0b4d4 (diff)
downloadtrackermap-server-722f69031f53bdab5a72e926f101ef4b0df1c828.tar.gz
trackermap-server-722f69031f53bdab5a72e926f101ef4b0df1c828.tar.bz2
trackermap-server-722f69031f53bdab5a72e926f101ef4b0df1c828.zip
Refactor Meitrack protocol decoder
Diffstat (limited to 'src/org')
-rw-r--r--src/org/traccar/protocol/MeitrackProtocolDecoder.java205
1 files changed, 84 insertions, 121 deletions
diff --git a/src/org/traccar/protocol/MeitrackProtocolDecoder.java b/src/org/traccar/protocol/MeitrackProtocolDecoder.java
index 872055f19..e6e3dd057 100644
--- a/src/org/traccar/protocol/MeitrackProtocolDecoder.java
+++ b/src/org/traccar/protocol/MeitrackProtocolDecoder.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2012 - 2014 Anton Tananaev (anton.tananaev@gmail.com)
+ * Copyright 2012 - 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.
@@ -17,16 +17,16 @@ package org.traccar.protocol;
import java.net.SocketAddress;
import java.nio.charset.Charset;
-import java.util.Calendar;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
-import java.util.TimeZone;
-import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.jboss.netty.buffer.ChannelBuffer;
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.helper.UnitsConverter;
import org.traccar.model.Event;
import org.traccar.model.Position;
@@ -37,138 +37,114 @@ public class MeitrackProtocolDecoder extends BaseProtocolDecoder {
super(protocol);
}
- private static final Pattern PATTERN = Pattern.compile(
- "\\$\\$." + // Flag
- "\\d+," + // Length
- "(\\d+)," + // IMEI
- "\\p{XDigit}{3}," + // Command
- "(?:\\d+,)?" +
- "(\\d+)," + // Event
- "(-?\\d+\\.\\d+)," + // Latitude
- "(-?\\d+\\.\\d+)," + // Longitude
- "(\\d{2})(\\d{2})(\\d{2})" + // Date (YYMMDD)
- "(\\d{2})(\\d{2})(\\d{2})," + // Time (HHMMSS)
- "([AV])," + // Validity
- "(\\d+)," + // Satellites
- "(\\d+)," + // GSM Signal
- "(\\d+\\.?\\d*)," + // Speed
- "(\\d+)," + // Course
- "(\\d+\\.?\\d*)," + // HDOP
- "(-?\\d+)," + // Altitude
- "(\\d+)," + // Odometer
- "(\\d+)," + // Runtime
- "(\\d+\\|\\d+\\|\\p{XDigit}+\\|\\p{XDigit}+)," + // Cell
- "(\\p{XDigit}+)," + // State
- "(\\p{XDigit}+)?\\|" + // ADC1
- "(\\p{XDigit}+)?\\|" + // ADC2
- "(\\p{XDigit}+)?\\|" + // ADC3
- "(\\p{XDigit}+)\\|" + // Battery
- "(\\p{XDigit}+)," + // Power
- "(?:([^,]+)?," + // Event Specific
- "[^,]*," + // Reserved
- "\\d*," + // Protocol
- "(\\p{XDigit}{4})?)?" + // Fuel
- ".*\\*\\p{XDigit}{2}(?:\r\n)?");
+ private static final Pattern PATTERN = new PatternBuilder()
+ .text("$$").expression(".") // flag
+ .number("d+,") // length
+ .number("(d+),") // imei
+ .number("xxx,") // command
+ .number("d+,").optional()
+ .number("(d+),") // event
+ .number("(-?d+.d+),") // latitude
+ .number("(-?d+.d+),") // longitude
+ .number("(dd)(dd)(dd)") // date (ddmmyy)
+ .number("(dd)(dd)(dd),") // time
+ .number("([AV]),") // validity
+ .number("(d+),") // satellites
+ .number("(d+),") // gsm signal
+ .number("(d+.?d*),") // speed
+ .number("(d+),") // course
+ .number("(d+.?d*),") // hdop
+ .number("(-?d+),") // altitude
+ .number("(d+),") // odometer
+ .number("(d+),") // runtime
+ .number("(d+|")
+ .number("d+|")
+ .number("x+|")
+ .number("x+),") // cell
+ .number("(x+),") // state
+ .number("(x+)?|") // adc1
+ .number("(x+)?|") // adc2
+ .number("(x+)?|") // adc3
+ .number("(x+)|") // battery
+ .number("(x+),") // power
+ .groupBegin()
+ .expression("([^,]+)?,") // event specific
+ .expression("[^,]*,") // reserved
+ .number("d*,") // protocol
+ .number("(x{4})?") // fuel
+ .groupEnd("?")
+ .any()
+ .text("*")
+ .number("xx")
+ .text("\r\n").optional()
+ .compile();
private Position decodeRegularMessage(Channel channel, ChannelBuffer buf) {
- // Parse message
- String sentence = buf.toString(Charset.defaultCharset());
- Matcher parser = PATTERN.matcher(sentence);
+ Parser parser = new Parser(PATTERN, buf.toString(Charset.defaultCharset()));
if (!parser.matches()) {
return null;
}
- // Create new position
Position position = new Position();
position.setProtocol(getProtocolName());
- Integer index = 1;
-
- // Identification
- if (!identify(parser.group(index++), channel)) {
+ if (!identify(parser.next(), channel)) {
return null;
}
position.setDeviceId(getDeviceId());
- // Event
- int event = Integer.parseInt(parser.group(index++));
+ int event = parser.nextInt();
position.set(Event.KEY_EVENT, event);
- // Coordinates
- position.setLatitude(Double.parseDouble(parser.group(index++)));
- position.setLongitude(Double.parseDouble(parser.group(index++)));
-
- // Time
- 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
- position.setValid(parser.group(index++).compareTo("A") == 0);
+ position.setLatitude(parser.nextDouble());
+ position.setLongitude(parser.nextDouble());
- // Satellites
- position.set(Event.KEY_SATELLITES, parser.group(index++));
+ DateBuilder dateBuilder = new DateBuilder()
+ .setDate(parser.nextInt(), parser.nextInt(), parser.nextInt())
+ .setTime(parser.nextInt(), parser.nextInt(), parser.nextInt());
+ position.setTime(dateBuilder.getDate());
- // GSM Signal
- position.set(Event.KEY_GSM, parser.group(index++));
+ position.setValid(parser.next().equals("A"));
- // Speed
- position.setSpeed(UnitsConverter.knotsFromKph(Double.parseDouble(parser.group(index++))));
+ position.set(Event.KEY_SATELLITES, parser.next());
+ position.set(Event.KEY_GSM, parser.next());
- // Course
- position.setCourse(Double.parseDouble(parser.group(index++)));
+ position.setSpeed(UnitsConverter.knotsFromKph(parser.nextDouble()));
+ position.setCourse(parser.nextDouble());
- // HDOP
- position.set(Event.KEY_HDOP, parser.group(index++));
+ position.set(Event.KEY_HDOP, parser.next());
- // Altitude
- position.setAltitude(Double.parseDouble(parser.group(index++)));
+ position.setAltitude(parser.nextDouble());
- // Other
- position.set(Event.KEY_ODOMETER, parser.group(index++));
- position.set("runtime", parser.group(index++));
- position.set(Event.KEY_CELL, parser.group(index++));
- position.set(Event.KEY_STATUS, parser.group(index++));
+ position.set(Event.KEY_ODOMETER, parser.next());
+ position.set("runtime", parser.next());
+ position.set(Event.KEY_CELL, parser.next());
+ position.set(Event.KEY_STATUS, parser.next());
- // ADC
- String adc1 = parser.group(index++);
- if (adc1 != null) {
- position.set(Event.PREFIX_ADC + 1, Integer.parseInt(adc1, 16));
- }
- String adc2 = parser.group(index++);
- if (adc2 != null) {
- position.set(Event.PREFIX_ADC + 2, Integer.parseInt(adc2, 16));
- }
- String adc3 = parser.group(index++);
- if (adc3 != null) {
- position.set(Event.PREFIX_ADC + 3, Integer.parseInt(adc3, 16));
+ for (int i = 1; i <= 3; i++) {
+ if (parser.hasNext()) {
+ position.set(Event.PREFIX_ADC + i, parser.nextInt(16));
+ }
}
- position.set(Event.KEY_BATTERY, Integer.parseInt(parser.group(index++), 16));
- position.set(Event.KEY_POWER, Integer.parseInt(parser.group(index++), 16));
- // Event specific
- String data = parser.group(index++);
- if (data != null && !data.isEmpty()) {
+ position.set(Event.KEY_BATTERY, parser.nextInt(16));
+ position.set(Event.KEY_POWER, parser.nextInt(16));
+
+ String eventData = parser.next();
+ if (eventData != null && !eventData.isEmpty()) {
switch (event) {
case 37:
- position.set(Event.KEY_RFID, data);
+ position.set(Event.KEY_RFID, eventData);
break;
default:
- position.set("event-data", data);
+ position.set("event-data", eventData);
break;
}
}
- // Fuel
- String fuel = parser.group(index++);
- if (fuel != null) {
+ if (parser.hasNext()) {
+ String fuel = parser.next();
position.set(Event.KEY_FUEL,
Integer.parseInt(fuel.substring(0, 2), 16) + Integer.parseInt(fuel.substring(2), 16) * 0.01);
}
@@ -182,7 +158,6 @@ public class MeitrackProtocolDecoder extends BaseProtocolDecoder {
String flag = buf.toString(2, 1, Charset.defaultCharset());
int index = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) ',');
- // Identification
String imei = buf.toString(index + 1, 15, Charset.defaultCharset());
if (!identify(imei, channel)) {
return null;
@@ -196,38 +171,25 @@ public class MeitrackProtocolDecoder extends BaseProtocolDecoder {
position.setProtocol(getProtocolName());
position.setDeviceId(getDeviceId());
- // Event
position.set(Event.KEY_EVENT, buf.readUnsignedByte());
- // Location
position.setLatitude(buf.readInt() * 0.000001);
position.setLongitude(buf.readInt() * 0.000001);
- // Time (946684800 - timestamp for 2000-01-01)
- position.setTime(new Date((946684800 + buf.readUnsignedInt()) * 1000));
+ position.setTime(new Date((946684800 + buf.readUnsignedInt()) * 1000)); // 946684800 = 2000-01-01
- // Validity
position.setValid(buf.readUnsignedByte() == 1);
- // Satellites
position.set(Event.KEY_SATELLITES, buf.readUnsignedByte());
-
- // GSM Signal
position.set(Event.KEY_GSM, buf.readUnsignedByte());
- // Speed
position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedShort()));
-
- // Course
position.setCourse(buf.readUnsignedShort());
- // HDOP
position.set(Event.KEY_HDOP, buf.readUnsignedShort() * 0.1);
- // Altitude
position.setAltitude(buf.readUnsignedShort());
- // Other
position.set(Event.KEY_ODOMETER, buf.readUnsignedInt());
position.set("runtime", buf.readUnsignedInt());
position.set(Event.KEY_CELL,
@@ -235,25 +197,26 @@ public class MeitrackProtocolDecoder extends BaseProtocolDecoder {
buf.readUnsignedShort() + "|" + buf.readUnsignedShort());
position.set(Event.KEY_STATUS, buf.readUnsignedShort());
- // ADC
position.set(Event.PREFIX_ADC + 1, buf.readUnsignedShort());
position.set(Event.KEY_BATTERY, buf.readUnsignedShort() * 0.01);
position.set(Event.KEY_POWER, buf.readUnsignedShort());
buf.readUnsignedInt(); // geo-fence
+
positions.add(position);
}
- // Delete recorded data
if (channel != null) {
StringBuilder command = new StringBuilder("@@");
command.append(flag).append(27 + positions.size() / 10).append(",");
command.append(imei).append(",CCC,").append(positions.size()).append("*");
int checksum = 0;
- for (int i = 0; i < command.length(); i += 1) checksum += command.charAt(i);
+ for (int i = 0; i < command.length(); i += 1) {
+ checksum += command.charAt(i);
+ }
command.append(String.format("%02x", checksum & 0xff).toUpperCase());
command.append("\r\n");
- channel.write(command.toString());
+ channel.write(command.toString()); // delete processed data
}
return positions;