aboutsummaryrefslogtreecommitdiff
path: root/src/main/java
diff options
context:
space:
mode:
authorseym45 <seym45@gmail.com>2023-07-26 04:11:40 +0400
committerseym45 <seym45@gmail.com>2023-07-26 04:11:40 +0400
commit2407799dc370d741431cbec056c0f2acbe454bf3 (patch)
tree9e658adb0c49d088ee67c1921f2a0ab79969cc8e /src/main/java
parent6ac15f2c6fb8798c6906c24e97b0048736270215 (diff)
downloadtrackermap-server-2407799dc370d741431cbec056c0f2acbe454bf3.tar.gz
trackermap-server-2407799dc370d741431cbec056c0f2acbe454bf3.tar.bz2
trackermap-server-2407799dc370d741431cbec056c0f2acbe454bf3.zip
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
Diffstat (limited to 'src/main/java')
-rw-r--r--src/main/java/org/traccar/protocol/GatorProtocolEncoder.java83
1 files changed, 36 insertions, 47 deletions
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<String> 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;
}