aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/org/traccar/ServerManager.java11
-rw-r--r--src/org/traccar/protocol/Pt502FrameDecoder.java62
-rw-r--r--src/org/traccar/protocol/Pt502ProtocolDecoder.java1
-rw-r--r--test/org/traccar/protocol/Pt502ProtocolDecoderTest.java6
4 files changed, 71 insertions, 9 deletions
diff --git a/src/org/traccar/ServerManager.java b/src/org/traccar/ServerManager.java
index 2d3788720..33d364cc4 100644
--- a/src/org/traccar/ServerManager.java
+++ b/src/org/traccar/ServerManager.java
@@ -497,17 +497,16 @@ public class ServerManager {
private void initPt502Server(String protocol) throws SQLException {
if (isProtocolEnabled(properties, protocol)) {
- serverList.add(new TrackerServer(this, new ServerBootstrap(), protocol) {
+ TrackerServer server = new TrackerServer(this, new ServerBootstrap(), protocol) {
@Override
protected void addSpecificHandlers(ChannelPipeline pipeline) {
- byte delimiter[] = { (byte) '\r', (byte) '\n' };
- pipeline.addLast("frameDecoder",
- new DelimiterBasedFrameDecoder(1024, ChannelBuffers.wrappedBuffer(delimiter)));
+ pipeline.addLast("frameDecoder", new Pt502FrameDecoder());
pipeline.addLast("stringDecoder", new StringDecoder());
- pipeline.addLast("stringEncoder", new StringEncoder());
pipeline.addLast("objectDecoder", new Pt502ProtocolDecoder(ServerManager.this));
}
- });
+ };
+ server.setEndianness(ByteOrder.LITTLE_ENDIAN);
+ serverList.add(server);
}
}
diff --git a/src/org/traccar/protocol/Pt502FrameDecoder.java b/src/org/traccar/protocol/Pt502FrameDecoder.java
new file mode 100644
index 000000000..58409ecbf
--- /dev/null
+++ b/src/org/traccar/protocol/Pt502FrameDecoder.java
@@ -0,0 +1,62 @@
+/*
+ * Copyright 2014 Anton Tananaev (anton.tananaev@gmail.com)
+ *
+ * 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.channel.Channel;
+import org.jboss.netty.channel.ChannelHandlerContext;
+import org.jboss.netty.handler.codec.frame.FrameDecoder;
+import org.traccar.helper.ChannelBufferTools;
+
+public class Pt502FrameDecoder extends FrameDecoder {
+
+ private static final int BINARY_HEADER = 5;
+
+ @Override
+ protected Object decode(
+ ChannelHandlerContext ctx,
+ Channel channel,
+ ChannelBuffer buf) throws Exception {
+
+ if (buf.readableBytes() < BINARY_HEADER) {
+ return null;
+ }
+
+ if (buf.getUnsignedByte(buf.readerIndex()) == 0xbf && buf.getUnsignedByte(buf.readerIndex() + 1) == 0xfb) {
+
+ int length = buf.getShort(buf.readerIndex() + 3);
+ if (buf.readableBytes() >= length) {
+ buf.skipBytes(BINARY_HEADER);
+ ChannelBuffer result = buf.readBytes(length - BINARY_HEADER - 2);
+ buf.skipBytes(2);
+ return result;
+ }
+
+ } else {
+
+ Integer index = ChannelBufferTools.find(buf, 0, buf.readableBytes(), "\n");
+ if (index != null) {
+ ChannelBuffer result = buf.readBytes(index - 1);
+ buf.skipBytes(2);
+ return result;
+ }
+
+ }
+
+ return null;
+ }
+
+}
diff --git a/src/org/traccar/protocol/Pt502ProtocolDecoder.java b/src/org/traccar/protocol/Pt502ProtocolDecoder.java
index 179219038..b324e3c55 100644
--- a/src/org/traccar/protocol/Pt502ProtocolDecoder.java
+++ b/src/org/traccar/protocol/Pt502ProtocolDecoder.java
@@ -58,7 +58,6 @@ public class Pt502ProtocolDecoder extends BaseProtocolDecoder {
// Parse message
Matcher parser = pattern.matcher(sentence);
if (!parser.matches()) {
- Log.info("Parsing error");
return null;
}
diff --git a/test/org/traccar/protocol/Pt502ProtocolDecoderTest.java b/test/org/traccar/protocol/Pt502ProtocolDecoderTest.java
index bf7a3962c..d286dde77 100644
--- a/test/org/traccar/protocol/Pt502ProtocolDecoderTest.java
+++ b/test/org/traccar/protocol/Pt502ProtocolDecoderTest.java
@@ -31,8 +31,10 @@ public class Pt502ProtocolDecoderTest {
"$POS,353451000164,082405.000,A,1254.8501,N,10051.6752,E,0.00,237.99,160513,,,A/0000,0/0/55000//a71/"));
verify(decoder.decode(null, null,
- "\u00bf\u00fb\u0059\u006c\u0000$POS,012896008586486,154215.000,A,0118.0143,S,03646.9144,E,0.00,83.29,180714,,,A/0000,0/0/29200//644/"));
-
+ "$POS,012896008586486,154215.000,A,0118.0143,S,03646.9144,E,0.00,83.29,180714,,,A/0000,0/0/29200//644/"));
+
+ verify(decoder.decode(null, null,
+ "$POS,1151000,205326.000,A,0901.3037,N,07928.2751,W,48.79,30.55,170814,,,A/00010,10000/0,0,0,0/15986500//fb8/"));
}