aboutsummaryrefslogtreecommitdiff
path: root/src/org/traccar/protocol/TeltonikaProtocolDecoder.java
diff options
context:
space:
mode:
authorAnton Tananaev <anton.tananaev@gmail.com>2017-01-20 22:26:39 +1300
committerGitHub <noreply@github.com>2017-01-20 22:26:39 +1300
commit14a4dc2ac141ebcc99697736102c9571dc5034aa (patch)
treef04f5bddd9e7c4164d5c509c57342ef1cb77d142 /src/org/traccar/protocol/TeltonikaProtocolDecoder.java
parentd1d87af9559ee261fc1428d826d28408c7dc2c85 (diff)
parentd8e06f0f17f3c82c4410b7dc9e540e8c75e4faa7 (diff)
downloadtrackermap-server-14a4dc2ac141ebcc99697736102c9571dc5034aa.tar.gz
trackermap-server-14a4dc2ac141ebcc99697736102c9571dc5034aa.tar.bz2
trackermap-server-14a4dc2ac141ebcc99697736102c9571dc5034aa.zip
Merge pull request #2813 from srgian/feature/teltonika-decoder-with-udp
Added UDP support on Teltonika Decoder
Diffstat (limited to 'src/org/traccar/protocol/TeltonikaProtocolDecoder.java')
-rw-r--r--src/org/traccar/protocol/TeltonikaProtocolDecoder.java58
1 files changed, 47 insertions, 11 deletions
diff --git a/src/org/traccar/protocol/TeltonikaProtocolDecoder.java b/src/org/traccar/protocol/TeltonikaProtocolDecoder.java
index 074d89703..bc1a4704e 100644
--- a/src/org/traccar/protocol/TeltonikaProtocolDecoder.java
+++ b/src/org/traccar/protocol/TeltonikaProtocolDecoder.java
@@ -18,6 +18,7 @@ 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.socket.DatagramChannel;
import org.traccar.BaseProtocolDecoder;
import org.traccar.DeviceSession;
import org.traccar.helper.BitUtil;
@@ -38,7 +39,7 @@ public class TeltonikaProtocolDecoder extends BaseProtocolDecoder {
super(protocol);
}
- private void parseIdentification(Channel channel, SocketAddress remoteAddress, ChannelBuffer buf) {
+ private DeviceSession parseIdentification(Channel channel, SocketAddress remoteAddress, ChannelBuffer buf) {
int length = buf.readUnsignedShort();
String imei = buf.toString(buf.readerIndex(), length, StandardCharsets.US_ASCII);
@@ -53,6 +54,7 @@ public class TeltonikaProtocolDecoder extends BaseProtocolDecoder {
}
channel.write(response);
}
+ return deviceSession;
}
public static final int CODEC_GH3000 = 0x07;
@@ -211,15 +213,19 @@ public class TeltonikaProtocolDecoder extends BaseProtocolDecoder {
}
- private List<Position> parseData(Channel channel, SocketAddress remoteAddress, ChannelBuffer buf) {
+ private List<Position> parseData(
+ Channel channel, SocketAddress remoteAddress, ChannelBuffer buf, int packetId, String... imei) {
List<Position> positions = new LinkedList<>();
- buf.skipBytes(4); // marker
- buf.readUnsignedInt(); // data length
+ if (!(channel instanceof DatagramChannel)) {
+ buf.readUnsignedInt(); // data length
+ }
+
int codec = buf.readUnsignedByte();
int count = buf.readUnsignedByte();
- DeviceSession deviceSession = getDeviceSession(channel, remoteAddress);
+ DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, imei);
+
if (deviceSession == null) {
return null;
}
@@ -240,27 +246,57 @@ public class TeltonikaProtocolDecoder extends BaseProtocolDecoder {
}
if (channel != null) {
- ChannelBuffer response = ChannelBuffers.directBuffer(4);
- response.writeInt(count);
- channel.write(response);
+ if (channel instanceof DatagramChannel) {
+ ChannelBuffer response = ChannelBuffers.directBuffer(5);
+ response.writeShort(3);
+ response.writeShort(packetId);
+ response.writeByte(0x02);
+ channel.write(response, remoteAddress);
+ } else {
+ ChannelBuffer response = ChannelBuffers.directBuffer(4);
+ response.writeInt(count);
+ channel.write(response);
+ }
}
return positions;
}
@Override
- protected Object decode(
- Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
+ protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
ChannelBuffer buf = (ChannelBuffer) msg;
+ if (channel instanceof DatagramChannel) {
+ return decodeUdp(channel, remoteAddress, buf);
+ } else {
+ return decodeTcp(channel, remoteAddress, buf);
+ }
+ }
+
+ protected Object decodeTcp(Channel channel, SocketAddress remoteAddress, ChannelBuffer buf) throws Exception {
+
if (buf.getUnsignedShort(0) > 0) {
parseIdentification(channel, remoteAddress, buf);
} else {
- return parseData(channel, remoteAddress, buf);
+ buf.skipBytes(4);
+ return parseData(channel, remoteAddress, buf, 0);
}
return null;
}
+ protected Object decodeUdp(Channel channel, SocketAddress remoteAddress, ChannelBuffer buf) throws Exception {
+
+ buf.skipBytes(2);
+ int packetId = buf.readUnsignedShort();
+ buf.skipBytes(2);
+ int imeiLength = buf.readUnsignedShort();
+ String imei = buf.toString(8, imeiLength, StandardCharsets.US_ASCII);
+ buf.skipBytes(imeiLength);
+
+ return parseData(channel, remoteAddress, buf, packetId, imei);
+
+ }
+
}