aboutsummaryrefslogtreecommitdiff
path: root/src/org/traccar/protocol/commands
diff options
context:
space:
mode:
authorAnton Tananaev <anton.tananaev@gmail.com>2015-06-29 10:48:34 +1200
committerAnton Tananaev <anton.tananaev@gmail.com>2015-06-29 10:48:34 +1200
commitd1c4cd526845aad56c5b0a3e20454638bbc7fecc (patch)
tree565ee245ea0e98b88674b775147fbc6d3e1933cb /src/org/traccar/protocol/commands
parent88b3f00f3855aa408c7859051efdcb5b37fe559f (diff)
downloadtrackermap-server-d1c4cd526845aad56c5b0a3e20454638bbc7fecc.tar.gz
trackermap-server-d1c4cd526845aad56c5b0a3e20454638bbc7fecc.tar.bz2
trackermap-server-d1c4cd526845aad56c5b0a3e20454638bbc7fecc.zip
Merge commands implmentation (fix #1271)
Diffstat (limited to 'src/org/traccar/protocol/commands')
-rw-r--r--src/org/traccar/protocol/commands/CommandTemplate.java8
-rw-r--r--src/org/traccar/protocol/commands/CommandValueConversion.java5
-rw-r--r--src/org/traccar/protocol/commands/StringCommandTemplate.java56
3 files changed, 69 insertions, 0 deletions
diff --git a/src/org/traccar/protocol/commands/CommandTemplate.java b/src/org/traccar/protocol/commands/CommandTemplate.java
new file mode 100644
index 000000000..5d461b276
--- /dev/null
+++ b/src/org/traccar/protocol/commands/CommandTemplate.java
@@ -0,0 +1,8 @@
+package org.traccar.protocol.commands;
+
+import org.traccar.database.ActiveDevice;
+import org.traccar.http.commands.GpsCommand;
+
+public interface CommandTemplate<T extends GpsCommand> {
+ Object applyTo(ActiveDevice activeDevice, T command);
+}
diff --git a/src/org/traccar/protocol/commands/CommandValueConversion.java b/src/org/traccar/protocol/commands/CommandValueConversion.java
new file mode 100644
index 000000000..cfbf3ce5a
--- /dev/null
+++ b/src/org/traccar/protocol/commands/CommandValueConversion.java
@@ -0,0 +1,5 @@
+package org.traccar.protocol.commands;
+
+public interface CommandValueConversion<T> {
+ public String convert(T value);
+}
diff --git a/src/org/traccar/protocol/commands/StringCommandTemplate.java b/src/org/traccar/protocol/commands/StringCommandTemplate.java
new file mode 100644
index 000000000..2ac0ce9a8
--- /dev/null
+++ b/src/org/traccar/protocol/commands/StringCommandTemplate.java
@@ -0,0 +1,56 @@
+package org.traccar.protocol.commands;
+
+import org.traccar.database.ActiveDevice;
+import org.traccar.http.commands.GpsCommand;
+import org.traccar.protocol.commands.CommandValueConversion;
+
+import java.util.HashMap;
+import java.util.Map;
+
+public class StringCommandTemplate<T extends GpsCommand> implements CommandTemplate<T> {
+
+ private String messageTemplate;
+ private Map<Class<?>, CommandValueConversion> converters = new HashMap<Class<?>, CommandValueConversion>();
+
+ public StringCommandTemplate(String template, Object... replacements) {
+ this.messageTemplate = String.format(template, replacements);
+ }
+
+ @Override
+ public Object applyTo(ActiveDevice activeDevice, T command) {
+ String currentMessage = messageTemplate;
+ currentMessage = this.replace(currentMessage, GpsCommand.UNIQUE_ID, activeDevice.getUniqueId());
+
+ Map<String, Object> replacements = command.getReplacements();
+
+ for (Map.Entry<String, Object> entry : replacements.entrySet()) {
+ currentMessage = this.replace(currentMessage, entry.getKey(), entry.getValue());
+ }
+
+ return currentMessage;
+ }
+
+ public CommandTemplate addConverter(Class<?> type, CommandValueConversion converter) {
+ converters.put(type, converter);
+ return this;
+ }
+
+ protected CommandValueConversion getConverter(Class<?> type) {
+ return converters.containsKey(type) ? converters.get(type) : idConverter();
+ }
+
+ private CommandValueConversion idConverter() {
+ return new CommandValueConversion() {
+ @Override
+ public String convert(Object value) {
+ return value.toString();
+ }
+ };
+ }
+
+ private String replace(String currentMessage, String key, Object value) {
+ String replacementValue = getConverter(value.getClass()).convert(value);
+ return currentMessage.replace("[" + key + "]", replacementValue);
+ }
+
+}