From 387d798a8fc66ec6792851a199fc83c4636fc6d8 Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Tue, 5 Jul 2016 17:16:06 +0500 Subject: Granit protocol implementation --- debug.xml | 1 + setup/unix/traccar.xml | 1 + setup/windows/traccar.xml | 1 + src/org/traccar/model/Command.java | 1 + src/org/traccar/protocol/GranitFrameDecoder.java | 40 ++++ src/org/traccar/protocol/GranitProtocol.java | 51 +++++ .../traccar/protocol/GranitProtocolDecoder.java | 215 +++++++++++++++++++++ .../traccar/protocol/GranitProtocolEncoder.java | 57 ++++++ .../protocol/GranitProtocolDecoderTest.java | 33 ++++ 9 files changed, 400 insertions(+) create mode 100644 src/org/traccar/protocol/GranitFrameDecoder.java create mode 100644 src/org/traccar/protocol/GranitProtocol.java create mode 100644 src/org/traccar/protocol/GranitProtocolDecoder.java create mode 100644 src/org/traccar/protocol/GranitProtocolEncoder.java create mode 100644 test/org/traccar/protocol/GranitProtocolDecoderTest.java diff --git a/debug.xml b/debug.xml index 76f6fdfef..e3d23d5d1 100644 --- a/debug.xml +++ b/debug.xml @@ -431,5 +431,6 @@ 5110 5111 5112 + 5113 diff --git a/setup/unix/traccar.xml b/setup/unix/traccar.xml index c5759f87f..cad96982e 100644 --- a/setup/unix/traccar.xml +++ b/setup/unix/traccar.xml @@ -386,5 +386,6 @@ 5108 5109 5110 + 5113 diff --git a/setup/windows/traccar.xml b/setup/windows/traccar.xml index 1d573f282..fcefd1daa 100644 --- a/setup/windows/traccar.xml +++ b/setup/windows/traccar.xml @@ -386,5 +386,6 @@ 5108 5109 5110 + 5113 diff --git a/src/org/traccar/model/Command.java b/src/org/traccar/model/Command.java index 0a1a62216..d15eb9e70 100644 --- a/src/org/traccar/model/Command.java +++ b/src/org/traccar/model/Command.java @@ -21,6 +21,7 @@ import com.fasterxml.jackson.annotation.JsonIgnoreProperties; public class Command extends Message { public static final String TYPE_CUSTOM = "custom"; + public static final String TYPE_IDENTIFICATION = "deviceIdentification"; public static final String TYPE_POSITION_SINGLE = "positionSingle"; public static final String TYPE_POSITION_PERIODIC = "positionPeriodic"; public static final String TYPE_POSITION_STOP = "positionStop"; diff --git a/src/org/traccar/protocol/GranitFrameDecoder.java b/src/org/traccar/protocol/GranitFrameDecoder.java new file mode 100644 index 000000000..ad6ddc99d --- /dev/null +++ b/src/org/traccar/protocol/GranitFrameDecoder.java @@ -0,0 +1,40 @@ +/* + * Copyright 2016 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.StringFinder; + +public class GranitFrameDecoder extends FrameDecoder { + + @Override + protected Object decode( + ChannelHandlerContext ctx, Channel channel, ChannelBuffer buf) throws Exception { + + int index = buf.indexOf(buf.readerIndex(), buf.writerIndex(), new StringFinder("\r\n")); + if (index != -1) { + ChannelBuffer frame = buf.readBytes(index - buf.readerIndex()); + buf.skipBytes(2); + return frame; + } + + return null; + } + +} diff --git a/src/org/traccar/protocol/GranitProtocol.java b/src/org/traccar/protocol/GranitProtocol.java new file mode 100644 index 000000000..fb9d10809 --- /dev/null +++ b/src/org/traccar/protocol/GranitProtocol.java @@ -0,0 +1,51 @@ +/* + * Copyright 2016 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.bootstrap.ServerBootstrap; +import org.jboss.netty.channel.ChannelPipeline; +import org.traccar.BaseProtocol; +import org.traccar.TrackerServer; +import org.traccar.model.Command; + +import java.nio.ByteOrder; +import java.util.List; + +public class GranitProtocol extends BaseProtocol { + + public GranitProtocol() { + super("granit"); + setSupportedCommands( + Command.TYPE_IDENTIFICATION, + Command.TYPE_REBOOT_DEVICE, + Command.TYPE_POSITION_SINGLE); + } + + @Override + public void initTrackerServers(List serverList) { + TrackerServer server = new TrackerServer(new ServerBootstrap(), this.getName()) { + @Override + protected void addSpecificHandlers(ChannelPipeline pipeline) { + pipeline.addLast("frameDecoder", new GranitFrameDecoder()); + pipeline.addLast("objectEncoder", new GranitProtocolEncoder()); + pipeline.addLast("objectDecoder", new GranitProtocolDecoder(GranitProtocol.this)); + } + }; + server.setEndianness(ByteOrder.LITTLE_ENDIAN); + serverList.add(server); + } + +} diff --git a/src/org/traccar/protocol/GranitProtocolDecoder.java b/src/org/traccar/protocol/GranitProtocolDecoder.java new file mode 100644 index 000000000..16229dc18 --- /dev/null +++ b/src/org/traccar/protocol/GranitProtocolDecoder.java @@ -0,0 +1,215 @@ +/* + * Copyright 2016 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.buffer.ChannelBuffers; +import org.jboss.netty.channel.Channel; +import org.traccar.BaseProtocolDecoder; +import org.traccar.helper.BitUtil; +import org.traccar.helper.Checksum; +import org.traccar.model.Position; + +import java.net.SocketAddress; +import java.nio.ByteOrder; +import java.nio.charset.StandardCharsets; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; + +public class GranitProtocolDecoder extends BaseProtocolDecoder { + + private static final int HEADER_LENGTH = 6; + + public GranitProtocolDecoder(GranitProtocol protocol) { + super(protocol); + } + + public static void appendChecksum(ChannelBuffer buffer, int length) { + buffer.writeByte(0x2a); // asterisk + int checksum = Checksum.xor(buffer.toByteBuffer(0, length)) & 0xFF; + String checksumString = String.format("%02X", checksum); + buffer.writeBytes(checksumString.getBytes(StandardCharsets.US_ASCII)); + buffer.writeByte(0x0D); buffer.writeByte(0x0A); + } + + private static void sendResponseCurrent(Channel channel, int deviceId, long time) { + ChannelBuffer response = ChannelBuffers.directBuffer(ByteOrder.LITTLE_ENDIAN, 21); + response.writeBytes("BB+UGRC~".getBytes(StandardCharsets.US_ASCII)); + response.writeShort(6); //binary length + response.writeInt((int) time); + response.writeShort(deviceId); + appendChecksum(response, 16); + channel.write(response); + } + + private static void sendResponseArchive(Channel channel, int deviceId, int packNum) { + ChannelBuffer response = ChannelBuffers.directBuffer(ByteOrder.LITTLE_ENDIAN, 19); + response.writeBytes("BB+ARCF~".getBytes(StandardCharsets.US_ASCII)); + response.writeShort(4); //binary length + response.writeShort(packNum); + response.writeShort(deviceId); + appendChecksum(response, 14); + channel.write(response); + } + + private static void decodeStructure(ChannelBuffer buf, Position position) { + short flags = buf.readUnsignedByte(); + position.setValid(BitUtil.check(flags, 7)); + position.set(Position.KEY_ALARM, BitUtil.check(flags, 1)); + + short satDel = buf.readUnsignedByte(); + position.set(Position.KEY_SATELLITES, BitUtil.from(satDel, 4)); + + int pdop = BitUtil.to(satDel, 4); + position.set("pdop", pdop); + + int lonDegrees = buf.readUnsignedByte(); + int latDegrees = buf.readUnsignedByte(); + int lonMinutes = buf.readUnsignedShort(); + int latMinutes = buf.readUnsignedShort(); + double latitude = latDegrees + (double) latMinutes / 60000; + double longitude = lonDegrees + (double) lonMinutes / 60000; + if (!BitUtil.check(flags, 4)) { + latitude = -latDegrees; + } + if (!BitUtil.check(flags, 5)) { + longitude = -longitude; + } + position.setLongitude(longitude); + position.setLatitude(latitude); + + position.setSpeed(buf.readUnsignedByte()); + + int course = buf.readUnsignedByte(); + if (BitUtil.check(flags, 6)) { + course = course | 0x100; + } + position.setCourse(course); + + position.set(Position.KEY_DISTANCE, buf.readShort()); + + int analogIn1 = buf.readUnsignedByte(); + int analogIn2 = buf.readUnsignedByte(); + int analogIn3 = buf.readUnsignedByte(); + int analogIn4 = buf.readUnsignedByte(); + + int analogInHi = buf.readUnsignedByte(); + + analogIn1 = analogInHi << 8 & 0x300 | analogIn1; + analogIn2 = analogInHi << 6 & 0x300 | analogIn2; + analogIn3 = analogInHi << 4 & 0x300 | analogIn3; + analogIn4 = analogInHi << 2 & 0x300 | analogIn4; + + position.set(Position.PREFIX_ADC + 1, analogIn1); + position.set(Position.PREFIX_ADC + 2, analogIn2); + position.set(Position.PREFIX_ADC + 3, analogIn3); + position.set(Position.PREFIX_ADC + 4, analogIn4); + + position.setAltitude(buf.readUnsignedByte() * 10); + + short diOut = buf.readUnsignedByte(); + for (int i = 0; i < 8; i++) { + position.set(Position.PREFIX_IO + (i + 1), BitUtil.check(diOut, i)); + } + buf.skipBytes(1); //StatMess + } + + @Override + protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { + + ChannelBuffer buf = (ChannelBuffer) msg; + + if (hasDeviceId() && buf.toString(StandardCharsets.US_ASCII).contains("OK") + || buf.toString(StandardCharsets.US_ASCII).startsWith("ERROR") + || buf.toString(StandardCharsets.US_ASCII).startsWith("+PR") + || buf.toString(StandardCharsets.US_ASCII).startsWith("+IDNT") + || buf.toString(StandardCharsets.US_ASCII).startsWith("+BBMD")) { + Position position = new Position(); + position.setProtocol(getProtocolName()); + position.setDeviceId(getDeviceId()); + + position.setTime(new Date()); + getLastLocation(position, new Date()); + position.setValid(false); + position.set(Position.KEY_RESULT, buf.toString(StandardCharsets.US_ASCII)); + + return position; + } + + if (buf.readableBytes() < HEADER_LENGTH) { + return null; + } + + String header = buf.readBytes(HEADER_LENGTH).toString(StandardCharsets.US_ASCII); + + if (header.equals("+RRCB~")) { + buf.skipBytes(2); //binary length 26 + int deviceId = buf.readUnsignedShort(); + if (!identify(String.valueOf(deviceId), channel, remoteAddress)) { + return null; + } + long unixTime = buf.readUnsignedInt(); + if (channel != null) { + sendResponseCurrent(channel, deviceId, unixTime); + } + Position position = new Position(); + position.setProtocol(getProtocolName()); + position.setDeviceId(getDeviceId()); + + position.setTime(new Date(unixTime * 1000)); + + decodeStructure(buf, position); + return position; + + } else if (header.equals("+DDAT~")) { + buf.skipBytes(2); //binary length + int deviceId = buf.readUnsignedShort(); + if (!identify(String.valueOf(deviceId), channel, remoteAddress)) { + return null; + } + byte format = buf.readByte(); + if (format != 4) { + return null; + } + byte nblocks = buf.readByte(); + int packNum = buf.readUnsignedShort(); + if (channel != null) { + sendResponseArchive(channel, deviceId, packNum); + } + List positions = new ArrayList<>(); + while (nblocks > 0) { + nblocks--; + long unixTime = buf.readUnsignedInt(); + for (int i = 0; i < 6; i++) { + if (buf.getUnsignedByte(buf.readerIndex()) != 0xFE) { + Position position = new Position(); + position.setProtocol(getProtocolName()); + position.setDeviceId(getDeviceId()); + position.setTime(new Date(unixTime * 1000)); + decodeStructure(buf, position); + positions.add(position); + } else { + buf.skipBytes(20); // skip filled 0xFE structure + } + } + buf.skipBytes(2); // increment + } + return positions; + } + return null; + } +} diff --git a/src/org/traccar/protocol/GranitProtocolEncoder.java b/src/org/traccar/protocol/GranitProtocolEncoder.java new file mode 100644 index 000000000..2f9e0aa10 --- /dev/null +++ b/src/org/traccar/protocol/GranitProtocolEncoder.java @@ -0,0 +1,57 @@ +/* + * Copyright 2016 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 java.nio.ByteOrder; +import java.nio.charset.StandardCharsets; + +import org.jboss.netty.buffer.ChannelBuffer; +import org.jboss.netty.buffer.ChannelBuffers; +import org.traccar.BaseProtocolEncoder; +import org.traccar.helper.Log; +import org.traccar.model.Command; + +public class GranitProtocolEncoder extends BaseProtocolEncoder { + @Override + protected Object encodeCommand(Command command) { + + ChannelBuffer commandString; + + switch (command.getType()) { + case Command.TYPE_IDENTIFICATION: + commandString = ChannelBuffers.directBuffer(ByteOrder.LITTLE_ENDIAN, 12); + commandString.writeBytes("BB+IDNT".getBytes(StandardCharsets.US_ASCII)); + GranitProtocolDecoder.appendChecksum(commandString, 7); + return commandString; + case Command.TYPE_REBOOT_DEVICE: + commandString = ChannelBuffers.directBuffer(ByteOrder.LITTLE_ENDIAN, 13); + commandString.writeBytes("BB+RESET".getBytes(StandardCharsets.US_ASCII)); + GranitProtocolDecoder.appendChecksum(commandString, 8); + return commandString; + case Command.TYPE_POSITION_SINGLE: + commandString = ChannelBuffers.directBuffer(ByteOrder.LITTLE_ENDIAN, 12); + commandString.writeBytes("BB+RRCD".getBytes(StandardCharsets.US_ASCII)); + GranitProtocolDecoder.appendChecksum(commandString, 7); + return commandString; + default: + Log.warning(new UnsupportedOperationException(command.getType())); + break; + } + + return null; + } + +} diff --git a/test/org/traccar/protocol/GranitProtocolDecoderTest.java b/test/org/traccar/protocol/GranitProtocolDecoderTest.java new file mode 100644 index 000000000..9b1c188c1 --- /dev/null +++ b/test/org/traccar/protocol/GranitProtocolDecoderTest.java @@ -0,0 +1,33 @@ +package org.traccar.protocol; + +import java.nio.ByteOrder; + +import org.junit.Test; +import org.traccar.ProtocolTest; + +public class GranitProtocolDecoderTest extends ProtocolTest { + + @Test + public void testDecode() throws Exception { + + GranitProtocolDecoder decoder = new GranitProtocolDecoder(new GranitProtocol()); + + verifyPosition(decoder, binary(ByteOrder.LITTLE_ENDIAN, + "2b525243427e1a003e2934757c57b8b03c38d279b4e61e9bd7006b000000001c00002a45330d0a")); + + verifyPositions(decoder, binary(ByteOrder.LITTLE_ENDIAN, + "2b444441547e84003e290401d01690737c57b8903c383c7fa0e5081b64006b000000001c0000b8803c388e7fe7e5102197006c000000001c0000b8813c38ad7f02e6042035006c000000001d0000b8813c38bf7f13e6001d1e006c000000001d0000b8813c38bf7f13e6001d00006c000000001d0000b8903c38977f34e6091065006c000000001e000014002a39320d0a")); + + verifyPositions(decoder, binary(ByteOrder.LITTLE_ENDIAN, + "2b444441547e84003e290401d41680747c57f8a03c38987f50e6005300006c000000001c0000f8b03c38987f50e6005300006c000000001c0000fefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefe14002a43460d0a")); + + //+IDNT: Navigator.04x Firmware version 0712GLN *21 + verifyAttributes(decoder, binary(ByteOrder.LITTLE_ENDIAN, + "2b49444e543a204e6176696761746f722e30347820204669726d776172652076657273696f6e202030373132474c4e202a32310d0a")); + + //ERROR WRONG CHECKSUM_1 + verifyAttributes(decoder, binary(ByteOrder.LITTLE_ENDIAN, + "4552524f522057524f4e4720434845434b53554d5f310d0a")); + } + +} -- cgit v1.2.3 From 49f00254695ae56aa03c0174f5aadfe1a0e4c836 Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Wed, 6 Jul 2016 14:54:24 +0500 Subject: Style fixes and optimizations --- src/org/traccar/protocol/GranitFrameDecoder.java | 40 ---------------------- src/org/traccar/protocol/GranitProtocol.java | 3 +- .../traccar/protocol/GranitProtocolDecoder.java | 23 ++++++------- .../protocol/GranitProtocolDecoderTest.java | 4 +-- 4 files changed, 14 insertions(+), 56 deletions(-) delete mode 100644 src/org/traccar/protocol/GranitFrameDecoder.java diff --git a/src/org/traccar/protocol/GranitFrameDecoder.java b/src/org/traccar/protocol/GranitFrameDecoder.java deleted file mode 100644 index ad6ddc99d..000000000 --- a/src/org/traccar/protocol/GranitFrameDecoder.java +++ /dev/null @@ -1,40 +0,0 @@ -/* - * Copyright 2016 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.StringFinder; - -public class GranitFrameDecoder extends FrameDecoder { - - @Override - protected Object decode( - ChannelHandlerContext ctx, Channel channel, ChannelBuffer buf) throws Exception { - - int index = buf.indexOf(buf.readerIndex(), buf.writerIndex(), new StringFinder("\r\n")); - if (index != -1) { - ChannelBuffer frame = buf.readBytes(index - buf.readerIndex()); - buf.skipBytes(2); - return frame; - } - - return null; - } - -} diff --git a/src/org/traccar/protocol/GranitProtocol.java b/src/org/traccar/protocol/GranitProtocol.java index fb9d10809..07357b786 100644 --- a/src/org/traccar/protocol/GranitProtocol.java +++ b/src/org/traccar/protocol/GranitProtocol.java @@ -17,6 +17,7 @@ package org.traccar.protocol; import org.jboss.netty.bootstrap.ServerBootstrap; import org.jboss.netty.channel.ChannelPipeline; +import org.jboss.netty.handler.codec.frame.LineBasedFrameDecoder; import org.traccar.BaseProtocol; import org.traccar.TrackerServer; import org.traccar.model.Command; @@ -39,7 +40,7 @@ public class GranitProtocol extends BaseProtocol { TrackerServer server = new TrackerServer(new ServerBootstrap(), this.getName()) { @Override protected void addSpecificHandlers(ChannelPipeline pipeline) { - pipeline.addLast("frameDecoder", new GranitFrameDecoder()); + pipeline.addLast("frameDecoder", new LineBasedFrameDecoder(1024, true, false)); pipeline.addLast("objectEncoder", new GranitProtocolEncoder()); pipeline.addLast("objectDecoder", new GranitProtocolDecoder(GranitProtocol.this)); } diff --git a/src/org/traccar/protocol/GranitProtocolDecoder.java b/src/org/traccar/protocol/GranitProtocolDecoder.java index 16229dc18..6f334d3c5 100644 --- a/src/org/traccar/protocol/GranitProtocolDecoder.java +++ b/src/org/traccar/protocol/GranitProtocolDecoder.java @@ -39,11 +39,11 @@ public class GranitProtocolDecoder extends BaseProtocolDecoder { } public static void appendChecksum(ChannelBuffer buffer, int length) { - buffer.writeByte(0x2a); // asterisk + buffer.writeByte('*'); int checksum = Checksum.xor(buffer.toByteBuffer(0, length)) & 0xFF; String checksumString = String.format("%02X", checksum); buffer.writeBytes(checksumString.getBytes(StandardCharsets.US_ASCII)); - buffer.writeByte(0x0D); buffer.writeByte(0x0A); + buffer.writeByte('\r'); buffer.writeByte('\n'); } private static void sendResponseCurrent(Channel channel, int deviceId, long time) { @@ -132,12 +132,10 @@ public class GranitProtocolDecoder extends BaseProtocolDecoder { protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { ChannelBuffer buf = (ChannelBuffer) msg; - - if (hasDeviceId() && buf.toString(StandardCharsets.US_ASCII).contains("OK") - || buf.toString(StandardCharsets.US_ASCII).startsWith("ERROR") - || buf.toString(StandardCharsets.US_ASCII).startsWith("+PR") - || buf.toString(StandardCharsets.US_ASCII).startsWith("+IDNT") - || buf.toString(StandardCharsets.US_ASCII).startsWith("+BBMD")) { + String bufString = buf.toString(StandardCharsets.US_ASCII); + if (hasDeviceId() && bufString.contains("OK") || bufString.startsWith("ERROR") + || bufString.startsWith("+PR") || bufString.startsWith("+IDNT") + || bufString.startsWith("+BBMD")) { Position position = new Position(); position.setProtocol(getProtocolName()); position.setDeviceId(getDeviceId()); @@ -145,7 +143,7 @@ public class GranitProtocolDecoder extends BaseProtocolDecoder { position.setTime(new Date()); getLastLocation(position, new Date()); position.setValid(false); - position.set(Position.KEY_RESULT, buf.toString(StandardCharsets.US_ASCII)); + position.set(Position.KEY_RESULT, bufString); return position; } @@ -153,10 +151,9 @@ public class GranitProtocolDecoder extends BaseProtocolDecoder { if (buf.readableBytes() < HEADER_LENGTH) { return null; } + buf.skipBytes(HEADER_LENGTH); - String header = buf.readBytes(HEADER_LENGTH).toString(StandardCharsets.US_ASCII); - - if (header.equals("+RRCB~")) { + if (bufString.startsWith("+RRCB~")) { buf.skipBytes(2); //binary length 26 int deviceId = buf.readUnsignedShort(); if (!identify(String.valueOf(deviceId), channel, remoteAddress)) { @@ -175,7 +172,7 @@ public class GranitProtocolDecoder extends BaseProtocolDecoder { decodeStructure(buf, position); return position; - } else if (header.equals("+DDAT~")) { + } else if (bufString.startsWith("+DDAT~")) { buf.skipBytes(2); //binary length int deviceId = buf.readUnsignedShort(); if (!identify(String.valueOf(deviceId), channel, remoteAddress)) { diff --git a/test/org/traccar/protocol/GranitProtocolDecoderTest.java b/test/org/traccar/protocol/GranitProtocolDecoderTest.java index 9b1c188c1..9f1f07bf7 100644 --- a/test/org/traccar/protocol/GranitProtocolDecoderTest.java +++ b/test/org/traccar/protocol/GranitProtocolDecoderTest.java @@ -16,10 +16,10 @@ public class GranitProtocolDecoderTest extends ProtocolTest { "2b525243427e1a003e2934757c57b8b03c38d279b4e61e9bd7006b000000001c00002a45330d0a")); verifyPositions(decoder, binary(ByteOrder.LITTLE_ENDIAN, - "2b444441547e84003e290401d01690737c57b8903c383c7fa0e5081b64006b000000001c0000b8803c388e7fe7e5102197006c000000001c0000b8813c38ad7f02e6042035006c000000001d0000b8813c38bf7f13e6001d1e006c000000001d0000b8813c38bf7f13e6001d00006c000000001d0000b8903c38977f34e6091065006c000000001e000014002a39320d0a")); + "2b444441547e84003e290401d01690737c57b8903c383c7fa0e5081b64006b000000001c0000b8803c388e7fe7e5102197006c000000001c0000b8813c38ad7f02e6042035006c000000001d0000b8813c38bf7f13e6001d1e006c000000001d0000b8813c38bf7f13e6001d00006c000000001d0000b8903c38977f34e6091065006c000000001e000014002a39320d0a")); verifyPositions(decoder, binary(ByteOrder.LITTLE_ENDIAN, - "2b444441547e84003e290401d41680747c57f8a03c38987f50e6005300006c000000001c0000f8b03c38987f50e6005300006c000000001c0000fefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefe14002a43460d0a")); + "2b444441547e84003e290401d41680747c57f8a03c38987f50e6005300006c000000001c0000f8b03c38987f50e6005300006c000000001c0000fefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefe14002a43460d0a")); //+IDNT: Navigator.04x Firmware version 0712GLN *21 verifyAttributes(decoder, binary(ByteOrder.LITTLE_ENDIAN, -- cgit v1.2.3 From 801f9e25448be8a3b27c3b56798525b2fff27633 Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Wed, 6 Jul 2016 15:43:37 +0500 Subject: Use dynamicBuffer and some hardcoded lengths. --- .../traccar/protocol/GranitProtocolDecoder.java | 4 +-- .../traccar/protocol/GranitProtocolEncoder.java | 30 ++++++++++------------ 2 files changed, 16 insertions(+), 18 deletions(-) diff --git a/src/org/traccar/protocol/GranitProtocolDecoder.java b/src/org/traccar/protocol/GranitProtocolDecoder.java index 6f334d3c5..c919b3d65 100644 --- a/src/org/traccar/protocol/GranitProtocolDecoder.java +++ b/src/org/traccar/protocol/GranitProtocolDecoder.java @@ -47,7 +47,7 @@ public class GranitProtocolDecoder extends BaseProtocolDecoder { } private static void sendResponseCurrent(Channel channel, int deviceId, long time) { - ChannelBuffer response = ChannelBuffers.directBuffer(ByteOrder.LITTLE_ENDIAN, 21); + ChannelBuffer response = ChannelBuffers.dynamicBuffer(ByteOrder.LITTLE_ENDIAN, 0); response.writeBytes("BB+UGRC~".getBytes(StandardCharsets.US_ASCII)); response.writeShort(6); //binary length response.writeInt((int) time); @@ -57,7 +57,7 @@ public class GranitProtocolDecoder extends BaseProtocolDecoder { } private static void sendResponseArchive(Channel channel, int deviceId, int packNum) { - ChannelBuffer response = ChannelBuffers.directBuffer(ByteOrder.LITTLE_ENDIAN, 19); + ChannelBuffer response = ChannelBuffers.dynamicBuffer(ByteOrder.LITTLE_ENDIAN, 0); response.writeBytes("BB+ARCF~".getBytes(StandardCharsets.US_ASCII)); response.writeShort(4); //binary length response.writeShort(packNum); diff --git a/src/org/traccar/protocol/GranitProtocolEncoder.java b/src/org/traccar/protocol/GranitProtocolEncoder.java index 2f9e0aa10..961f183f5 100644 --- a/src/org/traccar/protocol/GranitProtocolEncoder.java +++ b/src/org/traccar/protocol/GranitProtocolEncoder.java @@ -15,7 +15,6 @@ */ package org.traccar.protocol; -import java.nio.ByteOrder; import java.nio.charset.StandardCharsets; import org.jboss.netty.buffer.ChannelBuffer; @@ -28,29 +27,28 @@ public class GranitProtocolEncoder extends BaseProtocolEncoder { @Override protected Object encodeCommand(Command command) { - ChannelBuffer commandString; + String commandString = ""; switch (command.getType()) { case Command.TYPE_IDENTIFICATION: - commandString = ChannelBuffers.directBuffer(ByteOrder.LITTLE_ENDIAN, 12); - commandString.writeBytes("BB+IDNT".getBytes(StandardCharsets.US_ASCII)); - GranitProtocolDecoder.appendChecksum(commandString, 7); - return commandString; + commandString = "BB+IDNT"; + break; case Command.TYPE_REBOOT_DEVICE: - commandString = ChannelBuffers.directBuffer(ByteOrder.LITTLE_ENDIAN, 13); - commandString.writeBytes("BB+RESET".getBytes(StandardCharsets.US_ASCII)); - GranitProtocolDecoder.appendChecksum(commandString, 8); - return commandString; + commandString = "BB+RESET"; + break; case Command.TYPE_POSITION_SINGLE: - commandString = ChannelBuffers.directBuffer(ByteOrder.LITTLE_ENDIAN, 12); - commandString.writeBytes("BB+RRCD".getBytes(StandardCharsets.US_ASCII)); - GranitProtocolDecoder.appendChecksum(commandString, 7); - return commandString; + commandString = "BB+RRCD"; + break; default: Log.warning(new UnsupportedOperationException(command.getType())); - break; + return null; + } + if (!commandString.isEmpty()) { + ChannelBuffer commandBuf = ChannelBuffers.dynamicBuffer(); + commandBuf.writeBytes(commandString.getBytes(StandardCharsets.US_ASCII)); + GranitProtocolDecoder.appendChecksum(commandBuf, commandString.length()); + return commandBuf; } - return null; } -- cgit v1.2.3 From f8f21ce131556ca5c1ea44ede496ace9d810c844 Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Wed, 6 Jul 2016 16:52:23 +0500 Subject: More optimizations --- src/org/traccar/protocol/GranitProtocol.java | 2 +- src/org/traccar/protocol/GranitProtocolDecoder.java | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/org/traccar/protocol/GranitProtocol.java b/src/org/traccar/protocol/GranitProtocol.java index 07357b786..a33c43d20 100644 --- a/src/org/traccar/protocol/GranitProtocol.java +++ b/src/org/traccar/protocol/GranitProtocol.java @@ -40,7 +40,7 @@ public class GranitProtocol extends BaseProtocol { TrackerServer server = new TrackerServer(new ServerBootstrap(), this.getName()) { @Override protected void addSpecificHandlers(ChannelPipeline pipeline) { - pipeline.addLast("frameDecoder", new LineBasedFrameDecoder(1024, true, false)); + pipeline.addLast("frameDecoder", new LineBasedFrameDecoder(1024)); pipeline.addLast("objectEncoder", new GranitProtocolEncoder()); pipeline.addLast("objectDecoder", new GranitProtocolDecoder(GranitProtocol.this)); } diff --git a/src/org/traccar/protocol/GranitProtocolDecoder.java b/src/org/traccar/protocol/GranitProtocolDecoder.java index c919b3d65..b92bc72e2 100644 --- a/src/org/traccar/protocol/GranitProtocolDecoder.java +++ b/src/org/traccar/protocol/GranitProtocolDecoder.java @@ -81,8 +81,8 @@ public class GranitProtocolDecoder extends BaseProtocolDecoder { int latDegrees = buf.readUnsignedByte(); int lonMinutes = buf.readUnsignedShort(); int latMinutes = buf.readUnsignedShort(); - double latitude = latDegrees + (double) latMinutes / 60000; - double longitude = lonDegrees + (double) lonMinutes / 60000; + double latitude = latDegrees + latMinutes / 60000.0; + double longitude = lonDegrees + lonMinutes / 60000.0; if (!BitUtil.check(flags, 4)) { latitude = -latDegrees; } -- cgit v1.2.3