diff options
author | Anton Tananaev <anton.tananaev@gmail.com> | 2020-07-29 20:58:02 -0700 |
---|---|---|
committer | Anton Tananaev <anton.tananaev@gmail.com> | 2020-07-29 20:58:02 -0700 |
commit | 250dc821dd304ca72058012b5a387194489cbe72 (patch) | |
tree | b8aba5cdd8c40442734e8e20b7ab05c03fdad813 /src | |
parent | 02156420e2b7e8ca60982c60c26245c77069dd6e (diff) | |
download | trackermap-server-250dc821dd304ca72058012b5a387194489cbe72.tar.gz trackermap-server-250dc821dd304ca72058012b5a387194489cbe72.tar.bz2 trackermap-server-250dc821dd304ca72058012b5a387194489cbe72.zip |
Implement PST commands
Diffstat (limited to 'src')
6 files changed, 173 insertions, 15 deletions
diff --git a/src/main/java/org/traccar/protocol/PstFrameEncoder.java b/src/main/java/org/traccar/protocol/PstFrameEncoder.java new file mode 100644 index 000000000..33ae43fca --- /dev/null +++ b/src/main/java/org/traccar/protocol/PstFrameEncoder.java @@ -0,0 +1,41 @@ +/* + * 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.ChannelHandlerContext; +import io.netty.handler.codec.MessageToByteEncoder; + +public class PstFrameEncoder extends MessageToByteEncoder<ByteBuf> { + + @Override + protected void encode(ChannelHandlerContext ctx, ByteBuf msg, ByteBuf out) { + + out.writeByte('('); + while (msg.isReadable()) { + int b = msg.readUnsignedByte(); + if (b == 0x27 || b == 0x28 || b == 0x29) { + out.writeByte(0x27); + out.writeByte(b ^ 0x40); + } else { + out.writeByte(b); + } + } + out.writeByte(')'); + + msg.release(); + } +} diff --git a/src/main/java/org/traccar/protocol/PstProtocol.java b/src/main/java/org/traccar/protocol/PstProtocol.java index 0ed9affd8..d8c7008cb 100644 --- a/src/main/java/org/traccar/protocol/PstProtocol.java +++ b/src/main/java/org/traccar/protocol/PstProtocol.java @@ -1,5 +1,5 @@ /* - * Copyright 2019 Anton Tananaev (anton@traccar.org) + * Copyright 2019 - 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. @@ -18,20 +18,27 @@ package org.traccar.protocol; import org.traccar.BaseProtocol; import org.traccar.PipelineBuilder; import org.traccar.TrackerServer; +import org.traccar.model.Command; public class PstProtocol extends BaseProtocol { public PstProtocol() { + setSupportedDataCommands( + Command.TYPE_ENGINE_STOP, + Command.TYPE_ENGINE_RESUME); addServer(new TrackerServer(true, getName()) { @Override protected void addProtocolHandlers(PipelineBuilder pipeline) { + pipeline.addLast(new PstProtocolEncoder(PstProtocol.this)); pipeline.addLast(new PstProtocolDecoder(PstProtocol.this)); } }); addServer(new TrackerServer(false, getName()) { @Override protected void addProtocolHandlers(PipelineBuilder pipeline) { + pipeline.addLast(new PstFrameEncoder()); pipeline.addLast(new PstFrameDecoder()); + pipeline.addLast(new PstProtocolEncoder(PstProtocol.this)); pipeline.addLast(new PstProtocolDecoder(PstProtocol.this)); } }); diff --git a/src/main/java/org/traccar/protocol/PstProtocolDecoder.java b/src/main/java/org/traccar/protocol/PstProtocolDecoder.java index 62cc203d2..e3fe1af62 100644 --- a/src/main/java/org/traccar/protocol/PstProtocolDecoder.java +++ b/src/main/java/org/traccar/protocol/PstProtocolDecoder.java @@ -38,6 +38,7 @@ public class PstProtocolDecoder extends BaseProtocolDecoder { public static final int MSG_ACK = 0x00; public static final int MSG_STATUS = 0x05; + public static final int MSG_COMMAND = 0x06; private Date readDate(ByteBuf buf) { long value = buf.readUnsignedInt(); @@ -61,21 +62,13 @@ public class PstProtocolDecoder extends BaseProtocolDecoder { 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(')'); + response.writeInt((int) id); + response.writeByte(version); + response.writeInt((int) index); + response.writeByte(MSG_ACK); + response.writeByte(type); + response.writeShort(Checksum.crc16(Checksum.CRC16_XMODEM, response.nioBuffer())); channel.writeAndFlush(new NetworkMessage(response, remoteAddress)); diff --git a/src/main/java/org/traccar/protocol/PstProtocolEncoder.java b/src/main/java/org/traccar/protocol/PstProtocolEncoder.java new file mode 100644 index 000000000..f3d193324 --- /dev/null +++ b/src/main/java/org/traccar/protocol/PstProtocolEncoder.java @@ -0,0 +1,62 @@ +/* + * 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.buffer.Unpooled; +import org.traccar.BaseProtocolEncoder; +import org.traccar.Protocol; +import org.traccar.helper.Checksum; +import org.traccar.model.Command; + +public class PstProtocolEncoder extends BaseProtocolEncoder { + + public PstProtocolEncoder(Protocol protocol) { + super(protocol); + } + + private ByteBuf encodeContent(long deviceId, int type, int data1, int data2) { + + ByteBuf buf = Unpooled.buffer(); + + buf.writeInt((int) Long.parseLong(getUniqueId(deviceId))); + buf.writeByte(0x06); // version + + buf.writeInt(1); // index + buf.writeByte(PstProtocolDecoder.MSG_COMMAND); + buf.writeShort(type); + buf.writeShort(data1); + buf.writeShort(data2); + + buf.writeShort(Checksum.crc16(Checksum.CRC16_XMODEM, buf.nioBuffer())); + + return buf; + } + + @Override + protected Object encodeCommand(Command command) { + + switch (command.getType()) { + case Command.TYPE_ENGINE_STOP: + return encodeContent(command.getDeviceId(), 0x0002, 0xffff, 0xffff); + case Command.TYPE_ENGINE_RESUME: + return encodeContent(command.getDeviceId(), 0x0001, 0xffff, 0xffff); + default: + return null; + } + } + +} diff --git a/src/test/java/org/traccar/protocol/PstFrameEncoderTest.java b/src/test/java/org/traccar/protocol/PstFrameEncoderTest.java new file mode 100644 index 000000000..bc458c398 --- /dev/null +++ b/src/test/java/org/traccar/protocol/PstFrameEncoderTest.java @@ -0,0 +1,20 @@ +package org.traccar.protocol; + +import io.netty.buffer.ByteBuf; +import io.netty.buffer.Unpooled; +import org.junit.Test; +import org.traccar.ProtocolTest; + +public class PstFrameEncoderTest extends ProtocolTest { + + @Test + public void testDecode() throws Exception { + + PstFrameEncoder encoder = new PstFrameEncoder(); + + ByteBuf result = Unpooled.buffer(); + encoder.encode(null, binary("2FAF0B10059A0000B001022FAF0B10E91349A2AD3B1DAD2FF8A78228A58F"), result); + verifyFrame(binary("282FAF0B10059A0000B001022FAF0B10E91349A2AD3B1DAD2FF8A7822768A58F29"), result); + } + +} diff --git a/src/test/java/org/traccar/protocol/PstProtocolEncoderTest.java b/src/test/java/org/traccar/protocol/PstProtocolEncoderTest.java new file mode 100644 index 000000000..ddf6d460c --- /dev/null +++ b/src/test/java/org/traccar/protocol/PstProtocolEncoderTest.java @@ -0,0 +1,35 @@ +package org.traccar.protocol; + +import org.junit.Test; +import org.traccar.ProtocolTest; +import org.traccar.model.Command; + +public class PstProtocolEncoderTest extends ProtocolTest { + + @Test + public void testEncodeEngineStop() { + + PstProtocolEncoder encoder = new PstProtocolEncoder(null); + + Command command = new Command(); + command.setDeviceId(1); + command.setType(Command.TYPE_ENGINE_STOP); + + verifyCommand(encoder, command, binary("860ddf790600000001060002ffffffffe42b")); + + } + + @Test + public void testEncodeEngineResume() { + + PstProtocolEncoder encoder = new PstProtocolEncoder(null); + + Command command = new Command(); + command.setDeviceId(1); + command.setType(Command.TYPE_ENGINE_RESUME); + + verifyCommand(encoder, command, binary("860ddf790600000001060001ffffffff0af9")); + + } + +} |