diff options
-rw-r--r-- | src/org/traccar/helper/ChannelBufferTools.java | 20 | ||||
-rw-r--r-- | src/org/traccar/protocol/Jt600ProtocolDecoder.java | 4 | ||||
-rw-r--r-- | test/org/traccar/helper/ChannelBufferToolsTest.java | 11 | ||||
-rw-r--r-- | test/org/traccar/protocol/Jt600ProtocolDecoderTest.java (renamed from test/org/traccar/protocol/Jt600ProtocolDecoderTest1.java) | 2 |
4 files changed, 34 insertions, 3 deletions
diff --git a/src/org/traccar/helper/ChannelBufferTools.java b/src/org/traccar/helper/ChannelBufferTools.java index 9139ed7ae..4f1ebb7c0 100644 --- a/src/org/traccar/helper/ChannelBufferTools.java +++ b/src/org/traccar/helper/ChannelBufferTools.java @@ -15,6 +15,7 @@ */ package org.traccar.helper; +import java.util.Formatter; import org.jboss.netty.buffer.ChannelBuffer; /** @@ -77,4 +78,23 @@ public class ChannelBufferTools { return result; } + /** + * Return hex string + */ + public static String readHexString(ChannelBuffer buf, int length) { + + StringBuilder result = new StringBuilder(); + Formatter formatter = new Formatter(result); + + for (int i = 0; i < length / 2; i++) { + formatter.format("%02x", buf.readByte()); + } + + if (length % 2 == 1) { + int b = buf.getUnsignedByte(buf.readerIndex()); + formatter.format("%01x", b >>> 4); + } + + return result.toString(); + } } diff --git a/src/org/traccar/protocol/Jt600ProtocolDecoder.java b/src/org/traccar/protocol/Jt600ProtocolDecoder.java index 8a2be63fb..11932f8ce 100644 --- a/src/org/traccar/protocol/Jt600ProtocolDecoder.java +++ b/src/org/traccar/protocol/Jt600ProtocolDecoder.java @@ -50,8 +50,8 @@ public class Jt600ProtocolDecoder extends GenericProtocolDecoder { buf.readByte(); // header // Get device by identifier - int id = ChannelBufferTools.readHexInteger(buf, 10); - position.setDeviceId(getDataManager().getDeviceByImei(String.valueOf(id)).getId()); + String id = Long.valueOf(ChannelBufferTools.readHexString(buf, 10)).toString(); + position.setDeviceId(getDataManager().getDeviceByImei(id).getId()); buf.readByte(); // protocol version + data type diff --git a/test/org/traccar/helper/ChannelBufferToolsTest.java b/test/org/traccar/helper/ChannelBufferToolsTest.java index 16ff036b8..792770dab 100644 --- a/test/org/traccar/helper/ChannelBufferToolsTest.java +++ b/test/org/traccar/helper/ChannelBufferToolsTest.java @@ -20,4 +20,15 @@ public class ChannelBufferToolsTest { factory.getBuffer(buf, 0, buf.length), 5); assertEquals(1903, result); } + + @Test + public void testReadHexString() { + byte[] buf = {0x01,(byte)0x90,0x34}; + String result = ChannelBufferTools.readHexString( + factory.getBuffer(buf, 0, buf.length), 5); + assertEquals("01903", result); + + result = Long.valueOf(result).toString(); + assertEquals("1903", result); + } } diff --git a/test/org/traccar/protocol/Jt600ProtocolDecoderTest1.java b/test/org/traccar/protocol/Jt600ProtocolDecoderTest.java index cdf8f1568..c08ca0605 100644 --- a/test/org/traccar/protocol/Jt600ProtocolDecoderTest1.java +++ b/test/org/traccar/protocol/Jt600ProtocolDecoderTest.java @@ -5,7 +5,7 @@ import org.jboss.netty.buffer.ChannelBuffers; import static org.junit.Assert.assertNotNull; import org.junit.Test; -public class Jt600ProtocolDecoderTest1 { +public class Jt600ProtocolDecoderTest { @Test public void testDecode() throws Exception { |