From 1ee9e9396d7274f057f5e6d2d69e72ec1a5c0135 Mon Sep 17 00:00:00 2001 From: seym45 Date: Mon, 24 Jul 2023 20:15:47 +0400 Subject: Add method to encodeId --- .../traccar/protocol/GatorProtocolEncoderTest.java | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 src/test/java/org/traccar/protocol/GatorProtocolEncoderTest.java (limited to 'src/test/java/org/traccar') diff --git a/src/test/java/org/traccar/protocol/GatorProtocolEncoderTest.java b/src/test/java/org/traccar/protocol/GatorProtocolEncoderTest.java new file mode 100644 index 000000000..7e8f51008 --- /dev/null +++ b/src/test/java/org/traccar/protocol/GatorProtocolEncoderTest.java @@ -0,0 +1,19 @@ +package org.traccar.protocol; + +import org.junit.jupiter.api.Test; +import org.traccar.ProtocolTest; +import org.traccar.model.Command; + +import java.nio.charset.StandardCharsets; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class GatorProtocolEncoderTest extends ProtocolTest { + + @Test + void encodeId() throws Exception { + var encoder = inject(new GatorProtocolEncoder(null)); + assertEquals("2008958C", encoder.encodeId(13332082112L)); + + } +} -- cgit v1.2.3 From 103fbde721753b8be65c39e0e9118943c6bebc68 Mon Sep 17 00:00:00 2001 From: seym45 Date: Mon, 24 Jul 2023 20:34:00 +0400 Subject: Add Gator content encoder and test snippets --- src/main/java/org/traccar/protocol/GatorProtocolEncoder.java | 10 ++++++++-- .../java/org/traccar/protocol/GatorProtocolEncoderTest.java | 12 +++++++++--- 2 files changed, 17 insertions(+), 5 deletions(-) (limited to 'src/test/java/org/traccar') diff --git a/src/main/java/org/traccar/protocol/GatorProtocolEncoder.java b/src/main/java/org/traccar/protocol/GatorProtocolEncoder.java index 0d4353c8f..125031eee 100644 --- a/src/main/java/org/traccar/protocol/GatorProtocolEncoder.java +++ b/src/main/java/org/traccar/protocol/GatorProtocolEncoder.java @@ -75,8 +75,14 @@ public class GatorProtocolEncoder extends BaseProtocolEncoder { } private ByteBuf encodeContent(long deviceId, String mainOrder, String content) { - // FIXME: implement this method - return null; + String pseudoIPAddress = encodeId(deviceId); + int length = 4 + 1 + 1; // ip 4 bytes, calibration byte and end byte + String hexStringLength = String.format("%02X", length); + + String packet = "2424" + mainOrder + "00" + hexStringLength + pseudoIPAddress; + String calibration = getCalibrationByteFromHexString(packet); + packet = packet + calibration + "0D"; + return Unpooled.wrappedBuffer(DataConverter.parseHex(packet)); } diff --git a/src/test/java/org/traccar/protocol/GatorProtocolEncoderTest.java b/src/test/java/org/traccar/protocol/GatorProtocolEncoderTest.java index 7e8f51008..74ac35440 100644 --- a/src/test/java/org/traccar/protocol/GatorProtocolEncoderTest.java +++ b/src/test/java/org/traccar/protocol/GatorProtocolEncoderTest.java @@ -4,16 +4,22 @@ import org.junit.jupiter.api.Test; import org.traccar.ProtocolTest; import org.traccar.model.Command; -import java.nio.charset.StandardCharsets; - import static org.junit.jupiter.api.Assertions.assertEquals; public class GatorProtocolEncoderTest extends ProtocolTest { @Test - void encodeId() throws Exception { + void encodeId() throws Exception { var encoder = inject(new GatorProtocolEncoder(null)); assertEquals("2008958C", encoder.encodeId(13332082112L)); + } + @Test + public void testEncode() throws Exception { + var encoder = inject(new GatorProtocolEncoder(null)); + Command command = new Command(); + command.setDeviceId(13332082112L); + command.setType(Command.TYPE_ROLLCALL); + verifyCommand(encoder, command, binary("24243000062008958C070D")); } } -- cgit v1.2.3 From 8c227973608eee7ba5519f22c5781f912dd4e9b5 Mon Sep 17 00:00:00 2001 From: seym45 Date: Tue, 25 Jul 2023 10:56:55 +0400 Subject: Change command type --- src/main/java/org/traccar/model/Command.java | 1 - src/main/java/org/traccar/protocol/GatorProtocol.java | 2 +- src/main/java/org/traccar/protocol/GatorProtocolEncoder.java | 4 ++-- src/test/java/org/traccar/protocol/GatorProtocolEncoderTest.java | 2 +- 4 files changed, 4 insertions(+), 5 deletions(-) (limited to 'src/test/java/org/traccar') diff --git a/src/main/java/org/traccar/model/Command.java b/src/main/java/org/traccar/model/Command.java index e0c1c3976..99988dd82 100644 --- a/src/main/java/org/traccar/model/Command.java +++ b/src/main/java/org/traccar/model/Command.java @@ -45,7 +45,6 @@ public class Command extends BaseCommand { public static final String TYPE_SILENCE_TIME = "silenceTime"; public static final String TYPE_SET_PHONEBOOK = "setPhonebook"; public static final String TYPE_MESSAGE = "message"; - public static final String TYPE_ROLLCALL = "rollcall"; public static final String TYPE_VOICE_MESSAGE = "voiceMessage"; public static final String TYPE_OUTPUT_CONTROL = "outputControl"; public static final String TYPE_VOICE_MONITORING = "voiceMonitoring"; diff --git a/src/main/java/org/traccar/protocol/GatorProtocol.java b/src/main/java/org/traccar/protocol/GatorProtocol.java index c54b1b53d..bb0e017f4 100644 --- a/src/main/java/org/traccar/protocol/GatorProtocol.java +++ b/src/main/java/org/traccar/protocol/GatorProtocol.java @@ -29,7 +29,7 @@ public class GatorProtocol extends BaseProtocol { @Inject public GatorProtocol(Config config) { setSupportedDataCommands( - Command.TYPE_ROLLCALL + Command.TYPE_POSITION_SINGLE ); addServer(new TrackerServer(config, getName(), false) { @Override diff --git a/src/main/java/org/traccar/protocol/GatorProtocolEncoder.java b/src/main/java/org/traccar/protocol/GatorProtocolEncoder.java index 125031eee..efe8715e3 100644 --- a/src/main/java/org/traccar/protocol/GatorProtocolEncoder.java +++ b/src/main/java/org/traccar/protocol/GatorProtocolEncoder.java @@ -90,11 +90,11 @@ public class GatorProtocolEncoder extends BaseProtocolEncoder { protected Object encodeCommand(Command command) { switch (command.getType()) { - case Command.TYPE_ROLLCALL: + // According to Protocol Documentation, 0x30 is for rollcall command + case Command.TYPE_POSITION_SINGLE: return encodeContent(command.getDeviceId(), "30", null); default: return null; } } - } diff --git a/src/test/java/org/traccar/protocol/GatorProtocolEncoderTest.java b/src/test/java/org/traccar/protocol/GatorProtocolEncoderTest.java index 74ac35440..04fce0ac8 100644 --- a/src/test/java/org/traccar/protocol/GatorProtocolEncoderTest.java +++ b/src/test/java/org/traccar/protocol/GatorProtocolEncoderTest.java @@ -19,7 +19,7 @@ public class GatorProtocolEncoderTest extends ProtocolTest { var encoder = inject(new GatorProtocolEncoder(null)); Command command = new Command(); command.setDeviceId(13332082112L); - command.setType(Command.TYPE_ROLLCALL); + command.setType(Command.TYPE_POSITION_SINGLE); verifyCommand(encoder, command, binary("24243000062008958C070D")); } } -- cgit v1.2.3 From 6ac15f2c6fb8798c6906c24e97b0048736270215 Mon Sep 17 00:00:00 2001 From: seym45 Date: Wed, 26 Jul 2023 03:02:34 +0400 Subject: Format code structure --- src/main/java/org/traccar/protocol/GatorProtocol.java | 4 +--- src/test/java/org/traccar/protocol/GatorProtocolEncoderTest.java | 2 +- 2 files changed, 2 insertions(+), 4 deletions(-) (limited to 'src/test/java/org/traccar') diff --git a/src/main/java/org/traccar/protocol/GatorProtocol.java b/src/main/java/org/traccar/protocol/GatorProtocol.java index bb0e017f4..46862f583 100644 --- a/src/main/java/org/traccar/protocol/GatorProtocol.java +++ b/src/main/java/org/traccar/protocol/GatorProtocol.java @@ -28,9 +28,7 @@ public class GatorProtocol extends BaseProtocol { @Inject public GatorProtocol(Config config) { - setSupportedDataCommands( - Command.TYPE_POSITION_SINGLE - ); + setSupportedDataCommands(Command.TYPE_POSITION_SINGLE); addServer(new TrackerServer(config, getName(), false) { @Override protected void addProtocolHandlers(PipelineBuilder pipeline, Config config) { diff --git a/src/test/java/org/traccar/protocol/GatorProtocolEncoderTest.java b/src/test/java/org/traccar/protocol/GatorProtocolEncoderTest.java index 04fce0ac8..a3bc3af2b 100644 --- a/src/test/java/org/traccar/protocol/GatorProtocolEncoderTest.java +++ b/src/test/java/org/traccar/protocol/GatorProtocolEncoderTest.java @@ -9,7 +9,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; public class GatorProtocolEncoderTest extends ProtocolTest { @Test - void encodeId() throws Exception { + void testEncodeId() throws Exception { var encoder = inject(new GatorProtocolEncoder(null)); assertEquals("2008958C", encoder.encodeId(13332082112L)); } -- cgit v1.2.3 From 2407799dc370d741431cbec056c0f2acbe454bf3 Mon Sep 17 00:00:00 2001 From: seym45 Date: Wed, 26 Jul 2023 04:11:40 +0400 Subject: Refactor method encodeId and encodeContent - Replace method getCalibrationByteFromHexString with xor checksum - Rewrite method encodeContent with ByteBuf instead of String - Refactor encodeId by replacing short variable names and use ByteBuf --- .../org/traccar/protocol/GatorProtocolEncoder.java | 83 ++++++++++------------ .../traccar/protocol/GatorProtocolEncoderTest.java | 9 ++- 2 files changed, 44 insertions(+), 48 deletions(-) (limited to 'src/test/java/org/traccar') diff --git a/src/main/java/org/traccar/protocol/GatorProtocolEncoder.java b/src/main/java/org/traccar/protocol/GatorProtocolEncoder.java index efe8715e3..296ff4f13 100644 --- a/src/main/java/org/traccar/protocol/GatorProtocolEncoder.java +++ b/src/main/java/org/traccar/protocol/GatorProtocolEncoder.java @@ -1,5 +1,5 @@ /* - * Copyright 2023 - Hossain Mohammad Seym + * Copyright 2023 Hossain Mohammad Seym (seym45@gmail.com) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -19,14 +19,11 @@ import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; import org.traccar.BaseProtocolEncoder; import org.traccar.Protocol; -import org.traccar.config.Keys; import org.traccar.helper.Checksum; -import org.traccar.helper.DataConverter; -import org.traccar.helper.model.AttributeUtil; import org.traccar.model.Command; -import org.traccar.model.Device; -import java.nio.charset.StandardCharsets; +import java.util.ArrayList; +import java.util.List; public class GatorProtocolEncoder extends BaseProtocolEncoder { @@ -34,65 +31,57 @@ public class GatorProtocolEncoder extends BaseProtocolEncoder { super(protocol); } + public static ByteBuf encodeId(long deviceId) { + ByteBuf buf = Unpooled.buffer(); - public static String encodeId(long deviceId) { - StringBuilder encodedId = new StringBuilder(); - - String imei = String.valueOf(deviceId); - String a = imei.substring(1, 3); - String b = imei.substring(3, 5); - String c = imei.substring(5, 7); - String d = imei.substring(7, 9); - String e = imei.substring(9); - String[] arr = {b, c, d, e}; + 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)); + } - String binaryFirstDigit = Integer.toBinaryString(Integer.valueOf(a) - 30); - binaryFirstDigit = String.format("%4s", binaryFirstDigit).replace(' ', '0'); + int firstDigit = Integer.parseInt(partialDigits.get(0)) - 30; - for (int i = 0; i < 4; i++) { - int sum = Integer.parseInt(arr[i]) + Integer.parseInt(String.valueOf(binaryFirstDigit.charAt(i)) + "0000000", 2); - arr[i] = Integer.toHexString(sum).toUpperCase(); - arr[i] = String.format("%2s", arr[i]).replace(' ', '0'); - } + 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; - for (String s : arr) { - encodedId.append(s); + buf.writeByte(sum); } - return encodedId.toString(); + return buf; } - private static String getCalibrationByteFromHexString(String data) { - String response = ""; - int calib = 0; - int length = data.length() / 2; - for (int i = 0; i < length; i++) { - calib = calib ^ Integer.parseInt(data.substring(i * 2, i * 2 + 2), 16); - } - response = Integer.toHexString(calib).toUpperCase(); - response = String.format("%2s", response).replace(' ', '0'); - return response; - } - private ByteBuf encodeContent(long deviceId, String mainOrder, String content) { - String pseudoIPAddress = encodeId(deviceId); + private ByteBuf encodeContent(long deviceId, int mainOrder) { + ByteBuf buf = Unpooled.buffer(); + + buf.writeByte(0x24); + buf.writeByte(0x24); + buf.writeByte(mainOrder); + buf.writeByte(0x00); + int length = 4 + 1 + 1; // ip 4 bytes, calibration byte and end byte - String hexStringLength = String.format("%02X", length); + buf.writeByte(length); - String packet = "2424" + mainOrder + "00" + hexStringLength + pseudoIPAddress; - String calibration = getCalibrationByteFromHexString(packet); - packet = packet + calibration + "0D"; - return Unpooled.wrappedBuffer(DataConverter.parseHex(packet)); - } + ByteBuf pseudoIPAddress = encodeId(deviceId); + buf.writeBytes(pseudoIPAddress); + + int checksum = Checksum.xor(buf.nioBuffer()); + buf.writeByte(checksum); + buf.writeByte(0x0D); + + return buf; + } @Override protected Object encodeCommand(Command command) { switch (command.getType()) { - // According to Protocol Documentation, 0x30 is for rollcall command case Command.TYPE_POSITION_SINGLE: - return encodeContent(command.getDeviceId(), "30", null); + return encodeContent(command.getDeviceId(), 0x30); default: return null; } diff --git a/src/test/java/org/traccar/protocol/GatorProtocolEncoderTest.java b/src/test/java/org/traccar/protocol/GatorProtocolEncoderTest.java index a3bc3af2b..babd7285d 100644 --- a/src/test/java/org/traccar/protocol/GatorProtocolEncoderTest.java +++ b/src/test/java/org/traccar/protocol/GatorProtocolEncoderTest.java @@ -1,5 +1,7 @@ package org.traccar.protocol; +import io.netty.buffer.ByteBuf; +import io.netty.buffer.Unpooled; import org.junit.jupiter.api.Test; import org.traccar.ProtocolTest; import org.traccar.model.Command; @@ -11,7 +13,12 @@ public class GatorProtocolEncoderTest extends ProtocolTest { @Test void testEncodeId() throws Exception { var encoder = inject(new GatorProtocolEncoder(null)); - assertEquals("2008958C", encoder.encodeId(13332082112L)); + ByteBuf pseudoId = Unpooled.buffer(); + pseudoId.writeByte(0x20); + pseudoId.writeByte(0x08); + pseudoId.writeByte(0x95); + pseudoId.writeByte(0x8C); + assertEquals(pseudoId, encoder.encodeId(13332082112L)); } @Test -- cgit v1.2.3 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 +++++++--------------- .../traccar/protocol/GatorProtocolEncoderTest.java | 12 ++++++++-- 2 files changed, 19 insertions(+), 21 deletions(-) (limited to 'src/test/java/org/traccar') 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 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")); } -- cgit v1.2.3 From 1dfc61e3e981a23060d02c46451b19a600a87fa0 Mon Sep 17 00:00:00 2001 From: seym45 Date: Thu, 27 Jul 2023 11:27:44 +0400 Subject: Remove explicit test encodeId - encodeId tested inside testEncode --- .../org/traccar/protocol/GatorProtocolEncoderTest.java | 17 ----------------- 1 file changed, 17 deletions(-) (limited to 'src/test/java/org/traccar') diff --git a/src/test/java/org/traccar/protocol/GatorProtocolEncoderTest.java b/src/test/java/org/traccar/protocol/GatorProtocolEncoderTest.java index 3f8fe2451..af6c71e37 100644 --- a/src/test/java/org/traccar/protocol/GatorProtocolEncoderTest.java +++ b/src/test/java/org/traccar/protocol/GatorProtocolEncoderTest.java @@ -1,31 +1,14 @@ package org.traccar.protocol; -import io.netty.buffer.ByteBuf; -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(1)); - } - @Test public void testEncode() throws Exception { var encoder = inject(new GatorProtocolEncoder(null)); -- cgit v1.2.3