diff options
author | Anton Tananaev <anton.tananaev@gmail.com> | 2015-10-25 21:00:19 +1300 |
---|---|---|
committer | Anton Tananaev <anton.tananaev@gmail.com> | 2015-10-25 21:00:19 +1300 |
commit | db08f36b51f7f51e901cc778a79ba8949c1d6d08 (patch) | |
tree | b39e8d0a51cd266d498271b344b2ff99f0404967 /src/org | |
parent | 78f7e386fe4fb46fc0c9295853a2467408330dfd (diff) | |
download | traccar-server-db08f36b51f7f51e901cc778a79ba8949c1d6d08.tar.gz traccar-server-db08f36b51f7f51e901cc778a79ba8949c1d6d08.tar.bz2 traccar-server-db08f36b51f7f51e901cc778a79ba8949c1d6d08.zip |
Refactor Meiligao protocol decoder
Diffstat (limited to 'src/org')
-rw-r--r-- | src/org/traccar/helper/PatternBuilder.java | 2 | ||||
-rw-r--r-- | src/org/traccar/protocol/MeiligaoProtocolDecoder.java | 87 |
2 files changed, 51 insertions, 38 deletions
diff --git a/src/org/traccar/helper/PatternBuilder.java b/src/org/traccar/helper/PatternBuilder.java index 4ce4295dd..6742e7130 100644 --- a/src/org/traccar/helper/PatternBuilder.java +++ b/src/org/traccar/helper/PatternBuilder.java @@ -49,7 +49,7 @@ public class PatternBuilder { s = s.replace("xxxx", "x{4}").replace("xxx", "x{3}").replace("xx", "x{2}"); s = s.replace("d", "\\d").replace("x", "\\p{XDigit}").replaceAll("([\\.])", "\\\\$1"); - s = s.replaceAll("\\|$", "\\\\|"); // special case for delimiter + s = s.replaceAll("\\|$", "\\\\|").replaceAll("^\\|", "\\\\|"); // special case for delimiter fragments.add(s); return this; diff --git a/src/org/traccar/protocol/MeiligaoProtocolDecoder.java b/src/org/traccar/protocol/MeiligaoProtocolDecoder.java index a63554fc4..3bc27f55c 100644 --- a/src/org/traccar/protocol/MeiligaoProtocolDecoder.java +++ b/src/org/traccar/protocol/MeiligaoProtocolDecoder.java @@ -27,6 +27,7 @@ import org.traccar.Context; import org.traccar.helper.Checksum; 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; @@ -36,37 +37,51 @@ public class MeiligaoProtocolDecoder extends BaseProtocolDecoder { super(protocol); } - private static final Pattern PATTERN = Pattern.compile( - "(\\d{2})(\\d{2})(\\d{2})\\.?(\\d+)?," + // Time (HHMMSS.SSS) - "([AV])," + // Validity - "(\\d+)(\\d{2}\\.\\d+)," + // Latitude (DDMM.MMMM) - "([NS])," + - "(\\d+)(\\d{2}\\.\\d+)," + // Longitude (DDDMM.MMMM) - "([EW])," + - "(\\d+\\.?\\d*)?," + // Speed - "(\\d+\\.?\\d*)?," + // Course - "(\\d{2})(\\d{2})(\\d{2})" + // Date (DDMMYY) - "[^\\|]*" + - "(?:\\|(\\d+\\.\\d+)?" + // HDOP - "\\|(-?\\d+\\.?\\d*)?" + // Altitude - "\\|(\\p{XDigit}{4})?" + // State - "(?:\\|(\\p{XDigit}{4}),(\\p{XDigit}{4})" + // ADC - "(?:,(\\p{XDigit}{4}),(\\p{XDigit}{4}),(\\p{XDigit}{4}),(\\p{XDigit}{4}),(\\p{XDigit}{4}),(\\p{XDigit}{4}))?" + - "(?:\\|" + - "(?:(\\p{XDigit}{16})" + // Cell - "\\|(\\p{XDigit}{2})" + // GSM - "\\|(\\p{XDigit}{8})|" + // Odometer - "(\\p{XDigit}{9})" + // Odometer - "(?:\\|(\\p{XDigit}{5,}))?)?)?)?)?" + // RFID - ".*"); - - private static final Pattern PATTERN_RFID = Pattern.compile( - "\\|(\\d{2})(\\d{2})(\\d{2})," + // Time (HHMMSS) - "(\\d{2})(\\d{2})(\\d{2})," + // Date (DDMMYY) - "(\\d+)(\\d{2}\\.\\d+)," + // Latitude (DDMM.MMMM) - "([NS])," + - "(\\d+)(\\d{2}\\.\\d+)," + // Longitude (DDDMM.MMMM) - "([EW])"); + private static final Pattern PATTERN = new PatternBuilder() + .number("(dd)(dd)(dd).?(d+)?,") // time + .expression("([AV]),") // validity + .number("(d+)(dd.d+),") // latitude + .expression("([NS]),") + .number("(d+)(dd.d+),") // longitude + .expression("([EW]),") + .number("(d+.?d*)?,") // speed + .number("(d+.?d*)?,") // course + .number("(dd)(dd)(dd)") // date (ddmmyy) + .expression("[^\\|]*") + .groupBegin() + .number("|(d+.d+)?") // hdop + .number("|(-?d+.?d*)?") // altitude + .number("|(xxxx)?") // state + .groupBegin() + .number("|(xxxx),(xxxx)") // adc + .groupBegin() + .number(",(xxxx),(xxxx),(xxxx),(xxxx),(xxxx),(xxxx)") + .groupEnd("?") + .groupBegin() + .text("|") + .groupBegin() + .number("(x{16})") // cell + .number("|(xx)") // gsm + .number("|(x{8})|") // odometer + .number("(x{9})") // odometer + .groupBegin() + .number("|(x{5,})") // rfid + .groupEnd("?") + .groupEnd("?") + .groupEnd("?") + .groupEnd("?") + .groupEnd("?") + .any() + .compile(); + + private static final Pattern PATTERN_RFID = new PatternBuilder() + .number("|(dd)(dd)(dd),") // time + .number("(dd)(dd)(dd),") // Date (ddmmyy) + .number("(d+)(dd.d+),") // latitude + .expression("([NS]),") + .number("(d+)(dd.d+),") // longitude + .expression("([EW])") + .compile(); public static final int MSG_HEARTBEAT = 0x0001; public static final int MSG_SERVER = 0x0002; @@ -143,8 +158,7 @@ public class MeiligaoProtocolDecoder extends BaseProtocolDecoder { @Override protected Object decode( - Channel channel, SocketAddress remoteAddress, Object msg) - throws Exception { + Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { ChannelBuffer buf = (ChannelBuffer) msg; buf.skipBytes(2); // header @@ -182,7 +196,6 @@ public class MeiligaoProtocolDecoder extends BaseProtocolDecoder { return null; } - // Create new position Position position = new Position(); position.setProtocol(getProtocolName()); @@ -193,13 +206,11 @@ public class MeiligaoProtocolDecoder extends BaseProtocolDecoder { buf.skipBytes(6); } - // Get device by id if (!identify(id, channel)) { return null; } position.setDeviceId(getDeviceId()); - // RFID if (command == MSG_RFID) { for (int i = 0; i < 15; i++) { long rfid = buf.readUnsignedInt(); @@ -218,7 +229,8 @@ public class MeiligaoProtocolDecoder extends BaseProtocolDecoder { pattern = PATTERN; } - Parser parser = new Parser(pattern, buf.toString(buf.readerIndex(), buf.readableBytes() - 4, Charset.defaultCharset())); + Parser parser = new Parser( + pattern, buf.toString(buf.readerIndex(), buf.readableBytes() - 4, Charset.defaultCharset())); if (!parser.matches()) { return null; } @@ -230,6 +242,7 @@ public class MeiligaoProtocolDecoder extends BaseProtocolDecoder { .setDateReverse(parser.nextInt(), parser.nextInt(), parser.nextInt()); position.setTime(dateBuilder.getDate()); + position.setValid(true); position.setLatitude(parser.nextCoordinate()); position.setLongitude(parser.nextCoordinate()); |