diff options
-rw-r--r-- | src/org/traccar/protocol/WatchProtocolEncoder.java | 17 | ||||
-rw-r--r-- | test/org/traccar/protocol/WatchProtocolEncoderTest.java | 14 |
2 files changed, 29 insertions, 2 deletions
diff --git a/src/org/traccar/protocol/WatchProtocolEncoder.java b/src/org/traccar/protocol/WatchProtocolEncoder.java index 7c96c8a83..7c353e26d 100644 --- a/src/org/traccar/protocol/WatchProtocolEncoder.java +++ b/src/org/traccar/protocol/WatchProtocolEncoder.java @@ -21,13 +21,26 @@ import org.traccar.model.Command; import javax.xml.bind.DatatypeConverter; import java.nio.charset.StandardCharsets; +import java.text.DecimalFormat; import java.util.HashMap; import java.util.Map; -public class WatchProtocolEncoder extends StringProtocolEncoder { +public class WatchProtocolEncoder extends StringProtocolEncoder implements StringProtocolEncoder.ValueFormatter { + + @Override + public String formatValue(String key, Object value) { + if (key.equals(Command.KEY_TIMEZONE)) { + double offset = ((Number) value).longValue() / 60.0; + DecimalFormat fmt = new DecimalFormat("+#.##;-#.##"); + return fmt.format(offset); + } + + return null; + } + protected String formatCommand(Command command, String format, String... keys) { - String content = super.formatCommand(command, format, keys); + String content = super.formatCommand(command, format, this, keys); return String.format("[CS*%s*%04x*%s]", getUniqueId(command.getDeviceId()), content.length(), content); } diff --git a/test/org/traccar/protocol/WatchProtocolEncoderTest.java b/test/org/traccar/protocol/WatchProtocolEncoderTest.java index 4e707a47b..eaf290216 100644 --- a/test/org/traccar/protocol/WatchProtocolEncoderTest.java +++ b/test/org/traccar/protocol/WatchProtocolEncoderTest.java @@ -32,6 +32,20 @@ public class WatchProtocolEncoderTest extends ProtocolTest { command.set(Command.KEY_DATA, "3333"); Assert.assertEquals("[CS*123456789012345*0005*TK,33]", encoder.encodeCommand(command)); + command = new Command(); + command.setDeviceId(1); + command.setType(Command.TYPE_SET_TIMEZONE); + command.set(Command.KEY_TIMEZONE, 60); + Assert.assertEquals("[CS*123456789012345*0006*LZ,,+1]", encoder.encodeCommand(command)); + + command.set(Command.KEY_TIMEZONE, 90); + Assert.assertEquals("[CS*123456789012345*0008*LZ,,+1.5]", encoder.encodeCommand(command)); + + command.set(Command.KEY_TIMEZONE, -60); + Assert.assertEquals("[CS*123456789012345*0006*LZ,,-1]", encoder.encodeCommand(command)); + + command.set(Command.KEY_TIMEZONE, -11 * 60 - 30); + Assert.assertEquals("[CS*123456789012345*0009*LZ,,-11.5]", encoder.encodeCommand(command)); } } |