aboutsummaryrefslogtreecommitdiff
path: root/src/org/traccar/protocol
diff options
context:
space:
mode:
authorAnton Tananaev <anton.tananaev@gmail.com>2015-07-17 22:51:09 +1200
committerAnton Tananaev <anton.tananaev@gmail.com>2015-07-17 22:51:09 +1200
commit466e704934894ed44458bec798fa9fad0532bb4c (patch)
treeeaf48175234c9d4d1b0456a542887a90c1377d00 /src/org/traccar/protocol
parent6bfaa4bdedcf6719de75afa905a309296aa1bd9d (diff)
parentff640cf8eb19cfeb3c8475ffd9b0aeccbd769f96 (diff)
downloadtraccar-server-466e704934894ed44458bec798fa9fad0532bb4c.tar.gz
traccar-server-466e704934894ed44458bec798fa9fad0532bb4c.tar.bz2
traccar-server-466e704934894ed44458bec798fa9fad0532bb4c.zip
Merge commands implementation (fix #7)
Diffstat (limited to 'src/org/traccar/protocol')
-rw-r--r--src/org/traccar/protocol/Gps103Protocol.java33
-rw-r--r--src/org/traccar/protocol/Gps103ProtocolEncoder.java66
-rw-r--r--src/org/traccar/protocol/GpsMarkerProtocol.java1
-rw-r--r--src/org/traccar/protocol/Gt06Protocol.java60
-rw-r--r--src/org/traccar/protocol/Gt06ProtocolEncoder.java64
5 files changed, 144 insertions, 80 deletions
diff --git a/src/org/traccar/protocol/Gps103Protocol.java b/src/org/traccar/protocol/Gps103Protocol.java
index 84275a556..59d8e62a7 100644
--- a/src/org/traccar/protocol/Gps103Protocol.java
+++ b/src/org/traccar/protocol/Gps103Protocol.java
@@ -15,6 +15,7 @@
*/
package org.traccar.protocol;
+import java.util.List;
import org.jboss.netty.bootstrap.ConnectionlessBootstrap;
import org.jboss.netty.bootstrap.ServerBootstrap;
import org.jboss.netty.channel.ChannelPipeline;
@@ -23,35 +24,17 @@ import org.jboss.netty.handler.codec.string.StringEncoder;
import org.traccar.BaseProtocol;
import org.traccar.CharacterDelimiterFrameDecoder;
import org.traccar.TrackerServer;
-import org.traccar.command.CommandType;
-import org.traccar.command.Duration;
-import org.traccar.command.FixPositioningCommand;
-import org.traccar.command.GpsCommand;
-import org.traccar.command.CommandTemplate;
-import org.traccar.command.CommandValueConversion;
-import org.traccar.command.StringCommandTemplate;
-
-import java.util.List;
-import java.util.Map;
+import org.traccar.model.Command;
public class Gps103Protocol extends BaseProtocol {
public Gps103Protocol() {
super("gps103");
- }
-
- @Override
- protected void initCommandsTemplates(Map<CommandType, CommandTemplate> templates) {
- templates.put(CommandType.STOP_POSITIONING, new StringCommandTemplate("**,imei:[%s],A", GpsCommand.UNIQUE_ID));
- templates.put(CommandType.FIX_POSITIONING, new StringCommandTemplate("**,imei:[%s],C,[%s]", GpsCommand.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());
- }
- }));
- templates.put(CommandType.RESUME_ENGINE, new StringCommandTemplate("**,imei:[%s],J", GpsCommand.UNIQUE_ID));
- templates.put(CommandType.STOP_ENGINE, new StringCommandTemplate("**,imei:[%s],K", GpsCommand.UNIQUE_ID));
+ setSupportedCommands(
+ Command.TYPE_POSITION_STOP,
+ Command.TYPE_POSITION_FIX,
+ Command.TYPE_ENGINE_STOP,
+ Command.TYPE_ENGINE_RESUME);
}
@Override
@@ -63,6 +46,7 @@ public class Gps103Protocol extends BaseProtocol {
pipeline.addLast("stringDecoder", new StringDecoder());
pipeline.addLast("stringEncoder", new StringEncoder());
pipeline.addLast("objectDecoder", new Gps103ProtocolDecoder(Gps103Protocol.this));
+ pipeline.addLast("objectEncoder", new Gps103ProtocolEncoder());
}
});
serverList.add(new TrackerServer(new ConnectionlessBootstrap(), this.getName()) {
@@ -71,6 +55,7 @@ public class Gps103Protocol extends BaseProtocol {
pipeline.addLast("stringDecoder", new StringDecoder());
pipeline.addLast("stringEncoder", new StringEncoder());
pipeline.addLast("objectDecoder", new Gps103ProtocolDecoder(Gps103Protocol.this));
+ pipeline.addLast("objectEncoder", new Gps103ProtocolEncoder());
}
});
}
diff --git a/src/org/traccar/protocol/Gps103ProtocolEncoder.java b/src/org/traccar/protocol/Gps103ProtocolEncoder.java
new file mode 100644
index 000000000..27fea8237
--- /dev/null
+++ b/src/org/traccar/protocol/Gps103ProtocolEncoder.java
@@ -0,0 +1,66 @@
+/*
+ * 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 java.util.Map;
+import org.traccar.BaseProtocolEncoder;
+import org.traccar.model.Command;
+
+public class Gps103ProtocolEncoder extends BaseProtocolEncoder {
+
+ 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);
+ }
+ } 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(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);
+ case Command.TYPE_ENGINE_STOP:
+ return formatCommand(command, "**,imei:{%s},K", Command.KEY_UNIQUE_ID);
+ case Command.TYPE_ENGINE_RESUME:
+ return formatCommand(command, "**,imei:{%s},J", Command.KEY_UNIQUE_ID);
+ }
+
+ return null;
+ }
+
+}
diff --git a/src/org/traccar/protocol/GpsMarkerProtocol.java b/src/org/traccar/protocol/GpsMarkerProtocol.java
index 0ef8cad70..7961e2340 100644
--- a/src/org/traccar/protocol/GpsMarkerProtocol.java
+++ b/src/org/traccar/protocol/GpsMarkerProtocol.java
@@ -23,7 +23,6 @@ import org.jboss.netty.handler.codec.string.StringEncoder;
import org.traccar.BaseProtocol;
import org.traccar.CharacterDelimiterFrameDecoder;
import org.traccar.TrackerServer;
-import org.traccar.command.*;
import java.util.List;
import java.util.Map;
diff --git a/src/org/traccar/protocol/Gt06Protocol.java b/src/org/traccar/protocol/Gt06Protocol.java
index d4bd48288..0c59f3a0c 100644
--- a/src/org/traccar/protocol/Gt06Protocol.java
+++ b/src/org/traccar/protocol/Gt06Protocol.java
@@ -15,31 +15,20 @@
*/
package org.traccar.protocol;
+import java.util.List;
import org.jboss.netty.bootstrap.ServerBootstrap;
-import org.jboss.netty.buffer.ChannelBuffer;
-import org.jboss.netty.buffer.ChannelBuffers;
import org.jboss.netty.channel.ChannelPipeline;
import org.traccar.BaseProtocol;
import org.traccar.TrackerServer;
-import org.traccar.database.ActiveDevice;
-import org.traccar.helper.Crc;
-import org.traccar.command.CommandType;
-import org.traccar.command.NoParameterCommand;
-import org.traccar.command.CommandTemplate;
-
-import java.util.List;
-import java.util.Map;
+import org.traccar.model.Command;
public class Gt06Protocol extends BaseProtocol {
public Gt06Protocol() {
super("gt06");
- }
-
- @Override
- protected void initCommandsTemplates(Map<CommandType, CommandTemplate> templates) {
- templates.put(CommandType.STOP_ENGINE, new Gt06CommandTemplate("DYD#"));
- templates.put(CommandType.RESUME_ENGINE, new Gt06CommandTemplate("HFYD#"));
+ setSupportedCommands(
+ Command.TYPE_ENGINE_STOP,
+ Command.TYPE_ENGINE_STOP);
}
@Override
@@ -53,43 +42,4 @@ public class Gt06Protocol extends BaseProtocol {
});
}
- class Gt06CommandTemplate implements CommandTemplate<NoParameterCommand> {
-
- private String commandContent;
-
- public Gt06CommandTemplate(String commandContent) {
- this.commandContent = commandContent;
- }
-
- @Override
- public Object applyTo(ActiveDevice activeDevice, NoParameterCommand command) {
- int serverFlagBit = 0x0;
- int commandLength = serverFlagBit + commandContent.length();
- int packetLength = 0x80 /*Protocol Number */ + commandContent.length() /* Information Content */ + 2 /*Information Serial Number */+ 2 /*Error Check */;
-
- int informationSerialNumber = 1;
-
-
- ChannelBuffer response = ChannelBuffers.directBuffer(10);
- response.writeBytes(new byte[]{0x78, 0x78}); // Start Bit
- response.writeByte(packetLength); // Packet Length
- response.writeByte(0x80); // Protocol Number
-
- // Information Content
- response.writeByte(commandLength); // Length of command
- response.writeByte(serverFlagBit); // Server Flag Bit
- response.writeBytes(commandContent.getBytes()); // Command Content
- response.writeBytes(new byte[]{0x00, 0x02}); // Language
-
- response.writeShort(informationSerialNumber); // Information Serial Number
-
- int crc = Crc.crc16Ccitt(response.toByteBuffer(2, response.writerIndex()));
- response.writeShort(crc); // Error Check
-
- response.writeBytes(new byte[] {0x0D, 0x0A}); // Stop Bit
-
- return response;
- }
- }
-
}
diff --git a/src/org/traccar/protocol/Gt06ProtocolEncoder.java b/src/org/traccar/protocol/Gt06ProtocolEncoder.java
new file mode 100644
index 000000000..b8177d60b
--- /dev/null
+++ b/src/org/traccar/protocol/Gt06ProtocolEncoder.java
@@ -0,0 +1,64 @@
+/*
+ * 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.jboss.netty.buffer.ChannelBuffer;
+import org.jboss.netty.buffer.ChannelBuffers;
+import org.traccar.BaseProtocolEncoder;
+import org.traccar.helper.Crc;
+import org.traccar.model.Command;
+
+public class Gt06ProtocolEncoder extends BaseProtocolEncoder {
+
+ @Override
+ protected Object encodeCommand(Command command) {
+
+ String content = "";
+ switch (command.getType()) {
+ case Command.TYPE_ENGINE_STOP:
+ content = "DYD#";
+ break;
+ case Command.TYPE_ENGINE_RESUME:
+ content = "HFYD#";
+ break;
+ }
+
+ int serverFlagBit = 0x00;
+ int commandLength = serverFlagBit + content.length();
+ int packetLength = 0x80 + content.length() + 2 + 2;
+
+ ChannelBuffer response = ChannelBuffers.directBuffer(10);
+ response.writeBytes(new byte[]{0x78, 0x78}); // Start Bit
+ response.writeByte(packetLength); // Packet Length
+ response.writeByte(0x80); // Protocol Number
+
+ // Information Content
+ response.writeByte(commandLength); // Length of command
+ response.writeByte(serverFlagBit); // Server Flag Bit
+ response.writeBytes(content.getBytes()); // Command Content
+ response.writeBytes(new byte[]{0x00, 0x02}); // Language
+
+ response.writeShort(1); // Information Serial Number
+
+ int crc = Crc.crc16Ccitt(response.toByteBuffer(2, response.writerIndex()));
+ response.writeShort(crc); // Error Check
+
+ response.writeBytes(new byte[] {0x0D, 0x0A}); // Stop Bit
+
+ return response;
+ }
+
+}