From ba76f1e5d1c2acedfad92f5e60d54ecb6ed3f362 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Sat, 4 Jul 2020 17:38:14 -0700 Subject: Implement PST acknowledgement --- .../org/traccar/protocol/PstProtocolDecoder.java | 39 +++++++++++++++++++--- .../traccar/protocol/PstProtocolDecoderTest.java | 3 ++ 2 files changed, 38 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/main/java/org/traccar/protocol/PstProtocolDecoder.java b/src/main/java/org/traccar/protocol/PstProtocolDecoder.java index a7cb8ef5c..62cc203d2 100644 --- a/src/main/java/org/traccar/protocol/PstProtocolDecoder.java +++ b/src/main/java/org/traccar/protocol/PstProtocolDecoder.java @@ -16,11 +16,14 @@ package org.traccar.protocol; import io.netty.buffer.ByteBuf; +import io.netty.buffer.Unpooled; import io.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; import org.traccar.DeviceSession; +import org.traccar.NetworkMessage; import org.traccar.Protocol; import org.traccar.helper.BitUtil; +import org.traccar.helper.Checksum; import org.traccar.helper.DateBuilder; import org.traccar.model.Position; @@ -33,6 +36,7 @@ public class PstProtocolDecoder extends BaseProtocolDecoder { super(protocol); } + public static final int MSG_ACK = 0x00; public static final int MSG_STATUS = 0x05; private Date readDate(ByteBuf buf) { @@ -53,23 +57,50 @@ public class PstProtocolDecoder extends BaseProtocolDecoder { return sign * (BitUtil.from(value, 16) + BitUtil.to(value, 16) / 10000.0) / 60; } + private void sendResponse( + Channel channel, SocketAddress remoteAddress, long id, int version, long index, int type) { + if (channel != null) { + + ByteBuf content = Unpooled.buffer(); + content.writeInt((int) id); + content.writeByte(version); + content.writeInt((int) index); + content.writeByte(MSG_ACK); + content.writeByte(type); + + int checksum = Checksum.crc16(Checksum.CRC16_XMODEM, content.nioBuffer()); + + ByteBuf response = Unpooled.buffer(); + response.writeByte('('); + response.writeBytes(content); + content.release(); + response.writeShort(checksum); + response.writeByte(')'); + + channel.writeAndFlush(new NetworkMessage(response, remoteAddress)); + + } + } + @Override protected Object decode( Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { ByteBuf buf = (ByteBuf) msg; - String id = String.valueOf(buf.readUnsignedInt()); - DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, id); + long id = buf.readUnsignedInt(); + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, String.valueOf(id)); if (deviceSession == null) { return null; } - buf.readUnsignedByte(); // version - buf.readUnsignedInt(); // index + int version = buf.readUnsignedByte(); + long index = buf.readUnsignedInt(); int type = buf.readUnsignedByte(); + sendResponse(channel, remoteAddress, id, version, index, type); + if (type == MSG_STATUS) { Position position = new Position(getProtocolName()); diff --git a/src/test/java/org/traccar/protocol/PstProtocolDecoderTest.java b/src/test/java/org/traccar/protocol/PstProtocolDecoderTest.java index dd2564982..d0276eb69 100644 --- a/src/test/java/org/traccar/protocol/PstProtocolDecoderTest.java +++ b/src/test/java/org/traccar/protocol/PstProtocolDecoderTest.java @@ -10,6 +10,9 @@ public class PstProtocolDecoderTest extends ProtocolTest { PstProtocolDecoder decoder = new PstProtocolDecoder(null); + verifyPosition(decoder, binary( + "2faf97de06000024db0551380cbb08070b040000015a0c09b50177e5100a1822da0d010d0f0451380628101451380cc384b800488a84036901b202d3010001061103ffff00150203523687")); + verifyPosition(decoder, binary( "2fafb18306000005ab055166ee8f08080b0400008ec00c09b50022aef924d320f30d01130f045166ee8f10145166ee92805118978b59109900b3000b0000010c110300a7011404b58000001502039ff6e4")); -- cgit v1.2.3