From b607711e6471cc6018c5bbb20a1df7c399bfb878 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Sun, 12 May 2019 19:23:04 -0700 Subject: Support binary custom command --- .../traccar/protocol/TeltonikaProtocolEncoder.java | 25 +++++++++++++--------- 1 file changed, 15 insertions(+), 10 deletions(-) (limited to 'src/main/java/org/traccar/protocol') diff --git a/src/main/java/org/traccar/protocol/TeltonikaProtocolEncoder.java b/src/main/java/org/traccar/protocol/TeltonikaProtocolEncoder.java index 944cec024..705d01d07 100644 --- a/src/main/java/org/traccar/protocol/TeltonikaProtocolEncoder.java +++ b/src/main/java/org/traccar/protocol/TeltonikaProtocolEncoder.java @@ -1,5 +1,5 @@ /* - * Copyright 2016 - 2018 Anton Tananaev (anton@traccar.org) + * Copyright 2016 - 2019 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. @@ -17,6 +17,7 @@ package org.traccar.protocol; import org.traccar.BaseProtocolEncoder; import org.traccar.helper.Checksum; +import org.traccar.helper.DataConverter; import org.traccar.model.Command; import io.netty.buffer.ByteBuf; @@ -26,17 +27,17 @@ import java.nio.charset.StandardCharsets; public class TeltonikaProtocolEncoder extends BaseProtocolEncoder { - private ByteBuf encodeContent(String content) { + private ByteBuf encodeContent(byte[] content) { ByteBuf buf = Unpooled.buffer(); buf.writeInt(0); - buf.writeInt(content.length() + 10); + buf.writeInt(content.length + 10); buf.writeByte(TeltonikaProtocolDecoder.CODEC_12); buf.writeByte(1); // quantity buf.writeByte(5); // type - buf.writeInt(content.length() + 2); - buf.writeBytes(content.getBytes(StandardCharsets.US_ASCII)); + buf.writeInt(content.length + 2); + buf.writeBytes(content); buf.writeByte('\r'); buf.writeByte('\n'); buf.writeByte(1); // quantity @@ -48,11 +49,15 @@ public class TeltonikaProtocolEncoder extends BaseProtocolEncoder { @Override protected Object encodeCommand(Command command) { - switch (command.getType()) { - case Command.TYPE_CUSTOM: - return encodeContent(command.getString(Command.KEY_DATA)); - default: - return null; + if (command.getType().equals(Command.TYPE_CUSTOM)) { + String data = command.getString(Command.KEY_DATA); + if (data.matches("(\\p{XDigit}{2})+")) { + return encodeContent(DataConverter.parseHex(data)); + } else { + return encodeContent(data.getBytes(StandardCharsets.US_ASCII)); + } + } else { + return null; } } -- cgit v1.2.3