diff options
Diffstat (limited to 'src/main/java/org')
3 files changed, 69 insertions, 14 deletions
diff --git a/src/main/java/org/traccar/protocol/DingtekFrameDecoder.java b/src/main/java/org/traccar/protocol/DingtekFrameDecoder.java new file mode 100644 index 000000000..a8e35cc5c --- /dev/null +++ b/src/main/java/org/traccar/protocol/DingtekFrameDecoder.java @@ -0,0 +1,43 @@ +/* + * 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 io.netty.channel.ChannelHandlerContext; +import org.traccar.BaseFrameDecoder; + +import java.nio.charset.StandardCharsets; + +public class DingtekFrameDecoder extends BaseFrameDecoder { + + @Override + protected Object decode( + ChannelHandlerContext ctx, Channel channel, ByteBuf buf) throws Exception { + + if (buf.readableBytes() < 10) { + return null; + } + + int length = Integer.parseInt(buf.toString(8, 2, StandardCharsets.US_ASCII), 16) * 2; + if (buf.readableBytes() >= length) { + return buf.readRetainedSlice(length); + } + + return null; + } + +} diff --git a/src/main/java/org/traccar/protocol/DingtekProtocol.java b/src/main/java/org/traccar/protocol/DingtekProtocol.java index 31962202c..cf2a6c0f5 100644 --- a/src/main/java/org/traccar/protocol/DingtekProtocol.java +++ b/src/main/java/org/traccar/protocol/DingtekProtocol.java @@ -15,7 +15,8 @@ */ package org.traccar.protocol; -import io.netty.handler.codec.LengthFieldBasedFrameDecoder; +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; @@ -26,7 +27,9 @@ public class DingtekProtocol extends BaseProtocol { addServer(new TrackerServer(false, getName()) { @Override protected void addProtocolHandlers(PipelineBuilder pipeline) { - pipeline.addLast(new LengthFieldBasedFrameDecoder(1024, 4, 1)); + pipeline.addLast(new DingtekFrameDecoder()); + pipeline.addLast(new StringDecoder()); + pipeline.addLast(new StringEncoder()); pipeline.addLast(new DingtekProtocolDecoder(DingtekProtocol.this)); } }); diff --git a/src/main/java/org/traccar/protocol/DingtekProtocolDecoder.java b/src/main/java/org/traccar/protocol/DingtekProtocolDecoder.java index 1488c2cd6..98fe4b7b3 100644 --- a/src/main/java/org/traccar/protocol/DingtekProtocolDecoder.java +++ b/src/main/java/org/traccar/protocol/DingtekProtocolDecoder.java @@ -17,13 +17,16 @@ package org.traccar.protocol; import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBufUtil; +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.model.Position; import java.net.SocketAddress; +import java.util.Date; public class DingtekProtocolDecoder extends BaseProtocolDecoder { @@ -35,23 +38,29 @@ public class DingtekProtocolDecoder extends BaseProtocolDecoder { protected Object decode( Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { - ByteBuf buf = (ByteBuf) msg; + String sentence = (String) msg; - buf.readUnsignedByte(); // header - buf.readUnsignedByte(); // forced - buf.readUnsignedByte(); // device type - int type = buf.readUnsignedByte(); - buf.readUnsignedByte(); // length - - String imei = ByteBufUtil.hexDump(buf.slice(buf.writerIndex() - 9, 8)).substring(1); - DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, imei); - if (deviceSession == null) { - return null; - } + int type = Integer.parseInt(sentence.substring(6, 8), 16); if (type == 0x01 || type == 0x02 || type == 0x04) { + + ByteBuf buf = Unpooled.wrappedBuffer(DataConverter.parseHex(sentence)); + + buf.readUnsignedByte(); // header + buf.readUnsignedByte(); // forced + buf.readUnsignedByte(); // device type + buf.readUnsignedByte(); // type + buf.readUnsignedByte(); // length + + String imei = ByteBufUtil.hexDump(buf.slice(buf.writerIndex() - 9, 8)).substring(1); + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, imei); + if (deviceSession == null) { + return null; + } + Position position = new Position(getProtocolName()); position.setDeviceId(deviceSession.getDeviceId()); + position.setTime(new Date()); position.set("height", buf.readUnsignedShort()); |