aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorValerii Vyshniak <valeravi@vi-soft.com.ua>2017-11-22 02:05:59 +0100
committerValerii Vyshniak <valeravi@vi-soft.com.ua>2017-11-22 02:05:59 +0100
commite5a8be1987aecf3735801165ee19c1bcdd4fcc05 (patch)
tree2704face5d94dd5aab2d4d224578ce33fab05dc9
parent8d8bab8a2df30629fb612b0573b2bd5f46c4b05e (diff)
downloadtrackermap-server-e5a8be1987aecf3735801165ee19c1bcdd4fcc05.tar.gz
trackermap-server-e5a8be1987aecf3735801165ee19c1bcdd4fcc05.tar.bz2
trackermap-server-e5a8be1987aecf3735801165ee19c1bcdd4fcc05.zip
T580W: review comments fixes 3, re-implemented tk103 frame decoder
-rw-r--r--src/org/traccar/protocol/Tk103FrameDecoder.java66
-rw-r--r--src/org/traccar/protocol/Tk103Protocol.java3
-rw-r--r--src/org/traccar/protocol/Tk103ProtocolDecoder.java2
-rw-r--r--test/org/traccar/BaseTest.java2
-rw-r--r--test/org/traccar/FilterHandlerTest.java2
-rw-r--r--test/org/traccar/protocol/Tk103ProtocolDecoderTest.java1
-rw-r--r--test/org/traccar/protocol/Tk103ProtocolEncoderTest.java32
7 files changed, 76 insertions, 32 deletions
diff --git a/src/org/traccar/protocol/Tk103FrameDecoder.java b/src/org/traccar/protocol/Tk103FrameDecoder.java
new file mode 100644
index 000000000..17e791553
--- /dev/null
+++ b/src/org/traccar/protocol/Tk103FrameDecoder.java
@@ -0,0 +1,66 @@
+/*
+ * Copyright 2017 Valerii Vyshniak (val@val.one)
+ * Copyright 2017 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.jboss.netty.buffer.ChannelBuffer;
+import org.jboss.netty.buffer.ChannelBuffers;
+import org.jboss.netty.channel.Channel;
+import org.jboss.netty.channel.ChannelHandlerContext;
+import org.jboss.netty.handler.codec.frame.FrameDecoder;
+
+import java.nio.ByteOrder;
+
+public class Tk103FrameDecoder extends FrameDecoder {
+
+ @Override
+ protected Object decode(
+ ChannelHandlerContext ctx,
+ Channel channel,
+ ChannelBuffer buf) throws Exception {
+
+ if (buf.readableBytes() < 2) {
+ return null;
+ }
+
+ int index;
+ for (index = buf.readerIndex(); true; index++) {
+ index = buf.indexOf(index, buf.writerIndex(), (byte) ')');
+ if (index == -1) {
+ break;
+ }
+ int cnt = 0;
+ for (int i = buf.readerIndex(); i < index; i++) {
+ if (buf.getByte(i) == (byte) '$') {
+ cnt++;
+ }
+ }
+ if (cnt % 2 == 0) {
+ break;
+ }
+ }
+
+ if (index != -1) {
+ ChannelBuffer result = ChannelBuffers.buffer(ByteOrder.LITTLE_ENDIAN, index + 1 - buf.readerIndex());
+ buf.readBytes(result, index + 1 - buf.readerIndex());
+ result.writerIndex(result.writerIndex() - 1);
+ return result;
+ }
+
+ return null;
+ }
+
+}
diff --git a/src/org/traccar/protocol/Tk103Protocol.java b/src/org/traccar/protocol/Tk103Protocol.java
index 3360dab32..6ef9c0a56 100644
--- a/src/org/traccar/protocol/Tk103Protocol.java
+++ b/src/org/traccar/protocol/Tk103Protocol.java
@@ -22,7 +22,6 @@ import org.jboss.netty.channel.ChannelPipeline;
import org.jboss.netty.handler.codec.string.StringDecoder;
import org.jboss.netty.handler.codec.string.StringEncoder;
import org.traccar.BaseProtocol;
-import org.traccar.CharacterDelimiterFrameDecoder;
import org.traccar.TrackerServer;
import org.traccar.model.Command;
@@ -56,7 +55,7 @@ public class Tk103Protocol extends BaseProtocol {
serverList.add(new TrackerServer(new ServerBootstrap(), getName()) {
@Override
protected void addSpecificHandlers(ChannelPipeline pipeline) {
- pipeline.addLast("frameDecoder", new CharacterDelimiterFrameDecoder(1024, ')'));
+ pipeline.addLast("frameDecoder", new Tk103FrameDecoder());
pipeline.addLast("stringDecoder", new StringDecoder());
pipeline.addLast("stringEncoder", new StringEncoder());
pipeline.addLast("objectEncoder", new Tk103ProtocolEncoder());
diff --git a/src/org/traccar/protocol/Tk103ProtocolDecoder.java b/src/org/traccar/protocol/Tk103ProtocolDecoder.java
index 2ce7def45..2fc6bf8d5 100644
--- a/src/org/traccar/protocol/Tk103ProtocolDecoder.java
+++ b/src/org/traccar/protocol/Tk103ProtocolDecoder.java
@@ -353,7 +353,7 @@ public class Tk103ProtocolDecoder extends BaseProtocolDecoder {
sentence = sentence.substring(beginIndex + 1);
}
- if (channel != null && sentence.length() >= 16) {
+ if (channel != null) {
String id = sentence.substring(0, 12);
String type = sentence.substring(12, 16);
if (type.equals("BP00")) {
diff --git a/test/org/traccar/BaseTest.java b/test/org/traccar/BaseTest.java
index 87ff3842c..4b9ee5451 100644
--- a/test/org/traccar/BaseTest.java
+++ b/test/org/traccar/BaseTest.java
@@ -40,7 +40,7 @@ public class BaseTest {
@Override
public boolean lookupAttributeBoolean(
long deviceId, String attributeName, boolean defaultValue, boolean lookupConfig) {
- return (deviceId == 2 && attributeName == "tk103.alternative" && lookupConfig) ? true : defaultValue;
+ return defaultValue;
}
@Override
diff --git a/test/org/traccar/FilterHandlerTest.java b/test/org/traccar/FilterHandlerTest.java
index e896aa450..7ebab3af5 100644
--- a/test/org/traccar/FilterHandlerTest.java
+++ b/test/org/traccar/FilterHandlerTest.java
@@ -48,7 +48,7 @@ public class FilterHandlerTest {
@Override
public boolean lookupAttributeBoolean(
long deviceId, String attributeName, boolean defaultValue, boolean lookupConfig) {
- return (deviceId == 2 && attributeName == "tk103.alternative" && lookupConfig) ? true : defaultValue;
+ return defaultValue;
}
@Override
diff --git a/test/org/traccar/protocol/Tk103ProtocolDecoderTest.java b/test/org/traccar/protocol/Tk103ProtocolDecoderTest.java
index 74edd10ab..0ceb3404e 100644
--- a/test/org/traccar/protocol/Tk103ProtocolDecoderTest.java
+++ b/test/org/traccar/protocol/Tk103ProtocolDecoderTest.java
@@ -159,7 +159,6 @@ public class Tk103ProtocolDecoderTest extends ProtocolTest {
verifyPosition(decoder, text(
"(094050000111BP05000094050000111150808A3804.2418N04616.7468E000.0201447133.3501000011L0028019DT000)"));
- //T580W
verifyPosition(decoder, text(
"(864555555555555,DW3B,131117,A,5544.02870N,01315.08194E,1.597,223707,291.65,-0.10,4)"));
diff --git a/test/org/traccar/protocol/Tk103ProtocolEncoderTest.java b/test/org/traccar/protocol/Tk103ProtocolEncoderTest.java
index f902268ce..0c16ccee6 100644
--- a/test/org/traccar/protocol/Tk103ProtocolEncoderTest.java
+++ b/test/org/traccar/protocol/Tk103ProtocolEncoderTest.java
@@ -31,10 +31,6 @@ public class Tk103ProtocolEncoderTest extends ProtocolTest {
Assert.assertEquals("(123456789012345AP00)", encoder.encodeCommand(command));
- command.setDeviceId(2);
-
- Assert.assertEquals("[begin]sms2,*getposl*,[end]", encoder.encodeCommand(command));
-
}
@Test
@@ -49,10 +45,6 @@ public class Tk103ProtocolEncoderTest extends ProtocolTest {
Assert.assertEquals("(123456789012345AR00003C0000)", encoder.encodeCommand(command));
- command.setDeviceId(2);
-
- Assert.assertEquals("[begin]sms2,*routetrack*99*,[end]", encoder.encodeCommand(command));
-
}
@Test
@@ -66,10 +58,6 @@ public class Tk103ProtocolEncoderTest extends ProtocolTest {
Assert.assertEquals("(123456789012345AR0000000000)", encoder.encodeCommand(command));
- command.setDeviceId(2);
-
- Assert.assertEquals("[begin]sms2,*routetrackoff*,[end]", encoder.encodeCommand(command));
-
}
@Test
@@ -83,10 +71,6 @@ public class Tk103ProtocolEncoderTest extends ProtocolTest {
Assert.assertEquals("(123456789012345AP07)", encoder.encodeCommand(command));
- command.setDeviceId(2);
-
- Assert.assertEquals("[begin]sms2,*about*,[end]", encoder.encodeCommand(command));
-
}
@Test
@@ -100,10 +84,6 @@ public class Tk103ProtocolEncoderTest extends ProtocolTest {
Assert.assertEquals("(123456789012345AT00)", encoder.encodeCommand(command));
- command.setDeviceId(2);
-
- Assert.assertEquals("[begin]sms2,88888888,[end]", encoder.encodeCommand(command));
-
}
@Test
@@ -125,7 +105,7 @@ public class Tk103ProtocolEncoderTest extends ProtocolTest {
Tk103ProtocolEncoder encoder = new Tk103ProtocolEncoder();
Command command = new Command();
- command.setDeviceId(2);
+ command.setDeviceId(1);
command.setType(Command.TYPE_IDENTIFICATION);
Assert.assertEquals("[begin]sms2,999999,[end]", encoder.encodeCommand(command));
@@ -138,7 +118,7 @@ public class Tk103ProtocolEncoderTest extends ProtocolTest {
Tk103ProtocolEncoder encoder = new Tk103ProtocolEncoder();
Command command = new Command();
- command.setDeviceId(2);
+ command.setDeviceId(1);
command.setType(Command.TYPE_ALARM_SOS);
command.set(Command.KEY_ENABLE, true);
@@ -152,7 +132,7 @@ public class Tk103ProtocolEncoderTest extends ProtocolTest {
Tk103ProtocolEncoder encoder = new Tk103ProtocolEncoder();
Command command = new Command();
- command.setDeviceId(2);
+ command.setDeviceId(1);
command.setType(Command.TYPE_ALARM_SOS);
command.set(Command.KEY_ENABLE, false);
@@ -166,7 +146,7 @@ public class Tk103ProtocolEncoderTest extends ProtocolTest {
Tk103ProtocolEncoder encoder = new Tk103ProtocolEncoder();
Command command = new Command();
- command.setDeviceId(2);
+ command.setDeviceId(1);
command.setType(Command.TYPE_CUSTOM);
command.set(Command.KEY_DATA, "any text is ok");
@@ -180,7 +160,7 @@ public class Tk103ProtocolEncoderTest extends ProtocolTest {
Tk103ProtocolEncoder encoder = new Tk103ProtocolEncoder();
Command command = new Command();
- command.setDeviceId(2);
+ command.setDeviceId(1);
command.setType(Command.TYPE_SET_CONNECTION);
command.set(Command.KEY_SERVER, "1.2.3.4");
command.set(Command.KEY_PORT, "5555");
@@ -195,7 +175,7 @@ public class Tk103ProtocolEncoderTest extends ProtocolTest {
Tk103ProtocolEncoder encoder = new Tk103ProtocolEncoder();
Command command = new Command();
- command.setDeviceId(2);
+ command.setDeviceId(1);
command.setType(Command.TYPE_SOS_NUMBER);
command.set(Command.KEY_INDEX, "0");
command.set(Command.KEY_PHONE, "+55555555555");