diff options
-rw-r--r-- | src/org/traccar/helper/Parser.java | 2 | ||||
-rw-r--r-- | src/org/traccar/protocol/VisiontekProtocolDecoder.java | 139 | ||||
-rw-r--r-- | test/org/traccar/ProtocolDecoderTest.java | 4 | ||||
-rw-r--r-- | test/org/traccar/protocol/VisiontekProtocolDecoderTest.java | 11 |
4 files changed, 70 insertions, 86 deletions
diff --git a/src/org/traccar/helper/Parser.java b/src/org/traccar/helper/Parser.java index 9f24cacd0..f70fe2e89 100644 --- a/src/org/traccar/helper/Parser.java +++ b/src/org/traccar/helper/Parser.java @@ -101,7 +101,7 @@ public class Parser { switch (format) { case DEG_MIN_MIN_HEM: coordinate = nextInt(); - coordinate += (nextInt() + nextInt() / 1000.0) / 60; + coordinate += Double.parseDouble(next() + '.' + next()) / 60; hemisphere = next(); break; case HEM_DEG: diff --git a/src/org/traccar/protocol/VisiontekProtocolDecoder.java b/src/org/traccar/protocol/VisiontekProtocolDecoder.java index f974d08be..0dce4954d 100644 --- a/src/org/traccar/protocol/VisiontekProtocolDecoder.java +++ b/src/org/traccar/protocol/VisiontekProtocolDecoder.java @@ -22,6 +22,9 @@ 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,107 +35,81 @@ public class VisiontekProtocolDecoder extends BaseProtocolDecoder { super(protocol); } - private static final Pattern PATTERN = Pattern.compile( - "\\$1," + - "([^,]+)," + // Identifier - "(?:(\\d+),)?" + // IMEI - "(\\d{2}),(\\d{2}),(\\d{2})," + // Date - "(\\d{2}),(\\d{2}),(\\d{2})," + // Time - "(\\d{2})(\\d{2}\\.?\\d{4})([NS])," + // Latitude - "(\\d{3})(\\d{2}\\.?\\d{4})([EW])," + // Longitude - "(\\d+\\.?\\d+)," + // Speed - "(\\d+)," + // Course - "(?:(\\d+)," + // Altitude - "(\\d+),)?" + // Satellites - "(\\d+)," + // Odometer - "(?:(\\d)," + // Ignition - "(\\d)," + // Input 1 - "(\\d)," + // Input 2 - "(\\d)," + // Immobilizer - "(\\d)," + // External Battery Status - "(\\d+),)?" + // GSM - "([AV]),?" + // Validity - "(\\d+)?" + // RFID - ".*"); + private static final Pattern PATTERN = new PatternBuilder() + .text("$1,") + .expression("([^,]+),") // identifier + .number("(d+),").optional() // imei + .number("(dd),(dd),(dd),") // date + .number("(dd),(dd),(dd),") // time + .number("(dd)(dd).?(dddd)") // latitude + .expression("([NS]),") + .number("(ddd)(dd).?(dddd)") // longitude + .expression("([EW]),") + .number("(d+.?d+),") // speed + .number("(d+),") // course + .groupBegin() + .number("(d+),") // altitude + .number("(d+),") // satellites + .groupEnd("?") + .number("(d+),") // odometer + .groupBegin() + .number("(d),") // ignition + .number("(d),") // input 1 + .number("(d),") // input 2 + .number("(d),") // immobilizer + .number("(d),") // external battery status + .number("(d+),") // gsm + .groupEnd("?") + .expression("([AV]),?") // validity + .number("(d+)?") // rfid + .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; - - // Device identification - String id = parser.group(index++); - String imei = parser.group(index++); + String id = parser.next(); + String imei = parser.next(); if (!identify(id, channel, null, false) && !identify(imei, channel)) { return null; } position.setDeviceId(getDeviceId()); - // Date - Calendar time = Calendar.getInstance(TimeZone.getTimeZone("UTC")); - time.clear(); - time.set(Calendar.DAY_OF_MONTH, Integer.parseInt(parser.group(index++))); - time.set(Calendar.MONTH, Integer.parseInt(parser.group(index++)) - 1); - time.set(Calendar.YEAR, 2000 + 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()); - - // Latitude - Double latitude = Double.parseDouble(parser.group(index++)); - latitude += Double.parseDouble(parser.group(index++).replace(".", "")) / 600000; - if (parser.group(index++).compareTo("S") == 0) latitude = -latitude; - position.setLatitude(latitude); - - // Longitude - Double longitude = Double.parseDouble(parser.group(index++)); - longitude += Double.parseDouble(parser.group(index++).replace(".", "")) / 600000; - if (parser.group(index++).compareTo("W") == 0) longitude = -longitude; - position.setLongitude(longitude); - - // Speed + DateBuilder dateBuilder = new DateBuilder() + .setDateReverse(parser.nextInt(), parser.nextInt(), parser.nextInt()) + .setTime(parser.nextInt(), parser.nextInt(), parser.nextInt()); + position.setTime(dateBuilder.getDate()); + + position.setLatitude(parser.nextCoordinate(Parser.CoordinateFormat.DEG_MIN_MIN_HEM)); + position.setLongitude(parser.nextCoordinate(Parser.CoordinateFormat.DEG_MIN_MIN_HEM)); + position.setSpeed(UnitsConverter.knotsFromKph(Double.parseDouble( - parser.group(index++).replace(".", "")) / 10)); + parser.next().replace(".", "")) / 10)); - // Course - position.setCourse(Double.parseDouble(parser.group(index++))); + position.setCourse(parser.nextDouble()); + position.setAltitude(parser.nextDouble()); - // Altitude - String altitude = parser.group(index++); - if (altitude != null) { - position.setAltitude(Double.parseDouble(altitude)); - } + position.set(Event.KEY_SATELLITES, parser.next()); + position.set(Event.KEY_ODOMETER, parser.next()); + position.set(Event.KEY_IGNITION, parser.next()); + position.set(Event.PREFIX_IO + 1, parser.next()); + position.set(Event.PREFIX_IO + 2, parser.next()); + position.set("immobilizer", parser.next()); + position.set(Event.KEY_POWER, parser.next()); + position.set(Event.KEY_GSM, parser.next()); + + position.setValid(parser.next().equals("A")); - // Additional data - position.set(Event.KEY_SATELLITES, parser.group(index++)); - position.set(Event.KEY_ODOMETER, parser.group(index++)); - position.set(Event.KEY_IGNITION, parser.group(index++)); - position.set(Event.PREFIX_IO + 1, parser.group(index++)); - position.set(Event.PREFIX_IO + 2, parser.group(index++)); - position.set("immobilizer", parser.group(index++)); - position.set(Event.KEY_POWER, parser.group(index++)); - position.set(Event.KEY_GSM, parser.group(index++)); - - // Validity - position.setValid(parser.group(index++).compareTo("A") == 0); - - // RFID - position.set(Event.KEY_RFID, parser.group(index++)); + position.set(Event.KEY_RFID, parser.next()); return position; } diff --git a/test/org/traccar/ProtocolDecoderTest.java b/test/org/traccar/ProtocolDecoderTest.java index d91675f10..ed06cf1fb 100644 --- a/test/org/traccar/ProtocolDecoderTest.java +++ b/test/org/traccar/ProtocolDecoderTest.java @@ -148,8 +148,8 @@ public class ProtocolDecoderTest { private void verifyDecodedPosition(Object decodedObject) { - 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/VisiontekProtocolDecoderTest.java b/test/org/traccar/protocol/VisiontekProtocolDecoderTest.java index ad637256e..3a31d3c28 100644 --- a/test/org/traccar/protocol/VisiontekProtocolDecoderTest.java +++ b/test/org/traccar/protocol/VisiontekProtocolDecoderTest.java @@ -9,12 +9,19 @@ public class VisiontekProtocolDecoderTest extends ProtocolDecoderTest { public void testDecode() throws Exception { VisiontekProtocolDecoder decoder = new VisiontekProtocolDecoder(new VisiontekProtocol()); - + + /*verifyPosition(decoder, text( + "$1,VMC,358072044271838,26,10,15,10,43,20,17.066418N,080.395667E,000.0,285,00.8,0074,6390,0,0,0,0,0,0,0,0,00.00,00.00,00,00,0000,12.7,4.0,24,10,0000000000000,A,0")); + + verifyPosition(decoder, text( + "$1,VMC,358072044271838,25,10,15,09,19,40,00.0000000,000.0000000,000.0,000,00.0,0000,6070,0,0,0,0,0,0,0,0,00.00,00.00,00,00,0000,12.5,4.0,99,00,0000000000000,V,0"));*/ + //verifyPosition(decoder, text( // "$1,AP116,05,06,15,11,48,32,1725.0460N,07824.5289E,0617,07,0,030,2091,0,0,0,1,1,1,1,20,00.0000,00.3820,00.0000,VAJRA V1.00,A#")); verifyPosition(decoder, text( - "$1,AP09BU9397,861785006462448,20,06,14,15,03,28,17267339N,078279407E,060.0,073,0550,11,0,1,0,0,1,1,26,A,0000000000")); + "$1,AP09BU9397,861785006462448,20,06,14,15,03,28,17267339N,078279407E,060.0,073,0550,11,0,1,0,0,1,1,26,A,0000000000"), + position("2014-06-20 15:03:28.000", true, 17.44556, 78.46567)); verifyNothing(decoder, text( "$1,AP09BU9397,861785006462448,20,06,14,15,03,28,000000000,0000000000,000.0,000,0000,00,0,1,0,0,1,1,24,V,0000000000")); |