From fd6d1aa06ba983e17833b11393cfb344e5825fba Mon Sep 17 00:00:00 2001 From: Gavriel Fleischer Date: Sat, 9 Nov 2019 23:50:29 +0200 Subject: replace missing command keys with empty string --- src/main/java/org/traccar/StringProtocolEncoder.java | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'src/main/java/org/traccar') diff --git a/src/main/java/org/traccar/StringProtocolEncoder.java b/src/main/java/org/traccar/StringProtocolEncoder.java index d9acce7f0..7041a31ab 100644 --- a/src/main/java/org/traccar/StringProtocolEncoder.java +++ b/src/main/java/org/traccar/StringProtocolEncoder.java @@ -17,7 +17,10 @@ package org.traccar; import org.traccar.model.Command; +import java.util.Arrays; +import java.util.HashSet; import java.util.Map; +import java.util.Set; public abstract class StringProtocolEncoder extends BaseProtocolEncoder { @@ -33,6 +36,7 @@ public abstract class StringProtocolEncoder extends BaseProtocolEncoder { String result = String.format(format, (Object[]) keys); + Set missingKeys = new HashSet<>(Arrays.asList(keys)); result = result.replaceAll("\\{" + Command.KEY_UNIQUE_ID + "}", getUniqueId(command.getDeviceId())); for (Map.Entry entry : command.getAttributes().entrySet()) { String value = null; @@ -43,6 +47,10 @@ public abstract class StringProtocolEncoder extends BaseProtocolEncoder { value = entry.getValue().toString(); } result = result.replaceAll("\\{" + entry.getKey() + "}", value); + missingKeys.remove(entry.getKey()); + } + for (String key : missingKeys) { + result = result.replaceAll("\\{" + key + "}", ""); } return result; -- cgit v1.2.3 From 47a1af1554b0216eae8248227407486c0eba8ef3 Mon Sep 17 00:00:00 2001 From: Gavriel Fleischer Date: Sat, 9 Nov 2019 23:51:41 +0200 Subject: added language to watch protocol's set_timezone command --- src/main/java/org/traccar/model/Command.java | 1 + src/main/java/org/traccar/protocol/WatchProtocolEncoder.java | 4 +++- src/test/java/org/traccar/protocol/WatchProtocolEncoderTest.java | 4 ++++ 3 files changed, 8 insertions(+), 1 deletion(-) (limited to 'src/main/java/org/traccar') diff --git a/src/main/java/org/traccar/model/Command.java b/src/main/java/org/traccar/model/Command.java index 336fc61f4..abe538a10 100644 --- a/src/main/java/org/traccar/model/Command.java +++ b/src/main/java/org/traccar/model/Command.java @@ -68,6 +68,7 @@ public class Command extends Message implements Cloneable { public static final String KEY_UNIQUE_ID = "uniqueId"; public static final String KEY_FREQUENCY = "frequency"; + public static final String KEY_LANGUAGE = "language"; public static final String KEY_TIMEZONE = "timezone"; public static final String KEY_DEVICE_PASSWORD = "devicePassword"; public static final String KEY_RADIUS = "radius"; diff --git a/src/main/java/org/traccar/protocol/WatchProtocolEncoder.java b/src/main/java/org/traccar/protocol/WatchProtocolEncoder.java index b433dfd2a..1c535e9de 100644 --- a/src/main/java/org/traccar/protocol/WatchProtocolEncoder.java +++ b/src/main/java/org/traccar/protocol/WatchProtocolEncoder.java @@ -47,6 +47,8 @@ public class WatchProtocolEncoder extends StringProtocolEncoder implements Strin return DataConverter.printHex(value.toString().getBytes(StandardCharsets.UTF_16BE)); } else if (key.equals(Command.KEY_ENABLE)) { return (boolean) value ? "1" : "0"; + } else if (key.equals(Command.KEY_LANGUAGE)) { + return String.valueOf(value); } return null; @@ -161,7 +163,7 @@ public class WatchProtocolEncoder extends StringProtocolEncoder implements Strin case Command.TYPE_POSITION_PERIODIC: return formatTextCommand(channel, command, "UPLOAD,{%s}", Command.KEY_FREQUENCY); case Command.TYPE_SET_TIMEZONE: - return formatTextCommand(channel, command, "LZ,,{%s}", Command.KEY_TIMEZONE); + return formatTextCommand(channel, command, "LZ,{%s},{%s}", Command.KEY_LANGUAGE, Command.KEY_TIMEZONE); case Command.TYPE_SET_INDICATOR: return formatTextCommand(channel, command, "FLOWER,{%s}", Command.KEY_DATA); default: diff --git a/src/test/java/org/traccar/protocol/WatchProtocolEncoderTest.java b/src/test/java/org/traccar/protocol/WatchProtocolEncoderTest.java index 798213c7b..a4a795050 100644 --- a/src/test/java/org/traccar/protocol/WatchProtocolEncoderTest.java +++ b/src/test/java/org/traccar/protocol/WatchProtocolEncoderTest.java @@ -78,6 +78,10 @@ public class WatchProtocolEncoderTest extends ProtocolTest { command.set(Command.KEY_TIMEZONE, "GMT-11:30"); verifyFrame(buffer("[CS*123456789012345*0009*LZ,,-11.5]"), encoder.encodeCommand(null, command)); + command.set(Command.KEY_LANGUAGE, 0); + command.set(Command.KEY_TIMEZONE, "GMT+05:45"); + verifyFrame(buffer("[CS*123456789012345*000a*LZ,0,+5.75]"), encoder.encodeCommand(null, command)); + } } -- cgit v1.2.3 From 61d07a123beeaa1e85f44046413a680aabda0c9c Mon Sep 17 00:00:00 2001 From: Gavriel Fleischer Date: Mon, 11 Nov 2019 00:26:01 +0200 Subject: fixed NPE for optional language parameter --- src/main/java/org/traccar/StringProtocolEncoder.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/main/java/org/traccar') diff --git a/src/main/java/org/traccar/StringProtocolEncoder.java b/src/main/java/org/traccar/StringProtocolEncoder.java index 40dfb25b6..e9fb65500 100644 --- a/src/main/java/org/traccar/StringProtocolEncoder.java +++ b/src/main/java/org/traccar/StringProtocolEncoder.java @@ -39,7 +39,7 @@ public abstract class StringProtocolEncoder extends BaseProtocolEncoder { if (valueFormatter != null) { value = valueFormatter.formatValue(keys[i], object); } - if (value == null) { + if (value == null && object != null) { value = object.toString(); } if (value == null) { -- cgit v1.2.3