aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAnton Tananaev <anton@traccar.org>2024-03-03 08:24:49 -0800
committerAnton Tananaev <anton@traccar.org>2024-03-03 08:24:49 -0800
commit75b404db5c790bf37c05fabf1cbbd2027ad1db25 (patch)
tree9dd4c5b37e58a1991aace5101e86941a95c9b93e /src
parentf1680218e85ff3310eb5898d8ec28feaac5797c6 (diff)
downloadtrackermap-server-75b404db5c790bf37c05fabf1cbbd2027ad1db25.tar.gz
trackermap-server-75b404db5c790bf37c05fabf1cbbd2027ad1db25.tar.bz2
trackermap-server-75b404db5c790bf37c05fabf1cbbd2027ad1db25.zip
Refactor XRB28 implementation
Diffstat (limited to 'src')
-rw-r--r--src/main/java/org/traccar/protocol/Xrb28ProtocolDecoder.java51
1 files changed, 35 insertions, 16 deletions
diff --git a/src/main/java/org/traccar/protocol/Xrb28ProtocolDecoder.java b/src/main/java/org/traccar/protocol/Xrb28ProtocolDecoder.java
index 88f2d07e5..6033293c4 100644
--- a/src/main/java/org/traccar/protocol/Xrb28ProtocolDecoder.java
+++ b/src/main/java/org/traccar/protocol/Xrb28ProtocolDecoder.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2018 Anton Tananaev (anton@traccar.org)
+ * Copyright 2018 - 2024 Anton Tananaev (anton@traccar.org)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -27,6 +27,7 @@ import org.traccar.model.Command;
import org.traccar.model.Position;
import java.net.SocketAddress;
+import java.util.Arrays;
import java.util.regex.Pattern;
public class Xrb28ProtocolDecoder extends BaseProtocolDecoder {
@@ -46,6 +47,7 @@ public class Xrb28ProtocolDecoder extends BaseProtocolDecoder {
.expression("....,")
.expression("..,") // vendor
.number("d{15},") // imei
+ .number("d{12},").optional() // time
.expression("..,") // type
.number("[01],") // reserved
.number("(dd)(dd)(dd).d+,") // time (hhmmss)
@@ -67,23 +69,44 @@ public class Xrb28ProtocolDecoder extends BaseProtocolDecoder {
Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
String sentence = (String) msg;
+ String[] values = sentence.replaceAll("#$", "").split(",");
- DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, sentence.substring(9, 24));
+ int index = 0;
+ String header = values[index++];
+ String vendor = values[index++];
+
+ String imei = values[index++];
+ DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, imei);
if (deviceSession == null) {
return null;
}
- String type = sentence.substring(25, 27);
+ String time;
+ if (values[index].length() == 12) {
+ time = values[index++];
+ } else {
+ time = null;
+ }
+
+ String type = values[index++];
if (channel != null) {
+ StringBuilder response = new StringBuilder("\u00ff\u00ff");
+ response.append(header.replaceAll("R$", "S")).append(',');
+ response.append(vendor).append(',');
+ response.append(imei).append(',');
+ if (time != null) {
+ response.append(time).append(',');
+ }
if (type.matches("L0|L1|W0|E1")) {
- channel.write(new NetworkMessage(
- "\u00ff\u00ff*SCOS" + sentence.substring(5, 27) + "#\n",
- remoteAddress));
+ response.append(type).append("#\n");
+ channel.write(new NetworkMessage(response.toString(), remoteAddress));
} else if (type.equals("R0") && pendingCommand != null) {
- String command = pendingCommand.equals(Command.TYPE_ALARM_ARM) ? "L1," : "L0,";
- channel.write(new NetworkMessage(
- "\u00ff\u00ff*SCOS" + sentence.substring(5, 25) + command + sentence.substring(30) + "\n",
- remoteAddress));
+ String command = pendingCommand.equals(Command.TYPE_ALARM_ARM) ? "L1" : "L0";
+ response.append(command);
+ String[] remaining = Arrays.copyOfRange(values, index, values.length);
+ response.append(String.join(",", remaining));
+ response.append("#\n");
+ channel.write(new NetworkMessage(response.toString(), remoteAddress));
pendingCommand = null;
}
}
@@ -95,11 +118,6 @@ public class Xrb28ProtocolDecoder extends BaseProtocolDecoder {
getLastLocation(position, null);
- String payload = sentence.substring(25, sentence.length() - 1);
-
- int index = 0;
- String[] values = payload.substring(3).split(",");
-
switch (type) {
case "Q0":
position.set(Position.KEY_BATTERY, Integer.parseInt(values[index++]) * 0.01);
@@ -146,7 +164,8 @@ public class Xrb28ProtocolDecoder extends BaseProtocolDecoder {
case "K0":
case "I0":
case "M0":
- position.set(Position.KEY_RESULT, payload);
+ String[] remaining = Arrays.copyOfRange(values, index, values.length);
+ position.set(Position.KEY_RESULT, String.join(",", remaining));
break;
default:
break;