From 73112d542cb4a2fa2ac47d85ebe071c9d3c6f894 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Fri, 6 Nov 2015 18:17:51 +1300 Subject: Refactor ARDI01 protocol decoder --- .../traccar/protocol/Ardi01ProtocolDecoder.java | 85 +++++++++------------- .../protocol/Ardi01ProtocolDecoderTest.java | 4 + 2 files changed, 37 insertions(+), 52 deletions(-) diff --git a/src/org/traccar/protocol/Ardi01ProtocolDecoder.java b/src/org/traccar/protocol/Ardi01ProtocolDecoder.java index 738af9bb1..80186894a 100644 --- a/src/org/traccar/protocol/Ardi01ProtocolDecoder.java +++ b/src/org/traccar/protocol/Ardi01ProtocolDecoder.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,75 +32,56 @@ public class Ardi01ProtocolDecoder extends BaseProtocolDecoder { super(protocol); } - private static final Pattern PATTERN = Pattern.compile( - "(\\d+)," + // IMEI - "(\\d{4})(\\d{2})(\\d{2})" + // Date (YYYYMMDD) - "(\\d{2})(\\d{2})(\\d{2})," + // Time (HHMMSS) - "(-?\\d+\\.\\d+)," + // Longitude - "(-?\\d+\\.\\d+)," + // Latitude - "(\\d+\\.?\\d*)," + // Speed - "(\\d+\\.?\\d*)," + // Course - "(-?\\d+\\.?\\d*)," + // Altitude - "(\\d+)," + // Satellites - "(\\d+)," + // Event - "(\\d+)," + // Battery - "(-?\\d+)"); // Temperature + private static final Pattern PATTERN = new PatternBuilder() + .number("(d+),") // imei + .number("(dddd)(dd)(dd)") // date + .number("(dd)(dd)(dd),") // time + .number("(-?d+.d+),") // longitude + .number("(-?d+.d+),") // latitude + .number("(d+.?d*),") // speed + .number("(d+.?d*),") // course + .number("(-?d+.?d*),") // altitude + .number("(d+),") // satellites + .number("(d+),") // event + .number("(d+),") // battery + .number("(-?d+)") // temperature + .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; - // Detect device - if (!identify(parser.group(index++), channel)) { + if (!identify(parser.next(), channel)) { return null; } position.setDeviceId(getDeviceId()); - // 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()); + DateBuilder dateBuilder = new DateBuilder() + .setDate(parser.nextInt(), parser.nextInt(), parser.nextInt()) + .setTime(parser.nextInt(), parser.nextInt(), parser.nextInt()); + position.setTime(dateBuilder.getDate()); - // Location data - position.setLongitude(Double.parseDouble(parser.group(index++))); - position.setLatitude(Double.parseDouble(parser.group(index++))); - position.setSpeed(UnitsConverter.knotsFromKph(Double.parseDouble(parser.group(index++)))); - position.setCourse(Double.parseDouble(parser.group(index++))); - position.setAltitude(Double.parseDouble(parser.group(index++))); + position.setLongitude(parser.nextDouble()); + position.setLatitude(parser.nextDouble()); + position.setSpeed(UnitsConverter.knotsFromKph(parser.nextDouble())); + position.setCourse(parser.nextDouble()); + position.setAltitude(parser.nextDouble()); - // Satellites - int satellites = Integer.parseInt(parser.group(index++)); + int satellites = parser.nextInt(); position.setValid(satellites >= 3); position.set(Event.KEY_SATELLITES, satellites); - // Event - position.set(Event.KEY_EVENT, parser.group(index++)); - - // Input - position.set(Event.KEY_BATTERY, parser.group(index++)); - - // Output - position.set(Event.PREFIX_TEMP + 1, parser.group(index++)); + position.set(Event.KEY_EVENT, parser.next()); + position.set(Event.KEY_BATTERY, parser.next()); + position.set(Event.PREFIX_TEMP + 1, parser.next()); return position; } diff --git a/test/org/traccar/protocol/Ardi01ProtocolDecoderTest.java b/test/org/traccar/protocol/Ardi01ProtocolDecoderTest.java index be5578683..717dfd958 100644 --- a/test/org/traccar/protocol/Ardi01ProtocolDecoderTest.java +++ b/test/org/traccar/protocol/Ardi01ProtocolDecoderTest.java @@ -10,6 +10,10 @@ public class Ardi01ProtocolDecoderTest extends ProtocolDecoderTest { Ardi01ProtocolDecoder decoder = new Ardi01ProtocolDecoder(new Ardi01Protocol()); + verifyPosition(decoder, text( + "013227003054776,20141010052719,24.4736042,56.8445807,110,289,40,7,5,78,-1"), + position("2014-10-10 05:27:19.000", true, 56.84458, 24.47360)); + verifyPosition(decoder, text( "013227003054776,20141010052719,24.4736042,56.8445807,110,289,40,7,5,78,-1")); -- cgit v1.2.3