From fb4db390187fbfcf1220dc05fa03565799a829e8 Mon Sep 17 00:00:00 2001 From: seym45 Date: Thu, 27 Jul 2023 02:19:03 +0400 Subject: Simplify encodeId method for Gator Protocol --- .../org/traccar/protocol/GatorProtocolEncoder.java | 28 +++++++--------------- 1 file changed, 9 insertions(+), 19 deletions(-) (limited to 'src/main') diff --git a/src/main/java/org/traccar/protocol/GatorProtocolEncoder.java b/src/main/java/org/traccar/protocol/GatorProtocolEncoder.java index f2c522067..3d38b7455 100644 --- a/src/main/java/org/traccar/protocol/GatorProtocolEncoder.java +++ b/src/main/java/org/traccar/protocol/GatorProtocolEncoder.java @@ -22,43 +22,33 @@ import org.traccar.Protocol; import org.traccar.helper.Checksum; import org.traccar.model.Command; -import java.util.ArrayList; -import java.util.List; - public class GatorProtocolEncoder extends BaseProtocolEncoder { public GatorProtocolEncoder(Protocol protocol) { super(protocol); } - public static ByteBuf encodeId(long deviceId) { + public ByteBuf encodeId(long deviceId) { ByteBuf buf = Unpooled.buffer(); - String deviceIdStrVal = String.valueOf(deviceId); - List partialDigits = new ArrayList<>(); - for (int i = 1; i < deviceIdStrVal.length(); i += 2) { - partialDigits.add(deviceIdStrVal.substring(i, i + 2)); - } - - int firstDigit = Integer.parseInt(partialDigits.get(0)) - 30; + String id = getUniqueId(deviceId); - for (int i = 1; i < partialDigits.size(); i++) { - int shiftCount = 4 - i; - int addend = ((firstDigit & (1 << shiftCount)) >> shiftCount) << 7; - int sum = Integer.parseInt(partialDigits.get(i)) | addend; + int firstDigit = Integer.parseInt(id.substring(1, 3)) - 30; - buf.writeByte(sum); - } + buf.writeByte(Integer.parseInt(id.substring(3, 5)) | (((firstDigit >> 3) & 1) << 7)); + buf.writeByte(Integer.parseInt(id.substring(5, 7)) | (((firstDigit >> 2) & 1) << 7)); + buf.writeByte(Integer.parseInt(id.substring(7, 9)) | (((firstDigit >> 1) & 1) << 7)); + buf.writeByte(Integer.parseInt(id.substring(9)) | ((firstDigit & 1) << 7)); return buf; } - private ByteBuf encodeContent(long deviceId, int mainOrder) { + private ByteBuf encodeContent(long deviceId, int type) { ByteBuf buf = Unpooled.buffer(); buf.writeByte(0x24); buf.writeByte(0x24); - buf.writeByte(mainOrder); + buf.writeByte(type); buf.writeByte(0x00); buf.writeByte(4 + 1 + 1); // ip 4 bytes, checksum and end byte -- cgit v1.2.3