From eb4e0be40343a6b0cd91b083ba185729f8aed334 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Sat, 30 May 2020 12:21:12 -0700 Subject: Implement Wireless Links protocol Devices: STX/Piccolo Plus/ATX2S/TMX/Hybrid/ATX/ATX_II --- .../java/org/traccar/protocol/WliFrameDecoder.java | 60 +++++++++ .../java/org/traccar/protocol/WliProtocol.java | 34 ++++++ .../org/traccar/protocol/WliProtocolDecoder.java | 134 +++++++++++++++++++++ .../org/traccar/protocol/WliFrameDecoderTest.java | 23 ++++ .../traccar/protocol/WliProtocolDecoderTest.java | 24 ++++ 5 files changed, 275 insertions(+) create mode 100644 src/main/java/org/traccar/protocol/WliFrameDecoder.java create mode 100644 src/main/java/org/traccar/protocol/WliProtocol.java create mode 100644 src/main/java/org/traccar/protocol/WliProtocolDecoder.java create mode 100644 src/test/java/org/traccar/protocol/WliFrameDecoderTest.java create mode 100644 src/test/java/org/traccar/protocol/WliProtocolDecoderTest.java (limited to 'src') diff --git a/src/main/java/org/traccar/protocol/WliFrameDecoder.java b/src/main/java/org/traccar/protocol/WliFrameDecoder.java new file mode 100644 index 000000000..d918be1c3 --- /dev/null +++ b/src/main/java/org/traccar/protocol/WliFrameDecoder.java @@ -0,0 +1,60 @@ +/* + * 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; + +public class WliFrameDecoder extends BaseFrameDecoder { + + @Override + protected Object decode( + ChannelHandlerContext ctx, Channel channel, ByteBuf buf) throws Exception { + + if (buf.readableBytes() < 2) { + return null; + } + + int index = buf.indexOf(buf.readerIndex() + 1, buf.writerIndex(), (byte) 0x03); + if (index != -1) { + ByteBuf result = Unpooled.buffer(index + 1 - buf.readerIndex()); + + while (buf.readerIndex() <= index) { + int b = buf.readUnsignedByte(); + if (b == 0xDB) { + int ext = buf.readUnsignedByte(); + if (ext == 0xD2) { + result.writeByte(0x02); + } else if (ext == 0xD3) { + result.writeByte(0x03); + } else if (ext == 0xDD) { + result.writeByte(0xDB); + } + } else { + result.writeByte(b); + } + } + + return result; + } + + return null; + } + +} diff --git a/src/main/java/org/traccar/protocol/WliProtocol.java b/src/main/java/org/traccar/protocol/WliProtocol.java new file mode 100644 index 000000000..6573572d3 --- /dev/null +++ b/src/main/java/org/traccar/protocol/WliProtocol.java @@ -0,0 +1,34 @@ +/* + * 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 org.traccar.BaseProtocol; +import org.traccar.PipelineBuilder; +import org.traccar.TrackerServer; + +public class WliProtocol extends BaseProtocol { + + public WliProtocol() { + addServer(new TrackerServer(false, getName()) { + @Override + protected void addProtocolHandlers(PipelineBuilder pipeline) { + pipeline.addLast(new WliFrameDecoder()); + pipeline.addLast(new Gt02ProtocolDecoder(WliProtocol.this)); + } + }); + } + +} diff --git a/src/main/java/org/traccar/protocol/WliProtocolDecoder.java b/src/main/java/org/traccar/protocol/WliProtocolDecoder.java new file mode 100644 index 000000000..fb3d076c1 --- /dev/null +++ b/src/main/java/org/traccar/protocol/WliProtocolDecoder.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.channel.Channel; +import org.traccar.BaseProtocolDecoder; +import org.traccar.DeviceSession; +import org.traccar.Protocol; +import org.traccar.helper.DateBuilder; +import org.traccar.helper.UnitsConverter; +import org.traccar.model.Position; + +import java.net.SocketAddress; +import java.nio.charset.StandardCharsets; +import java.util.Date; + +public class WliProtocolDecoder extends BaseProtocolDecoder { + + public WliProtocolDecoder(Protocol protocol) { + super(protocol); + } + + @Override + protected Object decode( + Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { + + ByteBuf buf = (ByteBuf) msg; + + buf.readUnsignedByte(); // header + int type = buf.readUnsignedByte(); + + if (type == '1') { + + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress); + if (deviceSession == null) { + return null; + } + + Position position = new Position(getProtocolName()); + position.setDeviceId(deviceSession.getDeviceId()); + + position.set(Position.KEY_INDEX, buf.readUnsignedShort()); + + buf.readUnsignedShort(); // length + buf.readUnsignedShort(); // checksum + buf.readUnsignedByte(); // application message type + buf.readUnsignedByte(); // delimiter + + while (buf.readableBytes() > 1) { + + int fieldNumber = buf.readUnsignedByte(); + + buf.readUnsignedByte(); // delimiter + + if (buf.getUnsignedByte(buf.readerIndex()) == 0xFF) { + + buf.readUnsignedByte(); // binary type indication + int endIndex = buf.readUnsignedShort() + buf.readerIndex(); + + if (fieldNumber == 52) { + position.setValid(true); + buf.readUnsignedByte(); // reason + buf.readUnsignedByte(); // century + DateBuilder dateBuilder = new DateBuilder() + .setDate(buf.readUnsignedByte(), buf.readUnsignedByte(), buf.readUnsignedByte()) + .setTime(buf.readUnsignedByte(), buf.readUnsignedByte(), buf.readUnsignedByte()); + position.setFixTime(dateBuilder.getDate()); + position.setLatitude(buf.readInt() / 600000.0); + position.setLongitude(buf.readInt() / 600000.0); + position.setSpeed(buf.readUnsignedShort()); + position.setCourse(buf.readUnsignedShort() * 0.1); + position.set(Position.KEY_ODOMETER, UnitsConverter.metersFromFeet(buf.readUnsignedInt())); + position.setAltitude(buf.readInt() * 0.1); + } + + buf.readerIndex(endIndex); + + } else { + + int endIndex = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) 0); + String value = buf.readCharSequence( + endIndex - buf.readerIndex(), StandardCharsets.US_ASCII).toString(); + + switch (fieldNumber) { + case 246: + String[] values = value.split(","); + position.set(Position.KEY_POWER, Integer.parseInt(values[2]) * 0.01); + position.set(Position.KEY_BATTERY, Integer.parseInt(values[3]) * 0.01); + break; + case 255: + position.setDeviceTime(new Date(Long.parseLong(value) * 1000)); + break; + default: + break; + } + + } + + buf.readUnsignedByte(); // delimiter + + } + + if (!position.getValid()) { + getLastLocation(position, position.getDeviceTime()); + } + + return position; + + } else if (type == '2') { + + String id = buf.toString(buf.readerIndex(), buf.readableBytes() - 1, StandardCharsets.US_ASCII); + getDeviceSession(channel, remoteAddress, id); + return null; + + } + + return null; + } + +} diff --git a/src/test/java/org/traccar/protocol/WliFrameDecoderTest.java b/src/test/java/org/traccar/protocol/WliFrameDecoderTest.java new file mode 100644 index 000000000..48380738e --- /dev/null +++ b/src/test/java/org/traccar/protocol/WliFrameDecoderTest.java @@ -0,0 +1,23 @@ +package org.traccar.protocol; + +import org.junit.Test; +import org.traccar.ProtocolTest; + +public class WliFrameDecoderTest extends ProtocolTest { + + @Test + public void testDecode() throws Exception { + + WliFrameDecoder decoder = new WliFrameDecoder(); + + verifyFrame( + binary("0231000101bba758c900010034000500ff001001258fc9013e80ed00001183350101e20006003200090030000a0032000b003331000c0031000d00343438000e003530000f003100100031303800130032001b003134001c0033392c33352c32382c33382c34302c33372c33332c33382c33352c34322c33372c3335001d003130001e0038002300343235002400303100250031303432320026003336343733002700323800280037002900312c312c312c312c31302e3232352e3135312e3230342c36353533352c302c39392c39392c3235352c3235352c3235352c323535002a0030002c0030003000300032003000330031003400ff001c0214130502061b0101258fc9013e80ed000001e2000000000000004d004500302c323031392f30352f30322c30363a33333a30302c323031392f30352f30322c30363a32363a3230005a003000f100352c302c342c302c2d312c2d3100f2003300f3003100f50038363634323530333137303639323400f600312c302c302c3431322c3000f70038343437373200f80032312c31312c302c302c302c302c2c2c2c2c2c302c3000f9003300fa00393100fb0032313100fc0032313000ff00313535363737383533340003"), + decoder.decode(null, null, binary("0231000101bba758c900010034000500ff001001258fc9013e80ed00001183350101e20006003200090030000a0032000b003331000c0031000d00343438000e003530000f003100100031303800130032001b003134001c0033392c33352c32382c33382c34302c33372c33332c33382c33352c34322c33372c3335001d003130001e0038002300343235002400303100250031303432320026003336343733002700323800280037002900312c312c312c312c31302e3232352e3135312e3230342c36353533352c302c39392c39392c3235352c3235352c3235352c323535002a0030002c0030003000300032003000330031003400ff001cdbd2141305dbd2061b0101258fc9013e80ed000001e2000000000000004d004500302c323031392f30352f30322c30363a33333a30302c323031392f30352f30322c30363a32363a3230005a003000f100352c302c342c302c2d312c2d3100f2003300f3003100f50038363634323530333137303639323400f600312c302c302c3431322c3000f70038343437373200f80032312c31312c302c302c302c302c2c2c2c2c2c302c3000f9003300fa00393100fb0032313100fc0032313000ff00313535363737383533340003"))); + + verifyFrame( + binary("0231001f008931c7fe00010032353200030030352f32362f323000040031373a32353a34300007004d322e31313954204d617220323020323031340008003000090031303800f600332c382c313338372c33363200f70038313634383200f800302c313134372c302c322c302c3000f9003300fa00383900fb0032353400fc0031383400ff00313539303531333934300003"), + decoder.decode(null, null, binary("0231001f008931c7fe00010032353200dbd30030352f32362f323000040031373a32353a34300007004d322e31313954204d617220323020323031340008003000090031303800f600332c382c313338372c33363200f70038313634383200f800302c313134372c302c322c302c3000f9003300fa00383900fb0032353400fc0031383400ff00313539303531333934300003"))); + + } + +} diff --git a/src/test/java/org/traccar/protocol/WliProtocolDecoderTest.java b/src/test/java/org/traccar/protocol/WliProtocolDecoderTest.java new file mode 100644 index 000000000..642201222 --- /dev/null +++ b/src/test/java/org/traccar/protocol/WliProtocolDecoderTest.java @@ -0,0 +1,24 @@ +package org.traccar.protocol; + +import org.junit.Test; +import org.traccar.ProtocolTest; + +public class WliProtocolDecoderTest extends ProtocolTest { + + @Test + public void testDecode() throws Exception { + + WliProtocolDecoder decoder = new WliProtocolDecoder(null); + + verifyNull(decoder, binary( + "0232776c693a30343930333332303332343103")); + + verifyPosition(decoder, binary( + "0231000101bba758c900010034000500ff001001258fc9013e80ed00001183350101e20006003200090030000a0032000b003331000c0031000d00343438000e003530000f003100100031303800130032001b003134001c0033392c33352c32382c33382c34302c33372c33332c33382c33352c34322c33372c3335001d003130001e0038002300343235002400303100250031303432320026003336343733002700323800280037002900312c312c312c312c31302e3232352e3135312e3230342c36353533352c302c39392c39392c3235352c3235352c3235352c323535002a0030002c0030003000300032003000330031003400ff001c0214130502061b0101258fc9013e80ed000001e2000000000000004d004500302c323031392f30352f30322c30363a33333a30302c323031392f30352f30322c30363a32363a3230005a003000f100352c302c342c302c2d312c2d3100f2003300f3003100f50038363634323530333137303639323400f600312c302c302c3431322c3000f70038343437373200f80032312c31312c302c302c302c302c2c2c2c2c2c302c3000f9003300fa00393100fb0032313100fc0032313000ff00313535363737383533340003")); + + verifyAttributes(decoder, binary( + "0231001f008931c7fe00010032353200030030352f32362f323000040031373a32353a34300007004d322e31313954204d617220323020323031340008003000090031303800f600332c382c313338372c33363200f70038313634383200f800302c313134372c302c322c302c3000f9003300fa00383900fb0032353400fc0031383400ff00313539303531333934300003")); + + } + +} -- cgit v1.2.3