diff options
author | Anton Tananaev <anton.tananaev@gmail.com> | 2015-10-26 11:58:08 +1300 |
---|---|---|
committer | Anton Tananaev <anton.tananaev@gmail.com> | 2015-10-26 11:58:08 +1300 |
commit | 7ee16ab6fe928171ded0e188811a631743285a58 (patch) | |
tree | 3679b75ebc1f8f13e7914d9c114bff058418e6e8 /src/org/traccar/protocol | |
parent | 64aa76a78916310cfbc14ec3ae6083555b95a4a0 (diff) | |
download | trackermap-server-7ee16ab6fe928171ded0e188811a631743285a58.tar.gz trackermap-server-7ee16ab6fe928171ded0e188811a631743285a58.tar.bz2 trackermap-server-7ee16ab6fe928171ded0e188811a631743285a58.zip |
Refactor Riti protocol decoder
Diffstat (limited to 'src/org/traccar/protocol')
-rw-r--r-- | src/org/traccar/protocol/EnforaProtocolDecoder.java | 5 | ||||
-rw-r--r-- | src/org/traccar/protocol/RitiProtocolDecoder.java | 94 |
2 files changed, 35 insertions, 64 deletions
diff --git a/src/org/traccar/protocol/EnforaProtocolDecoder.java b/src/org/traccar/protocol/EnforaProtocolDecoder.java index 0c0b5afe9..18f68a32d 100644 --- a/src/org/traccar/protocol/EnforaProtocolDecoder.java +++ b/src/org/traccar/protocol/EnforaProtocolDecoder.java @@ -26,6 +26,7 @@ import org.jboss.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; import org.traccar.helper.ChannelBufferTools; import org.traccar.helper.Log; +import org.traccar.helper.StringFinder; import org.traccar.model.Position; public class EnforaProtocolDecoder extends BaseProtocolDecoder { @@ -77,8 +78,8 @@ public class EnforaProtocolDecoder extends BaseProtocolDecoder { } // Find GPSMC string - Integer start = ChannelBufferTools.find(buf, 0, buf.readableBytes(), "GPRMC"); - if (start == null) { + int start = buf.indexOf(buf.readerIndex(), buf.writerIndex(), new StringFinder("GPRMC")); + if (start == -1) { // Message does not contain GPS data return null; } diff --git a/src/org/traccar/protocol/RitiProtocolDecoder.java b/src/org/traccar/protocol/RitiProtocolDecoder.java index bb7c0b583..7e86dd535 100644 --- a/src/org/traccar/protocol/RitiProtocolDecoder.java +++ b/src/org/traccar/protocol/RitiProtocolDecoder.java @@ -24,7 +24,9 @@ import java.util.regex.Pattern; import org.jboss.netty.buffer.ChannelBuffer; import org.jboss.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; -import org.traccar.helper.ChannelBufferTools; +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; @@ -34,33 +36,31 @@ public class RitiProtocolDecoder extends BaseProtocolDecoder { super(protocol); } - private static final Pattern PATTERN = Pattern.compile( - "\\$GPRMC," + - "(\\d{2})(\\d{2})(\\d{2})\\.?\\d*," + // Time (HHMMSS.SSS) - "([AV])," + // Validity - "(\\d{2})(\\d{2}\\.\\d+)," + // Latitude (DDMM.MMMM) - "([NS])," + - "(\\d{3})(\\d{2}\\.\\d+)," + // Longitude (DDDMM.MMMM) - "([EW])," + - "(\\d+\\.?\\d*)?," + // Speed - "(\\d+\\.?\\d*)?," + // Course - "(\\d{2})(\\d{2})(\\d{2})" + // Date (DDMMYY) - ".+"); + private static final Pattern PATTERN = new PatternBuilder() + .text("$GPRMC,") + .number("(dd)(dd)(dd).?d*,") // time + .expression("([AV]),") // validity + .number("(dd)(dd.d+),") // latitude + .expression("([NS]),") + .number("(ddd)(dd.d+),") // longitude + .expression("([EW]),") + .number("(d+.?d*)?,") // speed + .number("(d+.?d*)?,") // course + .number("(dd)(dd)(dd)") // date (ddmmyy) + .any() + .compile(); @Override protected Object decode( - Channel channel, SocketAddress remoteAddress, Object msg) - throws Exception { + Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { ChannelBuffer buf = (ChannelBuffer) msg; - // Create new position + buf.skipBytes(2); // header + Position position = new Position(); position.setProtocol(getProtocolName()); - buf.skipBytes(2); // header - - // Get device id if (!identify(String.valueOf(buf.readUnsignedShort()), channel)) { return null; } @@ -78,55 +78,25 @@ public class RitiProtocolDecoder extends BaseProtocolDecoder { position.set(Event.KEY_ODOMETER, buf.readUnsignedInt()); // Parse GPRMC - Integer end = ChannelBufferTools.find(buf, buf.readerIndex(), buf.readerIndex() + 80, "*"); - String gprmc = buf.toString( - buf.readerIndex(), end - buf.readerIndex(), Charset.defaultCharset()); - Matcher parser = PATTERN.matcher(gprmc); + int end = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) '*'); + String gprmc = buf.toString(buf.readerIndex(), end - buf.readerIndex(), Charset.defaultCharset()); + Parser parser = new Parser(PATTERN, gprmc); if (!parser.matches()) { return null; } - Integer index = 1; - - // Time - Calendar time = Calendar.getInstance(TimeZone.getTimeZone("UTC")); - time.clear(); - 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++))); - - // 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); - - // 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); - - // Speed - String speed = parser.group(index++); - if (speed != null) { - position.setSpeed(Double.parseDouble(speed)); - } + DateBuilder dateBuilder = new DateBuilder() + .setTime(parser.nextInt(), parser.nextInt(), parser.nextInt()); - // Course - String course = parser.group(index++); - if (course != null) { - position.setCourse(Double.parseDouble(course)); - } + position.setValid(parser.next().equals("A")); + position.setLatitude(parser.nextCoordinate()); + position.setLongitude(parser.nextCoordinate()); + position.setSpeed(parser.nextDouble()); + position.setCourse(parser.nextDouble()); + + dateBuilder.setDateReverse(parser.nextInt(), parser.nextInt(), parser.nextInt()); + position.setTime(dateBuilder.getDate()); - // Date - 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++))); - position.setTime(time.getTime()); return position; } |