aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorseym45 <seym45@gmail.com>2023-07-27 02:19:03 +0400
committerseym45 <seym45@gmail.com>2023-07-27 02:55:53 +0400
commitfb4db390187fbfcf1220dc05fa03565799a829e8 (patch)
tree651a3d4a1e5a0687fa19e6153e6660d634747b59
parent18c1e761fd203c994445ac85e808ca6f672d0ec5 (diff)
downloadtrackermap-server-fb4db390187fbfcf1220dc05fa03565799a829e8.tar.gz
trackermap-server-fb4db390187fbfcf1220dc05fa03565799a829e8.tar.bz2
trackermap-server-fb4db390187fbfcf1220dc05fa03565799a829e8.zip
Simplify encodeId method for Gator Protocol
-rw-r--r--src/main/java/org/traccar/protocol/GatorProtocolEncoder.java28
-rw-r--r--src/test/java/org/traccar/protocol/GatorProtocolEncoderTest.java12
2 files changed, 19 insertions, 21 deletions
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<String> 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
diff --git a/src/test/java/org/traccar/protocol/GatorProtocolEncoderTest.java b/src/test/java/org/traccar/protocol/GatorProtocolEncoderTest.java
index babd7285d..3f8fe2451 100644
--- a/src/test/java/org/traccar/protocol/GatorProtocolEncoderTest.java
+++ b/src/test/java/org/traccar/protocol/GatorProtocolEncoderTest.java
@@ -5,27 +5,35 @@ import io.netty.buffer.Unpooled;
import org.junit.jupiter.api.Test;
import org.traccar.ProtocolTest;
import org.traccar.model.Command;
+import org.traccar.model.Device;
import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.mockito.Mockito.when;
public class GatorProtocolEncoderTest extends ProtocolTest {
@Test
void testEncodeId() throws Exception {
var encoder = inject(new GatorProtocolEncoder(null));
+ var device = encoder.getCacheManager().getObject(Device.class, 1);
+ when(device.getUniqueId()).thenReturn("13332082112");
+
ByteBuf pseudoId = Unpooled.buffer();
pseudoId.writeByte(0x20);
pseudoId.writeByte(0x08);
pseudoId.writeByte(0x95);
pseudoId.writeByte(0x8C);
- assertEquals(pseudoId, encoder.encodeId(13332082112L));
+ assertEquals(pseudoId, encoder.encodeId(1));
}
@Test
public void testEncode() throws Exception {
var encoder = inject(new GatorProtocolEncoder(null));
+ var device = encoder.getCacheManager().getObject(Device.class, 1);
+ when(device.getUniqueId()).thenReturn("13332082112");
+
Command command = new Command();
- command.setDeviceId(13332082112L);
+ command.setDeviceId(1);
command.setType(Command.TYPE_POSITION_SINGLE);
verifyCommand(encoder, command, binary("24243000062008958C070D"));
}