diff options
Diffstat (limited to 'src/org/traccar/helper')
-rw-r--r-- | src/org/traccar/helper/Checksum.java | 13 | ||||
-rw-r--r-- | src/org/traccar/helper/ObdDecoder.java | 39 | ||||
-rw-r--r-- | src/org/traccar/helper/Parser.java | 7 |
3 files changed, 49 insertions, 10 deletions
diff --git a/src/org/traccar/helper/Checksum.java b/src/org/traccar/helper/Checksum.java index e6b6cd2c8..c156e53f7 100644 --- a/src/org/traccar/helper/Checksum.java +++ b/src/org/traccar/helper/Checksum.java @@ -158,13 +158,20 @@ public final class Checksum { public static String nmea(String msg) { int checksum = 0; - byte[] bytes = msg.getBytes(Charset.defaultCharset()); - for (int i = 1; i < msg.length(); i++) { - checksum ^= bytes[i]; + for (byte b : msg.getBytes(Charset.defaultCharset())) { + checksum ^= b; } return String.format("*%02x", checksum).toUpperCase(); } + public static String sum(String msg) { + byte checksum = 0; + for (byte b : msg.getBytes(Charset.defaultCharset())) { + checksum += b; + } + return String.format("%02X", checksum).toUpperCase(); + } + public static long luhn(long imei) { long checksum = 0; long remain = imei; diff --git a/src/org/traccar/helper/ObdDecoder.java b/src/org/traccar/helper/ObdDecoder.java index 35fa4dc07..c2d9b1841 100644 --- a/src/org/traccar/helper/ObdDecoder.java +++ b/src/org/traccar/helper/ObdDecoder.java @@ -1,5 +1,5 @@ /* - * Copyright 2015 Anton Tananaev (anton.tananaev@gmail.com) + * Copyright 2015 - 2016 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. @@ -25,8 +25,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 +38,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<String, Object> decode(int mode, int pid, String value) { + public static Map.Entry<String, Object> 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 +54,30 @@ public final class ObdDecoder { return new AbstractMap.SimpleEntry<>(key, value); } + private static Map.Entry<String, Object> 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 1: + codes.append('C'); + break; + case 2: + codes.append('B'); + break; + case 3: + codes.append('U'); + break; + default: + codes.append('P'); + break; + } + codes.append(String.format("%04X", numValue & 0x3FFF)); + } + return createEntry("dtcs", codes.toString().replaceFirst(",", "")); + } + private static Map.Entry<String, Object> decodeData(int pid, String value) { int intValue = Integer.parseInt(value, 16); switch (pid) { @@ -69,7 +96,7 @@ public final class ObdDecoder { case PID_FUEL_LEVEL: return createEntry(Event.KEY_FUEL, intValue * 100 / 255); case PID_DISTANCE_CLEARED: - return createEntry(Event.KEY_FUEL, intValue); + return createEntry("cleared-distance", intValue); default: return null; } diff --git a/src/org/traccar/helper/Parser.java b/src/org/traccar/helper/Parser.java index a3f118df0..dcea1c8f7 100644 --- a/src/org/traccar/helper/Parser.java +++ b/src/org/traccar/helper/Parser.java @@ -37,12 +37,17 @@ public class Parser { return matcher.find(); } + public void skip(int number) { + position += number; + } + public boolean hasNext() { return hasNext(1); } public boolean hasNext(int number) { - if (matcher.group(position) != null) { + String value = matcher.group(position); + if (value != null && !value.isEmpty()) { return true; } else { position += number; |