aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnton Tananaev <anton.tananaev@gmail.com>2020-05-29 23:11:19 -0700
committerAnton Tananaev <anton.tananaev@gmail.com>2020-05-29 23:11:19 -0700
commitc524d170f3324ccf10944887faa3d9f35e99e31a (patch)
tree15ddad1449f81cbad53f266665eddbc7615599f6
parentab1022d0c571af09b2036f8694db71537fa430cd (diff)
downloadtrackermap-server-c524d170f3324ccf10944887faa3d9f35e99e31a.tar.gz
trackermap-server-c524d170f3324ccf10944887faa3d9f35e99e31a.tar.bz2
trackermap-server-c524d170f3324ccf10944887faa3d9f35e99e31a.zip
Fix protocol decoding
-rw-r--r--src/main/java/org/traccar/protocol/DingtekFrameDecoder.java43
-rw-r--r--src/main/java/org/traccar/protocol/DingtekProtocol.java7
-rw-r--r--src/main/java/org/traccar/protocol/DingtekProtocolDecoder.java33
-rw-r--r--src/test/java/org/traccar/protocol/DingtekFrameDecoderTest.java19
-rw-r--r--src/test/java/org/traccar/protocol/DingtekProtocolDecoderTest.java7
5 files changed, 92 insertions, 17 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());
diff --git a/src/test/java/org/traccar/protocol/DingtekFrameDecoderTest.java b/src/test/java/org/traccar/protocol/DingtekFrameDecoderTest.java
new file mode 100644
index 000000000..3bc2715db
--- /dev/null
+++ b/src/test/java/org/traccar/protocol/DingtekFrameDecoderTest.java
@@ -0,0 +1,19 @@
+package org.traccar.protocol;
+
+import org.junit.Test;
+import org.traccar.ProtocolTest;
+
+public class DingtekFrameDecoderTest extends ProtocolTest {
+
+ @Test
+ public void testDecode() throws Exception {
+
+ DingtekFrameDecoder decoder = new DingtekFrameDecoder();
+
+ verifyFrame(
+ binary("383030303031303131453032383830303138303030303030303030313545303038303545433430303031313836383832323034303130343433303831"),
+ decoder.decode(null, null, binary("383030303031303131453032383830303138303030303030303030313545303038303545433430303031313836383832323034303130343433303831")));
+
+ }
+
+}
diff --git a/src/test/java/org/traccar/protocol/DingtekProtocolDecoderTest.java b/src/test/java/org/traccar/protocol/DingtekProtocolDecoderTest.java
index ccf23a5c2..3a7567106 100644
--- a/src/test/java/org/traccar/protocol/DingtekProtocolDecoderTest.java
+++ b/src/test/java/org/traccar/protocol/DingtekProtocolDecoderTest.java
@@ -1,20 +1,21 @@
package org.traccar.protocol;
-import org.junit.Ignore;
import org.junit.Test;
import org.traccar.ProtocolTest;
public class DingtekProtocolDecoderTest extends ProtocolTest {
- @Ignore
@Test
public void testDecode() throws Exception {
DingtekProtocolDecoder decoder = new DingtekProtocolDecoder(null);
- verifyPosition(decoder, binary(
+ verifyPosition(decoder, text(
"800001011e0692001a00000000016e008027c40000186962703655111781"));
+ verifyNull(decoder, text(
+ "8000010333020E180A1E4B1E124801042015693400000000173.249.23.186;6181;159.138.4.6;8888;000000000000000081"));
+
}
}