aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnton Tananaev <anton.tananaev@gmail.com>2014-03-19 00:24:42 +1300
committerAnton Tananaev <anton.tananaev@gmail.com>2014-03-19 00:24:42 +1300
commit04b35596c86b0cd4b06ce54c26ed84c6166e10b4 (patch)
tree964d676641faa2dbb8038b6bab18a321abe919da
parentf758046ece7fcdd720916cf36f8f8836607dc2db (diff)
downloadtrackermap-server-04b35596c86b0cd4b06ce54c26ed84c6166e10b4.tar.gz
trackermap-server-04b35596c86b0cd4b06ce54c26ed84c6166e10b4.tar.bz2
trackermap-server-04b35596c86b0cd4b06ce54c26ed84c6166e10b4.zip
Atrack protocol code cleanup
-rw-r--r--src/org/traccar/protocol/AtrackProtocolDecoder.java68
-rw-r--r--test/org/traccar/protocol/AtrackProtocolDecoderTest.java3
2 files changed, 29 insertions, 42 deletions
diff --git a/src/org/traccar/protocol/AtrackProtocolDecoder.java b/src/org/traccar/protocol/AtrackProtocolDecoder.java
index 8ebb402cf..db1e5284e 100644
--- a/src/org/traccar/protocol/AtrackProtocolDecoder.java
+++ b/src/org/traccar/protocol/AtrackProtocolDecoder.java
@@ -35,18 +35,6 @@ public class AtrackProtocolDecoder extends BaseProtocolDecoder {
super(serverManager);
}
- private String readImei(ChannelBuffer buf) {
- int b = buf.readUnsignedByte();
- StringBuilder imei = new StringBuilder();
- imei.append(b & 0x0F);
- for (int i = 0; i < 7; i++) {
- b = buf.readUnsignedByte();
- imei.append((b & 0xF0) >> 4);
- imei.append(b & 0x0F);
- }
- return imei.toString();
- }
-
private static final int MSG_HEARTBEAT = 0x1A;
private static final int MSG_DATA = 0x10;
@@ -60,22 +48,35 @@ public class AtrackProtocolDecoder extends BaseProtocolDecoder {
}
}
+ private static String readString(ChannelBuffer buf) {
+
+ int length = 0;
+ while (buf.getByte(buf.readerIndex() + length) != 0) {
+ length += 1;
+ }
+ if (length != 0) {
+ String result = buf.toString(buf.readerIndex(), length, Charset.defaultCharset());
+ buf.skipBytes(length);
+ return result;
+ }
+ buf.readByte();
+
+ return null;
+ }
+
@Override
protected Object decode(
ChannelHandlerContext ctx, Channel channel, Object msg)
throws Exception {
-
- List<Position> positions = new LinkedList<Position>();
ChannelBuffer buf = (ChannelBuffer) msg;
buf.skipBytes(2); // prefix
buf.readUnsignedShort(); // checksum
-
buf.readUnsignedShort(); // length
int index = buf.readUnsignedShort();
// Get device id
- long deviceId;
+ long deviceId;
long rawId = buf.readLong();
String id = String.valueOf(rawId);
try {
@@ -88,9 +89,11 @@ public class AtrackProtocolDecoder extends BaseProtocolDecoder {
// Send acknowledgement
sendResponse(channel, rawId, index);
- while (buf.readable()) {
-
- // Create new position
+ List<Position> positions = new LinkedList<Position>();
+
+ while (buf.readable()) {
+
+ // Create new position
Position position = new Position();
position.setDeviceId(deviceId);
ExtendedInfoFormatter extendedInfo = new ExtendedInfoFormatter("atrack");
@@ -131,32 +134,14 @@ public class AtrackProtocolDecoder extends BaseProtocolDecoder {
extendedInfo.set("adc", buf.readUnsignedShort() * 0.001);
// Driver
- int length = 0;
- while (buf.getByte(buf.readerIndex() + length) != 0) {
- length += 1;
- }
- if (length != 0) {
- extendedInfo.set("driver",
- buf.toString(buf.readerIndex(), length, Charset.defaultCharset()));
- buf.skipBytes(length);
- }
- buf.readByte();
+ extendedInfo.set("driver", readString(buf));
// Temperature
extendedInfo.set("temperature1", buf.readShort() * 0.1);
extendedInfo.set("temperature2", buf.readShort() * 0.1);
// Text Message
- length = 0;
- while (buf.getByte(buf.readerIndex() + length) != 0) {
- length += 1;
- }
- if (length != 0) {
- extendedInfo.set("textmessage",
- buf.toString(buf.readerIndex(), length, Charset.defaultCharset()));
- buf.skipBytes(length);
- }
- buf.readByte();
+ extendedInfo.set("message", readString(buf));
// With AT$FORM Command you can extend atrack protocol.
// For example adding AT$FORM %FC /Fuel used you can add the line in this position:
@@ -165,9 +150,8 @@ public class AtrackProtocolDecoder extends BaseProtocolDecoder {
position.setExtendedInfo(extendedInfo.toString());
positions.add(position);
}
-
+
return positions;
-
}
-} \ No newline at end of file
+}
diff --git a/test/org/traccar/protocol/AtrackProtocolDecoderTest.java b/test/org/traccar/protocol/AtrackProtocolDecoderTest.java
index 62cec4516..1cdaa8363 100644
--- a/test/org/traccar/protocol/AtrackProtocolDecoderTest.java
+++ b/test/org/traccar/protocol/AtrackProtocolDecoderTest.java
@@ -27,6 +27,9 @@ public class AtrackProtocolDecoderTest {
// 7-byte date
//int[] buf4 = {0x02,0x03,0xb4,0x94,0x00,0x3c,0x00,0xeb,0x00,0x01,0x41,0x04,0xd8,0xdd,0x3a,0x3e,0x07,0xde,0x01,0x1b,0x0b,0x1f,0x03,0x07,0xde,0x01,0x1b,0x0b,0x1f,0x03,0x07,0xde,0x01,0x1b,0x0b,0x1f,0x03,0x00,0x30,0x7f,0x28,0x03,0x05,0x74,0xd3,0x00,0x00,0x02,0x00,0x00,0x00,0x06,0x00,0x16,0x01,0x00,0x02,0x00,0x00,0x00,0x00,0x07,0xd0,0x07,0xd0,0x00};
//verify(decoder.decode(null, null, ChannelBuffers.wrappedBuffer(ChannelBufferTools.convertArray(buf4))));
+
+ //int[] buf5 = {0x40,0x50,0xd2,0xc5,0x00,0xda,0x05,0x52,0x00,0x01,0x41,0x04,0xd8,0xf1,0x96,0x82,0x53,0x07,0x55,0x51,0x53,0x07,0x55,0x50,0x53,0x07,0x55,0x81,0xff,0xbb,0xa6,0x6a,0x02,0x31,0x29,0x5c,0x00,0x19,0x02,0x00,0x00,0x00,0xda,0x00,0x0a,0x01,0x00,0x83,0x00,0x00,0x00,0x00,0x07,0xd0,0x07,0xd0,0x00,0x00,0x00,0x00,0x00,0x12,0x00,0x08,0x0e,0x09,0x00,0x85,0x53,0x07,0x55,0x60,0x53,0x07,0x55,0x5f,0x53,0x07,0x55,0x82,0xff,0xbb,0xb0,0x41,0x02,0x31,0x3b,0x4b,0x00,0x18,0x02,0x00,0x00,0x00,0xe0,0x00,0x0c,0x01,0x00,0x85,0x00,0x00,0x00,0x00,0x07,0xd0,0x07,0xd0,0x00,0x00,0x00,0x00,0x00,0x12,0x00,0x08,0x0d,0x00,0x00,0x86,0x53,0x07,0x55,0x6f,0x53,0x07,0x55,0x6e,0x53,0x07,0x55,0x82,0xff,0xbb,0xbb,0xea,0x02,0x31,0x4b,0x49,0x00,0x24,0x02,0x00,0x00,0x00,0xe5,0x00,0x0a,0x01,0x00,0x7b,0x00,0x00,0x00,0x00,0x07,0xd0,0x07,0xd0,0x00,0x00,0x00,0x00,0x00,0x12,0x00,0x08,0x0d,0x02,0x00,0x85,0x53,0x07,0x55,0x7e,0x53,0x07,0x55,0x7d,0x53,0x07,0x55,0x82,0xff,0xbb,0xc9,0x87,0x02,0x31,0x59,0x82,0x00,0x25,0x02,0x00,0x00,0x00,0xea,0x00,0x09,0x01,0x00,0x7a,0x00,0x00,0x00,0x00,0x07,0xd0,0x07,0xd0,0x00,0x00,0x00,0x00,0x00,0x13,0x00,0x18,0x0d,0x08,0x00,0x7b};
+ //verify(decoder.decode(null, null, ChannelBuffers.wrappedBuffer(ChannelBufferTools.convertArray(buf5))));
}