diff options
-rw-r--r-- | src/org/traccar/protocol/MeitrackProtocolDecoder.java | 205 | ||||
-rw-r--r-- | test/org/traccar/protocol/MeitrackProtocolDecoderTest.java | 8 |
2 files changed, 89 insertions, 124 deletions
diff --git a/src/org/traccar/protocol/MeitrackProtocolDecoder.java b/src/org/traccar/protocol/MeitrackProtocolDecoder.java index 872055f19..e6e3dd057 100644 --- a/src/org/traccar/protocol/MeitrackProtocolDecoder.java +++ b/src/org/traccar/protocol/MeitrackProtocolDecoder.java @@ -1,5 +1,5 @@ /* - * Copyright 2012 - 2014 Anton Tananaev (anton.tananaev@gmail.com) + * Copyright 2012 - 2015 Anton Tananaev (anton.tananaev@gmail.com) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,16 +17,16 @@ package org.traccar.protocol; import java.net.SocketAddress; import java.nio.charset.Charset; -import java.util.Calendar; import java.util.Date; import java.util.LinkedList; import java.util.List; -import java.util.TimeZone; -import java.util.regex.Matcher; 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.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; @@ -37,138 +37,114 @@ public class MeitrackProtocolDecoder extends BaseProtocolDecoder { super(protocol); } - private static final Pattern PATTERN = Pattern.compile( - "\\$\\$." + // Flag - "\\d+," + // Length - "(\\d+)," + // IMEI - "\\p{XDigit}{3}," + // Command - "(?:\\d+,)?" + - "(\\d+)," + // Event - "(-?\\d+\\.\\d+)," + // Latitude - "(-?\\d+\\.\\d+)," + // Longitude - "(\\d{2})(\\d{2})(\\d{2})" + // Date (YYMMDD) - "(\\d{2})(\\d{2})(\\d{2})," + // Time (HHMMSS) - "([AV])," + // Validity - "(\\d+)," + // Satellites - "(\\d+)," + // GSM Signal - "(\\d+\\.?\\d*)," + // Speed - "(\\d+)," + // Course - "(\\d+\\.?\\d*)," + // HDOP - "(-?\\d+)," + // Altitude - "(\\d+)," + // Odometer - "(\\d+)," + // Runtime - "(\\d+\\|\\d+\\|\\p{XDigit}+\\|\\p{XDigit}+)," + // Cell - "(\\p{XDigit}+)," + // State - "(\\p{XDigit}+)?\\|" + // ADC1 - "(\\p{XDigit}+)?\\|" + // ADC2 - "(\\p{XDigit}+)?\\|" + // ADC3 - "(\\p{XDigit}+)\\|" + // Battery - "(\\p{XDigit}+)," + // Power - "(?:([^,]+)?," + // Event Specific - "[^,]*," + // Reserved - "\\d*," + // Protocol - "(\\p{XDigit}{4})?)?" + // Fuel - ".*\\*\\p{XDigit}{2}(?:\r\n)?"); + private static final Pattern PATTERN = new PatternBuilder() + .text("$$").expression(".") // flag + .number("d+,") // length + .number("(d+),") // imei + .number("xxx,") // command + .number("d+,").optional() + .number("(d+),") // event + .number("(-?d+.d+),") // latitude + .number("(-?d+.d+),") // longitude + .number("(dd)(dd)(dd)") // date (ddmmyy) + .number("(dd)(dd)(dd),") // time + .number("([AV]),") // validity + .number("(d+),") // satellites + .number("(d+),") // gsm signal + .number("(d+.?d*),") // speed + .number("(d+),") // course + .number("(d+.?d*),") // hdop + .number("(-?d+),") // altitude + .number("(d+),") // odometer + .number("(d+),") // runtime + .number("(d+|") + .number("d+|") + .number("x+|") + .number("x+),") // cell + .number("(x+),") // state + .number("(x+)?|") // adc1 + .number("(x+)?|") // adc2 + .number("(x+)?|") // adc3 + .number("(x+)|") // battery + .number("(x+),") // power + .groupBegin() + .expression("([^,]+)?,") // event specific + .expression("[^,]*,") // reserved + .number("d*,") // protocol + .number("(x{4})?") // fuel + .groupEnd("?") + .any() + .text("*") + .number("xx") + .text("\r\n").optional() + .compile(); private Position decodeRegularMessage(Channel channel, ChannelBuffer buf) { - // Parse message - String sentence = buf.toString(Charset.defaultCharset()); - Matcher parser = PATTERN.matcher(sentence); + Parser parser = new Parser(PATTERN, buf.toString(Charset.defaultCharset())); if (!parser.matches()) { return null; } - // Create new position Position position = new Position(); position.setProtocol(getProtocolName()); - Integer index = 1; - - // Identification - if (!identify(parser.group(index++), channel)) { + if (!identify(parser.next(), channel)) { return null; } position.setDeviceId(getDeviceId()); - // Event - int event = Integer.parseInt(parser.group(index++)); + int event = parser.nextInt(); position.set(Event.KEY_EVENT, event); - // Coordinates - position.setLatitude(Double.parseDouble(parser.group(index++))); - position.setLongitude(Double.parseDouble(parser.group(index++))); - - // Time - Calendar time = Calendar.getInstance(TimeZone.getTimeZone("UTC")); - time.clear(); - time.set(Calendar.YEAR, 2000 + 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()); - - // Validity - position.setValid(parser.group(index++).compareTo("A") == 0); + position.setLatitude(parser.nextDouble()); + position.setLongitude(parser.nextDouble()); - // Satellites - position.set(Event.KEY_SATELLITES, parser.group(index++)); + DateBuilder dateBuilder = new DateBuilder() + .setDate(parser.nextInt(), parser.nextInt(), parser.nextInt()) + .setTime(parser.nextInt(), parser.nextInt(), parser.nextInt()); + position.setTime(dateBuilder.getDate()); - // GSM Signal - position.set(Event.KEY_GSM, parser.group(index++)); + position.setValid(parser.next().equals("A")); - // Speed - position.setSpeed(UnitsConverter.knotsFromKph(Double.parseDouble(parser.group(index++)))); + position.set(Event.KEY_SATELLITES, parser.next()); + position.set(Event.KEY_GSM, parser.next()); - // Course - position.setCourse(Double.parseDouble(parser.group(index++))); + position.setSpeed(UnitsConverter.knotsFromKph(parser.nextDouble())); + position.setCourse(parser.nextDouble()); - // HDOP - position.set(Event.KEY_HDOP, parser.group(index++)); + position.set(Event.KEY_HDOP, parser.next()); - // Altitude - position.setAltitude(Double.parseDouble(parser.group(index++))); + position.setAltitude(parser.nextDouble()); - // Other - position.set(Event.KEY_ODOMETER, parser.group(index++)); - position.set("runtime", parser.group(index++)); - position.set(Event.KEY_CELL, parser.group(index++)); - position.set(Event.KEY_STATUS, parser.group(index++)); + position.set(Event.KEY_ODOMETER, parser.next()); + position.set("runtime", parser.next()); + position.set(Event.KEY_CELL, parser.next()); + position.set(Event.KEY_STATUS, parser.next()); - // ADC - String adc1 = parser.group(index++); - if (adc1 != null) { - position.set(Event.PREFIX_ADC + 1, Integer.parseInt(adc1, 16)); - } - String adc2 = parser.group(index++); - if (adc2 != null) { - position.set(Event.PREFIX_ADC + 2, Integer.parseInt(adc2, 16)); - } - String adc3 = parser.group(index++); - if (adc3 != null) { - position.set(Event.PREFIX_ADC + 3, Integer.parseInt(adc3, 16)); + for (int i = 1; i <= 3; i++) { + if (parser.hasNext()) { + position.set(Event.PREFIX_ADC + i, parser.nextInt(16)); + } } - position.set(Event.KEY_BATTERY, Integer.parseInt(parser.group(index++), 16)); - position.set(Event.KEY_POWER, Integer.parseInt(parser.group(index++), 16)); - // Event specific - String data = parser.group(index++); - if (data != null && !data.isEmpty()) { + position.set(Event.KEY_BATTERY, parser.nextInt(16)); + position.set(Event.KEY_POWER, parser.nextInt(16)); + + String eventData = parser.next(); + if (eventData != null && !eventData.isEmpty()) { switch (event) { case 37: - position.set(Event.KEY_RFID, data); + position.set(Event.KEY_RFID, eventData); break; default: - position.set("event-data", data); + position.set("event-data", eventData); break; } } - // Fuel - String fuel = parser.group(index++); - if (fuel != null) { + if (parser.hasNext()) { + String fuel = parser.next(); position.set(Event.KEY_FUEL, Integer.parseInt(fuel.substring(0, 2), 16) + Integer.parseInt(fuel.substring(2), 16) * 0.01); } @@ -182,7 +158,6 @@ public class MeitrackProtocolDecoder extends BaseProtocolDecoder { String flag = buf.toString(2, 1, Charset.defaultCharset()); int index = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) ','); - // Identification String imei = buf.toString(index + 1, 15, Charset.defaultCharset()); if (!identify(imei, channel)) { return null; @@ -196,38 +171,25 @@ public class MeitrackProtocolDecoder extends BaseProtocolDecoder { position.setProtocol(getProtocolName()); position.setDeviceId(getDeviceId()); - // Event position.set(Event.KEY_EVENT, buf.readUnsignedByte()); - // Location position.setLatitude(buf.readInt() * 0.000001); position.setLongitude(buf.readInt() * 0.000001); - // Time (946684800 - timestamp for 2000-01-01) - position.setTime(new Date((946684800 + buf.readUnsignedInt()) * 1000)); + position.setTime(new Date((946684800 + buf.readUnsignedInt()) * 1000)); // 946684800 = 2000-01-01 - // Validity position.setValid(buf.readUnsignedByte() == 1); - // Satellites position.set(Event.KEY_SATELLITES, buf.readUnsignedByte()); - - // GSM Signal position.set(Event.KEY_GSM, buf.readUnsignedByte()); - // Speed position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedShort())); - - // Course position.setCourse(buf.readUnsignedShort()); - // HDOP position.set(Event.KEY_HDOP, buf.readUnsignedShort() * 0.1); - // Altitude position.setAltitude(buf.readUnsignedShort()); - // Other position.set(Event.KEY_ODOMETER, buf.readUnsignedInt()); position.set("runtime", buf.readUnsignedInt()); position.set(Event.KEY_CELL, @@ -235,25 +197,26 @@ public class MeitrackProtocolDecoder extends BaseProtocolDecoder { buf.readUnsignedShort() + "|" + buf.readUnsignedShort()); position.set(Event.KEY_STATUS, buf.readUnsignedShort()); - // ADC position.set(Event.PREFIX_ADC + 1, buf.readUnsignedShort()); position.set(Event.KEY_BATTERY, buf.readUnsignedShort() * 0.01); position.set(Event.KEY_POWER, buf.readUnsignedShort()); buf.readUnsignedInt(); // geo-fence + positions.add(position); } - // Delete recorded data if (channel != null) { StringBuilder command = new StringBuilder("@@"); command.append(flag).append(27 + positions.size() / 10).append(","); command.append(imei).append(",CCC,").append(positions.size()).append("*"); int checksum = 0; - for (int i = 0; i < command.length(); i += 1) checksum += command.charAt(i); + for (int i = 0; i < command.length(); i += 1) { + checksum += command.charAt(i); + } command.append(String.format("%02x", checksum & 0xff).toUpperCase()); command.append("\r\n"); - channel.write(command.toString()); + channel.write(command.toString()); // delete processed data } return positions; diff --git a/test/org/traccar/protocol/MeitrackProtocolDecoderTest.java b/test/org/traccar/protocol/MeitrackProtocolDecoderTest.java index 2c513272c..88d11d551 100644 --- a/test/org/traccar/protocol/MeitrackProtocolDecoderTest.java +++ b/test/org/traccar/protocol/MeitrackProtocolDecoderTest.java @@ -12,8 +12,9 @@ public class MeitrackProtocolDecoderTest extends ProtocolDecoderTest { MeitrackProtocolDecoder decoder = new MeitrackProtocolDecoder(new MeitrackProtocol()); verifyPosition(decoder, buffer( - "$$A158,79007001520234,AAA,35,40.996370,-8.575065,150730184834,A,8,24,0,1,1.3,173,32573389,31405012,268|3|2BC0|250B,2000,|||0A2D|0000,00000001,,50,,,,,,,,,,,,,*4A")); - + "$$A158,79007001520234,AAA,35,40.996370,-8.575065,150730184834,A,8,24,0,1,1.3,173,32573389,31405012,268|3|2BC0|250B,2000,|||0A2D|0000,00000001,,50,,,,,,,,,,,,,*4A"), + position("2015-07-30 18:48:34.000", true, 40.99637, -8.57507)); + verifyPosition(decoder, buffer( "$$G145,862106024274815,AAA,35,-1.287125,36.906061,150530054639,A,10,13,12,67,0.8,1621,38359791,42330881,639|2|FB2|2F3,0000,3|0|0|A58|432,,,1,0009,*26")); @@ -63,7 +64,8 @@ public class MeitrackProtocolDecoderTest extends ProtocolDecoderTest { "$$J163,123123123123123,AFF,0004,35,58.588926,16.180473,140928192856,A,10,27,0,161,1.2,19,1648894,435695,240|24|88B9|E435,0000,|||0A22|0000,00000001,,50,,,,,,,,,,,,,*70\r\n")); verifyPositions(decoder, binary(ByteOrder.LITTLE_ENDIAN, - "24245838362c3336393830303031343039303032312c4343432c020134000100000023381f91ffe354b806c5e3121b0009130000000000000000d33801007cbf0200fe0101000435feeb02000500a3010000000000002a62650d0a")); + "24245838362c3336393830303031343039303032312c4343432c020134000100000023381f91ffe354b806c5e3121b0009130000000000000000d33801007cbf0200fe0101000435feeb02000500a3010000000000002a62650d0a"), + position("2014-05-24 04:59:49.000", false, -7.26650, 112.74365)); verifyPositions(decoder, binary(ByteOrder.LITTLE_ENDIAN, "2424473937302c3336393830303031333436303637342c4343432c020134005b000000010ce304035db9e000ec6f591a000013000000000c001801edb70200c96d0100e60001004838576501000300a101c20400000000010ce304035db9e000ee6f591a000013000000000c001801edb70200ca6d0100e60001004838576501000300a101c20400000000010ce304035db9e000ef6f591a000013000000000c001801edb70200cc6d0100e60001004838576501000300a101c20400000000020ce304035db9e000f76f591a000016000000000c001801edb70200d36d0100e60001004838576502000300a101bf04000000000a0ce304035db9e000f76f591a000016000000000c001801edb70200d46d0100e60001004838576500000300a101bf0400000000020ce304035db9e000fb6f591a000016000000000c001801edb70200d86d0100e60001004838576502000300a101760400000000180ce304035db9e000fc6f591a0000120000000000008c00edb70200d96d0100e60001004838576502000300a10176040000000019b1e2040323b9e0000b70591a0105150600bb0012002901edb70200e76d0100e60001004838576502000300a2017005000000002023e304031fb9e0001070591a010615070027010d001601fcb70200ec6d0100e60001004838576502000300a201800500000000201fe3040302b9e0001170591a010615090019010d001501feb70200ed6d0100e60001004838576502000300a2018005000000002018e30403dcb8e0001270591a0106150b0011010d00150100b80200ee6d0100e60001004838576502000300a2018005000000002036e3040345b8e0001570591a0107150b002d010b0013010ab80200f16d0100e60001004838576502000300a2018005000000002053e3040326b8e0001670591a0107150d0041010b0013010eb80200f26d0100e60001004838576502000300a2018005000000002070e3040310b8e0001770591a0107150e004f010b00130111b80200f36d0100e60001004838576502000300a2018005000000002095e3040306b8e0001870591a0107150d005a010b00140115b80200f46d0100e60001004838576502000300a20180050000000020b3e3040305b8e0001970591a0107150b0060010b00140118b80200f56d0100e60001004838576502000300a20183050000000020cfe3040308b8e0001a70591a0107150b0066010b0014011bb80200f66d0100e60001004838576502000300a20183050000000020eee304030cb8e0001b70591a0106170b0004000d0014011eb80200f76d0100e60001004838576502000300a2018305000000002a62350d0a")); |