aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/org/traccar/BaseProtocol.java2
-rw-r--r--src/org/traccar/model/Command.java1
-rw-r--r--src/org/traccar/protocol/Gps103ProtocolEncoder.java41
3 files changed, 23 insertions, 21 deletions
diff --git a/src/org/traccar/BaseProtocol.java b/src/org/traccar/BaseProtocol.java
index 31f278739..aa98d776c 100644
--- a/src/org/traccar/BaseProtocol.java
+++ b/src/org/traccar/BaseProtocol.java
@@ -42,7 +42,7 @@ public abstract class BaseProtocol implements Protocol {
@Override
public void sendCommand(ActiveDevice activeDevice, Command command) {
if (!supportedCommands.contains(command.getType())) {
- throw new RuntimeException("Command " + command + " is not supported in protocol " + this.getName());
+ throw new RuntimeException("Command " + command + " is not supported in protocol " + getName());
}
activeDevice.write(command);
}
diff --git a/src/org/traccar/model/Command.java b/src/org/traccar/model/Command.java
index 58aa61a7d..f7702dbd0 100644
--- a/src/org/traccar/model/Command.java
+++ b/src/org/traccar/model/Command.java
@@ -27,6 +27,7 @@ public class Command implements Factory {
public static final String TYPE_ENGINE_STOP = "engineStop";
public static final String TYPE_ENGINE_RESUME = "engineResume";
+ public static final String KEY_UNIQUE_ID = "uniqueId";
public static final String KEY_FREQUENCY = "frequency";
}
diff --git a/src/org/traccar/protocol/Gps103ProtocolEncoder.java b/src/org/traccar/protocol/Gps103ProtocolEncoder.java
index d7cddd1ba..27fea8237 100644
--- a/src/org/traccar/protocol/Gps103ProtocolEncoder.java
+++ b/src/org/traccar/protocol/Gps103ProtocolEncoder.java
@@ -21,42 +21,43 @@ import org.traccar.model.Command;
public class Gps103ProtocolEncoder extends BaseProtocolEncoder {
- /*@Override
- protected void initCommandsTemplates(Map<CommandType, CommandTemplate> templates) {
- templates.put(CommandType.FIX_POSITIONING, new StringCommandTemplate("**,imei:[%s],C,[%s]", Command.UNIQUE_ID, FixPositioningCommand.FREQUENCY)
- .addConverter(Duration.class, new CommandValueConversion<Duration>() {
- @Override
- public String convert(Duration value) {
- return String.format("%02d%s", value.getValue(), value.getUnit().getCommandFormat());
- }
- }));
- }*/
-
- private String formatCommand(String format, Command command) {
+ private String formatCommand(Command command, String format, String... keys) {
- String result = format;
+ String result = String.format(format, (Object[]) keys);
- result = result.replaceAll("\\{uniqueId}", getUniqueId(command.getDeviceId()));
+ result = result.replaceAll("\\{" + Command.KEY_UNIQUE_ID + "}", getUniqueId(command.getDeviceId()));
for (Map.Entry<String, Object> entry : command.getOther().entrySet()) {
- result = result.replaceAll("\\{" + entry.getKey() + "}", entry.getValue().toString());
+ String value;
+ if (entry.getKey().equals(Command.KEY_FREQUENCY)) {
+ long frequency = (Long) entry.getValue();
+ if (frequency / 60 / 60 > 0) {
+ value = String.format("%02dh", frequency / 60 / 60);
+ } else if (frequency / 60 > 0) {
+ value = String.format("%02dm", frequency / 60);
+ } else {
+ value = String.format("%02ds", frequency);
+ }
+ } else {
+ value = entry.getValue().toString();
+ }
+ result = result.replaceAll("\\{" + entry.getKey() + "}", value);
}
return result;
}
-
@Override
protected Object encodeCommand(Command command) {
switch (command.getType()) {
case Command.TYPE_POSITION_STOP:
- return formatCommand("**,imei:{uniqueId},A", command);
+ return formatCommand(command, "**,imei:{%s},A", Command.KEY_UNIQUE_ID);
case Command.TYPE_POSITION_FIX:
- return formatCommand("**,imei:{uniqueId},C,{time}", command); // TODO
+ return formatCommand(command, "**,imei:{%s},C,{%s}", Command.KEY_UNIQUE_ID, Command.KEY_FREQUENCY);
case Command.TYPE_ENGINE_STOP:
- return formatCommand("**,imei:{uniqueId},K", command);
+ return formatCommand(command, "**,imei:{%s},K", Command.KEY_UNIQUE_ID);
case Command.TYPE_ENGINE_RESUME:
- return formatCommand("**,imei:{uniqueId},J", command);
+ return formatCommand(command, "**,imei:{%s},J", Command.KEY_UNIQUE_ID);
}
return null;