From abbe79284a3c78125fc655329a0656428dcb2920 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Mon, 20 Jan 2014 23:29:11 +1300 Subject: Extend meiligao id to IMEI --- src/org/traccar/helper/Crc.java | 17 +++++++++++++++++ src/org/traccar/protocol/MeiligaoProtocolDecoder.java | 15 ++++++--------- .../traccar/protocol/MeiligaoProtocolDecoderTest.java | 3 +++ 3 files changed, 26 insertions(+), 9 deletions(-) diff --git a/src/org/traccar/helper/Crc.java b/src/org/traccar/helper/Crc.java index 5a668f30a..213d1192a 100644 --- a/src/org/traccar/helper/Crc.java +++ b/src/org/traccar/helper/Crc.java @@ -154,4 +154,21 @@ public class Crc { return String.format("*%02x", checksum).toUpperCase(); } + public static String luhnChecksum(String s) { + int sum = 0; + boolean evenPosition = true; + for (int i = s.length() - 1; i >= 0; i--) { + int n = Integer.parseInt(s.substring(i, i + 1)); + if (evenPosition) { + n *= 2; + if (n > 9) { + n = (n % 10) + 1; + } + } + sum += n; + evenPosition = !evenPosition; + } + return String.valueOf(10 - sum % 10); + } + } diff --git a/src/org/traccar/protocol/MeiligaoProtocolDecoder.java b/src/org/traccar/protocol/MeiligaoProtocolDecoder.java index b28b38dc8..7ad91a807 100644 --- a/src/org/traccar/protocol/MeiligaoProtocolDecoder.java +++ b/src/org/traccar/protocol/MeiligaoProtocolDecoder.java @@ -1,5 +1,5 @@ /* - * Copyright 2012 - 2013 Anton Tananaev (anton.tananaev@gmail.com) + * Copyright 2012 - 2014 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. @@ -60,10 +60,7 @@ public class MeiligaoProtocolDecoder extends BaseProtocolDecoder { "(?:\\|([0-9a-fA-F]+))?" + // Milage ".*"); // TODO: parse ADC - /** - * Parse device id - */ - private String getId(ChannelBuffer buf) { + private String getImei(ChannelBuffer buf) { String id = ""; for (int i = 0; i < 7; i++) { @@ -80,7 +77,7 @@ public class MeiligaoProtocolDecoder extends BaseProtocolDecoder { id += d2; } - return id; + return id + Crc.luhnChecksum(id); } @Override @@ -135,11 +132,11 @@ public class MeiligaoProtocolDecoder extends BaseProtocolDecoder { } // Get device by id - String id = getId(buf); + String imei = getImei(buf); try { - position.setDeviceId(getDataManager().getDeviceByImei(id).getId()); + position.setDeviceId(getDataManager().getDeviceByImei(imei).getId()); } catch(Exception error) { - Log.warning("Unknown device - " + id); + Log.warning("Unknown device - " + imei); return null; } diff --git a/test/org/traccar/protocol/MeiligaoProtocolDecoderTest.java b/test/org/traccar/protocol/MeiligaoProtocolDecoderTest.java index 0580b998d..2c281ccfd 100644 --- a/test/org/traccar/protocol/MeiligaoProtocolDecoderTest.java +++ b/test/org/traccar/protocol/MeiligaoProtocolDecoderTest.java @@ -56,6 +56,9 @@ public class MeiligaoProtocolDecoderTest { int[] buf13 = {0x10,0x07,0xff,0xff,0xff,0xff,0xff,0x99,0x55,0x31,0x36,0x32,0x33,0x30,0x39,0x2e,0x30,0x35,0x34,0x2c,0x56,0x2c,0x30,0x39,0x33,0x31,0x2e,0x39,0x31,0x36,0x33,0x2c,0x4e,0x2c,0x30,0x36,0x39,0x31,0x31,0x2e,0x38,0x32,0x33,0x33,0x2c,0x57,0x2c,0x2c,0x2c,0x32,0x35,0x31,0x31,0x31,0x33,0x2c,0x2c,0x2c,0x4e,0x2a,0x36,0x43,0x7c,0x7c,0x31,0x35,0x38,0x7c,0x30,0x30,0x30,0x30,0x9c,0xc6,0x0d,0x0a}; verify(decoder.decode(null, null, ChannelBuffers.wrappedBuffer(ChannelBufferTools.convertArray(buf13)))); + + int[] buf14 = {0x35,0x63,0x07,0x04,0x35,0x65,0x20,0x99,0x55,0x30,0x35,0x32,0x30,0x34,0x32,0x2e,0x30,0x30,0x30,0x2c,0x41,0x2c,0x34,0x34,0x35,0x38,0x2e,0x33,0x35,0x36,0x35,0x2c,0x4e,0x2c,0x30,0x34,0x31,0x30,0x34,0x2e,0x34,0x38,0x31,0x33,0x2c,0x45,0x2c,0x30,0x2e,0x30,0x30,0x2c,0x30,0x2e,0x30,0x30,0x2c,0x31,0x39,0x30,0x31,0x31,0x34,0x2c,0x2c,0x2a,0x39,0x43,0x7c,0x30,0x2e,0x37,0x30,0x30,0x30,0x30,0x30,0x7c,0x2d,0x38,0x35,0x39,0x31,0x31,0x37,0x33,0x37,0x36,0x7c,0x30,0x31,0x30,0x30,0x7c,0x30,0x7c,0x7c,0x7c,0x4f,0x2a,0x0d,0x0a}; + verify(decoder.decode(null, null, ChannelBuffers.wrappedBuffer(ChannelBufferTools.convertArray(buf14)))); } -- cgit v1.2.3