diff options
author | Anton Tananaev <anton.tananaev@gmail.com> | 2019-11-30 12:22:58 -0800 |
---|---|---|
committer | Anton Tananaev <anton.tananaev@gmail.com> | 2019-11-30 12:22:58 -0800 |
commit | 511520f9ac393735ec2c9fc5a18b2d4be8d071c5 (patch) | |
tree | 09a1b4ee25adf09eabc72112952cf3d361cb4291 /src | |
parent | 44f861b487d8f607d750d9f9c6b3e162b3f012b3 (diff) | |
download | traccar-server-511520f9ac393735ec2c9fc5a18b2d4be8d071c5.tar.gz traccar-server-511520f9ac393735ec2c9fc5a18b2d4be8d071c5.tar.bz2 traccar-server-511520f9ac393735ec2c9fc5a18b2d4be8d071c5.zip |
Implement Omnicomm protocol
Diffstat (limited to 'src')
5 files changed, 254 insertions, 0 deletions
diff --git a/src/main/java/org/traccar/protocol/OmnicommFrameDecoder.java b/src/main/java/org/traccar/protocol/OmnicommFrameDecoder.java new file mode 100644 index 000000000..d6fedc40e --- /dev/null +++ b/src/main/java/org/traccar/protocol/OmnicommFrameDecoder.java @@ -0,0 +1,57 @@ +/* + * 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.buffer.Unpooled; +import io.netty.channel.Channel; +import io.netty.channel.ChannelHandlerContext; +import org.traccar.BaseFrameDecoder; + +public class OmnicommFrameDecoder extends BaseFrameDecoder { + + @Override + protected Object decode( + ChannelHandlerContext ctx, Channel channel, ByteBuf buf) throws Exception { + + if (buf.readableBytes() < 10) { + return null; + } + + int endIndex = buf.getUnsignedShortLE(2) + buf.readerIndex() + 5; + if (buf.writerIndex() < endIndex) { + return null; + } + + ByteBuf result = Unpooled.buffer(); + result.writeByte(buf.readUnsignedByte()); + while (buf.readerIndex() <= endIndex) { + int b = buf.readUnsignedByte(); + if (b == 0xDB) { + int ext = buf.readUnsignedByte(); + if (ext == 0xDC) { + result.writeByte(0xC0); + } else if (ext == 0xDD) { + result.writeByte(0xDB); + } + } else { + result.writeByte(b); + } + } + return result; + } + +} diff --git a/src/main/java/org/traccar/protocol/OmnicommProtocol.java b/src/main/java/org/traccar/protocol/OmnicommProtocol.java new file mode 100644 index 000000000..bf408243b --- /dev/null +++ b/src/main/java/org/traccar/protocol/OmnicommProtocol.java @@ -0,0 +1,35 @@ +/* + * 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.handler.codec.LengthFieldBasedFrameDecoder; +import org.traccar.BaseProtocol; +import org.traccar.PipelineBuilder; +import org.traccar.TrackerServer; + +public class OmnicommProtocol extends BaseProtocol { + + public OmnicommProtocol() { + addServer(new TrackerServer(false, getName()) { + @Override + protected void addProtocolHandlers(PipelineBuilder pipeline) { + pipeline.addLast(new LengthFieldBasedFrameDecoder(256, 2, 1, 2, 0)); + pipeline.addLast(new Gt02ProtocolDecoder(OmnicommProtocol.this)); + } + }); + } + +} diff --git a/src/main/java/org/traccar/protocol/OmnicommProtocolDecoder.java b/src/main/java/org/traccar/protocol/OmnicommProtocolDecoder.java new file mode 100644 index 000000000..7d237e705 --- /dev/null +++ b/src/main/java/org/traccar/protocol/OmnicommProtocolDecoder.java @@ -0,0 +1,116 @@ +/* + * 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 com.google.protobuf.InvalidProtocolBufferException; +import io.netty.buffer.ByteBuf; +import io.netty.buffer.ByteBufUtil; +import io.netty.channel.Channel; +import org.traccar.BaseProtocolDecoder; +import org.traccar.DeviceSession; +import org.traccar.Protocol; +import org.traccar.helper.UnitsConverter; +import org.traccar.model.Position; +import org.traccar.protobuf.OmnicommMessageOuterClass; + +import java.net.SocketAddress; +import java.util.Date; +import java.util.LinkedList; +import java.util.List; + +public class OmnicommProtocolDecoder extends BaseProtocolDecoder { + + public OmnicommProtocolDecoder(Protocol protocol) { + super(protocol); + } + + public static final int MSG_IDENTIFICATION = 0x80; + public static final int MSG_CONTAINER_IM = 0x86; + + private OmnicommMessageOuterClass.OmnicommMessage parseProto( + ByteBuf buf, int length) throws InvalidProtocolBufferException { + + final byte[] array; + final int offset; + if (buf.hasArray()) { + array = buf.array(); + offset = buf.arrayOffset() + buf.readerIndex(); + } else { + array = ByteBufUtil.getBytes(buf, buf.readerIndex(), length, false); + offset = 0; + } + buf.skipBytes(length); + + return OmnicommMessageOuterClass.OmnicommMessage + .getDefaultInstance().getParserForType().parseFrom(array, offset, length); + } + + @Override + protected Object decode( + Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { + + ByteBuf buf = (ByteBuf) msg; + + buf.readUnsignedByte(); // prefix + int type = buf.readUnsignedByte(); + buf.readUnsignedShortLE(); // length + + if (type == MSG_IDENTIFICATION) { + + getDeviceSession(channel, remoteAddress, String.valueOf(buf.readUnsignedIntLE())); + + } else if (type == MSG_CONTAINER_IM) { + + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress); + if (deviceSession == null) { + return null; + } + + buf.readUnsignedIntLE(); // record number + buf.readUnsignedIntLE(); // time + buf.readUnsignedByte(); // priority + + List<Position> positions = new LinkedList<>(); + + while (buf.readableBytes() > 2) { + + OmnicommMessageOuterClass.OmnicommMessage message = parseProto(buf, buf.readUnsignedShortLE()); + + Position position = new Position(getProtocolName()); + position.setDeviceId(deviceSession.getDeviceId()); + + if (message.hasNAV()) { + OmnicommMessageOuterClass.OmnicommMessage.NAV nav = message.getNAV(); + position.setValid(true); + position.setTime(new Date((nav.getGPSTime() + 1230768000) * 1000L)); // from 2009-01-01 12:00 + position.setLatitude(nav.getLAT() * 0.0000001); + position.setLongitude(nav.getLON() * 0.0000001); + position.setSpeed(UnitsConverter.knotsFromKph(nav.getGPSVel() * 0.1)); + position.setCourse(nav.getGPSDir()); + position.setAltitude(nav.getGPSAlt() * 0.1); + position.set(Position.KEY_SATELLITES, nav.getGPSNSat()); + } + + positions.add(position); + } + + return positions; + } + + return null; + } + +} diff --git a/src/test/java/org/traccar/protocol/OmnicommFrameDecoderTest.java b/src/test/java/org/traccar/protocol/OmnicommFrameDecoderTest.java new file mode 100644 index 000000000..a35677093 --- /dev/null +++ b/src/test/java/org/traccar/protocol/OmnicommFrameDecoderTest.java @@ -0,0 +1,23 @@ +package org.traccar.protocol; + +import org.junit.Test; +import org.traccar.ProtocolTest; + +public class OmnicommFrameDecoderTest extends ProtocolTest { + + @Test + public void testDecode() throws Exception { + + OmnicommFrameDecoder decoder = new OmnicommFrameDecoder(); + + verifyFrame( + binary("c080080061a61915340100001dec"), + decoder.decode(null, null, binary("c080080061a61915340100001dec"))); + + verifyFrame( + binary("C0866300CD1400002273231400580008011308A2E68DA10110002006280030003800400048005000600068007000142B08EC979EB60410EEB7CC8C02180020002804300038A2E68DA1012C33080010001800200028003000344308381000180220382800300244DF2A"), + decoder.decode(null, null, binary("C0866300CD1400002273231400580008011308A2E68DA10110002006280030003800400048005000600068007000142B08EC979EB60410EEB7CC8C02180020002804300038A2E68DA1012C33080010001800200028003000344308381000180220382800300244DF2A"))); + + } + +} diff --git a/src/test/java/org/traccar/protocol/OmnicommProtocolDecoderTest.java b/src/test/java/org/traccar/protocol/OmnicommProtocolDecoderTest.java new file mode 100644 index 000000000..5219e125b --- /dev/null +++ b/src/test/java/org/traccar/protocol/OmnicommProtocolDecoderTest.java @@ -0,0 +1,23 @@ +package org.traccar.protocol; + +import org.junit.Ignore; +import org.junit.Test; +import org.traccar.ProtocolTest; + +public class OmnicommProtocolDecoderTest extends ProtocolTest { + + //@Ignore + @Test + public void testDecode() throws Exception { + + OmnicommProtocolDecoder decoder = new OmnicommProtocolDecoder(null); + + verifyNull(decoder, binary( + "c080080061a61915340100001dec")); + + verifyPositions(decoder, binary( + "C0866300CD1400002273231400580008011308A2E68DA10110002006280030003800400048005000600068007000142B08EC979EB60410EEB7CC8C02180020002804300038A2E68DA1012C33080010001800200028003000344308381000180220382800300244DF2A")); + + } + +} |