aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/traccar/protocol/Xexun2ProtocolEncoder.java
diff options
context:
space:
mode:
authorAnton Tananaev <anton.tananaev@gmail.com>2022-09-21 09:57:52 -0700
committerGitHub <noreply@github.com>2022-09-21 09:57:52 -0700
commitd5b1385596f79da2514ea7cccf27e9bdeebcacae (patch)
treef7b2d44f09dee52c09c2c7e2384f0c27f1e65fa1 /src/main/java/org/traccar/protocol/Xexun2ProtocolEncoder.java
parent561647947118a86256dbb584f6da450334462739 (diff)
parent7aa230b9ddcd5540e7ff9c9b3d9bcf5119175c46 (diff)
downloadtrackermap-server-d5b1385596f79da2514ea7cccf27e9bdeebcacae.tar.gz
trackermap-server-d5b1385596f79da2514ea7cccf27e9bdeebcacae.tar.bz2
trackermap-server-d5b1385596f79da2514ea7cccf27e9bdeebcacae.zip
Merge pull request #4912 from stefanclark/Xexun2Encoder
Implement Xexun2 encoder
Diffstat (limited to 'src/main/java/org/traccar/protocol/Xexun2ProtocolEncoder.java')
-rw-r--r--src/main/java/org/traccar/protocol/Xexun2ProtocolEncoder.java70
1 files changed, 70 insertions, 0 deletions
diff --git a/src/main/java/org/traccar/protocol/Xexun2ProtocolEncoder.java b/src/main/java/org/traccar/protocol/Xexun2ProtocolEncoder.java
new file mode 100644
index 000000000..8f3fa5672
--- /dev/null
+++ b/src/main/java/org/traccar/protocol/Xexun2ProtocolEncoder.java
@@ -0,0 +1,70 @@
+/*
+ * Copyright 2022 Stefan Clark (stefan@stefanclark.co.uk)
+ *
+ * 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.helper.Checksum;
+import org.traccar.helper.DataConverter;
+import org.traccar.model.Command;
+import org.traccar.Protocol;
+
+import java.nio.charset.StandardCharsets;
+
+public class Xexun2ProtocolEncoder extends BaseProtocolEncoder {
+
+ public Xexun2ProtocolEncoder(Protocol protocol) {
+ super(protocol);
+ }
+
+ private static ByteBuf encodeContent(String uniqueId, String content) {
+ ByteBuf buf = Unpooled.buffer();
+
+ ByteBuf message = Unpooled.copiedBuffer(content.getBytes(StandardCharsets.US_ASCII));
+
+ buf.writeShort(Xexun2ProtocolDecoder.FLAG);
+ buf.writeShort(Xexun2ProtocolDecoder.MSG_COMMAND);
+ buf.writeShort(1); // index
+ buf.writeBytes(DataConverter.parseHex(uniqueId + "0"));
+ buf.writeShort(message.readableBytes());
+ buf.writeShort(Checksum.ip(message.nioBuffer()));
+ buf.writeBytes(message);
+ buf.writeShort(Xexun2ProtocolDecoder.FLAG);
+
+ return buf;
+ }
+
+ @Override
+ protected Object encodeCommand(Command command) {
+ String uniqueId = getUniqueId(command.getDeviceId());
+
+ switch (command.getType()) {
+ case Command.TYPE_CUSTOM:
+ return encodeContent(uniqueId, command.getString(Command.KEY_DATA));
+ case Command.TYPE_POSITION_PERIODIC:
+ return encodeContent(uniqueId,
+ String.format("tracking_send=%1$d,%1$d", command.getInteger(Command.KEY_FREQUENCY)));
+ case Command.TYPE_POWER_OFF:
+ return encodeContent(uniqueId, "of=1");
+ case Command.TYPE_REBOOT_DEVICE:
+ return encodeContent(uniqueId, "reset");
+ default:
+ return null;
+ }
+ }
+
+}