From ff62eefa74f7d8cb36388adce981e1f6c306180a Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Wed, 24 Apr 2019 23:31:44 -0700 Subject: Support binary Fifotrack messages --- .../traccar/protocol/FifotrackFrameDecoder.java | 49 ++++++++++++++++++++++ .../org/traccar/protocol/FifotrackProtocol.java | 2 - .../traccar/protocol/FifotrackProtocolDecoder.java | 26 ++++++++---- .../protocol/FifotrackFrameDecoderTest.java | 19 +++++++++ .../protocol/FifotrackProtocolDecoderTest.java | 13 +++--- 5 files changed, 93 insertions(+), 16 deletions(-) create mode 100644 src/main/java/org/traccar/protocol/FifotrackFrameDecoder.java create mode 100644 src/test/java/org/traccar/protocol/FifotrackFrameDecoderTest.java diff --git a/src/main/java/org/traccar/protocol/FifotrackFrameDecoder.java b/src/main/java/org/traccar/protocol/FifotrackFrameDecoder.java new file mode 100644 index 000000000..967b17a64 --- /dev/null +++ b/src/main/java/org/traccar/protocol/FifotrackFrameDecoder.java @@ -0,0 +1,49 @@ +/* + * Copyright 2019 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 io.netty.buffer.ByteBuf; +import io.netty.channel.Channel; +import io.netty.channel.ChannelHandlerContext; +import org.traccar.BaseFrameDecoder; + +import java.nio.charset.StandardCharsets; + +public class FifotrackFrameDecoder extends BaseFrameDecoder { + + @Override + protected Object decode( + ChannelHandlerContext ctx, Channel channel, ByteBuf buf) throws Exception { + + if (buf.readableBytes() < 10) { + return null; + } + + int index = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) ','); + if (index != -1) { + int length = index - buf.readerIndex() + 3 + Integer.parseInt( + buf.toString(buf.readerIndex() + 2, index - buf.readerIndex() - 2, StandardCharsets.US_ASCII)); + if (buf.readableBytes() >= length + 2) { + ByteBuf frame = buf.readRetainedSlice(length); + buf.skipBytes(2); // delimiter + return frame; + } + } + + return null; + } + +} diff --git a/src/main/java/org/traccar/protocol/FifotrackProtocol.java b/src/main/java/org/traccar/protocol/FifotrackProtocol.java index 371e01e55..2c2e6e1d3 100644 --- a/src/main/java/org/traccar/protocol/FifotrackProtocol.java +++ b/src/main/java/org/traccar/protocol/FifotrackProtocol.java @@ -16,7 +16,6 @@ package org.traccar.protocol; import io.netty.handler.codec.LineBasedFrameDecoder; -import io.netty.handler.codec.string.StringDecoder; import io.netty.handler.codec.string.StringEncoder; import org.traccar.BaseProtocol; import org.traccar.PipelineBuilder; @@ -30,7 +29,6 @@ public class FifotrackProtocol extends BaseProtocol { protected void addProtocolHandlers(PipelineBuilder pipeline) { pipeline.addLast(new LineBasedFrameDecoder(1024)); pipeline.addLast(new StringEncoder()); - pipeline.addLast(new StringDecoder()); pipeline.addLast(new FifotrackProtocolDecoder(FifotrackProtocol.this)); } }); diff --git a/src/main/java/org/traccar/protocol/FifotrackProtocolDecoder.java b/src/main/java/org/traccar/protocol/FifotrackProtocolDecoder.java index 35696ee12..2719464c9 100644 --- a/src/main/java/org/traccar/protocol/FifotrackProtocolDecoder.java +++ b/src/main/java/org/traccar/protocol/FifotrackProtocolDecoder.java @@ -23,7 +23,6 @@ import org.traccar.DeviceSession; import org.traccar.NetworkMessage; import org.traccar.Protocol; import org.traccar.helper.Checksum; -import org.traccar.helper.DataConverter; import org.traccar.helper.Parser; import org.traccar.helper.PatternBuilder; import org.traccar.helper.UnitsConverter; @@ -32,6 +31,7 @@ import org.traccar.model.Network; import org.traccar.model.Position; import java.net.SocketAddress; +import java.nio.charset.StandardCharsets; import java.util.regex.Pattern; public class FifotrackProtocolDecoder extends BaseProtocolDecoder { @@ -87,12 +87,11 @@ public class FifotrackProtocolDecoder extends BaseProtocolDecoder { .text("$$") .number("d+,") // length .number("(d+),") // imei - .expression("([^*]+),") // photo id + .number("x+,") // index + .expression("[^,]+,") // type + .expression("([^,]+),") // photo id .number("(d+),") // offset .number("(d+),") // size - .number("(x+)") // data - .text("*") - .number("xx") .compile(); private void requestPhoto(Channel channel, SocketAddress socketAddress, String imei, String file) { @@ -165,11 +164,14 @@ public class FifotrackProtocolDecoder extends BaseProtocolDecoder { protected Object decode( Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { - String sentence = (String) msg; - int typeIndex = sentence.indexOf(',', sentence.indexOf(',', sentence.indexOf(',') + 1) + 1) + 1; - String type = sentence.substring(typeIndex, typeIndex + 3); + ByteBuf buf = (ByteBuf) msg; + int typeIndex = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) ',') + 1; + typeIndex = buf.indexOf(typeIndex, buf.writerIndex(), (byte) ',') + 1; + typeIndex = buf.indexOf(typeIndex, buf.writerIndex(), (byte) ',') + 1; + String type = buf.toString(typeIndex, 3, StandardCharsets.US_ASCII); if (type.equals("D05")) { + String sentence = buf.toString(StandardCharsets.US_ASCII); Parser parser = new Parser(PATTERN_PHOTO, sentence); if (parser.matches()) { String imei = parser.next(); @@ -179,16 +181,22 @@ public class FifotrackProtocolDecoder extends BaseProtocolDecoder { requestPhoto(channel, remoteAddress, imei, photoId); } } else if (type.equals("D06")) { + int dataIndex = buf.indexOf(typeIndex + 4, buf.writerIndex(), (byte) ',') + 1; + dataIndex = buf.indexOf(dataIndex, buf.writerIndex(), (byte) ',') + 1; + dataIndex = buf.indexOf(dataIndex, buf.writerIndex(), (byte) ',') + 1; + String sentence = buf.toString(buf.readerIndex(), dataIndex, StandardCharsets.US_ASCII); Parser parser = new Parser(PATTERN_PHOTO_DATA, sentence); if (parser.matches()) { String imei = parser.next(); String photoId = parser.next(); parser.nextInt(); // offset parser.nextInt(); // size - photo.writeBytes(DataConverter.parseHex(parser.next())); + buf.readerIndex(dataIndex); + photo.writeBytes(buf.readBytes(buf.readableBytes() - 3)); // ignore checksum requestPhoto(channel, remoteAddress, imei, photoId); } } else { + String sentence = buf.toString(StandardCharsets.US_ASCII); return decodeLocation(channel, remoteAddress, sentence); } diff --git a/src/test/java/org/traccar/protocol/FifotrackFrameDecoderTest.java b/src/test/java/org/traccar/protocol/FifotrackFrameDecoderTest.java new file mode 100644 index 000000000..6b39b6c17 --- /dev/null +++ b/src/test/java/org/traccar/protocol/FifotrackFrameDecoderTest.java @@ -0,0 +1,19 @@ +package org.traccar.protocol; + +import org.junit.Test; +import org.traccar.ProtocolTest; + +public class FifotrackFrameDecoderTest extends ProtocolTest { + + @Test + public void testDecode() throws Exception { + + FifotrackFrameDecoder decoder = new FifotrackFrameDecoder(); + + verifyFrame( + binary("24243132362c3836393436373034393239303738372c324138432c4130312c2c3139303431333135333235342c412c2d31352e3132373836382c33392e3236323530362c302c3136322c3431352c38303937323234332c302c303030302c30302c302c3634337c337c353141467c424632462c3141337c3446367c3833327c302c312c2a3534"), + decoder.decode(null, null, binary("24243132362c3836393436373034393239303738372c324138432c4130312c2c3139303431333135333235342c412c2d31352e3132373836382c33392e3236323530362c302c3136322c3431352c38303937323234332c302c303030302c30302c302c3634337c337c353141467c424632462c3141337c3446367c3833327c302c312c2a35340d0a"))); + + } + +} diff --git a/src/test/java/org/traccar/protocol/FifotrackProtocolDecoderTest.java b/src/test/java/org/traccar/protocol/FifotrackProtocolDecoderTest.java index 1dcfc89c4..11492fb6f 100644 --- a/src/test/java/org/traccar/protocol/FifotrackProtocolDecoderTest.java +++ b/src/test/java/org/traccar/protocol/FifotrackProtocolDecoderTest.java @@ -10,19 +10,22 @@ public class FifotrackProtocolDecoderTest extends ProtocolTest { FifotrackProtocolDecoder decoder = new FifotrackProtocolDecoder(null); - verifyNull(decoder, text( + verifyNull(decoder, buffer( "$$79,868345037864709,382,D05,190220085833,22.643210,114.018176,1,1,1,13152,23FFD339*25")); - verifyPosition(decoder, text( + verifyNull(decoder, binary( + "2424313036332c3836383334353033373836343730392c312c4430362c32343434424438362c302c313032342cffd8ffdb008400140e0f120f0d14121012171514181e32211e1c1c1e3d2c2e243249404c4b47404645505a736250556d5645466488656d777b8182814e608d978c7d96737e817c011517171e1a1e3b21213b7c5346537c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7cffc000110801e0028003012100021101031101ffdd0004000affc401a20000010501010101010100000000000000000102030405060708090a0b100002010303020403050504040000017d01020300041105122131410613516107227114328191a1082342b1c11552d1f02433627282090a161718191a25262728292a3435363738393a434445464748494a535455565758595a636465666768696a737475767778797a838485868788898a92939495969798999aa2a3a4a5a6a7a8a9aab2b3b4b5b6b7b8b9bac2c3c4c5c6c7c8c9cad2d3d4d5d6d7d8d9dae1e2e3e4e5e6e7e8e9eaf1f2f3f4f5f6f7f8f9fa0100030101010101010101010000000000000102030405060708090a0b1100020102040403040705040400010277000102031104052131061241510761711322328108144291a1b1c109233352f0156272d10a162434e125f11718191a262728292a35363738393a434445464748494a535455565758595a636465666768696a737475767778797a82838485868788898a92939495969798999aa2a3a4a5a6a7a8a9aab2b3b4b5b6b7b8b9bac2c3c4c5c6c7c8c9cad2d3d4d5d6d7d8d9dae2e3e4e5e6e7e8e9eaf2f3f4f5f6f7f8f9faffda000c03010002110311003f00b0148a705c8cd00479e6917ef7b5003c9ec29b90bf5a00457c366a4620806801921f41da999cf02801ebf4a4e73cf14002b153f2d4a5cb0c8506802261cf4a50b8a007053d718a4c1cf340099c526ecd007fffd07f6a55c86140126e19e69acdcd0037a9a4a002909a004eb4e030334001a4ce280141cd2138a004ed4982074e6800ed49de801698793401ffd18cf4a65002af5a4ce1a8026cf14d278a008f760d20ebf350031cf6149183bb8a009c03de901f9a801c0e78a31400b9c518a004c5140094b8a00fffd28b1462800c518a00414b400b8a00e68016814001a2800a5eb40062908cd002628a0028a00fffd3998e4734b1b7c981400c3d79a7829b7ef73e98a0069f6a4c50034a926a551b47340037a1e4d424734012c43820529001e72680060bfc34a1f6f02800618e6a3c9cd003c336304d0091d680187ad211401fffd47f34a48079a0091946327d2a173e9400a290d002f6a4c7ad00205cf4a7f3b680131c52639a00304521140098a42c68010138a00e28014034d391401fffd58c9a69e6801a3341a004dc69439140085b3da909cd001b69369cf14013019148cb40028229dcd0014b4005142a3739")); + + verifyPosition(decoder, buffer( "$$105,866104023179743,AB,A00,,161007085534,A,54.738791,25.271918,0,350,151,0,17929,0000,0,,246|1|65|96DB,936|0*0B")); - verifyPosition(decoder, text( + verifyPosition(decoder, buffer( "$$103,866104023179743,5,A00,,161006192841,A,54.738791,25.271918,0,342,200,0,4265,0000,0,,246|1|65|96DB,9C4|0*75")); - verifyPosition(decoder, text( + verifyPosition(decoder, buffer( "$$103,866104023179743,4,A00,,161006192810,V,54.738791,25.271918,0,158,122,0,4235,0000,0,,246|1|65|96DB,9C5|0*69")); - verifyPosition(decoder, text( + verifyPosition(decoder, buffer( "$$135,866104023192332,29,A01,,160606093046,A,22.546430,114.079730,0,186,181,0,415322,0000,02,2,460|0|27B3|EA7,A2F|3B9|3|0,940C7E,31.76|30.98*46")); } -- cgit v1.2.3