diff options
author | Anton Tananaev <anton.tananaev@gmail.com> | 2015-06-30 09:41:41 +1200 |
---|---|---|
committer | Anton Tananaev <anton.tananaev@gmail.com> | 2015-06-30 09:41:41 +1200 |
commit | 54c54703d66e8991472c1f7476ba4acb424b27d9 (patch) | |
tree | b43f2f0225b7722a1543a973a568ee2854e7b413 /src/org/traccar/command | |
parent | c646926a7fb0e66407c1ed7aa18daa1c9fc41cd8 (diff) | |
download | trackermap-server-54c54703d66e8991472c1f7476ba4acb424b27d9.tar.gz trackermap-server-54c54703d66e8991472c1f7476ba4acb424b27d9.tar.bz2 trackermap-server-54c54703d66e8991472c1f7476ba4acb424b27d9.zip |
Move commands related classes
Diffstat (limited to 'src/org/traccar/command')
-rw-r--r-- | src/org/traccar/command/CommandTemplate.java | 8 | ||||
-rw-r--r-- | src/org/traccar/command/CommandType.java | 28 | ||||
-rw-r--r-- | src/org/traccar/command/CommandValueConversion.java | 5 | ||||
-rw-r--r-- | src/org/traccar/command/Duration.java | 38 | ||||
-rw-r--r-- | src/org/traccar/command/FixPositioningCommand.java | 25 | ||||
-rw-r--r-- | src/org/traccar/command/GpsCommand.java | 27 | ||||
-rw-r--r-- | src/org/traccar/command/NoParameterCommand.java | 11 | ||||
-rw-r--r-- | src/org/traccar/command/StringCommandTemplate.java | 54 |
8 files changed, 196 insertions, 0 deletions
diff --git a/src/org/traccar/command/CommandTemplate.java b/src/org/traccar/command/CommandTemplate.java new file mode 100644 index 000000000..cdc9be7ab --- /dev/null +++ b/src/org/traccar/command/CommandTemplate.java @@ -0,0 +1,8 @@ +package org.traccar.command; + +import org.traccar.database.ActiveDevice; +import org.traccar.command.GpsCommand; + +public interface CommandTemplate<T extends GpsCommand> { + Object applyTo(ActiveDevice activeDevice, T command); +} diff --git a/src/org/traccar/command/CommandType.java b/src/org/traccar/command/CommandType.java new file mode 100644 index 000000000..b5f6393b6 --- /dev/null +++ b/src/org/traccar/command/CommandType.java @@ -0,0 +1,28 @@ +package org.traccar.command; + +import org.traccar.model.Factory; + +public enum CommandType implements Factory { + STOP_POSITIONING(NoParameterCommand.class), + FIX_POSITIONING(FixPositioningCommand.class), + STOP_ENGINE(NoParameterCommand.class), + RESUME_ENGINE(NoParameterCommand.class); + + + private Class<? extends GpsCommand> commandClass; + + CommandType(Class<? extends GpsCommand> commandClass) { + this.commandClass = commandClass; + } + + @Override + public Object create() { + try { + return commandClass.newInstance(); + } catch (InstantiationException e) { + throw new RuntimeException(e); + } catch (IllegalAccessException e) { + throw new RuntimeException(e); + } + } +} diff --git a/src/org/traccar/command/CommandValueConversion.java b/src/org/traccar/command/CommandValueConversion.java new file mode 100644 index 000000000..5c8b75313 --- /dev/null +++ b/src/org/traccar/command/CommandValueConversion.java @@ -0,0 +1,5 @@ +package org.traccar.command; + +public interface CommandValueConversion<T> { + public String convert(T value); +} diff --git a/src/org/traccar/command/Duration.java b/src/org/traccar/command/Duration.java new file mode 100644 index 000000000..a2ed2c991 --- /dev/null +++ b/src/org/traccar/command/Duration.java @@ -0,0 +1,38 @@ +package org.traccar.command; + +public class Duration { + + public enum TimeUnit { + SECOND("s"), MINUTE("m"), HOUR("h"); + + private final String commandFormat; + + TimeUnit(String commandFormat) { + this.commandFormat = commandFormat; + } + + public String getCommandFormat() { + return commandFormat; + } + } + + + private TimeUnit unit; + private int value; + + public TimeUnit getUnit() { + return unit; + } + + public void setUnit(TimeUnit unit) { + this.unit = unit; + } + + public int getValue() { + return value; + } + + public void setValue(int value) { + this.value = value; + } +} diff --git a/src/org/traccar/command/FixPositioningCommand.java b/src/org/traccar/command/FixPositioningCommand.java new file mode 100644 index 000000000..1304e1f3f --- /dev/null +++ b/src/org/traccar/command/FixPositioningCommand.java @@ -0,0 +1,25 @@ +package org.traccar.command; + +import java.util.HashMap; +import java.util.Map; + +public class FixPositioningCommand extends GpsCommand { + public static final String FREQUENCY = "frequency"; + + private Duration data; + + @Override + public Map<String, Object> getReplacements() { + Map<String, Object> replacements = new HashMap<String, Object>(); + replacements.put(FREQUENCY, data); + return replacements; + } + + public Duration getData() { + return data; + } + + public void setData(Duration data) { + this.data = data; + } +} diff --git a/src/org/traccar/command/GpsCommand.java b/src/org/traccar/command/GpsCommand.java new file mode 100644 index 000000000..7097c4807 --- /dev/null +++ b/src/org/traccar/command/GpsCommand.java @@ -0,0 +1,27 @@ +package org.traccar.command; + +import java.util.Map; + +public abstract class GpsCommand { + public static final String UNIQUE_ID = "uniqueId"; + private String uniqueId; + private CommandType type; + + public String getUniqueId() { + return uniqueId; + } + + public void setUniqueId(String uniqueId) { + this.uniqueId = uniqueId; + } + + public CommandType getType() { + return type; + } + + public void setType(CommandType type) { + this.type = type; + } + + public abstract Map<String, Object> getReplacements(); +} diff --git a/src/org/traccar/command/NoParameterCommand.java b/src/org/traccar/command/NoParameterCommand.java new file mode 100644 index 000000000..afb0f3aee --- /dev/null +++ b/src/org/traccar/command/NoParameterCommand.java @@ -0,0 +1,11 @@ +package org.traccar.command; + +import java.util.HashMap; +import java.util.Map; + +public class NoParameterCommand extends GpsCommand { + @Override + public Map<String, Object> getReplacements() { + return new HashMap<String, Object>(); + } +} diff --git a/src/org/traccar/command/StringCommandTemplate.java b/src/org/traccar/command/StringCommandTemplate.java new file mode 100644 index 000000000..12470d441 --- /dev/null +++ b/src/org/traccar/command/StringCommandTemplate.java @@ -0,0 +1,54 @@ +package org.traccar.command; + +import org.traccar.database.ActiveDevice; + +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); + } + +} |