diff options
author | Anton Tananaev <anton.tananaev@gmail.com> | 2015-11-06 11:56:19 +1300 |
---|---|---|
committer | Anton Tananaev <anton.tananaev@gmail.com> | 2015-11-06 11:56:19 +1300 |
commit | 23f83e9f1c6c0f183256a3952a8b3a819dc0ea1c (patch) | |
tree | 6bc1a27981a68e9ac8936f30f6f2a0cd77e09bed | |
parent | 04a40e1cfb9484f3ab25a3390249355bc3e711ae (diff) | |
download | trackermap-server-23f83e9f1c6c0f183256a3952a8b3a819dc0ea1c.tar.gz trackermap-server-23f83e9f1c6c0f183256a3952a8b3a819dc0ea1c.tar.bz2 trackermap-server-23f83e9f1c6c0f183256a3952a8b3a819dc0ea1c.zip |
Finish OBD decoder implementation
-rw-r--r-- | src/org/traccar/helper/ObdDecoder.java | 50 | ||||
-rw-r--r-- | src/org/traccar/model/Extensible.java | 6 | ||||
-rw-r--r-- | src/org/traccar/protocol/UlbotechProtocolDecoder.java | 11 | ||||
-rw-r--r-- | test/org/traccar/helper/ObdDecoderTest.java | 7 | ||||
-rw-r--r-- | test/org/traccar/protocol/UlbotechProtocolDecoderTest.java | 3 |
5 files changed, 69 insertions, 8 deletions
diff --git a/src/org/traccar/helper/ObdDecoder.java b/src/org/traccar/helper/ObdDecoder.java index 63fd30b09..684702bba 100644 --- a/src/org/traccar/helper/ObdDecoder.java +++ b/src/org/traccar/helper/ObdDecoder.java @@ -15,13 +15,61 @@ */ package org.traccar.helper; +import org.traccar.model.Event; + +import java.util.AbstractMap; +import java.util.Map; + public class ObdDecoder { private static final int MODE_CURRENT = 0x01; private static final int MODE_FREEZE_FRAME = 0x02; - private static final int MODE_DTC = 0x03; // diagnostic trouble codes + private static final int PID_ENGINE_LOAD = 0x04; + private static final int PID_COOLANT_TEMPERATURE = 0x05; + private static final int PID_ENGINE_RPM = 0x0C; + private static final int PID_VEHICLE_SPEED = 0x0D; + private static final int PID_THROTTLE_POSITION = 0x11; + private static final int PID_MIL_DISTANCE = 0x21; + private static final int PID_FUEL_LEVEL = 0x2F; + private static final int PID_DISTANCE_CLEARED = 0x31; + + public static Map.Entry<String, Object> decode(int mode, int pid, String value) { + switch (mode) { + case MODE_CURRENT: + case MODE_FREEZE_FRAME: + return decodeData(pid, value); + default: + return null; + } + } + private static Map.Entry<String, Object> createEntry(String key, Object value) { + return new AbstractMap.SimpleEntry<>(key, value); + } + private static Map.Entry<String, Object> decodeData(int pid, String value) { + int intValue = Integer.parseInt(value, 16); + switch (pid) { + case PID_ENGINE_LOAD: + return createEntry("engine-load", intValue * 100 / 255); + case PID_COOLANT_TEMPERATURE: + return createEntry("coolant-temperature", intValue - 40); + case PID_ENGINE_RPM: + return createEntry(Event.KEY_RPM, intValue / 4); + case PID_VEHICLE_SPEED: + return createEntry(Event.KEY_OBD_SPEED, intValue); + case PID_THROTTLE_POSITION: + return createEntry("throttle", intValue * 100 / 255); + case PID_MIL_DISTANCE: + return createEntry("mil-distance", intValue); + case PID_FUEL_LEVEL: + return createEntry(Event.KEY_FUEL, intValue * 100 / 255); + case PID_DISTANCE_CLEARED: + return createEntry(Event.KEY_FUEL, intValue); + default: + return null; + } + } } diff --git a/src/org/traccar/model/Extensible.java b/src/org/traccar/model/Extensible.java index a821d0e43..40a286987 100644 --- a/src/org/traccar/model/Extensible.java +++ b/src/org/traccar/model/Extensible.java @@ -52,4 +52,10 @@ public abstract class Extensible extends Message { } } + public void add(Map.Entry<String, Object> entry) { + if (entry.getValue() != null) { + attributes.put(entry.getKey(), entry.getValue()); + } + } + } diff --git a/src/org/traccar/protocol/UlbotechProtocolDecoder.java b/src/org/traccar/protocol/UlbotechProtocolDecoder.java index a3edfdb07..68d04906d 100644 --- a/src/org/traccar/protocol/UlbotechProtocolDecoder.java +++ b/src/org/traccar/protocol/UlbotechProtocolDecoder.java @@ -24,6 +24,7 @@ import org.jboss.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; import org.traccar.Context; import org.traccar.helper.BitUtil; +import org.traccar.helper.ObdDecoder; import org.traccar.helper.UnitsConverter; import org.traccar.model.Event; import org.traccar.model.Position; @@ -58,10 +59,9 @@ public class UlbotechProtocolDecoder extends BaseProtocolDecoder { int end = buf.readerIndex() + length; while (buf.readerIndex() < end) { - int parameterLength = buf.readUnsignedByte() >> 4; - String key = String.format("pid%02X", buf.readUnsignedByte()); - String value = ChannelBuffers.hexDump(buf.readBytes(parameterLength - 2)); - position.set(key, value); + int parameterLength = buf.getUnsignedByte(buf.readerIndex()) >> 4; + position.add(ObdDecoder.decode(buf.readUnsignedByte() & 0x0F, buf.readUnsignedByte(), + ChannelBuffers.hexDump(buf.readBytes(parameterLength - 2)))); } } @@ -105,18 +105,15 @@ public class UlbotechProtocolDecoder extends BaseProtocolDecoder { buf.readUnsignedByte(); // version buf.readUnsignedByte(); // type - // Create new position Position position = new Position(); position.setProtocol(getProtocolName()); - // Get device id String imei = ChannelBuffers.hexDump(buf.readBytes(8)).substring(1); if (!identify(imei, channel)) { return null; } position.setDeviceId(getDeviceId()); - // Time long seconds = buf.readUnsignedInt() & 0x7fffffffL; seconds += 946684800L; // 2000-01-01 00:00 seconds -= timeZone; diff --git a/test/org/traccar/helper/ObdDecoderTest.java b/test/org/traccar/helper/ObdDecoderTest.java index fb74f5d3c..238f3e0a6 100644 --- a/test/org/traccar/helper/ObdDecoderTest.java +++ b/test/org/traccar/helper/ObdDecoderTest.java @@ -7,6 +7,13 @@ public class ObdDecoderTest { @Test public void testDecode() { + + Assert.assertEquals(83, ObdDecoder.decode(0x01, 0x05, "7b").getValue()); + Assert.assertEquals(1225, ObdDecoder.decode(0x01, 0x0C, "1324").getValue()); + Assert.assertEquals(20, ObdDecoder.decode(0x01, 0x0D, "14").getValue()); + Assert.assertEquals(64050, ObdDecoder.decode(0x01, 0x31, "fa32").getValue()); + Assert.assertEquals(25, ObdDecoder.decode(0x01, 0x2F, "41").getValue()); + } } diff --git a/test/org/traccar/protocol/UlbotechProtocolDecoderTest.java b/test/org/traccar/protocol/UlbotechProtocolDecoderTest.java index 7e321fa8e..30e040e3d 100644 --- a/test/org/traccar/protocol/UlbotechProtocolDecoderTest.java +++ b/test/org/traccar/protocol/UlbotechProtocolDecoderTest.java @@ -13,6 +13,9 @@ public class UlbotechProtocolDecoderTest extends ProtocolDecoderTest { UlbotechProtocolDecoder decoder = new UlbotechProtocolDecoder(new UlbotechProtocol()); verifyPosition(decoder, binary( + "f8010108679650230651689dc8e45b010e01194a26fbd47fa6001f003c0054030402420000040400024d7b0506037c18692212071131057f410c0ee0310d1b312f41413112ef0804000dd59fcc32f8")); + + verifyPosition(decoder, binary( "f8010103596580419465449da89d16010efe5580fe0923d82100140129005903040242000004040001a7f10506037818be220e070e31057b410c1324310d144131fa3208040020b1418297f8")); verifyPosition(decoder, binary( |