aboutsummaryrefslogtreecommitdiff
path: root/src/org/traccar/protocol
diff options
context:
space:
mode:
Diffstat (limited to 'src/org/traccar/protocol')
-rw-r--r--src/org/traccar/protocol/CastelProtocolDecoder.java24
-rw-r--r--src/org/traccar/protocol/GatorProtocolDecoder.java6
-rw-r--r--src/org/traccar/protocol/V680ProtocolDecoder.java131
3 files changed, 64 insertions, 97 deletions
diff --git a/src/org/traccar/protocol/CastelProtocolDecoder.java b/src/org/traccar/protocol/CastelProtocolDecoder.java
index dcfa2868b..fe8d0a525 100644
--- a/src/org/traccar/protocol/CastelProtocolDecoder.java
+++ b/src/org/traccar/protocol/CastelProtocolDecoder.java
@@ -67,8 +67,14 @@ public class CastelProtocolDecoder extends BaseProtocolDecoder {
position.setCourse(buf.readUnsignedShort() % 360);
int flags = buf.readUnsignedByte();
- position.setLatitude((flags & 0x02) == 0 ? -lat : lat);
- position.setLongitude((flags & 0x01) == 0 ? -lon : lon);
+ if ((flags & 0x02) == 0) {
+ lat = -lat;
+ }
+ if ((flags & 0x01) == 0) {
+ lon = -lon;
+ }
+ position.setLatitude(lat);
+ position.setLongitude(lon);
position.setValid((flags & 0x0C) > 0);
position.set(Event.KEY_SATELLITES, flags >> 4);
}
@@ -95,17 +101,14 @@ public class CastelProtocolDecoder extends BaseProtocolDecoder {
response.writeByte(version);
response.writeBytes(id);
response.writeShort(ChannelBuffers.swapShort(MSG_HEARTBEAT_RESPONSE));
- response.writeShort(Checksum.crc16(Checksum.CRC16_X25, response.toByteBuffer(0, response.writerIndex())));
+ response.writeShort(
+ Checksum.crc16(Checksum.CRC16_X25, response.toByteBuffer(0, response.writerIndex())));
response.writeByte(0x0D); response.writeByte(0x0A);
channel.write(response, remoteAddress);
}
- } else if (
- type == MSG_LOGIN ||
- type == MSG_LOGOUT ||
- type == MSG_GPS ||
- type == MSG_ALARM ||
- type == MSG_CURRENT_LOCATION) {
+ } else if (type == MSG_LOGIN || type == MSG_LOGOUT || type == MSG_GPS
+ || type == MSG_ALARM || type == MSG_CURRENT_LOCATION) {
if (!identify(id.toString(Charset.defaultCharset()).trim(), channel, remoteAddress)) {
@@ -122,7 +125,8 @@ public class CastelProtocolDecoder extends BaseProtocolDecoder {
response.writeInt(0xFFFFFFFF);
response.writeShort(0);
response.writeInt((int) (System.currentTimeMillis() / 1000));
- response.writeShort(Checksum.crc16(Checksum.CRC16_X25, response.toByteBuffer(0, response.writerIndex())));
+ response.writeShort(
+ Checksum.crc16(Checksum.CRC16_X25, response.toByteBuffer(0, response.writerIndex())));
response.writeByte(0x0D); response.writeByte(0x0A);
channel.write(response, remoteAddress);
diff --git a/src/org/traccar/protocol/GatorProtocolDecoder.java b/src/org/traccar/protocol/GatorProtocolDecoder.java
index 2dfc2e56a..b8431003b 100644
--- a/src/org/traccar/protocol/GatorProtocolDecoder.java
+++ b/src/org/traccar/protocol/GatorProtocolDecoder.java
@@ -63,10 +63,8 @@ public class GatorProtocolDecoder extends BaseProtocolDecoder {
buf.readUnsignedByte(), buf.readUnsignedByte());
id = id.replaceFirst("^0+(?!$)", "");
- if (type == MSG_POSITION_DATA ||
- type == MSG_ROLLCALL_RESPONSE ||
- type == MSG_ALARM_DATA ||
- type == MSG_BLIND_AREA) {
+ if (type == MSG_POSITION_DATA || type == MSG_ROLLCALL_RESPONSE
+ || type == MSG_ALARM_DATA || type == MSG_BLIND_AREA) {
// Create new position
Position position = new Position();
diff --git a/src/org/traccar/protocol/V680ProtocolDecoder.java b/src/org/traccar/protocol/V680ProtocolDecoder.java
index 7628bed88..dc0dcd2f6 100644
--- a/src/org/traccar/protocol/V680ProtocolDecoder.java
+++ b/src/org/traccar/protocol/V680ProtocolDecoder.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.Event;
import org.traccar.model.Position;
@@ -31,24 +31,27 @@ public class V680ProtocolDecoder extends BaseProtocolDecoder {
super(protocol);
}
- private static final Pattern PATTERN = Pattern.compile(
- "(?:#(\\d+)#" + // IMEI
- "([^#]*)#)?" + // User
- "(\\d+)#" + // Fix
- "([^#]+)#" + // Password
- "([^#]+)#" + // Event
- "(\\d+)#" + // Packet number
- "([^#]+)?#?" + // GSM base station
- "(?:[^#]+#)?" +
- "(\\d+)?(\\d{2}\\.\\d+)," + // Longitude (DDDMM.MMMM)
- "([EW])," +
- "(\\d+)?(\\d{2}\\.\\d+)," + // Latitude (DDMM.MMMM)
- "([NS])," +
- "(\\d+\\.\\d+)," + // Speed
- "(\\d+\\.?\\d*)?#" + // Course
- "(\\d{2})(\\d{2})(\\d{2})#" + // Date (DDMMYY)
- "(\\d{2})(\\d{2})(\\d{2})" + // Time (HHMMSS)
- ".*");
+ private static final Pattern PATTERN = new PatternBuilder()
+ .groupBegin()
+ .num("#(d+)#") // imei
+ .xpr("([^#]*)#") // user
+ .groupEnd(true)
+ .num("(d+)#") // fix
+ .xpr("([^#]+)#") // password
+ .xpr("([^#]+)#") // event
+ .num("(d+)#") // packet number
+ .xpr("([^#]+)?#?") // gsm base station
+ .xpr("(?:[^#]+#)?")
+ .num("(d+)?(dd.d+),") // longitude
+ .xpr("([EW]),")
+ .num("(d+)?(dd.d+),") // latitude
+ .xpr("([NS]),")
+ .num("(d+.d+),") // speed
+ .num("(d+.?d*)?#") // course
+ .num("(dd)(dd)(dd)#") // date
+ .num("(dd)(dd)(dd)") // time
+ .any()
+ .compile();
@Override
protected Object decode(
@@ -58,89 +61,51 @@ public class V680ProtocolDecoder extends BaseProtocolDecoder {
String sentence = (String) msg;
sentence = sentence.trim();
- // Detect device ID
if (sentence.length() == 16) {
- String imei = sentence.substring(1, sentence.length());
- identify(imei, channel);
+
+ identify(sentence.substring(1, sentence.length()), channel);
+
} else {
- // Parse message
- 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());
- Integer index = 1;
- // Get device by IMEI
- String imei = parser.group(index++);
- if (imei != null) {
- identify(imei, channel);
+ if (parser.hasNext()) {
+ identify(parser.next(), channel);
}
if (!hasDeviceId()) {
return null;
}
position.setDeviceId(getDeviceId());
- // User
- position.set("user", parser.group(index++));
-
- // Validity
- position.setValid(Integer.parseInt(parser.group(index++)) > 0);
-
- // Password
- position.set("password", parser.group(index++));
-
- // Event
- position.set(Event.KEY_EVENT, parser.group(index++));
-
- // Packet number
- position.set("packet", parser.group(index++));
+ position.set("user", parser.next());
+ position.setValid(parser.nextInt() > 0);
+ position.set("password", parser.next());
+ position.set(Event.KEY_EVENT, parser.next());
+ position.set("packet", parser.next());
+ position.set(Event.KEY_GSM, parser.next());
- // GSM base station
- position.set(Event.KEY_GSM, parser.group(index++));
+ position.setLongitude(parser.nextCoordinate());
+ position.setLatitude(parser.nextCoordinate());
+ position.setSpeed(parser.nextDouble());
+ position.setCourse(parser.nextDouble());
- // Longitude
- String lon = parser.group(index++);
- Double longitude = (lon != null) ? Double.parseDouble(lon) : 0.0;
- longitude += Double.parseDouble(parser.group(index++)) / 60;
- if (parser.group(index++).compareTo("W") == 0) longitude = -longitude;
- position.setLongitude(longitude);
-
- // Latitude
- String lat = parser.group(index++);
- Double latitude = (lat != null) ? Double.parseDouble(lat) : 0.0;
- latitude += Double.parseDouble(parser.group(index++)) / 60;
- if (parser.group(index++).compareTo("S") == 0) latitude = -latitude;
- position.setLatitude(latitude);
-
- // Speed and Course
- position.setSpeed(Double.parseDouble(parser.group(index++)));
- String course = parser.group(index++);
- if (course != null) {
- position.setCourse(Double.parseDouble(course));
- }
-
- // Date
- Calendar time = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
- time.clear();
- int day = Integer.parseInt(parser.group(index++));
- int month = Integer.parseInt(parser.group(index++));
+ int day = parser.nextInt();
+ int month = parser.nextInt();
if (day == 0 && month == 0) {
return null; // invalid date
}
- time.set(Calendar.DAY_OF_MONTH, day);
- time.set(Calendar.MONTH, month - 1);
- time.set(Calendar.YEAR, 2000 + Integer.parseInt(parser.group(index++)));
-
- // Time
- 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());
+
+ DateBuilder dateBuilder = new DateBuilder()
+ .setDate(parser.nextInt(), month, day)
+ .setTime(parser.nextInt(), parser.nextInt(), parser.nextInt());
+ position.setTime(dateBuilder.getDate());
+
return position;
}