aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/org/traccar/protocol/CityeasyProtocolDecoder.java105
1 files changed, 47 insertions, 58 deletions
diff --git a/src/org/traccar/protocol/CityeasyProtocolDecoder.java b/src/org/traccar/protocol/CityeasyProtocolDecoder.java
index 67da05a8d..213dd90d0 100644
--- a/src/org/traccar/protocol/CityeasyProtocolDecoder.java
+++ b/src/org/traccar/protocol/CityeasyProtocolDecoder.java
@@ -17,15 +17,15 @@ package org.traccar.protocol;
import java.net.SocketAddress;
import java.nio.charset.Charset;
-import java.util.Calendar;
-import java.util.TimeZone;
-import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.buffer.ChannelBuffers;
import org.jboss.netty.channel.Channel;
import org.traccar.BaseProtocolDecoder;
import org.traccar.helper.Checksum;
+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;
@@ -35,21 +35,24 @@ public class CityeasyProtocolDecoder extends BaseProtocolDecoder {
super(protocol);
}
- private static final Pattern PATTERN = Pattern.compile(
- "(\\d{4})(\\d{2})(\\d{2})" + // Date
- "(\\d{2})(\\d{2})(\\d{2})," + // Time
- "([AV])," + // Validity
- "(\\d+)," + // Satellites
- "([NS]),(\\d+\\.\\d+)," + // Latitude
- "([EW]),(\\d+\\.\\d+)," + // Longitude
- "(\\d+\\.\\d)," + // Speed
- "(\\d+\\.\\d)," + // HDOP
- "(\\d+\\.\\d);" + // Altitude
- "(\\d+)," + // MCC
- "(\\d+)," + // MNC
- "(\\d+)," + // LAC
- "(\\d+)" + // Cell
- ".*");
+ private static final Pattern PATTERN = new PatternBuilder()
+ .groupBegin()
+ .number("(dddd)(dd)(dd)") // date
+ .number("(dd)(dd)(dd),") // time
+ .number("([AV]),") // validity
+ .number("(d+),") // satellites
+ .number("([NS]),(d+.d+),") // latitude
+ .number("([EW]),(d+.d+),") // longitude
+ .number("(d+.d),") // speed
+ .number("(d+.d),") // hdop
+ .number("(d+.d)") // altitude
+ .groupEnd("?").text(";")
+ .number("(d+),") // mcc
+ .number("(d+),") // mnc
+ .number("(d+),") // lac
+ .number("(d+)") // cell
+ .any()
+ .compile();
public static final int MSG_ADDRESS_REQUEST = 0x0001;
public static final int MSG_STATUS = 0x0002;
@@ -62,8 +65,7 @@ public class CityeasyProtocolDecoder extends BaseProtocolDecoder {
@Override
protected Object decode(
- Channel channel, SocketAddress remoteAddress, Object msg)
- throws Exception {
+ Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
ChannelBuffer buf = (ChannelBuffer) msg;
@@ -80,7 +82,7 @@ public class CityeasyProtocolDecoder extends BaseProtocolDecoder {
if (type == MSG_LOCATION_REPORT || type == MSG_LOCATION_REQUEST) {
String sentence = buf.toString(buf.readerIndex(), buf.readableBytes() - 8, Charset.defaultCharset());
- Matcher parser = PATTERN.matcher(sentence);
+ Parser parser = new Parser(PATTERN, sentence);
if (!parser.matches()) {
return null;
}
@@ -89,46 +91,33 @@ public class CityeasyProtocolDecoder extends BaseProtocolDecoder {
position.setProtocol(getProtocolName());
position.setDeviceId(getDeviceId());
- Integer index = 1;
-
- // Date and time
- Calendar time = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
- time.clear();
- time.set(Calendar.YEAR, 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());
-
- position.setValid(parser.group(index++).equals("A"));
- position.set(Event.KEY_SATELLITES, parser.group(index++));
-
- // Latitude
- String hemisphere = parser.group(index++);
- double latitude = Double.parseDouble(parser.group(index++));
- if (hemisphere.compareTo("S") == 0) {
- latitude = -latitude;
- }
- position.setLatitude(latitude);
+ if (parser.hasNext(15)) {
- // Longitude
- hemisphere = parser.group(index++);
- double longitude = Double.parseDouble(parser.group(index++));
- if (hemisphere.compareTo("W") == 0) {
- longitude = -longitude;
- }
- position.setLongitude(longitude);
+ DateBuilder dateBuilder = new DateBuilder()
+ .setDate(parser.nextInt(), parser.nextInt(), parser.nextInt())
+ .setTime(parser.nextInt(), parser.nextInt(), parser.nextInt());
+ position.setTime(dateBuilder.getDate());
+
+ position.setValid(parser.next().equals("A"));
+ position.set(Event.KEY_SATELLITES, parser.next());
+
+ position.setLatitude(parser.nextCoordinate(Parser.CoordinateFormat.HEM_DEG));
+ position.setLongitude(parser.nextCoordinate(Parser.CoordinateFormat.HEM_DEG));
- position.setSpeed(Double.parseDouble(parser.group(index++)));
- position.set(Event.KEY_HDOP, Double.parseDouble(parser.group(index++)));
- position.setAltitude(Double.parseDouble(parser.group(index++)));
+ position.setSpeed(parser.nextDouble());
+ position.set(Event.KEY_HDOP, parser.nextDouble());
+ position.setAltitude(parser.nextDouble());
+
+ } else {
+
+ getLastLocation(position, null);
+
+ }
- position.set(Event.KEY_MCC, Integer.parseInt(parser.group(index++)));
- position.set(Event.KEY_MNC, Integer.parseInt(parser.group(index++)));
- position.set(Event.KEY_LAC, Integer.parseInt(parser.group(index++)));
- position.set(Event.KEY_CELL, Integer.parseInt(parser.group(index++)));
+ position.set(Event.KEY_MCC, parser.nextInt());
+ position.set(Event.KEY_MNC, parser.nextInt());
+ position.set(Event.KEY_LAC, parser.nextInt());
+ position.set(Event.KEY_CELL, parser.nextInt());
return position;
}