aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/org/traccar/StringProtocolEncoder.java51
-rw-r--r--src/org/traccar/protocol/Gps103ProtocolEncoder.java43
-rw-r--r--src/org/traccar/protocol/SuntechProtocol.java7
-rw-r--r--src/org/traccar/protocol/SuntechProtocolEncoder.java39
4 files changed, 114 insertions, 26 deletions
diff --git a/src/org/traccar/StringProtocolEncoder.java b/src/org/traccar/StringProtocolEncoder.java
new file mode 100644
index 000000000..dcb9ccc0e
--- /dev/null
+++ b/src/org/traccar/StringProtocolEncoder.java
@@ -0,0 +1,51 @@
+/*
+ * Copyright 2015 Anton Tananaev (anton.tananaev@gmail.com)
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.traccar;
+
+import org.traccar.model.Command;
+
+import java.util.Map;
+
+public abstract class StringProtocolEncoder extends BaseProtocolEncoder {
+
+ public interface ValueFormatter {
+ public String formatValue(String key, Object value);
+ }
+
+ 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.getOther().entrySet()) {
+ String value = null;
+ if (valueFormatter != null) {
+ value = valueFormatter.formatValue(entry.getKey(), entry.getValue());
+ }
+ if (value == null) {
+ value = entry.getValue().toString();
+ }
+ result = result.replaceAll("\\{" + entry.getKey() + "}", value);
+ }
+
+ return result;
+ }
+
+ protected String formatCommand(Command command, String format, String... keys) {
+ return formatCommand(command, format, null, keys);
+ }
+
+}
diff --git a/src/org/traccar/protocol/Gps103ProtocolEncoder.java b/src/org/traccar/protocol/Gps103ProtocolEncoder.java
index 5d0e0ddd1..9d8c3e9ba 100644
--- a/src/org/traccar/protocol/Gps103ProtocolEncoder.java
+++ b/src/org/traccar/protocol/Gps103ProtocolEncoder.java
@@ -15,37 +15,28 @@
*/
package org.traccar.protocol;
-import java.util.Map;
-import org.traccar.BaseProtocolEncoder;
+import org.traccar.StringProtocolEncoder;
import org.traccar.model.Command;
-public class Gps103ProtocolEncoder extends BaseProtocolEncoder {
+public class Gps103ProtocolEncoder extends StringProtocolEncoder implements StringProtocolEncoder.ValueFormatter {
- private String formatCommand(Command command, String format, 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.getOther().entrySet()) {
- 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);
- }
+ @Override
+ public String formatValue(String key, Object value) {
+
+ if (key.equals(Command.KEY_FREQUENCY)) {
+ long frequency = (Long) value;
+ if (frequency / 60 / 60 > 0) {
+ return String.format("%02dh", frequency / 60 / 60);
+ } else if (frequency / 60 > 0) {
+ return String.format("%02dm", frequency / 60);
} else {
- value = entry.getValue().toString();
+ return String.format("%02ds", frequency);
}
- result = result.replaceAll("\\{" + entry.getKey() + "}", value);
}
-
- return result;
+
+ return null;
}
-
+
@Override
protected Object encodeCommand(Command command) {
@@ -53,7 +44,7 @@ public class Gps103ProtocolEncoder extends BaseProtocolEncoder {
case Command.TYPE_POSITION_STOP:
return formatCommand(command, "**,imei:{%s},A;", Command.KEY_UNIQUE_ID);
case Command.TYPE_POSITION_FIX:
- return formatCommand(command, "**,imei:{%s},C,{%s};", Command.KEY_UNIQUE_ID, Command.KEY_FREQUENCY);
+ return formatCommand(command, "**,imei:{%s},C,{%s};", this, Command.KEY_UNIQUE_ID, Command.KEY_FREQUENCY);
case Command.TYPE_ENGINE_STOP:
return formatCommand(command, "**,imei:{%s},K;", Command.KEY_UNIQUE_ID);
case Command.TYPE_ENGINE_RESUME:
@@ -62,5 +53,5 @@ public class Gps103ProtocolEncoder extends BaseProtocolEncoder {
return null;
}
-
+
}
diff --git a/src/org/traccar/protocol/SuntechProtocol.java b/src/org/traccar/protocol/SuntechProtocol.java
index 630cab971..d3f5d6009 100644
--- a/src/org/traccar/protocol/SuntechProtocol.java
+++ b/src/org/traccar/protocol/SuntechProtocol.java
@@ -18,9 +18,11 @@ package org.traccar.protocol;
import org.jboss.netty.bootstrap.ServerBootstrap;
import org.jboss.netty.channel.ChannelPipeline;
import org.jboss.netty.handler.codec.string.StringDecoder;
+import org.jboss.netty.handler.codec.string.StringEncoder;
import org.traccar.BaseProtocol;
import org.traccar.CharacterDelimiterFrameDecoder;
import org.traccar.TrackerServer;
+import org.traccar.model.Command;
import java.util.List;
@@ -28,6 +30,9 @@ public class SuntechProtocol extends BaseProtocol {
public SuntechProtocol() {
super("suntech");
+ setSupportedCommands(
+ Command.TYPE_ENGINE_STOP,
+ Command.TYPE_ENGINE_STOP);
}
@Override
@@ -37,7 +42,9 @@ public class SuntechProtocol extends BaseProtocol {
protected void addSpecificHandlers(ChannelPipeline pipeline) {
pipeline.addLast("frameDecoder", new CharacterDelimiterFrameDecoder(1024, '\r'));
pipeline.addLast("stringDecoder", new StringDecoder());
+ pipeline.addLast("stringEncoder", new StringEncoder());
pipeline.addLast("objectDecoder", new SuntechProtocolDecoder(SuntechProtocol.this));
+ pipeline.addLast("objectEncoder", new SuntechProtocolEncoder());
}
});
}
diff --git a/src/org/traccar/protocol/SuntechProtocolEncoder.java b/src/org/traccar/protocol/SuntechProtocolEncoder.java
new file mode 100644
index 000000000..7bcb02088
--- /dev/null
+++ b/src/org/traccar/protocol/SuntechProtocolEncoder.java
@@ -0,0 +1,39 @@
+/*
+ * Copyright 2015 Anton Tananaev (anton.tananaev@gmail.com)
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.traccar.protocol;
+
+import org.traccar.BaseProtocolEncoder;
+import org.traccar.StringProtocolEncoder;
+import org.traccar.model.Command;
+
+import java.util.Map;
+
+public class SuntechProtocolEncoder extends StringProtocolEncoder {
+
+ @Override
+ protected Object encodeCommand(Command command) {
+
+ switch (command.getType()) {
+ case Command.TYPE_ENGINE_STOP:
+ return formatCommand(command, "CMD;{%s};02;Enable1\r", Command.KEY_UNIQUE_ID);
+ case Command.TYPE_ENGINE_RESUME:
+ return formatCommand(command, "CMD;{%s};02;Disable1\n", Command.KEY_UNIQUE_ID);
+ }
+
+ return null;
+ }
+
+}