aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnton Tananaev <anton.tananaev@gmail.com>2015-10-27 18:37:21 +1300
committerAnton Tananaev <anton.tananaev@gmail.com>2015-10-27 18:37:21 +1300
commite59c4b0e015dd063aaf431e9f0296b9483024ab8 (patch)
tree0fe0693ec85a22549c01bab0673e335c9f0b4015
parent67c2bedcd4d338d9d3179c32b7194ac509b0fea4 (diff)
downloadtraccar-server-e59c4b0e015dd063aaf431e9f0296b9483024ab8.tar.gz
traccar-server-e59c4b0e015dd063aaf431e9f0296b9483024ab8.tar.bz2
traccar-server-e59c4b0e015dd063aaf431e9f0296b9483024ab8.zip
Refactor ManPower protocol decoder
-rw-r--r--src/org/traccar/protocol/ManPowerProtocolDecoder.java89
-rw-r--r--test/org/traccar/protocol/ManPowerProtocolDecoderTest.java3
2 files changed, 33 insertions, 59 deletions
diff --git a/src/org/traccar/protocol/ManPowerProtocolDecoder.java b/src/org/traccar/protocol/ManPowerProtocolDecoder.java
index 113215c1b..48c36bb6a 100644
--- a/src/org/traccar/protocol/ManPowerProtocolDecoder.java
+++ b/src/org/traccar/protocol/ManPowerProtocolDecoder.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,78 +31,51 @@ public class ManPowerProtocolDecoder extends BaseProtocolDecoder {
super(protocol);
}
- private static final Pattern PATTERN = Pattern.compile(
- "simei:" +
- "(\\d+)," + // IMEI
- "[^,]*,[^,]*," +
- "([^,]*)," + // Status
- "\\d+,\\d+,\\d+\\.?\\d*," +
- "(\\d{2})(\\d{2})(\\d{2})" + // Date
- "(\\d{2})(\\d{2})(\\d{2})," + // Time
- "([AV])," + // Validity
- "(\\d{2})(\\d{2}\\.\\d{4})," + // Latitude (DDMM.MMMM)
- "([NS])," +
- "(\\d{3})(\\d{2}\\.\\d{4})," + // Longitude (DDDMM.MMMM)
- "([EW])?," +
- "(\\d+\\.?\\d*)," + // Speed
- ".*");
+ private static final Pattern PATTERN = new PatternBuilder()
+ .text("simei:")
+ .number("(d+),") // imei
+ .expression("[^,]*,[^,]*,")
+ .expression("([^,]*),") // status
+ .number("d+,d+,d+.?d*,")
+ .number("(dd)(dd)(dd)") // date
+ .number("(dd)(dd)(dd),") // time
+ .expression("([AV]),") // validity
+ .number("(dd)(dd.dddd),") // latitude
+ .expression("([NS]),")
+ .number("(ddd)(dd.dddd),") // longitude
+ .expression("([EW])?,")
+ .number("(d+.?d*),") // speed
+ .any()
+ .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());
- Integer index = 1;
-
- // Get device by IMEI
- if (!identify(parser.group(index++), channel)) {
+ if (!identify(parser.next(), channel)) {
return null;
}
position.setDeviceId(getDeviceId());
- // Alarm message
- position.set(Event.KEY_STATUS, parser.group(index++));
-
- // Date
- 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);
-
- // Latitude
- Double latitude = Double.parseDouble(parser.group(index++));
- latitude += Double.parseDouble(parser.group(index++)) / 60;
- if (parser.group(index++).compareTo("S") == 0) latitude = -latitude;
- position.setLatitude(latitude);
+ position.set(Event.KEY_STATUS, parser.next());
- // Longitude
- Double longitude = Double.parseDouble(parser.group(index++));
- longitude += Double.parseDouble(parser.group(index++)) / 60;
- if (parser.group(index++).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());
- // Speed
- position.setSpeed(Double.parseDouble(parser.group(index++)));
+ position.setValid(parser.next().equals("A"));
+ position.setLatitude(parser.nextCoordinate());
+ position.setLongitude(parser.nextCoordinate());
+ position.setSpeed(parser.nextDouble());
return position;
}
diff --git a/test/org/traccar/protocol/ManPowerProtocolDecoderTest.java b/test/org/traccar/protocol/ManPowerProtocolDecoderTest.java
index 3d5d28698..debb6b55b 100644
--- a/test/org/traccar/protocol/ManPowerProtocolDecoderTest.java
+++ b/test/org/traccar/protocol/ManPowerProtocolDecoderTest.java
@@ -11,7 +11,8 @@ public class ManPowerProtocolDecoderTest extends ProtocolDecoderTest {
ManPowerProtocolDecoder decoder = new ManPowerProtocolDecoder(new ManPowerProtocol());
verifyPosition(decoder, text(
- "simei:352581250259539,,,tracker,51,24,1.73,130426023608,A,3201.5462,N,03452.2975,E,0.01,28B9,1DED,425,01,1x0x0*0x1*60x+2,en-us,"));
+ "simei:352581250259539,,,tracker,51,24,1.73,130426023608,A,3201.5462,N,03452.2975,E,0.01,28B9,1DED,425,01,1x0x0*0x1*60x+2,en-us,"),
+ position("2013-04-26 02:36:08.000", true, 32.02577, 34.87163));
verifyPosition(decoder, text(
"simei:352581250259539,,,weather,99,20,0.00,130426032310,V,3201.5517,N,03452.3064,E,1.24,28B9,25A1,425,01,1x0x0*0x1*60x+2,en-us,"));