aboutsummaryrefslogtreecommitdiff
path: root/src/org
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 /src/org
parent8598581c0bdfc7e5ca842da7363e107232943849 (diff)
downloadtraccar-server-08dc9eb08cf5a1f3c474d4ac5e421db4259ec92e.tar.gz
traccar-server-08dc9eb08cf5a1f3c474d4ac5e421db4259ec92e.tar.bz2
traccar-server-08dc9eb08cf5a1f3c474d4ac5e421db4259ec92e.zip
Refactor SunTech protocol decoder
Diffstat (limited to 'src/org')
-rw-r--r--src/org/traccar/protocol/SuntechProtocolDecoder.java100
1 files changed, 42 insertions, 58 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;
}