From 967b513318d4778b7ae5023e48cb544e2e9579a3 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Thu, 25 Feb 2016 23:03:37 +1300 Subject: Implement DTC codes for Ulbotech --- src/org/traccar/helper/ObdDecoder.java | 37 +++++++++++++++++++--- .../traccar/protocol/UlbotechProtocolDecoder.java | 4 +-- test/org/traccar/helper/ObdDecoderTest.java | 10 +++--- 3 files changed, 40 insertions(+), 11 deletions(-) diff --git a/src/org/traccar/helper/ObdDecoder.java b/src/org/traccar/helper/ObdDecoder.java index f52216cae..d847fb7c4 100644 --- a/src/org/traccar/helper/ObdDecoder.java +++ b/src/org/traccar/helper/ObdDecoder.java @@ -18,6 +18,8 @@ package org.traccar.helper; import org.traccar.model.Event; import java.util.AbstractMap; +import java.util.LinkedList; +import java.util.List; import java.util.Map; public final class ObdDecoder { @@ -25,8 +27,9 @@ public final class ObdDecoder { private ObdDecoder() { } - private static final int MODE_CURRENT = 0x01; - private static final int MODE_FREEZE_FRAME = 0x02; + public static final int MODE_CURRENT = 0x01; + public static final int MODE_FREEZE_FRAME = 0x02; + public static final int MODE_CODES = 0x03; private static final int PID_ENGINE_LOAD = 0x04; private static final int PID_COOLANT_TEMPERATURE = 0x05; @@ -37,11 +40,13 @@ public final class ObdDecoder { private static final int PID_FUEL_LEVEL = 0x2F; private static final int PID_DISTANCE_CLEARED = 0x31; - public static Map.Entry decode(int mode, int pid, String value) { + public static Map.Entry decode(int mode, String value) { switch (mode) { case MODE_CURRENT: case MODE_FREEZE_FRAME: - return decodeData(pid, value); + return decodeData(Integer.parseInt(value.substring(0, 2), 16), value.substring(2)); + case MODE_CODES: + return decodeCodes(value); default: return null; } @@ -51,6 +56,30 @@ public final class ObdDecoder { return new AbstractMap.SimpleEntry<>(key, value); } + private static Map.Entry decodeCodes(String value) { + StringBuilder codes = new StringBuilder(); + for (int i = 0; i < value.length() / 4; i++) { + int numValue = Integer.parseInt(value.substring(i * 4, (i + 1) * 4), 16); + codes.append(','); + switch (numValue >> 14) { + case 0: + codes.append('P'); + break; + case 1: + codes.append('C'); + break; + case 2: + codes.append('B'); + break; + case 3: + codes.append('U'); + break; + } + codes.append(String.format("%04X", numValue & 0x3FFF)); + } + return createEntry("dtcs", codes.toString().replaceFirst(",", "")); + } + private static Map.Entry decodeData(int pid, String value) { int intValue = Integer.parseInt(value, 16); switch (pid) { diff --git a/src/org/traccar/protocol/UlbotechProtocolDecoder.java b/src/org/traccar/protocol/UlbotechProtocolDecoder.java index bf0a9cc76..a5c608dd3 100644 --- a/src/org/traccar/protocol/UlbotechProtocolDecoder.java +++ b/src/org/traccar/protocol/UlbotechProtocolDecoder.java @@ -60,8 +60,8 @@ public class UlbotechProtocolDecoder extends BaseProtocolDecoder { while (buf.readerIndex() < end) { int parameterLength = buf.getUnsignedByte(buf.readerIndex()) >> 4; - position.add(ObdDecoder.decode(buf.readUnsignedByte() & 0x0F, buf.readUnsignedByte(), - ChannelBuffers.hexDump(buf.readBytes(parameterLength - 2)))); + int mode = buf.readUnsignedByte() & 0x0F; + position.add(ObdDecoder.decode(mode, ChannelBuffers.hexDump(buf.readBytes(parameterLength - 1)))); } } diff --git a/test/org/traccar/helper/ObdDecoderTest.java b/test/org/traccar/helper/ObdDecoderTest.java index 238f3e0a6..62881b66c 100644 --- a/test/org/traccar/helper/ObdDecoderTest.java +++ b/test/org/traccar/helper/ObdDecoderTest.java @@ -8,11 +8,11 @@ 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()); + Assert.assertEquals(83, ObdDecoder.decode(0x01, "057b").getValue()); + Assert.assertEquals(1225, ObdDecoder.decode(0x01, "0C1324").getValue()); + Assert.assertEquals(20, ObdDecoder.decode(0x01, "0D14").getValue()); + Assert.assertEquals(64050, ObdDecoder.decode(0x01, "31fa32").getValue()); + Assert.assertEquals(25, ObdDecoder.decode(0x01, "2F41").getValue()); } -- cgit v1.2.3