From 6cb4db1f6bb4f93ad42b013a38ace9a6a3b735f6 Mon Sep 17 00:00:00 2001 From: seym45 Date: Mon, 24 Jul 2023 01:25:05 +0400 Subject: Define command 'rollback' --- src/main/java/org/traccar/model/Command.java | 1 + .../java/org/traccar/protocol/GatorProtocol.java | 5 +++ .../org/traccar/protocol/GatorProtocolEncoder.java | 52 ++++++++++++++++++++++ 3 files changed, 58 insertions(+) create mode 100644 src/main/java/org/traccar/protocol/GatorProtocolEncoder.java (limited to 'src/main/java/org') diff --git a/src/main/java/org/traccar/model/Command.java b/src/main/java/org/traccar/model/Command.java index 99988dd82..e0c1c3976 100644 --- a/src/main/java/org/traccar/model/Command.java +++ b/src/main/java/org/traccar/model/Command.java @@ -45,6 +45,7 @@ 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 7341b69a3..c54b1b53d 100644 --- a/src/main/java/org/traccar/protocol/GatorProtocol.java +++ b/src/main/java/org/traccar/protocol/GatorProtocol.java @@ -20,6 +20,7 @@ import org.traccar.BaseProtocol; import org.traccar.PipelineBuilder; import org.traccar.TrackerServer; import org.traccar.config.Config; +import org.traccar.model.Command; import javax.inject.Inject; @@ -27,10 +28,14 @@ public class GatorProtocol extends BaseProtocol { @Inject public GatorProtocol(Config config) { + setSupportedDataCommands( + Command.TYPE_ROLLCALL + ); addServer(new TrackerServer(config, getName(), false) { @Override protected void addProtocolHandlers(PipelineBuilder pipeline, Config config) { pipeline.addLast(new LengthFieldBasedFrameDecoder(1024, 3, 2)); + pipeline.addLast(new GatorProtocolEncoder(GatorProtocol.this)); pipeline.addLast(new GatorProtocolDecoder(GatorProtocol.this)); } }); diff --git a/src/main/java/org/traccar/protocol/GatorProtocolEncoder.java b/src/main/java/org/traccar/protocol/GatorProtocolEncoder.java new file mode 100644 index 000000000..452fde275 --- /dev/null +++ b/src/main/java/org/traccar/protocol/GatorProtocolEncoder.java @@ -0,0 +1,52 @@ +/* + * Copyright 2023 - Hossain Mohammad Seym + * + * 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 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.model.AttributeUtil; +import org.traccar.model.Command; +import org.traccar.model.Device; + +import java.nio.charset.StandardCharsets; + +public class GatorProtocolEncoder extends BaseProtocolEncoder { + + public GatorProtocolEncoder(Protocol protocol) { + super(protocol); + } + + private ByteBuf encodeContent(long deviceId, String content) { + // FIXME: implement this method + return null; + } + + @Override + protected Object encodeCommand(Command command) { + + switch (command.getType()) { + case Command.TYPE_ROLLCALL: + return encodeContent(command.getDeviceId(), "0x30"); + default: + return null; + } + } + +} -- cgit v1.2.3 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 --- .../org/traccar/protocol/GatorProtocolEncoder.java | 36 ++++++++++++++++++++-- .../traccar/protocol/GatorProtocolEncoderTest.java | 19 ++++++++++++ 2 files changed, 52 insertions(+), 3 deletions(-) create mode 100644 src/test/java/org/traccar/protocol/GatorProtocolEncoderTest.java (limited to 'src/main/java/org') diff --git a/src/main/java/org/traccar/protocol/GatorProtocolEncoder.java b/src/main/java/org/traccar/protocol/GatorProtocolEncoder.java index 452fde275..df85a712c 100644 --- a/src/main/java/org/traccar/protocol/GatorProtocolEncoder.java +++ b/src/main/java/org/traccar/protocol/GatorProtocolEncoder.java @@ -21,6 +21,7 @@ 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; @@ -33,9 +34,38 @@ public class GatorProtocolEncoder extends BaseProtocolEncoder { super(protocol); } - private ByteBuf encodeContent(long deviceId, String content) { + + 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 binaryFirstDigit = Integer.toBinaryString(Integer.valueOf(a) - 30); + binaryFirstDigit = String.format("%4s", binaryFirstDigit).replace(' ', '0'); + + 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 (String s : arr) { + encodedId.append(s); + } + + return encodedId.toString(); + } + + + private ByteBuf encodeContent(long deviceId, String mainOrder, String content) { // FIXME: implement this method - return null; + return null; } @Override @@ -43,7 +73,7 @@ public class GatorProtocolEncoder extends BaseProtocolEncoder { switch (command.getType()) { case Command.TYPE_ROLLCALL: - return encodeContent(command.getDeviceId(), "0x30"); + 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 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 09daf15be1a874a8880645d4595e1b57d4cf2eee Mon Sep 17 00:00:00 2001 From: seym45 Date: Mon, 24 Jul 2023 20:29:29 +0400 Subject: Add method to get calibration byte --- src/main/java/org/traccar/protocol/GatorProtocolEncoder.java | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'src/main/java/org') diff --git a/src/main/java/org/traccar/protocol/GatorProtocolEncoder.java b/src/main/java/org/traccar/protocol/GatorProtocolEncoder.java index df85a712c..0d4353c8f 100644 --- a/src/main/java/org/traccar/protocol/GatorProtocolEncoder.java +++ b/src/main/java/org/traccar/protocol/GatorProtocolEncoder.java @@ -62,12 +62,24 @@ public class GatorProtocolEncoder extends BaseProtocolEncoder { return encodedId.toString(); } + 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) { // FIXME: implement this method return null; } + @Override protected Object encodeCommand(Command command) { -- 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/main/java/org') 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/main/java/org') 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/main/java/org') 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/main/java/org') 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 9b680956acccb5bd64b6c9c6eee1428e93387719 Mon Sep 17 00:00:00 2001 From: seym45 Date: Wed, 26 Jul 2023 04:21:23 +0400 Subject: Remove extra line --- src/main/java/org/traccar/protocol/GatorProtocolEncoder.java | 1 - 1 file changed, 1 deletion(-) (limited to 'src/main/java/org') diff --git a/src/main/java/org/traccar/protocol/GatorProtocolEncoder.java b/src/main/java/org/traccar/protocol/GatorProtocolEncoder.java index 296ff4f13..c4f06dbc5 100644 --- a/src/main/java/org/traccar/protocol/GatorProtocolEncoder.java +++ b/src/main/java/org/traccar/protocol/GatorProtocolEncoder.java @@ -53,7 +53,6 @@ public class GatorProtocolEncoder extends BaseProtocolEncoder { return buf; } - private ByteBuf encodeContent(long deviceId, int mainOrder) { ByteBuf buf = Unpooled.buffer(); -- cgit v1.2.3 From 18c1e761fd203c994445ac85e808ca6f672d0ec5 Mon Sep 17 00:00:00 2001 From: seym45 Date: Wed, 26 Jul 2023 11:11:15 +0400 Subject: Resolve suggestions - Define MSG_POSITION_REQUEST - Change comment "calibration" to "checksum" --- src/main/java/org/traccar/protocol/GatorProtocolDecoder.java | 1 + src/main/java/org/traccar/protocol/GatorProtocolEncoder.java | 6 ++---- 2 files changed, 3 insertions(+), 4 deletions(-) (limited to 'src/main/java/org') diff --git a/src/main/java/org/traccar/protocol/GatorProtocolDecoder.java b/src/main/java/org/traccar/protocol/GatorProtocolDecoder.java index 644caee81..f7da5dc75 100644 --- a/src/main/java/org/traccar/protocol/GatorProtocolDecoder.java +++ b/src/main/java/org/traccar/protocol/GatorProtocolDecoder.java @@ -37,6 +37,7 @@ public class GatorProtocolDecoder extends BaseProtocolDecoder { } public static final int MSG_HEARTBEAT = 0x21; + public static final int MSG_POSITION_REQUEST = 0x30; public static final int MSG_POSITION_DATA = 0x80; public static final int MSG_ROLLCALL_RESPONSE = 0x81; public static final int MSG_ALARM_DATA = 0x82; diff --git a/src/main/java/org/traccar/protocol/GatorProtocolEncoder.java b/src/main/java/org/traccar/protocol/GatorProtocolEncoder.java index c4f06dbc5..f2c522067 100644 --- a/src/main/java/org/traccar/protocol/GatorProtocolEncoder.java +++ b/src/main/java/org/traccar/protocol/GatorProtocolEncoder.java @@ -60,9 +60,7 @@ public class GatorProtocolEncoder extends BaseProtocolEncoder { buf.writeByte(0x24); buf.writeByte(mainOrder); buf.writeByte(0x00); - - int length = 4 + 1 + 1; // ip 4 bytes, calibration byte and end byte - buf.writeByte(length); + buf.writeByte(4 + 1 + 1); // ip 4 bytes, checksum and end byte ByteBuf pseudoIPAddress = encodeId(deviceId); buf.writeBytes(pseudoIPAddress); @@ -80,7 +78,7 @@ public class GatorProtocolEncoder extends BaseProtocolEncoder { switch (command.getType()) { case Command.TYPE_POSITION_SINGLE: - return encodeContent(command.getDeviceId(), 0x30); + return encodeContent(command.getDeviceId(), GatorProtocolDecoder.MSG_POSITION_REQUEST); default: return null; } -- 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/main/java/org') 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