diff options
-rw-r--r-- | src/main/java/org/traccar/protocol/TeltonikaProtocolEncoder.java | 25 | ||||
-rw-r--r-- | src/test/java/org/traccar/protocol/TeltonikaProtocolEncoderTest.java | 5 |
2 files changed, 19 insertions, 11 deletions
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; } } diff --git a/src/test/java/org/traccar/protocol/TeltonikaProtocolEncoderTest.java b/src/test/java/org/traccar/protocol/TeltonikaProtocolEncoderTest.java index 83ea961b4..0bd4efb72 100644 --- a/src/test/java/org/traccar/protocol/TeltonikaProtocolEncoderTest.java +++ b/src/test/java/org/traccar/protocol/TeltonikaProtocolEncoderTest.java @@ -14,10 +14,13 @@ public class TeltonikaProtocolEncoderTest extends ProtocolTest { Command command = new Command(); command.setDeviceId(1); command.setType(Command.TYPE_CUSTOM); - command.set(Command.KEY_DATA, "setdigout 11"); + command.set(Command.KEY_DATA, "setdigout 11"); verifyCommand(encoder, command, binary("00000000000000160C01050000000E7365746469676F75742031310D0A010000E258")); + command.set(Command.KEY_DATA, "03030000000185E8"); + verifyCommand(encoder, command, binary("00000000000000120c01050000000a03030000000185e80d0a010000cf19")); + } } |