diff options
4 files changed, 56 insertions, 40 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()); diff --git a/test/org/traccar/helper/PatternBuilderTest.java b/test/org/traccar/helper/PatternBuilderTest.java index 10621693e..36054bcea 100644 --- a/test/org/traccar/helper/PatternBuilderTest.java +++ b/test/org/traccar/helper/PatternBuilderTest.java @@ -13,6 +13,7 @@ public class PatternBuilderTest { Assert.assertEquals("a|b", new PatternBuilder().expression("a|b").toString()); Assert.assertEquals("ab\\|", new PatternBuilder().expression("ab|").toString()); Assert.assertEquals("|", new PatternBuilder().or().toString()); + Assert.assertEquals("\\|\\d|\\d\\|", new PatternBuilder().number("|d|d|").toString()); } } diff --git a/test/org/traccar/protocol/MeiligaoProtocolDecoderTest.java b/test/org/traccar/protocol/MeiligaoProtocolDecoderTest.java index 523679201..6d0ba50d5 100644 --- a/test/org/traccar/protocol/MeiligaoProtocolDecoderTest.java +++ b/test/org/traccar/protocol/MeiligaoProtocolDecoderTest.java @@ -13,10 +13,12 @@ public class MeiligaoProtocolDecoderTest extends ProtocolDecoderTest { MeiligaoProtocolDecoder decoder = new MeiligaoProtocolDecoder(new MeiligaoProtocol()); verifyPosition(decoder, binary( - "242400706796502079108999553131333131382e3030302c412c313033372e393637382c4e2c30363132312e353637392c572c302e35342c322e34322c3330303931352c2c2c412a37307c302e37377c392e397c303030307c303030302c303161327c3030313138373132374cae0d0a")); + "242400706796502079108999553131333131382e3030302c412c313033372e393637382c4e2c30363132312e353637392c572c302e35342c322e34322c3330303931352c2c2c412a37307c302e37377c392e397c303030307c303030302c303161327c3030313138373132374cae0d0a"), + position("2015-09-30 11:31:18.000", true, 10.63280, -61.35947)); verifyPosition(decoder, binary( - "24240074630700194707719966009E1F7F00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007C3132303334302C3238303630362C323430302E303030302C4E2C31323130302E303030302C458F7E0D0A")); + "24240074630700194707719966009E1F7F00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007C3132303334302C3238303630362C323430302E303030302C4E2C31323130302E303030302C458F7E0D0A"), + position("2006-06-28 12:03:40.000", true, 24.00000, 121.00000)); verifyPosition(decoder, binary( "24240076220720151fffff99660012b3ab00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007c3135303634382c3233303731352c313931352e37323835362c4e2c30373235322e35333034342c456dd00d0a")); |