From 05e44d8cf956f6a0d44497aa0b357036139425dd Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Sat, 23 Nov 2019 15:36:42 -0800 Subject: Use correct prefix for commands --- .../traccar/protocol/SuntechProtocolDecoder.java | 15 ++++++--- .../traccar/protocol/SuntechProtocolEncoder.java | 37 ++++++++++++++++------ 2 files changed, 39 insertions(+), 13 deletions(-) (limited to 'src/main') diff --git a/src/main/java/org/traccar/protocol/SuntechProtocolDecoder.java b/src/main/java/org/traccar/protocol/SuntechProtocolDecoder.java index 08d8252a3..601d127f1 100644 --- a/src/main/java/org/traccar/protocol/SuntechProtocolDecoder.java +++ b/src/main/java/org/traccar/protocol/SuntechProtocolDecoder.java @@ -38,6 +38,8 @@ import java.util.TimeZone; public class SuntechProtocolDecoder extends BaseProtocolDecoder { + private String prefix; + private int protocolType; private boolean hbm; private boolean includeAdc; @@ -48,6 +50,10 @@ public class SuntechProtocolDecoder extends BaseProtocolDecoder { super(protocol); } + public String getPrefix() { + return prefix; + } + public void setProtocolType(int protocolType) { this.protocolType = protocolType; } @@ -626,15 +632,16 @@ public class SuntechProtocolDecoder extends BaseProtocolDecoder { } else { String[] values = buf.toString(StandardCharsets.US_ASCII).split(";"); + prefix = values[0]; - if (values[0].length() < 5) { + if (prefix.length() < 5) { return decodeUniversal(channel, remoteAddress, values); - } else if (values[0].startsWith("ST9")) { + } else if (prefix.startsWith("ST9")) { return decode9(channel, remoteAddress, values); - } else if (values[0].startsWith("ST4")) { + } else if (prefix.startsWith("ST4")) { return decode4(channel, remoteAddress, values); } else { - return decode2356(channel, remoteAddress, values[0].substring(0, 5), values); + return decode2356(channel, remoteAddress, prefix.substring(0, 5), values); } } } diff --git a/src/main/java/org/traccar/protocol/SuntechProtocolEncoder.java b/src/main/java/org/traccar/protocol/SuntechProtocolEncoder.java index 55f2c73dd..3b4995110 100644 --- a/src/main/java/org/traccar/protocol/SuntechProtocolEncoder.java +++ b/src/main/java/org/traccar/protocol/SuntechProtocolEncoder.java @@ -15,6 +15,8 @@ */ package org.traccar.protocol; +import io.netty.channel.Channel; +import org.traccar.BasePipelineFactory; import org.traccar.StringProtocolEncoder; import org.traccar.model.Command; import org.traccar.Protocol; @@ -25,32 +27,49 @@ public class SuntechProtocolEncoder extends StringProtocolEncoder { super(protocol); } + private String getPrefix(Channel channel) { + String prefix = "SA200CMD"; + if (channel != null) { + SuntechProtocolDecoder protocolDecoder = + BasePipelineFactory.getHandler(channel.pipeline(), SuntechProtocolDecoder.class); + if (protocolDecoder != null) { + String decoderPrefix = protocolDecoder.getPrefix(); + if (decoderPrefix != null && decoderPrefix.length() > 5) { + prefix = decoderPrefix.substring(0, decoderPrefix.length() - 3) + "CMD"; + } + } + } + return prefix; + } + @Override - protected Object encodeCommand(Command command) { + protected Object encodeCommand(Channel channel, Command command) { + + String prefix = getPrefix(channel); switch (command.getType()) { case Command.TYPE_REBOOT_DEVICE: - return formatCommand(command, "SA200CMD;%s;02;Reboot\r", Command.KEY_UNIQUE_ID); + return formatCommand(command, prefix + ";%s;02;Reboot\r", Command.KEY_UNIQUE_ID); case Command.TYPE_POSITION_SINGLE: - return formatCommand(command, "SA200GTR;%s;02;\r", Command.KEY_UNIQUE_ID); + return formatCommand(command, prefix + ";%s;02;\r", Command.KEY_UNIQUE_ID); case Command.TYPE_OUTPUT_CONTROL: if (command.getAttributes().containsKey(Command.KEY_DATA)) { if (command.getAttributes().get(Command.KEY_DATA).equals("1")) { - return formatCommand(command, "SA200CMD;%s;02;Enable%s\r", + return formatCommand(command, prefix + ";%s;02;Enable%s\r", Command.KEY_UNIQUE_ID, Command.KEY_INDEX); } else { - return formatCommand(command, "SA200CMD;%s;02;Disable%s\r", + return formatCommand(command, prefix + ";%s;02;Disable%s\r", Command.KEY_UNIQUE_ID, Command.KEY_INDEX); } } case Command.TYPE_ENGINE_STOP: - return formatCommand(command, "SA200CMD;%s;02;Enable1\r", Command.KEY_UNIQUE_ID); + return formatCommand(command, prefix + ";%s;02;Enable1\r", Command.KEY_UNIQUE_ID); case Command.TYPE_ENGINE_RESUME: - return formatCommand(command, "SA200CMD;%s;02;Disable1\r", Command.KEY_UNIQUE_ID); + return formatCommand(command, prefix + ";%s;02;Disable1\r", Command.KEY_UNIQUE_ID); case Command.TYPE_ALARM_ARM: - return formatCommand(command, "SA200CMD;%s;02;Enable2\r", Command.KEY_UNIQUE_ID); + return formatCommand(command, prefix + ";%s;02;Enable2\r", Command.KEY_UNIQUE_ID); case Command.TYPE_ALARM_DISARM: - return formatCommand(command, "SA200CMD;%s;02;Disable2\r", Command.KEY_UNIQUE_ID); + return formatCommand(command, prefix + ";%s;02;Disable2\r", Command.KEY_UNIQUE_ID); default: return null; } -- cgit v1.2.3