aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAnton Tananaev <anton.tananaev@gmail.com>2016-09-16 17:36:45 +1200
committerAnton Tananaev <anton.tananaev@gmail.com>2016-09-16 17:36:45 +1200
commitd18e293a891c3ef1f3ab2ff9a3b663320a7f4f0b (patch)
treea2d04818c74fec3b8e2e213d3dd9947552c4629b /src
parent17f9739cf7f9db0ac974598b76eee1d3082174f4 (diff)
downloadtraccar-server-d18e293a891c3ef1f3ab2ff9a3b663320a7f4f0b.tar.gz
traccar-server-d18e293a891c3ef1f3ab2ff9a3b663320a7f4f0b.tar.bz2
traccar-server-d18e293a891c3ef1f3ab2ff9a3b663320a7f4f0b.zip
Implement SinoCastel OBD decoding
Diffstat (limited to 'src')
-rw-r--r--src/org/traccar/helper/ObdDecoder.java23
-rw-r--r--src/org/traccar/protocol/CastelProtocolDecoder.java130
2 files changed, 123 insertions, 30 deletions
diff --git a/src/org/traccar/helper/ObdDecoder.java b/src/org/traccar/helper/ObdDecoder.java
index 6ffe39662..8383a2b1a 100644
--- a/src/org/traccar/helper/ObdDecoder.java
+++ b/src/org/traccar/helper/ObdDecoder.java
@@ -42,7 +42,9 @@ public final class ObdDecoder {
switch (mode) {
case MODE_CURRENT:
case MODE_FREEZE_FRAME:
- return decodeData(Integer.parseInt(value.substring(0, 2), 16), value.substring(2));
+ return decodeData(
+ Integer.parseInt(value.substring(0, 2), 16),
+ Integer.parseInt(value.substring(2), 16));
case MODE_CODES:
return decodeCodes(value);
default:
@@ -82,25 +84,24 @@ public final class ObdDecoder {
}
}
- private static Map.Entry<String, Object> decodeData(int pid, String value) {
- int intValue = Integer.parseInt(value, 16);
+ public static Map.Entry<String, Object> decodeData(int pid, int value) {
switch (pid) {
case PID_ENGINE_LOAD:
- return createEntry("engineLoad", intValue * 100 / 255);
+ return createEntry("engineLoad", value * 100 / 255);
case PID_COOLANT_TEMPERATURE:
- return createEntry("coolantTemperature", intValue - 40);
+ return createEntry("coolantTemperature", value - 40);
case PID_ENGINE_RPM:
- return createEntry(Position.KEY_RPM, intValue / 4);
+ return createEntry(Position.KEY_RPM, value / 4);
case PID_VEHICLE_SPEED:
- return createEntry(Position.KEY_OBD_SPEED, intValue);
+ return createEntry(Position.KEY_OBD_SPEED, value);
case PID_THROTTLE_POSITION:
- return createEntry("throttle", intValue * 100 / 255);
+ return createEntry("throttle", value * 100 / 255);
case PID_MIL_DISTANCE:
- return createEntry("milDistance", intValue);
+ return createEntry("milDistance", value);
case PID_FUEL_LEVEL:
- return createEntry(Position.KEY_FUEL, intValue * 100 / 255);
+ return createEntry(Position.KEY_FUEL, value * 100 / 255);
case PID_DISTANCE_CLEARED:
- return createEntry("clearedDistance", intValue);
+ return createEntry("clearedDistance", value);
default:
return null;
}
diff --git a/src/org/traccar/protocol/CastelProtocolDecoder.java b/src/org/traccar/protocol/CastelProtocolDecoder.java
index b8faed077..ee5fac5e6 100644
--- a/src/org/traccar/protocol/CastelProtocolDecoder.java
+++ b/src/org/traccar/protocol/CastelProtocolDecoder.java
@@ -29,11 +29,46 @@ import org.traccar.model.Position;
import java.net.SocketAddress;
import java.nio.ByteOrder;
import java.nio.charset.StandardCharsets;
+import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
+import java.util.Map;
public class CastelProtocolDecoder extends BaseProtocolDecoder {
+ private static final Map<Integer, Integer> PID_LENGTH_MAP = new HashMap<>();
+
+ static {
+ int[] l1 = {
+ 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0b, 0x0d,
+ 0x0e, 0x0f, 0x11, 0x12, 0x13, 0x1c, 0x1d, 0x1e, 0x2c,
+ 0x2d, 0x2e, 0x2f, 0x30, 0x33, 0x43, 0x45, 0x46,
+ 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x51, 0x52,
+ 0x5a
+ };
+ int[] l2 = {
+ 0x02, 0x03, 0x0a, 0x0c, 0x10, 0x14, 0x15, 0x16,
+ 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1f, 0x21, 0x22,
+ 0x23, 0x31, 0x32, 0x3c, 0x3d, 0x3e, 0x3f, 0x42,
+ 0x44, 0x4d, 0x4e, 0x50, 0x53, 0x54, 0x55, 0x56,
+ 0x57, 0x58, 0x59
+ };
+ int[] l4 = {
+ 0x00, 0x01, 0x20, 0x24, 0x25, 0x26, 0x27, 0x28,
+ 0x29, 0x2a, 0x2b, 0x34, 0x35, 0x36, 0x37, 0x38,
+ 0x39, 0x3a, 0x3b, 0x40, 0x41, 0x4f
+ };
+ for (int i : l1) {
+ PID_LENGTH_MAP.put(i, 1);
+ }
+ for (int i : l2) {
+ PID_LENGTH_MAP.put(i, 2);
+ }
+ for (int i : l4) {
+ PID_LENGTH_MAP.put(i, 4);
+ }
+ }
+
public CastelProtocolDecoder(CastelProtocol protocol) {
super(protocol);
}
@@ -91,6 +126,63 @@ public class CastelProtocolDecoder extends BaseProtocolDecoder {
return position;
}
+ private Position createPosition(DeviceSession deviceSession) {
+
+ Position position = new Position();
+ position.setProtocol(getProtocolName());
+ position.setDeviceId(deviceSession.getDeviceId());
+
+ getLastLocation(position, null);
+
+ return position;
+ }
+
+ private void decodeObd(Position position, ChannelBuffer buf, boolean groups) {
+
+ int count = buf.readUnsignedByte();
+
+ int[] pids = new int[count];
+ for (int i = 0; i < count; i++) {
+ pids[i] = buf.readUnsignedShort() & 0xff;
+ }
+
+ if (groups) {
+ buf.readUnsignedByte(); // group count
+ buf.readUnsignedByte(); // group size
+ }
+
+ for (int i = 0; i < count; i++) {
+ int value;
+ switch (PID_LENGTH_MAP.get(pids[i])) {
+ case 1:
+ value = buf.readUnsignedByte();
+ break;
+ case 2:
+ value = buf.readUnsignedShort();
+ break;
+ case 4:
+ value = buf.readInt();
+ break;
+ default:
+ value = 0;
+ break;
+ }
+ position.add(ObdDecoder.decodeData(pids[i], value));
+ }
+ }
+
+ private void decodeStat(Position position, ChannelBuffer buf) {
+
+ buf.readUnsignedInt(); // ACC ON time
+ buf.readUnsignedInt(); // UTC time
+ position.set(Position.KEY_ODOMETER, buf.readUnsignedInt());
+ buf.readUnsignedInt(); // trip odometer
+ buf.readUnsignedInt(); // total fuel consumption
+ buf.readUnsignedShort(); // current fuel consumption
+ position.set(Position.KEY_STATUS, buf.readUnsignedInt());
+ buf.skipBytes(8);
+ }
+
private void sendResponse(
Channel channel, SocketAddress remoteAddress,
int version, ChannelBuffer id, short type, ChannelBuffer content) {
@@ -200,15 +292,22 @@ public class CastelProtocolDecoder extends BaseProtocolDecoder {
return readPosition(deviceSession, buf);
- } else if (type == MSG_SC_DTCS_PASSENGER) {
+ } else if (type == MSG_SC_PID_DATA) {
- Position position = new Position();
- position.setProtocol(getProtocolName());
- position.setDeviceId(deviceSession.getDeviceId());
+ Position position = createPosition(deviceSession);
- getLastLocation(position, null);
+ decodeStat(position, buf);
- buf.skipBytes(6 * 4 + 2 + 8);
+ buf.readUnsignedShort(); // sample rate
+ decodeObd(position, buf, true);
+
+ return position;
+
+ } else if (type == MSG_SC_DTCS_PASSENGER) {
+
+ Position position = createPosition(deviceSession);
+
+ decodeStat(position, buf);
buf.readUnsignedByte(); // flag
position.add(ObdDecoder.decodeCodes(ChannelBuffers.hexDump(buf.readBytes(buf.readUnsignedByte()))));
@@ -217,27 +316,20 @@ public class CastelProtocolDecoder extends BaseProtocolDecoder {
} else if (type == MSG_SC_OBD_DATA) {
- Position position = new Position();
- position.setProtocol(getProtocolName());
- position.setDeviceId(deviceSession.getDeviceId());
-
- getLastLocation(position, null);
+ Position position = createPosition(deviceSession);
- buf.skipBytes(6 * 4 + 2 + 8);
+ decodeStat(position, buf);
- // decode data
+ buf.readUnsignedByte(); // flag
+ decodeObd(position, buf, false);
return position;
} else if (type == MSG_SC_CELL) {
- Position position = new Position();
- position.setProtocol(getProtocolName());
- position.setDeviceId(deviceSession.getDeviceId());
-
- getLastLocation(position, null);
+ Position position = createPosition(deviceSession);
- buf.skipBytes(6 * 4 + 2 + 8);
+ decodeStat(position, buf);
position.set(Position.KEY_LAC, buf.readUnsignedShort());
position.set(Position.KEY_CID, buf.readUnsignedShort());