From d4bbb39025acc534453b8df7444f8d1b761c669e Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Sat, 12 Sep 2020 17:34:02 -0700 Subject: Implement FutureWay protocol --- .../traccar/protocol/FutureWayFrameDecoder.java | 47 ++++++++ .../org/traccar/protocol/FutureWayProtocol.java | 38 ++++++ .../traccar/protocol/FutureWayProtocolDecoder.java | 134 +++++++++++++++++++++ .../protocol/FutureWayFrameDecoderTest.java | 19 +++ .../protocol/FutureWayProtocolDecoderTest.java | 21 ++++ .../traccar/protocol/XexunFrameDecoderTest.java | 6 +- 6 files changed, 261 insertions(+), 4 deletions(-) create mode 100644 src/main/java/org/traccar/protocol/FutureWayFrameDecoder.java create mode 100644 src/main/java/org/traccar/protocol/FutureWayProtocol.java create mode 100644 src/main/java/org/traccar/protocol/FutureWayProtocolDecoder.java create mode 100644 src/test/java/org/traccar/protocol/FutureWayFrameDecoderTest.java create mode 100644 src/test/java/org/traccar/protocol/FutureWayProtocolDecoderTest.java (limited to 'src') diff --git a/src/main/java/org/traccar/protocol/FutureWayFrameDecoder.java b/src/main/java/org/traccar/protocol/FutureWayFrameDecoder.java new file mode 100644 index 000000000..812f78d02 --- /dev/null +++ b/src/main/java/org/traccar/protocol/FutureWayFrameDecoder.java @@ -0,0 +1,47 @@ +/* + * Copyright 2020 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.buffer.Unpooled; +import io.netty.channel.Channel; +import io.netty.channel.ChannelHandlerContext; +import org.traccar.BaseFrameDecoder; +import org.traccar.helper.DataConverter; + +import java.nio.charset.StandardCharsets; + +public class FutureWayFrameDecoder extends BaseFrameDecoder { + + @Override + protected Object decode( + ChannelHandlerContext ctx, Channel channel, ByteBuf buf) throws Exception { + + if (buf.readableBytes() < 10) { + return null; + } + + int length = Unpooled.wrappedBuffer(DataConverter.parseHex( + buf.getCharSequence(buf.readerIndex() + 2, 8, StandardCharsets.US_ASCII).toString())).readInt() + 17; + + if (buf.readableBytes() >= length) { + return buf.readRetainedSlice(length); + } + + return null; + } + +} diff --git a/src/main/java/org/traccar/protocol/FutureWayProtocol.java b/src/main/java/org/traccar/protocol/FutureWayProtocol.java new file mode 100644 index 000000000..73b53ee12 --- /dev/null +++ b/src/main/java/org/traccar/protocol/FutureWayProtocol.java @@ -0,0 +1,38 @@ +/* + * Copyright 2020 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.handler.codec.string.StringDecoder; +import io.netty.handler.codec.string.StringEncoder; +import org.traccar.BaseProtocol; +import org.traccar.PipelineBuilder; +import org.traccar.TrackerServer; + +public class FutureWayProtocol extends BaseProtocol { + + public FutureWayProtocol() { + addServer(new TrackerServer(false, getName()) { + @Override + protected void addProtocolHandlers(PipelineBuilder pipeline) { + pipeline.addLast(new FutureWayFrameDecoder()); + pipeline.addLast(new StringEncoder()); + pipeline.addLast(new StringDecoder()); + pipeline.addLast(new FutureWayProtocolDecoder(FutureWayProtocol.this)); + } + }); + } + +} diff --git a/src/main/java/org/traccar/protocol/FutureWayProtocolDecoder.java b/src/main/java/org/traccar/protocol/FutureWayProtocolDecoder.java new file mode 100644 index 000000000..4b0f46e34 --- /dev/null +++ b/src/main/java/org/traccar/protocol/FutureWayProtocolDecoder.java @@ -0,0 +1,134 @@ +/* + * Copyright 2020 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.buffer.Unpooled; +import io.netty.channel.Channel; +import org.traccar.BaseProtocolDecoder; +import org.traccar.DeviceSession; +import org.traccar.Protocol; +import org.traccar.helper.DataConverter; +import org.traccar.helper.Parser; +import org.traccar.helper.PatternBuilder; +import org.traccar.model.CellTower; +import org.traccar.model.Network; +import org.traccar.model.Position; +import org.traccar.model.WifiAccessPoint; + +import java.net.SocketAddress; +import java.util.regex.Pattern; + +public class FutureWayProtocolDecoder extends BaseProtocolDecoder { + + public FutureWayProtocolDecoder(Protocol protocol) { + super(protocol); + } + + private static final Pattern PATTERN_GPS = new PatternBuilder() + .text("GPS:") + .expression("([AV]),") // validity + .number("(dd)(dd)(dd)") // date (yymmdd) + .number("(dd)(dd)(dd),") // time (hhmmss) + .number("(d+.d+)([NS]),") // latitude + .number("(d+.d+)([EW]),") // longitude + .number("(d+.d+),") // speed + .number("(d+.d+)") // course + .compile(); + + @Override + protected Object decode( + Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { + + String sentence = (String) msg; + + ByteBuf header = Unpooled.wrappedBuffer(DataConverter.parseHex(sentence.substring(0, 16))); + sentence = sentence.substring(16, sentence.length() - 4); + + header.readUnsignedByte(); // header + header.readUnsignedInt(); // length + int type = header.readUnsignedByte(); + header.readUnsignedShort(); // index + + if (type == 0x20) { + + getDeviceSession(channel, remoteAddress, sentence.split(",")[1].substring(5)); + + } else if (type == 0xA0) { + + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress); + if (deviceSession == null) { + return null; + } + + Position position = new Position(getProtocolName()); + position.setDeviceId(deviceSession.getDeviceId()); + + Network network = new Network(); + + for (String line : sentence.split("\r\n")) { + + if (line.startsWith("GPS")) { + + Parser parser = new Parser(PATTERN_GPS, line); + if (!parser.matches()) { + return null; + } + + position.setValid(parser.next().equals("A")); + position.setTime(parser.nextDateTime()); + position.setLatitude(parser.nextCoordinate(Parser.CoordinateFormat.DEG_HEM)); + position.setLongitude(parser.nextCoordinate(Parser.CoordinateFormat.DEG_HEM)); + position.setSpeed(parser.nextDouble()); + position.setCourse(parser.nextDouble()); + + } else if (line.startsWith("WIFI")) { + + for (String item : line.substring(line.indexOf(',') + 1).split("&")) { + String[] values = item.split("\\|"); + network.addWifiAccessPoint( + WifiAccessPoint.from(values[1].replace('-', ':'), Integer.parseInt(values[2]))); + } + + } else if (line.startsWith("LBS")) { + + String[] values = line.substring("LBS:".length()).split(","); + network.addCellTower(CellTower.from( + Integer.parseInt(values[0]), + Integer.parseInt(values[1]), + Integer.parseInt(values[3]), + Integer.parseInt(values[2]))); + + } + + } + + if (!network.getCellTowers().isEmpty() || !network.getWifiAccessPoints().isEmpty()) { + position.setNetwork(network); + } + + if (position.getFixTime() == null) { + getLastLocation(position, null); + } + + return position; + + } + + return null; + } + +} diff --git a/src/test/java/org/traccar/protocol/FutureWayFrameDecoderTest.java b/src/test/java/org/traccar/protocol/FutureWayFrameDecoderTest.java new file mode 100644 index 000000000..fb1e395f1 --- /dev/null +++ b/src/test/java/org/traccar/protocol/FutureWayFrameDecoderTest.java @@ -0,0 +1,19 @@ +package org.traccar.protocol; + +import org.junit.Test; +import org.traccar.ProtocolTest; + +public class FutureWayFrameDecoderTest extends ProtocolTest { + + @Test + public void testDecode() throws Exception { + + FutureWayFrameDecoder decoder = new FutureWayFrameDecoder(); + + verifyFrame( + binary("34313030303030303346323030303032302c494d45493a3335343832383130303132363436312c62617474657279206c6576656c3a362c6e6574776f726b20747970653a372c4353513a323336463432"), + decoder.decode(null, null, binary("34313030303030303346323030303032302c494d45493a3335343832383130303132363436312c62617474657279206c6576656c3a362c6e6574776f726b20747970653a372c4353513a323336463432"))); + + } + +} diff --git a/src/test/java/org/traccar/protocol/FutureWayProtocolDecoderTest.java b/src/test/java/org/traccar/protocol/FutureWayProtocolDecoderTest.java new file mode 100644 index 000000000..951b3bb20 --- /dev/null +++ b/src/test/java/org/traccar/protocol/FutureWayProtocolDecoderTest.java @@ -0,0 +1,21 @@ +package org.traccar.protocol; + +import org.junit.Test; +import org.traccar.ProtocolTest; + +public class FutureWayProtocolDecoderTest extends ProtocolTest { + + @Test + public void testDecode() throws Exception { + + FutureWayProtocolDecoder decoder = new FutureWayProtocolDecoder(null); + + verifyNull(decoder, text( + "410000003F2000020,IMEI:354828100126461,battery level:6,network type:7,CSQ:236F42")); + + verifyPosition(decoder, text( + "410000009BA00004GPS:V,200902093333,0.000000N,0.000000E,0.000,0.000\r\nWIFI:3,1|90-67-1C-F7-21-6C|52&2|80-89-17-C6-79-A0|54&3|40-F4-20-EF-DD-2A|58\r\nLBS:460,0,46475066,69\r\n6A42")); + + } + +} diff --git a/src/test/java/org/traccar/protocol/XexunFrameDecoderTest.java b/src/test/java/org/traccar/protocol/XexunFrameDecoderTest.java index 8fc628bdb..b91cd0ebf 100644 --- a/src/test/java/org/traccar/protocol/XexunFrameDecoderTest.java +++ b/src/test/java/org/traccar/protocol/XexunFrameDecoderTest.java @@ -3,8 +3,6 @@ package org.traccar.protocol; import org.junit.Test; import org.traccar.ProtocolTest; -import static org.junit.Assert.assertEquals; - public class XexunFrameDecoderTest extends ProtocolTest { @Test @@ -12,11 +10,11 @@ public class XexunFrameDecoderTest extends ProtocolTest { XexunFrameDecoder decoder = new XexunFrameDecoder(); - assertEquals( + verifyFrame( binary("4750524d432c3230353933352e3030302c412c353134302e343335302c4e2c3530312e303638362c452c302e30302c302e30302c3132313031352c30302c303030302e302c412a37302c462c2c696d65693a3335393538373031343731383339322c"), decoder.decode(null, null, binary("313531303132313435392c2b33313635323435343932372c4750524d432c3230353933352e3030302c412c353134302e343335302c4e2c3530312e303638362c452c302e30302c302e30302c3132313031352c30302c303030302e302c412a37302c462c2c696d65693a3335393538373031343731383339322c31323249"))); - assertEquals( + verifyFrame( binary("4750524d432c3130333733312e3633362c412c343534352e353236362c4e2c30303434382e383235392c452c32312e31322c3237362e30312c3135303631352c2c2c412a35372c4c2c2c20696d65693a3031333934393030323032363637352c"), decoder.decode(null, null, binary("3135303631353132333733312c2b33333634373338343631312c4750524d432c3130333733312e3633362c412c343534352e353236362c4e2c30303434382e383235392c452c32312e31322c3237362e30312c3135303631352c2c2c412a35372c4c2c2c20696d65693a3031333934393030323032363637352c30342c333532322e392c463a332e3732562c302c3134322c32313734342c3230382c30312c303730322c394338430a0d"))); -- cgit v1.2.3