diff options
Diffstat (limited to 'src/main/java/org/traccar/StringProtocolEncoder.java')
-rw-r--r-- | src/main/java/org/traccar/StringProtocolEncoder.java | 38 |
1 files changed, 17 insertions, 21 deletions
diff --git a/src/main/java/org/traccar/StringProtocolEncoder.java b/src/main/java/org/traccar/StringProtocolEncoder.java index 7041a31ab..40dfb25b6 100644 --- a/src/main/java/org/traccar/StringProtocolEncoder.java +++ b/src/main/java/org/traccar/StringProtocolEncoder.java @@ -17,11 +17,6 @@ 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 { public StringProtocolEncoder(Protocol protocol) { @@ -34,26 +29,27 @@ public abstract class StringProtocolEncoder extends BaseProtocolEncoder { protected String formatCommand(Command command, String format, ValueFormatter valueFormatter, String... keys) { - String result = String.format(format, (Object[]) keys); - - Set<String> missingKeys = new HashSet<>(Arrays.asList(keys)); - result = result.replaceAll("\\{" + Command.KEY_UNIQUE_ID + "}", getUniqueId(command.getDeviceId())); - for (Map.Entry<String, Object> entry : command.getAttributes().entrySet()) { + Object[] values = new String[keys.length]; + for (int i = 0; i < keys.length; i++) { String value = null; - if (valueFormatter != null) { - value = valueFormatter.formatValue(entry.getKey(), entry.getValue()); + if (keys[i].equals(Command.KEY_UNIQUE_ID)) { + value = getUniqueId(command.getDeviceId()); + } else { + Object object = command.getAttributes().get(keys[i]); + if (valueFormatter != null) { + value = valueFormatter.formatValue(keys[i], object); + } + if (value == null) { + value = object.toString(); + } + if (value == null) { + value = ""; + } } - if (value == null) { - value = entry.getValue().toString(); - } - result = result.replaceAll("\\{" + entry.getKey() + "}", value); - missingKeys.remove(entry.getKey()); - } - for (String key : missingKeys) { - result = result.replaceAll("\\{" + key + "}", ""); + values[i] = value; } - return result; + return String.format(format, values); } protected String formatCommand(Command command, String format, String... keys) { |