From 3a528ddc88f72f39ff9566cf5e0f54938b1584ef Mon Sep 17 00:00:00 2001 From: Iván Ávalos Date: Mon, 27 Dec 2021 17:46:29 -0600 Subject: Implemented temporary support for Suntech M2M protocol --- .../traccar/protocol/SuntechProtocolDecoder.java | 10 ++ .../traccar/protocol/SuntechProtocolEncoder.java | 101 +++++++++++++++------ 2 files changed, 82 insertions(+), 29 deletions(-) diff --git a/src/main/java/org/traccar/protocol/SuntechProtocolDecoder.java b/src/main/java/org/traccar/protocol/SuntechProtocolDecoder.java index 4481b61f2..e13483449 100644 --- a/src/main/java/org/traccar/protocol/SuntechProtocolDecoder.java +++ b/src/main/java/org/traccar/protocol/SuntechProtocolDecoder.java @@ -45,6 +45,7 @@ public class SuntechProtocolDecoder extends BaseProtocolDecoder { private boolean includeAdc; private boolean includeRpm; private boolean includeTemp; + private boolean m2m; public SuntechProtocolDecoder(Protocol protocol) { super(protocol); @@ -98,6 +99,15 @@ public class SuntechProtocolDecoder extends BaseProtocolDecoder { return Context.getIdentityManager().lookupAttributeBoolean( deviceId, getProtocolName() + ".includeTemp", includeTemp, false, true); } + + public void setM2M(boolean m2m) { + this.m2m = m2m; + } + + public boolean isM2M(long deviceId) { + return Context.getIdentityManager().lookupAttributeBoolean( + deviceId, getProtocolName() + ".m2m", m2m, true, false); + } private Position decode9( Channel channel, SocketAddress remoteAddress, String[] values) throws ParseException { diff --git a/src/main/java/org/traccar/protocol/SuntechProtocolEncoder.java b/src/main/java/org/traccar/protocol/SuntechProtocolEncoder.java index 3b4995110..0638e29d3 100644 --- a/src/main/java/org/traccar/protocol/SuntechProtocolEncoder.java +++ b/src/main/java/org/traccar/protocol/SuntechProtocolEncoder.java @@ -17,12 +17,13 @@ package org.traccar.protocol; import io.netty.channel.Channel; import org.traccar.BasePipelineFactory; +import org.traccar.Context; import org.traccar.StringProtocolEncoder; import org.traccar.model.Command; import org.traccar.Protocol; public class SuntechProtocolEncoder extends StringProtocolEncoder { - + public SuntechProtocolEncoder(Protocol protocol) { super(protocol); } @@ -41,37 +42,79 @@ public class SuntechProtocolEncoder extends StringProtocolEncoder { } return prefix; } + + private boolean getM2M(Channel channel, long deviceId) { + boolean m2m = false; + if (channel != null) { + SuntechProtocolDecoder protocolDecoder = + BasePipelineFactory.getHandler(channel.pipeline(), SuntechProtocolDecoder.class); + if (protocolDecoder != null) { + m2m = protocolDecoder.isM2M(deviceId); + } + } + return m2m; + } @Override protected Object encodeCommand(Channel channel, Command command) { - - String prefix = getPrefix(channel); - - switch (command.getType()) { - case Command.TYPE_REBOOT_DEVICE: - return formatCommand(command, prefix + ";%s;02;Reboot\r", Command.KEY_UNIQUE_ID); - case Command.TYPE_POSITION_SINGLE: - 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, prefix + ";%s;02;Enable%s\r", - Command.KEY_UNIQUE_ID, Command.KEY_INDEX); - } else { - return formatCommand(command, prefix + ";%s;02;Disable%s\r", - Command.KEY_UNIQUE_ID, Command.KEY_INDEX); - } - } - case Command.TYPE_ENGINE_STOP: - return formatCommand(command, prefix + ";%s;02;Enable1\r", Command.KEY_UNIQUE_ID); - case Command.TYPE_ENGINE_RESUME: - return formatCommand(command, prefix + ";%s;02;Disable1\r", Command.KEY_UNIQUE_ID); - case Command.TYPE_ALARM_ARM: - return formatCommand(command, prefix + ";%s;02;Enable2\r", Command.KEY_UNIQUE_ID); - case Command.TYPE_ALARM_DISARM: - return formatCommand(command, prefix + ";%s;02;Disable2\r", Command.KEY_UNIQUE_ID); - default: - return null; + boolean m2m = getM2M(channel, command.getDeviceId()); + + if (m2m) { + String prefix = "CMD"; + switch (command.getType()) { + case Command.TYPE_REBOOT_DEVICE: + return formatCommand(command, prefix + ";%s;03;03\r", Command.KEY_UNIQUE_ID); + case Command.TYPE_POSITION_SINGLE: + 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, prefix + ";%s;02;Enable%s\r", + Command.KEY_UNIQUE_ID, Command.KEY_INDEX); + } else { + return formatCommand(command, prefix + ";%s;02;Disable%s\r", + Command.KEY_UNIQUE_ID, Command.KEY_INDEX); + } + } + case Command.TYPE_ENGINE_STOP: + return formatCommand(command, prefix + ";%s;04;01\r", Command.KEY_UNIQUE_ID); + case Command.TYPE_ENGINE_RESUME: + return formatCommand(command, prefix + ";%s;04;02\r", Command.KEY_UNIQUE_ID); + case Command.TYPE_ALARM_ARM: + return formatCommand(command, prefix + ";%s;04;03\r", Command.KEY_UNIQUE_ID); + case Command.TYPE_ALARM_DISARM: + return formatCommand(command, prefix + ";%s;04;04\r", Command.KEY_UNIQUE_ID); + default: + return null; + } + } else { + String prefix = getPrefix(channel); + switch (command.getType()) { + case Command.TYPE_REBOOT_DEVICE: + return formatCommand(command, prefix + ";%s;02;Reboot\r", Command.KEY_UNIQUE_ID); + case Command.TYPE_POSITION_SINGLE: + 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, prefix + ";%s;02;Enable%s\r", + Command.KEY_UNIQUE_ID, Command.KEY_INDEX); + } else { + return formatCommand(command, prefix + ";%s;02;Disable%s\r", + Command.KEY_UNIQUE_ID, Command.KEY_INDEX); + } + } + case Command.TYPE_ENGINE_STOP: + return formatCommand(command, prefix + ";%s;02;Enable1\r", Command.KEY_UNIQUE_ID); + case Command.TYPE_ENGINE_RESUME: + return formatCommand(command, prefix + ";%s;02;Disable1\r", Command.KEY_UNIQUE_ID); + case Command.TYPE_ALARM_ARM: + return formatCommand(command, prefix + ";%s;02;Enable2\r", Command.KEY_UNIQUE_ID); + case Command.TYPE_ALARM_DISARM: + return formatCommand(command, prefix + ";%s;02;Disable2\r", Command.KEY_UNIQUE_ID); + default: + return null; + } } } -- cgit v1.2.3