aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnton Tananaev <anton.tananaev@gmail.com>2015-11-05 14:04:08 +1300
committerAnton Tananaev <anton.tananaev@gmail.com>2015-11-05 14:04:08 +1300
commit08dc9eb08cf5a1f3c474d4ac5e421db4259ec92e (patch)
tree64d4b1006bd77533524a552fb81dae2832b1a4ec
parent8598581c0bdfc7e5ca842da7363e107232943849 (diff)
downloadtrackermap-server-08dc9eb08cf5a1f3c474d4ac5e421db4259ec92e.tar.gz
trackermap-server-08dc9eb08cf5a1f3c474d4ac5e421db4259ec92e.tar.bz2
trackermap-server-08dc9eb08cf5a1f3c474d4ac5e421db4259ec92e.zip
Refactor SunTech protocol decoder
-rw-r--r--src/org/traccar/protocol/SuntechProtocolDecoder.java100
-rw-r--r--test/org/traccar/protocol/SuntechProtocolDecoderTest.java6
2 files changed, 46 insertions, 60 deletions
diff --git a/src/org/traccar/protocol/SuntechProtocolDecoder.java b/src/org/traccar/protocol/SuntechProtocolDecoder.java
index 19eed1197..81a7c3041 100644
--- a/src/org/traccar/protocol/SuntechProtocolDecoder.java
+++ b/src/org/traccar/protocol/SuntechProtocolDecoder.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.helper.UnitsConverter;
import org.traccar.model.Event;
import org.traccar.model.Position;
@@ -32,82 +32,66 @@ public class SuntechProtocolDecoder extends BaseProtocolDecoder {
super(protocol);
}
- private static final Pattern PATTERN = Pattern.compile(
- "S.\\d{3}(?:\\w{3})?;" + // Header
- "(?:([^;]+);)?" + // Type
- "(\\d{6,});" + // Device ID
- "(?:\\d+;)?" +
- "(\\d+);" + // Version
- "(\\d{4})(\\d{2})(\\d{2});" + // Date (YYYYMMDD)
- "(\\d{2}):(\\d{2}):(\\d{2});" + // Time (HH:MM:SS)
- "(?:(\\p{XDigit}+);)?" + // Cell
- "([-\\+]\\d{2}\\.\\d+);" + // Latitude
- "([-\\+]\\d{3}\\.\\d+);" + // Longitude
- "(\\d{3}\\.\\d{3});" + // Speed
- "(\\d{3}\\.\\d{2});" + // Course
- "(?:\\d+;)?" +
- "(\\d+\\.\\d+)?" + // Battery
- ".*"); // Full format
+ private static final Pattern PATTERN = new PatternBuilder()
+ .expression("S.")
+ .number("ddd")
+ .expression("(?:[A-Z]{3})?;") // header
+ .expression("([^;]+);").optional() // type
+ .number("(d{6,});") // device id
+ .number("d+;").optional()
+ .number("(d+);") // version
+ .number("(dddd)(dd)(dd);") // date
+ .number("(dd):(dd):(dd);") // time
+ .number("(x+);").optional() // cell
+ .number("([-+]dd.d+);") // latitude
+ .number("([-+]ddd.d+);") // longitude
+ .number("(ddd.ddd);") // speed
+ .number("(ddd.dd);") // course
+ .number("d+;").optional()
+ .number("(d+.d+)?") // battery
+ .any() // full format
+ .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;
-
- // Parse message
- Matcher parser = PATTERN.matcher(sentence);
+ Parser parser = new Parser(PATTERN, (String) msg);
if (!parser.matches()) {
return null;
}
- // Create new position
Position position = new Position();
position.setProtocol(getProtocolName());
- int index = 1;
- String type = parser.group(index++);
- if (type != null && (type.equals("Alert") || type.equals("Emergency"))) {
- position.set(Event.KEY_ALARM, true);
+ if (parser.hasNext()) {
+ String type = parser.next();
+ if (type.equals("Alert") || type.equals("Emergency")) {
+ position.set(Event.KEY_ALARM, true);
+ }
}
- // Identifier
- if (!identify(parser.group(index++), channel)) {
+ if (!identify(parser.next(), channel)) {
return null;
}
position.setDeviceId(getDeviceId());
- // Version
- position.set(Event.KEY_VERSION, parser.group(index++));
-
- // 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());
-
- // Cell
- position.set(Event.KEY_CELL, parser.group(index++));
+ position.set(Event.KEY_VERSION, parser.next());
- // Coordinates
- position.setLatitude(Double.parseDouble(parser.group(index++)));
- position.setLongitude(Double.parseDouble(parser.group(index++)));
- position.setValid(true); // wrong?
+ DateBuilder dateBuilder = new DateBuilder()
+ .setDate(parser.nextInt(), parser.nextInt(), parser.nextInt())
+ .setTime(parser.nextInt(), parser.nextInt(), parser.nextInt());
+ position.setTime(dateBuilder.getDate());
- // Speed
- position.setSpeed(UnitsConverter.knotsFromKph(Double.parseDouble(parser.group(index++))));
+ position.set(Event.KEY_CELL, parser.next());
- // Course
- position.setCourse(Double.parseDouble(parser.group(index++)));
+ position.setValid(true);
+ position.setLatitude(parser.nextDouble());
+ position.setLongitude(parser.nextDouble());
+ position.setSpeed(UnitsConverter.knotsFromKph(parser.nextDouble()));
+ position.setCourse(parser.nextDouble());
- // Battery
- position.set(Event.KEY_BATTERY, parser.group(index++));
+ position.set(Event.KEY_BATTERY, parser.next());
return position;
}
diff --git a/test/org/traccar/protocol/SuntechProtocolDecoderTest.java b/test/org/traccar/protocol/SuntechProtocolDecoderTest.java
index 1632b391a..d0987f289 100644
--- a/test/org/traccar/protocol/SuntechProtocolDecoderTest.java
+++ b/test/org/traccar/protocol/SuntechProtocolDecoderTest.java
@@ -14,13 +14,15 @@ public class SuntechProtocolDecoderTest extends ProtocolDecoderTest {
"SA200ALV;317652"));
verifyPosition(decoder, text(
- "ST910;Alert;123456;410;20141018;18:30:12;+37.478774;+126.889690;000.000;000.00;0;4.0;1;6002"));
+ "ST910;Alert;123456;410;20141018;18:30:12;+37.478774;+126.889690;000.000;000.00;0;4.0;1;6002"),
+ position("2014-10-18 18:30:12.000", true, 37.47877, 126.88969));
verifyPosition(decoder, text(
"ST910;Alert;123456;410;20141018;18:30:12;+37.478774;+126.889690;000.000;000.00;0;4.0;1;6002;02;0;0310000100;450;01;-282;70;255;3;0"));
verifyPosition(decoder, text(
- "SA200STT;317652;042;20120718;15:37:12;16d41;-15.618755;-056.083241;000.024;000.00;8;1;41548;12.17;100000;2;1979"));
+ "SA200STT;317652;042;20120718;15:37:12;16d41;-15.618755;-056.083241;000.024;000.00;8;1;41548;12.17;100000;2;1979"),
+ position("2012-07-18 15:37:12.000", true, -15.61876, -56.08324));
verifyPosition(decoder, text(
"SA200STT;317652;042;20120721;19:04:30;16d41;-15.618743;-056.083221;000.001;000.00;12;1;41557;12.21;000000;1;3125"));