diff options
-rw-r--r-- | src/org/traccar/protocol/FreedomProtocolDecoder.java | 65 | ||||
-rw-r--r-- | test/org/traccar/ProtocolDecoderTest.java | 4 | ||||
-rw-r--r-- | test/org/traccar/protocol/FreedomProtocolDecoderTest.java | 6 |
3 files changed, 29 insertions, 46 deletions
diff --git a/src/org/traccar/protocol/FreedomProtocolDecoder.java b/src/org/traccar/protocol/FreedomProtocolDecoder.java index 95e7abb4a..40abe1b8d 100644 --- a/src/org/traccar/protocol/FreedomProtocolDecoder.java +++ b/src/org/traccar/protocol/FreedomProtocolDecoder.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.Position; public class FreedomProtocolDecoder extends BaseProtocolDecoder { @@ -30,68 +30,47 @@ public class FreedomProtocolDecoder extends BaseProtocolDecoder { super(protocol); } - private static final Pattern PATTERN = Pattern.compile( - "IMEI," + - "(\\d+)," + // IMEI - "(\\d{4}).(\\d{2}).(\\d{2}), " + // Date - "(\\d{2}):(\\d{2}):(\\d{2}), " + // Time - "([NS]), Lat:(\\d{2})(\\d+.\\d+), " + // Latitude - "([EW]), Lon:(\\d{3})(\\d+.\\d+), " + // Longitude - "Spd:(\\d+.\\d+)" + // Speed - ".*"); + private static final Pattern PATTERN = new PatternBuilder() + .text("IMEI,") + .number("(d+),") // IMEI + .number("(dddd)/(dd)/(dd), ") // Date + .number("(dd):(dd):(dd), ") // Time + .number("([NS]), Lat:(dd)(d+.d+), ") // Latitude + .number("([EW]), Lon:(ddd)(d+.d+), ") // Longitude + .text("Spd:").number("(d+.d+)") // Speed + .any() + .compile(); @Override protected Object decode( Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { - // Parse message - Matcher parser = PATTERN.matcher((String) msg); + 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; - // Identification - if (!identify(parser.group(index++), channel)) { + if (!identify(parser.next(), channel)) { return null; } position.setDeviceId(getDeviceId()); - // Validity position.setValid(true); - // 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()); + DateBuilder dateBuilder = new DateBuilder() + .setDate(parser.nextInt(), parser.nextInt(), parser.nextInt()) + .setTime(parser.nextInt(), parser.nextInt(), parser.nextInt()); + position.setTime(dateBuilder.getDate()); - // Latitude - String hemisphere = parser.group(index++); - Double latitude = Double.parseDouble(parser.group(index++)); - latitude += Double.parseDouble(parser.group(index++)) / 60; - if (hemisphere.compareTo("S") == 0) latitude = -latitude; - position.setLatitude(latitude); + position.setLatitude(parser.nextCoordinate(Parser.CoordinateFormat.HEM_DEG_MIN)); + position.setLongitude(parser.nextCoordinate(Parser.CoordinateFormat.HEM_DEG_MIN)); - // Longitude - hemisphere = parser.group(index++); - Double longitude = Double.parseDouble(parser.group(index++)); - longitude += Double.parseDouble(parser.group(index++)) / 60; - if (hemisphere.compareTo("W") == 0) longitude = -longitude; - position.setLongitude(longitude); + position.setSpeed(parser.nextDouble()); - // Speed - position.setSpeed(Double.parseDouble(parser.group(index++))); return position; } diff --git a/test/org/traccar/ProtocolDecoderTest.java b/test/org/traccar/ProtocolDecoderTest.java index 61c333d67..e6d5270e8 100644 --- a/test/org/traccar/ProtocolDecoderTest.java +++ b/test/org/traccar/ProtocolDecoderTest.java @@ -140,8 +140,8 @@ public class ProtocolDecoderTest { private void verifyDecodedPosition(Object decodedObject, Position expected) { - Assert.assertNotNull(decodedObject); - Assert.assertTrue(decodedObject instanceof Position); + Assert.assertNotNull("position is null", decodedObject); + Assert.assertTrue("not a position", decodedObject instanceof Position); Position position = (Position) decodedObject; diff --git a/test/org/traccar/protocol/FreedomProtocolDecoderTest.java b/test/org/traccar/protocol/FreedomProtocolDecoderTest.java index 521fe20ab..fe86da082 100644 --- a/test/org/traccar/protocol/FreedomProtocolDecoderTest.java +++ b/test/org/traccar/protocol/FreedomProtocolDecoderTest.java @@ -11,8 +11,12 @@ public class FreedomProtocolDecoderTest extends ProtocolDecoderTest { FreedomProtocolDecoder decoder = new FreedomProtocolDecoder(new FreedomProtocol()); verifyPosition(decoder, text( + "IMEI,353358011714362,2014/05/22, 20:49:32, N, Lat:4725.9624, E, Lon:01912.5483, Spd:5.05"), + position("2014-05-22 20:49:32.000", true, 47.43271, 19.20914)); + + verifyPosition(decoder, text( "IMEI,353358011714362,2014/05/22, 20:49:32, N, Lat:4725.9624, E, Lon:01912.5483, Spd:5.05")); - + } } |