aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/org/traccar/helper/BcdUtil.java (renamed from src/org/traccar/helper/ChannelBufferTools.java)19
-rw-r--r--src/org/traccar/protocol/GatorProtocolDecoder.java22
-rw-r--r--src/org/traccar/protocol/H02ProtocolDecoder.java22
-rw-r--r--src/org/traccar/protocol/HuabaoProtocolDecoder.java14
-rw-r--r--src/org/traccar/protocol/Jt600ProtocolDecoder.java22
-rw-r--r--src/org/traccar/protocol/KhdProtocolDecoder.java22
-rw-r--r--src/org/traccar/protocol/T800xProtocolDecoder.java18
-rw-r--r--test/org/traccar/helper/BcdUtilTest.java (renamed from test/org/traccar/helper/ChannelBufferToolsTest.java)8
-rw-r--r--test/org/traccar/protocol/CityeasyProtocolEncoderTest.java5
-rw-r--r--test/org/traccar/protocol/Gt06ProtocolEncoderTest.java5
-rw-r--r--test/org/traccar/protocol/KhdProtocolEncoderTest.java5
-rw-r--r--test/org/traccar/protocol/TramigoProtocolDecoderTest.java2
12 files changed, 71 insertions, 93 deletions
diff --git a/src/org/traccar/helper/ChannelBufferTools.java b/src/org/traccar/helper/BcdUtil.java
index ad2369bed..a248daf22 100644
--- a/src/org/traccar/helper/ChannelBufferTools.java
+++ b/src/org/traccar/helper/BcdUtil.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2012 Anton Tananaev (anton.tananaev@gmail.com)
+ * Copyright 2012 - 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.
@@ -17,19 +17,15 @@ package org.traccar.helper;
import org.jboss.netty.buffer.ChannelBuffer;
-public final class ChannelBufferTools {
+public final class BcdUtil {
- private ChannelBufferTools() {
+ private BcdUtil() {
}
- /**
- * Convert hex to integer (length in hex digits)
- */
- public static int readHexInteger(ChannelBuffer buf, int length) {
-
+ public static int readInteger(ChannelBuffer buf, int digits) {
int result = 0;
- for (int i = 0; i < length / 2; i++) {
+ for (int i = 0; i < digits / 2; i++) {
int b = buf.readUnsignedByte();
result *= 10;
result += b >>> 4;
@@ -37,7 +33,7 @@ public final class ChannelBufferTools {
result += b & 0x0f;
}
- if (length % 2 != 0) {
+ if (digits % 2 != 0) {
int b = buf.getUnsignedByte(buf.readerIndex());
result *= 10;
result += b >>> 4;
@@ -46,9 +42,6 @@ public final class ChannelBufferTools {
return result;
}
- /**
- * Read BCD coded coordinate (first byte has sign bit)
- */
public static double readCoordinate(ChannelBuffer buf) {
int b1 = buf.readUnsignedByte();
int b2 = buf.readUnsignedByte();
diff --git a/src/org/traccar/protocol/GatorProtocolDecoder.java b/src/org/traccar/protocol/GatorProtocolDecoder.java
index 22b3ebc3a..e28da82ba 100644
--- a/src/org/traccar/protocol/GatorProtocolDecoder.java
+++ b/src/org/traccar/protocol/GatorProtocolDecoder.java
@@ -19,7 +19,7 @@ import java.net.SocketAddress;
import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.channel.Channel;
import org.traccar.BaseProtocolDecoder;
-import org.traccar.helper.ChannelBufferTools;
+import org.traccar.helper.BcdUtil;
import org.traccar.helper.DateBuilder;
import org.traccar.helper.UnitsConverter;
import org.traccar.model.Event;
@@ -80,18 +80,18 @@ public class GatorProtocolDecoder extends BaseProtocolDecoder {
position.setDeviceId(getDeviceId());
DateBuilder dateBuilder = new DateBuilder()
- .setYear(ChannelBufferTools.readHexInteger(buf, 2))
- .setMonth(ChannelBufferTools.readHexInteger(buf, 2))
- .setDay(ChannelBufferTools.readHexInteger(buf, 2))
- .setHour(ChannelBufferTools.readHexInteger(buf, 2))
- .setMinute(ChannelBufferTools.readHexInteger(buf, 2))
- .setSecond(ChannelBufferTools.readHexInteger(buf, 2));
+ .setYear(BcdUtil.readInteger(buf, 2))
+ .setMonth(BcdUtil.readInteger(buf, 2))
+ .setDay(BcdUtil.readInteger(buf, 2))
+ .setHour(BcdUtil.readInteger(buf, 2))
+ .setMinute(BcdUtil.readInteger(buf, 2))
+ .setSecond(BcdUtil.readInteger(buf, 2));
position.setTime(dateBuilder.getDate());
- position.setLatitude(ChannelBufferTools.readCoordinate(buf));
- position.setLongitude(ChannelBufferTools.readCoordinate(buf));
- position.setSpeed(UnitsConverter.knotsFromKph(ChannelBufferTools.readHexInteger(buf, 4)));
- position.setCourse(ChannelBufferTools.readHexInteger(buf, 4));
+ position.setLatitude(BcdUtil.readCoordinate(buf));
+ position.setLongitude(BcdUtil.readCoordinate(buf));
+ position.setSpeed(UnitsConverter.knotsFromKph(BcdUtil.readInteger(buf, 4)));
+ position.setCourse(BcdUtil.readInteger(buf, 4));
int flags = buf.readUnsignedByte();
position.setValid((flags & 0x80) != 0);
diff --git a/src/org/traccar/protocol/H02ProtocolDecoder.java b/src/org/traccar/protocol/H02ProtocolDecoder.java
index c4ba6ffa3..b316d8016 100644
--- a/src/org/traccar/protocol/H02ProtocolDecoder.java
+++ b/src/org/traccar/protocol/H02ProtocolDecoder.java
@@ -22,8 +22,8 @@ import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.buffer.ChannelBuffers;
import org.jboss.netty.channel.Channel;
import org.traccar.BaseProtocolDecoder;
+import org.traccar.helper.BcdUtil;
import org.traccar.helper.BitUtil;
-import org.traccar.helper.ChannelBufferTools;
import org.traccar.helper.DateBuilder;
import org.traccar.helper.Parser;
import org.traccar.helper.PatternBuilder;
@@ -38,7 +38,7 @@ public class H02ProtocolDecoder extends BaseProtocolDecoder {
private static double readCoordinate(ChannelBuffer buf, boolean lon) {
- int degrees = ChannelBufferTools.readHexInteger(buf, 2);
+ int degrees = BcdUtil.readInteger(buf, 2);
if (lon) {
degrees = degrees * 10 + (buf.getUnsignedByte(buf.readerIndex()) >> 4);
}
@@ -53,7 +53,7 @@ public class H02ProtocolDecoder extends BaseProtocolDecoder {
length = 5;
}
- result = result * 10 + ChannelBufferTools.readHexInteger(buf, length) * 0.0001;
+ result = result * 10 + BcdUtil.readInteger(buf, length) * 0.0001;
result /= 60;
result += degrees;
@@ -83,12 +83,12 @@ public class H02ProtocolDecoder extends BaseProtocolDecoder {
position.setDeviceId(getDeviceId());
DateBuilder dateBuilder = new DateBuilder()
- .setHour(ChannelBufferTools.readHexInteger(buf, 2))
- .setMinute(ChannelBufferTools.readHexInteger(buf, 2))
- .setSecond(ChannelBufferTools.readHexInteger(buf, 2))
- .setDay(ChannelBufferTools.readHexInteger(buf, 2))
- .setMonth(ChannelBufferTools.readHexInteger(buf, 2))
- .setYear(ChannelBufferTools.readHexInteger(buf, 2));
+ .setHour(BcdUtil.readInteger(buf, 2))
+ .setMinute(BcdUtil.readInteger(buf, 2))
+ .setSecond(BcdUtil.readInteger(buf, 2))
+ .setDay(BcdUtil.readInteger(buf, 2))
+ .setMonth(BcdUtil.readInteger(buf, 2))
+ .setYear(BcdUtil.readInteger(buf, 2));
position.setTime(dateBuilder.getDate());
double latitude = readCoordinate(buf, false);
@@ -107,8 +107,8 @@ public class H02ProtocolDecoder extends BaseProtocolDecoder {
position.setLatitude(latitude);
position.setLongitude(longitude);
- position.setSpeed(ChannelBufferTools.readHexInteger(buf, 3));
- position.setCourse((buf.readUnsignedByte() & 0x0f) * 100.0 + ChannelBufferTools.readHexInteger(buf, 2));
+ position.setSpeed(BcdUtil.readInteger(buf, 3));
+ position.setCourse((buf.readUnsignedByte() & 0x0f) * 100.0 + BcdUtil.readInteger(buf, 2));
processStatus(position, buf.readUnsignedInt());
return position;
diff --git a/src/org/traccar/protocol/HuabaoProtocolDecoder.java b/src/org/traccar/protocol/HuabaoProtocolDecoder.java
index a9a4741fc..6ed490c1a 100644
--- a/src/org/traccar/protocol/HuabaoProtocolDecoder.java
+++ b/src/org/traccar/protocol/HuabaoProtocolDecoder.java
@@ -19,8 +19,8 @@ import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.buffer.ChannelBuffers;
import org.jboss.netty.channel.Channel;
import org.traccar.BaseProtocolDecoder;
+import org.traccar.helper.BcdUtil;
import org.traccar.helper.BitUtil;
-import org.traccar.helper.ChannelBufferTools;
import org.traccar.helper.Checksum;
import org.traccar.helper.DateBuilder;
import org.traccar.helper.UnitsConverter;
@@ -132,12 +132,12 @@ public class HuabaoProtocolDecoder extends BaseProtocolDecoder {
position.setCourse(buf.readUnsignedShort());
DateBuilder dateBuilder = new DateBuilder(TimeZone.getTimeZone("GMT+8"))
- .setYear(ChannelBufferTools.readHexInteger(buf, 2))
- .setMonth(ChannelBufferTools.readHexInteger(buf, 2))
- .setDay(ChannelBufferTools.readHexInteger(buf, 2))
- .setHour(ChannelBufferTools.readHexInteger(buf, 2))
- .setMinute(ChannelBufferTools.readHexInteger(buf, 2))
- .setSecond(ChannelBufferTools.readHexInteger(buf, 2));
+ .setYear(BcdUtil.readInteger(buf, 2))
+ .setMonth(BcdUtil.readInteger(buf, 2))
+ .setDay(BcdUtil.readInteger(buf, 2))
+ .setHour(BcdUtil.readInteger(buf, 2))
+ .setMinute(BcdUtil.readInteger(buf, 2))
+ .setSecond(BcdUtil.readInteger(buf, 2));
position.setTime(dateBuilder.getDate());
// additional information
diff --git a/src/org/traccar/protocol/Jt600ProtocolDecoder.java b/src/org/traccar/protocol/Jt600ProtocolDecoder.java
index 7c6ab8a4d..3cd6aedb7 100644
--- a/src/org/traccar/protocol/Jt600ProtocolDecoder.java
+++ b/src/org/traccar/protocol/Jt600ProtocolDecoder.java
@@ -22,7 +22,7 @@ import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.buffer.ChannelBuffers;
import org.jboss.netty.channel.Channel;
import org.traccar.BaseProtocolDecoder;
-import org.traccar.helper.ChannelBufferTools;
+import org.traccar.helper.BcdUtil;
import org.traccar.helper.DateBuilder;
import org.traccar.helper.Parser;
import org.traccar.helper.PatternBuilder;
@@ -55,21 +55,21 @@ public class Jt600ProtocolDecoder extends BaseProtocolDecoder {
}
position.setDeviceId(getDeviceId());
- int version = ChannelBufferTools.readHexInteger(buf, 1);
+ int version = BcdUtil.readInteger(buf, 1);
buf.readUnsignedByte(); // type
buf.readBytes(2); // length
DateBuilder dateBuilder = new DateBuilder()
- .setDay(ChannelBufferTools.readHexInteger(buf, 2))
- .setMonth(ChannelBufferTools.readHexInteger(buf, 2))
- .setYear(ChannelBufferTools.readHexInteger(buf, 2))
- .setHour(ChannelBufferTools.readHexInteger(buf, 2))
- .setMinute(ChannelBufferTools.readHexInteger(buf, 2))
- .setSecond(ChannelBufferTools.readHexInteger(buf, 2));
+ .setDay(BcdUtil.readInteger(buf, 2))
+ .setMonth(BcdUtil.readInteger(buf, 2))
+ .setYear(BcdUtil.readInteger(buf, 2))
+ .setHour(BcdUtil.readInteger(buf, 2))
+ .setMinute(BcdUtil.readInteger(buf, 2))
+ .setSecond(BcdUtil.readInteger(buf, 2));
position.setTime(dateBuilder.getDate());
- double latitude = convertCoordinate(ChannelBufferTools.readHexInteger(buf, 8));
- double longitude = convertCoordinate(ChannelBufferTools.readHexInteger(buf, 9));
+ double latitude = convertCoordinate(BcdUtil.readInteger(buf, 8));
+ double longitude = convertCoordinate(BcdUtil.readInteger(buf, 9));
byte flags = buf.readByte();
position.setValid((flags & 0x1) == 0x1);
@@ -82,7 +82,7 @@ public class Jt600ProtocolDecoder extends BaseProtocolDecoder {
}
position.setLongitude(longitude);
- position.setSpeed(ChannelBufferTools.readHexInteger(buf, 2));
+ position.setSpeed(BcdUtil.readInteger(buf, 2));
position.setCourse(buf.readUnsignedByte() * 2.0);
if (version == 1) {
diff --git a/src/org/traccar/protocol/KhdProtocolDecoder.java b/src/org/traccar/protocol/KhdProtocolDecoder.java
index c1ebb1bf4..07db78c94 100644
--- a/src/org/traccar/protocol/KhdProtocolDecoder.java
+++ b/src/org/traccar/protocol/KhdProtocolDecoder.java
@@ -20,7 +20,7 @@ import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.buffer.ChannelBuffers;
import org.jboss.netty.channel.Channel;
import org.traccar.BaseProtocolDecoder;
-import org.traccar.helper.ChannelBufferTools;
+import org.traccar.helper.BcdUtil;
import org.traccar.helper.Checksum;
import org.traccar.helper.DateBuilder;
import org.traccar.helper.UnitsConverter;
@@ -79,18 +79,18 @@ public class KhdProtocolDecoder extends BaseProtocolDecoder {
position.setDeviceId(getDeviceId());
DateBuilder dateBuilder = new DateBuilder()
- .setYear(ChannelBufferTools.readHexInteger(buf, 2))
- .setMonth(ChannelBufferTools.readHexInteger(buf, 2))
- .setDay(ChannelBufferTools.readHexInteger(buf, 2))
- .setHour(ChannelBufferTools.readHexInteger(buf, 2))
- .setMinute(ChannelBufferTools.readHexInteger(buf, 2))
- .setSecond(ChannelBufferTools.readHexInteger(buf, 2));
+ .setYear(BcdUtil.readInteger(buf, 2))
+ .setMonth(BcdUtil.readInteger(buf, 2))
+ .setDay(BcdUtil.readInteger(buf, 2))
+ .setHour(BcdUtil.readInteger(buf, 2))
+ .setMinute(BcdUtil.readInteger(buf, 2))
+ .setSecond(BcdUtil.readInteger(buf, 2));
position.setTime(dateBuilder.getDate());
- position.setLatitude(ChannelBufferTools.readCoordinate(buf));
- position.setLongitude(ChannelBufferTools.readCoordinate(buf));
- position.setSpeed(UnitsConverter.knotsFromKph(ChannelBufferTools.readHexInteger(buf, 4)));
- position.setCourse(ChannelBufferTools.readHexInteger(buf, 4));
+ position.setLatitude(BcdUtil.readCoordinate(buf));
+ position.setLongitude(BcdUtil.readCoordinate(buf));
+ position.setSpeed(UnitsConverter.knotsFromKph(BcdUtil.readInteger(buf, 4)));
+ position.setCourse(BcdUtil.readInteger(buf, 4));
int flags = buf.readUnsignedByte();
position.setValid((flags & 0x80) != 0);
diff --git a/src/org/traccar/protocol/T800xProtocolDecoder.java b/src/org/traccar/protocol/T800xProtocolDecoder.java
index 83d815e0f..c5dc103b1 100644
--- a/src/org/traccar/protocol/T800xProtocolDecoder.java
+++ b/src/org/traccar/protocol/T800xProtocolDecoder.java
@@ -19,8 +19,8 @@ import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.buffer.ChannelBuffers;
import org.jboss.netty.channel.Channel;
import org.traccar.BaseProtocolDecoder;
+import org.traccar.helper.BcdUtil;
import org.traccar.helper.BitUtil;
-import org.traccar.helper.ChannelBufferTools;
import org.traccar.helper.DateBuilder;
import org.traccar.helper.UnitsConverter;
import org.traccar.model.Event;
@@ -114,19 +114,19 @@ public class T800xProtocolDecoder extends BaseProtocolDecoder {
position.set(Event.KEY_ODOMETER, buf.readUnsignedInt());
- int battery = ChannelBufferTools.readHexInteger(buf, 2);
+ int battery = BcdUtil.readInteger(buf, 2);
if (battery == 0) {
battery = 100;
}
position.set(Event.KEY_BATTERY, battery);
DateBuilder dateBuilder = new DateBuilder()
- .setYear(ChannelBufferTools.readHexInteger(buf, 2))
- .setMonth(ChannelBufferTools.readHexInteger(buf, 2))
- .setDay(ChannelBufferTools.readHexInteger(buf, 2))
- .setHour(ChannelBufferTools.readHexInteger(buf, 2))
- .setMinute(ChannelBufferTools.readHexInteger(buf, 2))
- .setSecond(ChannelBufferTools.readHexInteger(buf, 2));
+ .setYear(BcdUtil.readInteger(buf, 2))
+ .setMonth(BcdUtil.readInteger(buf, 2))
+ .setDay(BcdUtil.readInteger(buf, 2))
+ .setHour(BcdUtil.readInteger(buf, 2))
+ .setMinute(BcdUtil.readInteger(buf, 2))
+ .setSecond(BcdUtil.readInteger(buf, 2));
if (BitUtil.check(locationStatus, 6)) {
@@ -136,7 +136,7 @@ public class T800xProtocolDecoder extends BaseProtocolDecoder {
position.setLongitude(readSwappedFloat(buf));
position.setLatitude(readSwappedFloat(buf));
position.setSpeed(UnitsConverter.knotsFromKph(
- ChannelBufferTools.readHexInteger(buf, 4) * 0.1));
+ BcdUtil.readInteger(buf, 4) * 0.1));
position.setCourse(buf.readUnsignedShort());
} else {
diff --git a/test/org/traccar/helper/ChannelBufferToolsTest.java b/test/org/traccar/helper/BcdUtilTest.java
index 6733a8f2a..8767ac890 100644
--- a/test/org/traccar/helper/ChannelBufferToolsTest.java
+++ b/test/org/traccar/helper/BcdUtilTest.java
@@ -4,12 +4,12 @@ import org.jboss.netty.buffer.ChannelBuffers;
import org.junit.Assert;
import org.junit.Test;
-public class ChannelBufferToolsTest {
+public class BcdUtilTest {
@Test
- public void testReadHexInteger() {
+ public void testReadInteger() {
byte[] buf = {0x01, (byte) 0x90, 0x34};
- int result = ChannelBufferTools.readHexInteger(
+ int result = BcdUtil.readInteger(
ChannelBuffers.wrappedBuffer(buf), 5);
Assert.assertEquals(1903, result);
}
@@ -17,7 +17,7 @@ public class ChannelBufferToolsTest {
@Test
public void testReadCoordinate() {
byte[] buf = {0x03, (byte) 0x85, 0x22, 0x59, 0x34};
- double result = ChannelBufferTools.readCoordinate(
+ double result = BcdUtil.readCoordinate(
ChannelBuffers.wrappedBuffer(buf));
Assert.assertEquals(38.870989, result, 0.00001);
}
diff --git a/test/org/traccar/protocol/CityeasyProtocolEncoderTest.java b/test/org/traccar/protocol/CityeasyProtocolEncoderTest.java
index 4ef3c48ef..96b3c2b74 100644
--- a/test/org/traccar/protocol/CityeasyProtocolEncoderTest.java
+++ b/test/org/traccar/protocol/CityeasyProtocolEncoderTest.java
@@ -1,14 +1,9 @@
package org.traccar.protocol;
-import org.jboss.netty.buffer.ChannelBuffers;
-import org.junit.Assert;
import org.junit.Test;
import org.traccar.ProtocolTest;
-import org.traccar.helper.ChannelBufferTools;
import org.traccar.model.Command;
-import javax.xml.bind.DatatypeConverter;
-
public class CityeasyProtocolEncoderTest extends ProtocolTest {
@Test
diff --git a/test/org/traccar/protocol/Gt06ProtocolEncoderTest.java b/test/org/traccar/protocol/Gt06ProtocolEncoderTest.java
index 71657c366..aceaef434 100644
--- a/test/org/traccar/protocol/Gt06ProtocolEncoderTest.java
+++ b/test/org/traccar/protocol/Gt06ProtocolEncoderTest.java
@@ -1,14 +1,9 @@
package org.traccar.protocol;
-import org.jboss.netty.buffer.ChannelBuffers;
-import org.junit.Assert;
import org.junit.Test;
import org.traccar.ProtocolTest;
-import org.traccar.helper.ChannelBufferTools;
import org.traccar.model.Command;
-import javax.xml.bind.DatatypeConverter;
-
public class Gt06ProtocolEncoderTest extends ProtocolTest {
@Test
diff --git a/test/org/traccar/protocol/KhdProtocolEncoderTest.java b/test/org/traccar/protocol/KhdProtocolEncoderTest.java
index e453deaf5..078b7c22e 100644
--- a/test/org/traccar/protocol/KhdProtocolEncoderTest.java
+++ b/test/org/traccar/protocol/KhdProtocolEncoderTest.java
@@ -1,14 +1,9 @@
package org.traccar.protocol;
-import org.jboss.netty.buffer.ChannelBuffers;
-import org.junit.Assert;
import org.junit.Test;
import org.traccar.ProtocolTest;
-import org.traccar.helper.ChannelBufferTools;
import org.traccar.model.Command;
-import javax.xml.bind.DatatypeConverter;
-
public class KhdProtocolEncoderTest extends ProtocolTest {
@Test
diff --git a/test/org/traccar/protocol/TramigoProtocolDecoderTest.java b/test/org/traccar/protocol/TramigoProtocolDecoderTest.java
index a11e69a6a..bc73d8f88 100644
--- a/test/org/traccar/protocol/TramigoProtocolDecoderTest.java
+++ b/test/org/traccar/protocol/TramigoProtocolDecoderTest.java
@@ -42,7 +42,7 @@ public class TramigoProtocolDecoderTest extends ProtocolTest {
// Tramigo: Parked, 0.12 km E of McDonald's H.V. dela Costa, Makati, 11:07 Mar 27
// Tramigo: Moving, 0.90 km SE of Cranebrook Fire Station, Penrith, Sydney, AU, -33.70722, 150.71759, SE with speed 37 km/h, 19:48 Jan 4 EOF
- //verifyPosition(decoder, text( ChannelBuffers.wrappedBuffer(ChannelBufferTools.convertArray(
+ //verifyPosition(decoder, text( ChannelBuffers.wrappedBuffer(BcdUtil.convertArray(
// new int[] {0x68,0x68,0x25,0x00,0x00,0x01,0x23,0x45,0x67,0x89,0x01,0x23,0x45,0x00,0x01,0x10,0x01,0x01,0x01,0x01,0x01,0x01,0x02,0x6B,0x3F,0x3E,0x02,0x6B,0x3F,0x3E,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x0D,0x0A}))));
}