diff options
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | src/main/java/org/traccar/protocol/SuntechProtocolDecoder.java | 18 | ||||
-rw-r--r-- | src/main/java/org/traccar/protocol/SuntechProtocolEncoder.java | 101 |
3 files changed, 89 insertions, 31 deletions
diff --git a/.gitignore b/.gitignore index f2672aada..d6f8a2709 100644 --- a/.gitignore +++ b/.gitignore @@ -11,3 +11,4 @@ nbactions.xml out build src/main/java/org/traccar/protobuf +/bin/ diff --git a/src/main/java/org/traccar/protocol/SuntechProtocolDecoder.java b/src/main/java/org/traccar/protocol/SuntechProtocolDecoder.java index 2d00ea81e..21d23c24d 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 { @@ -396,11 +406,15 @@ public class SuntechProtocolDecoder extends BaseProtocolDecoder { if (isHbm(deviceSession.getDeviceId())) { if (index < values.length) { - position.set(Position.KEY_HOURS, UnitsConverter.msFromMinutes(Integer.parseInt(values[index++]))); + try { + position.set(Position.KEY_HOURS, UnitsConverter.msFromMinutes(Integer.parseInt(values[index++]))); + } catch (NumberFormatException e) {} } if (index < values.length) { - position.set(Position.KEY_BATTERY, Double.parseDouble(values[index++])); + try { + position.set(Position.KEY_BATTERY, Double.parseDouble(values[index++])); + } catch (NumberFormatException e) {} } if (index < values.length && values[index++].equals("0")) { 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; + } } } |