aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/traccar/StringProtocolEncoder.java
diff options
context:
space:
mode:
authorAnton Tananaev <anton.tananaev@gmail.com>2019-11-10 12:20:21 -0800
committerAnton Tananaev <anton.tananaev@gmail.com>2019-11-10 12:20:21 -0800
commit5d1e2434b72187dba9f1a06c4922db06c28693ad (patch)
tree0279740e972f4a66c2a4ff9b7b1550da948d85dc /src/main/java/org/traccar/StringProtocolEncoder.java
parent36a49a88ea44da16b57e5256dc4c91c2e90a3792 (diff)
downloadtrackermap-server-5d1e2434b72187dba9f1a06c4922db06c28693ad.tar.gz
trackermap-server-5d1e2434b72187dba9f1a06c4922db06c28693ad.tar.bz2
trackermap-server-5d1e2434b72187dba9f1a06c4922db06c28693ad.zip
Refactor string protocol encoders
Diffstat (limited to 'src/main/java/org/traccar/StringProtocolEncoder.java')
-rw-r--r--src/main/java/org/traccar/StringProtocolEncoder.java30
1 files changed, 17 insertions, 13 deletions
diff --git a/src/main/java/org/traccar/StringProtocolEncoder.java b/src/main/java/org/traccar/StringProtocolEncoder.java
index d9acce7f0..40dfb25b6 100644
--- a/src/main/java/org/traccar/StringProtocolEncoder.java
+++ b/src/main/java/org/traccar/StringProtocolEncoder.java
@@ -17,8 +17,6 @@ package org.traccar;
import org.traccar.model.Command;
-import java.util.Map;
-
public abstract class StringProtocolEncoder extends BaseProtocolEncoder {
public StringProtocolEncoder(Protocol protocol) {
@@ -31,21 +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);
-
- 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 (value == null) {
- value = entry.getValue().toString();
+ 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 = "";
+ }
}
- result = result.replaceAll("\\{" + entry.getKey() + "}", value);
+ values[i] = value;
}
- return result;
+ return String.format(format, values);
}
protected String formatCommand(Command command, String format, String... keys) {