From 4b19601bea62bfcaf3d1d35b968c7cca70453dbd Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Mon, 3 Aug 2015 14:05:58 +1200 Subject: Fix GT06 commands format --- src/org/traccar/protocol/Gt06ProtocolEncoder.java | 57 +++++++++++----------- .../traccar/protocol/Gt06ProtocolEncoderTest.java | 25 ++++++++++ 2 files changed, 54 insertions(+), 28 deletions(-) create mode 100644 test/org/traccar/protocol/Gt06ProtocolEncoderTest.java diff --git a/src/org/traccar/protocol/Gt06ProtocolEncoder.java b/src/org/traccar/protocol/Gt06ProtocolEncoder.java index 13836945f..c96e49558 100644 --- a/src/org/traccar/protocol/Gt06ProtocolEncoder.java +++ b/src/org/traccar/protocol/Gt06ProtocolEncoder.java @@ -18,47 +18,48 @@ package org.traccar.protocol; import org.jboss.netty.buffer.ChannelBuffer; import org.jboss.netty.buffer.ChannelBuffers; import org.traccar.BaseProtocolEncoder; +import org.traccar.helper.ChannelBufferTools; import org.traccar.helper.Crc; import org.traccar.model.Command; public class Gt06ProtocolEncoder extends BaseProtocolEncoder { + + private ChannelBuffer encodeContent(String content) { + + ChannelBuffer buf = ChannelBuffers.dynamicBuffer(); + + buf.writeByte(0x78); + buf.writeByte(0x78); + + buf.writeByte(1 + 1 + 4 + content.length() + 2 + 2); // message length + + buf.writeByte(0x80); // message type + + buf.writeByte(content.length()); // command length + buf.writeInt(0); + buf.writeBytes(content.getBytes()); // command + + buf.writeShort(0); // message index + + buf.writeShort(Crc.crc16Ccitt(buf.toByteBuffer(2, buf.writerIndex() - 2))); + + buf.writeByte('\r'); + buf.writeByte('\n'); + + return buf; + } @Override protected Object encodeCommand(Command command) { - String content = ""; switch (command.getType()) { case Command.TYPE_ENGINE_STOP: - content = "DYD#"; - break; + return encodeContent("DYD,000000#"); case Command.TYPE_ENGINE_RESUME: - content = "HFYD#"; - break; + return encodeContent("HFYD,000000#"); } - - int serverFlagBit = 0x00; - int commandLength = serverFlagBit + content.length(); - int packetLength = 0x80 + content.length() + 2 + 2; - - ChannelBuffer response = ChannelBuffers.dynamicBuffer(); - response.writeBytes(new byte[]{0x78, 0x78}); // Start Bit - response.writeByte(packetLength); // Packet Length - response.writeByte(0x80); // Protocol Number - - // Information Content - response.writeByte(commandLength); // Length of command - response.writeByte(serverFlagBit); // Server Flag Bit - response.writeBytes(content.getBytes()); // Command Content - response.writeBytes(new byte[]{0x00, 0x02}); // Language - - response.writeShort(1); // Information Serial Number - - int crc = Crc.crc16Ccitt(response.toByteBuffer(2, response.writerIndex())); - response.writeShort(crc); // Error Check - - response.writeBytes(new byte[] {0x0D, 0x0A}); // Stop Bit - return response; + return null; } } diff --git a/test/org/traccar/protocol/Gt06ProtocolEncoderTest.java b/test/org/traccar/protocol/Gt06ProtocolEncoderTest.java new file mode 100644 index 000000000..a24045584 --- /dev/null +++ b/test/org/traccar/protocol/Gt06ProtocolEncoderTest.java @@ -0,0 +1,25 @@ +package org.traccar.protocol; + +import org.jboss.netty.buffer.ChannelBuffers; +import org.junit.Assert; +import org.junit.Test; +import org.traccar.helper.ChannelBufferTools; +import org.traccar.model.Command; + +public class Gt06ProtocolEncoderTest { + + @Test + public void testDecode() throws Exception { + + Gt06ProtocolEncoder encoder = new Gt06ProtocolEncoder(); + + Command command = new Command(); + command.setDeviceId(1); + command.setType(Command.TYPE_ENGINE_STOP); + + Assert.assertEquals(encoder.encodeCommand(command), ChannelBuffers.wrappedBuffer(ChannelBufferTools.convertHexString( + "787815800b000000004459442c303030303030230000f5010d0a"))); + + } + +} -- cgit v1.2.3