diff options
-rw-r--r-- | src/org/traccar/protocol/TmgFrameDecoder.java | 67 | ||||
-rw-r--r-- | src/org/traccar/protocol/TmgProtocol.java | 3 | ||||
-rw-r--r-- | src/org/traccar/protocol/TmgProtocolDecoder.java | 84 | ||||
-rw-r--r-- | test/org/traccar/protocol/TmgFrameDecoderTest.java | 20 | ||||
-rw-r--r-- | test/org/traccar/protocol/TmgProtocolDecoderTest.java | 9 |
5 files changed, 155 insertions, 28 deletions
diff --git a/src/org/traccar/protocol/TmgFrameDecoder.java b/src/org/traccar/protocol/TmgFrameDecoder.java new file mode 100644 index 000000000..07b4162db --- /dev/null +++ b/src/org/traccar/protocol/TmgFrameDecoder.java @@ -0,0 +1,67 @@ +/* + * Copyright 2017 Anton Tananaev (anton@traccar.org) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.traccar.protocol; + +import org.jboss.netty.buffer.ChannelBuffer; +import org.jboss.netty.buffer.ChannelBufferIndexFinder; +import org.jboss.netty.channel.Channel; +import org.jboss.netty.channel.ChannelHandlerContext; +import org.jboss.netty.handler.codec.frame.FrameDecoder; + +public class TmgFrameDecoder extends FrameDecoder { + + private boolean isLetter(byte c) { + return c >= 'a' && c <= 'z'; + } + + @Override + protected Object decode( + ChannelHandlerContext ctx, Channel channel, ChannelBuffer buf) throws Exception { + + int beginIndex = buf.indexOf(buf.readerIndex(), buf.writerIndex(), new ChannelBufferIndexFinder() { + @Override + public boolean find(ChannelBuffer buffer, int guessedIndex) { + if (buffer.getByte(guessedIndex) != (byte) '$' || buffer.writerIndex() - guessedIndex < 5) { + return false; + } + if (buffer.getByte(guessedIndex + 4) == ',' + && isLetter(buffer.getByte(guessedIndex + 1)) + && isLetter(buffer.getByte(guessedIndex + 2)) + && isLetter(buffer.getByte(guessedIndex + 3))) { + return true; + } + return false; + } + }); + + if (beginIndex > 0) { + + buf.readerIndex(beginIndex); + + int endIndex = buf.indexOf(beginIndex, buf.writerIndex(), (byte) '\n'); + + if (endIndex > 0) { + ChannelBuffer frame = buf.readBytes(endIndex - beginIndex); + buf.readByte(); // delimiter + return frame; + } + + } + + return null; + } + +} diff --git a/src/org/traccar/protocol/TmgProtocol.java b/src/org/traccar/protocol/TmgProtocol.java index f30d61e9a..c11762cac 100644 --- a/src/org/traccar/protocol/TmgProtocol.java +++ b/src/org/traccar/protocol/TmgProtocol.java @@ -17,7 +17,6 @@ package org.traccar.protocol; import org.jboss.netty.bootstrap.ServerBootstrap; import org.jboss.netty.channel.ChannelPipeline; -import org.jboss.netty.handler.codec.frame.LineBasedFrameDecoder; import org.jboss.netty.handler.codec.string.StringDecoder; import org.jboss.netty.handler.codec.string.StringEncoder; import org.traccar.BaseProtocol; @@ -36,7 +35,7 @@ public class TmgProtocol extends BaseProtocol { serverList.add(new TrackerServer(new ServerBootstrap(), getName()) { @Override protected void addSpecificHandlers(ChannelPipeline pipeline) { - pipeline.addLast("frameDecoder", new LineBasedFrameDecoder(1024)); + pipeline.addLast("frameDecoder", new TmgFrameDecoder()); pipeline.addLast("stringEncoder", new StringEncoder()); pipeline.addLast("stringDecoder", new StringDecoder()); pipeline.addLast("objectDecoder", new TmgProtocolDecoder(TmgProtocol.this)); diff --git a/src/org/traccar/protocol/TmgProtocolDecoder.java b/src/org/traccar/protocol/TmgProtocolDecoder.java index b8458dd52..a7ca35f70 100644 --- a/src/org/traccar/protocol/TmgProtocolDecoder.java +++ b/src/org/traccar/protocol/TmgProtocolDecoder.java @@ -36,7 +36,7 @@ public class TmgProtocolDecoder extends BaseProtocolDecoder { private static final Pattern PATTERN = new PatternBuilder() .text("$") .expression("(...),") // type - .expression("[LH],") // history + .expression("[LH],").optional() // history .number("(d+),") // imei .number("(dd)(dd)(dddd),") // date (ddmmyyyy) .number("(dd)(dd)(dd),") // time (hhmmss) @@ -47,6 +47,7 @@ public class TmgProtocolDecoder extends BaseProtocolDecoder { .expression("([EW]),") .number("(d+.?d*),") // speed .number("(d+.?d*),") // course + .groupBegin() .number("(-?d+.?d*),") // altitude .number("(d+.d+),") // hdop .number("(d+),") // satellites @@ -65,6 +66,20 @@ public class TmgProtocolDecoder extends BaseProtocolDecoder { .number("d+.?d*,") // trip meter .expression("([^,]*),") // software version .expression("([^,]*),").optional() // rfid + .or() + .number("[^,]*,") // cid + .number("(d+),") // rssi + .number("(d+),") // satellites + .number("(d+),") // battery level + .expression("([01]),") // ignition + .expression("[LH]{4},") // input + .expression("[NT]{4},") // tamper status + .expression("[LH]{2},") // output + .number("(d+.d+),") // adc1 + .number("(d+.d+),") // adc1 + .number("[^,]*,") // device id + .number("(d+),") // odometer + .groupEnd() .any() .compile(); @@ -115,36 +130,53 @@ public class TmgProtocolDecoder extends BaseProtocolDecoder { position.setTime(parser.nextDateTime(Parser.DateTimeFormat.DMY_HMS)); - position.setValid(parser.nextInt(0) > 0); + position.setValid(parser.nextInt() > 0); position.setLatitude(parser.nextCoordinate()); position.setLongitude(parser.nextCoordinate()); - position.setSpeed(UnitsConverter.knotsFromKph(parser.nextDouble(0))); - position.setCourse(parser.nextDouble(0)); - position.setAltitude(parser.nextDouble(0)); - - position.set(Position.KEY_HDOP, parser.nextDouble(0)); - position.set(Position.KEY_SATELLITES, parser.nextInt(0)); - position.set(Position.KEY_SATELLITES_VISIBLE, parser.nextInt(0)); - position.set(Position.KEY_OPERATOR, parser.next()); - position.set(Position.KEY_RSSI, parser.nextInt(0)); - position.set(Position.KEY_IGNITION, parser.nextInt(0) == 1); - position.set(Position.KEY_BATTERY, parser.nextDouble(0)); - position.set(Position.KEY_POWER, parser.nextDouble(0)); - - int input = parser.nextBinInt(0); - int output = parser.nextBinInt(0); - - if (!BitUtil.check(input, 0)) { - position.set(Position.KEY_ALARM, Position.ALARM_SOS); + position.setSpeed(UnitsConverter.knotsFromKph(parser.nextDouble())); + position.setCourse(parser.nextDouble()); + + if (parser.hasNext(15)) { + + position.setAltitude(parser.nextDouble()); + + position.set(Position.KEY_HDOP, parser.nextDouble()); + position.set(Position.KEY_SATELLITES, parser.nextInt()); + position.set(Position.KEY_SATELLITES_VISIBLE, parser.nextInt()); + position.set(Position.KEY_OPERATOR, parser.next()); + position.set(Position.KEY_RSSI, parser.nextInt()); + position.set(Position.KEY_IGNITION, parser.nextInt() == 1); + position.set(Position.KEY_BATTERY, parser.nextDouble()); + position.set(Position.KEY_POWER, parser.nextDouble()); + + int input = parser.nextBinInt(); + int output = parser.nextBinInt(); + + if (!BitUtil.check(input, 0)) { + position.set(Position.KEY_ALARM, Position.ALARM_SOS); + } + + position.set(Position.KEY_INPUT, input); + position.set(Position.KEY_OUTPUT, output); + + position.set(Position.PREFIX_ADC + 1, parser.nextDouble()); + position.set(Position.PREFIX_ADC + 2, parser.nextDouble()); + position.set(Position.KEY_VERSION_FW, parser.next()); + position.set(Position.KEY_DRIVER_UNIQUE_ID, parser.next()); + } - position.set(Position.KEY_INPUT, input); - position.set(Position.KEY_OUTPUT, output); + if (parser.hasNext()) { + + position.set(Position.KEY_RSSI, parser.nextInt()); + position.set(Position.KEY_SATELLITES, parser.nextInt()); + position.set(Position.KEY_BATTERY_LEVEL, parser.nextInt()); + position.set(Position.KEY_IGNITION, parser.nextInt() == 1); + position.set(Position.PREFIX_ADC + 1, parser.nextDouble()); + position.set(Position.PREFIX_ADC + 2, parser.nextDouble()); + position.set(Position.KEY_ODOMETER, parser.nextInt()); - position.set(Position.PREFIX_ADC + 1, parser.nextDouble(0)); - position.set(Position.PREFIX_ADC + 2, parser.nextDouble(0)); - position.set(Position.KEY_VERSION_FW, parser.next()); - position.set(Position.KEY_DRIVER_UNIQUE_ID, parser.next()); + } return position; } diff --git a/test/org/traccar/protocol/TmgFrameDecoderTest.java b/test/org/traccar/protocol/TmgFrameDecoderTest.java new file mode 100644 index 000000000..2ddc13652 --- /dev/null +++ b/test/org/traccar/protocol/TmgFrameDecoderTest.java @@ -0,0 +1,20 @@ +package org.traccar.protocol; + +import org.junit.Assert; +import org.junit.Test; +import org.traccar.ProtocolTest; + +public class TmgFrameDecoderTest extends ProtocolTest { + + @Test + public void testDecode() throws Exception { + + TmgFrameDecoder decoder = new TmgFrameDecoder(); + + verifyFrame( + binary("24696f662c3836343530323033373939393630342c323238323133323031372c383132343238302c302c3239393133363231362e2d3438323235383537362ca52c313337393234353339382e3831383733343637362c142c2d36393936393937332e302c313135333435343433372e2d313938363833343039322c3439323039373739392c32302c302c2d3332302c302c4c4c4c4c2c4e4e544e2c48482c302e31372c332e30312c3330313131363030312c302c56455230302e3161"), + decoder.decode(null, null, binary("111538360b383634353032303337393939363034eb0b1c8d00ffff23000000000000000000000000000000001c9401008c320c00188901000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000044ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff00000000000000000000000000001d940100c8950100bd0024696f662c3836343530323033373939393630342c323238323133323031372c383132343238302c302c3239393133363231362e2d3438323235383537362ca52c313337393234353339382e3831383733343637362c142c2d36393936393937332e302c313135333435343433372e2d313938363833343039322c3439323039373739392c32302c302c2d3332302c302c4c4c4c4c2c4e4e544e2c48482c302e31372c332e30312c3330313131363030312c302c56455230302e31610a000195010090960100bd0024746d702c3836343530323033373939393630342c323238323133323031372c383132343238302c302c3239393133363231362e2d3438323235383537362ca52c313337393234353339382e3831383733343637362c142c2d36393936393937332e302c313135333435343433372e2d313938363833343039322c3439323039373739392c32302c302c2d3237372c302c4c4c4c4c2c4e4e544e2c48482c302e31372c332e30312c3330313131363030312c302c56455230302e31610a00c9950100289701008b002462616b2c3836343530323033373939393630342c32313131323031372c3132303230312c312c323832362e353938312c4e2c30373731382e363436352c452c3030302e302c3239382e35322c35323834372c32322c332c2d3233392c302c4c4c4c4c2c4e4e544e2c48482c302e32342c332e30332c3330313131363030312c302c56455230302e31610a00000091960100c09701008b002462616b2c3836343530323033373939393630342c32313131323031372c3132303231322c312c323832362e353938312c4e2c30373731382e363437322c452c3030302e302c3037392e34322c35323834372c32332c332c2d3230352c302c4c4c4c4c2c4e4e544e2c48482c302e31352c332e30332c3330313131363030312c302c56455230302e31610a00000029970100589801008a002462616b2c3836343530323033373939393630342c32313131323031372c3132303232322c312c323832362e353938302c4e2c30373731382e363438352c452c3030302e302c3037372e332c35323834372c32332c332c2d3137342c302c4c4c4c4c2c4e4e544e2c48482c302e31382c332e30332c3330313131363030312c302c56455230302e31610a00000000c1970100f09801008a002462616b2c3836343530323033373939393630342c32313131323031372c3132303233312c312c323832362e353936342c4e2c30373731382e363437312c452c3030302e302c3133312e352c35323834372c32322c342c2d3134362c302c4c4c4c4c2c4e4e544e2c48482c302e31392c332e30332c3330313131363030312c302c56455230302e31610a0000000059980100889901008b002462616b2c3836343530323033373939393630342c32313131323031372c3132303234332c312c323832362e353935382c4e2c30373731382e363436382c452c3030302e302c3133392e36362c35323834372c32322c342c2d3132312c302c4c4c4c4c2c4e4e544e2c48482c302e31322c332e30332c3330313131363030312c302c56455230302e31610a000000f1980100209a01008a002462616b2c3836343530323033373939393630342c32313131323031372c3132303235332c312c323832362e353934392c4e2c30373731382e363436"))); + + } + +} diff --git a/test/org/traccar/protocol/TmgProtocolDecoderTest.java b/test/org/traccar/protocol/TmgProtocolDecoderTest.java index 2fac25dab..382d7fa63 100644 --- a/test/org/traccar/protocol/TmgProtocolDecoderTest.java +++ b/test/org/traccar/protocol/TmgProtocolDecoderTest.java @@ -11,6 +11,15 @@ public class TmgProtocolDecoderTest extends ProtocolTest { TmgProtocolDecoder decoder = new TmgProtocolDecoder(new TmgProtocol()); verifyPosition(decoder, text( + "$loc,869309013800417,08032014,094459,1,2826.1956,N,07659.7690,E,0.0,2.5,4441,31,6,95,1,LLLL,NNTN,HH,0.15,0.26,HR38AU1389,0,SW0.1a")); + + verifyPosition(decoder, text( + "$bak,864502037999604,21112017,120243,1,2826.5958,N,07718.6468,E,000.0,139.66,52847,22,4,99,0,LLLL,NNTN,HH,0.12,3.03,301116001,0,VER00.1a")); + + verifyNull(decoder, text( + "$iof,864502037999604,2282132017,8124280,0,299136216.-482258576,¥,1379245398.818734676,,-69969973.0,1153454437.-1986834092,492097799,20,0,-320,0,LLLL,NNTN,HH,0.17,3.01,301116001,0,VER00.1a")); + + verifyPosition(decoder, text( "$nor,L,868325023006341,14022017,103947,1,2836.6542,N,07706.2504,E,0.0,0.0,0.0,0.0,0,22,VODAFONE - DELH,15,49B7,1,2.57,13.2,00000010,00000000,0111,00.0,00.0,0.0,SW10.12,NA,#")); verifyPosition(decoder, text( |