From 3c93192e8a2a392fe9ddf7bdcd512a7d6cceb3a9 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Wed, 9 Jan 2013 20:21:30 +1300 Subject: Fix GT06 protocol --- hex.sh | 2 +- .../traccar/protocol/Gps103ProtocolDecoder.java | 3 +- src/org/traccar/protocol/Gt06ProtocolDecoder.java | 36 +++++++++++----------- .../protocol/Gps103ProtocolDecoderTest.java | 18 +++++++---- .../traccar/protocol/Gt06ProtocolDecoderTest.java | 22 ++++++++----- 5 files changed, 47 insertions(+), 34 deletions(-) diff --git a/hex.sh b/hex.sh index a061ac18c..45f6b11bc 100755 --- a/hex.sh +++ b/hex.sh @@ -1 +1 @@ -echo 2424613133382C3031323839363030303437353439382C4141412C33352C2D362E3133383330362C3130362E3931303635352C3132313230353130333430392C412C332C31312C302C302C312C33362C343138322C33343834372C3531307C31307C303038317C344634462C303030302C303030427C303030437C303030417C303846367C303030302C2C2A44350D0A | perl -ne 's/([0-9a-f]{2})/print chr hex $1/gie' | nc -v localhost 5020 +echo 78780d0103534190360660610001e0cd0d0a | perl -ne 's/([0-9a-f]{2})/print chr hex $1/gie' | nc -v -w 10 localhost 5023 diff --git a/src/org/traccar/protocol/Gps103ProtocolDecoder.java b/src/org/traccar/protocol/Gps103ProtocolDecoder.java index 9c9f56b2f..935697c51 100644 --- a/src/org/traccar/protocol/Gps103ProtocolDecoder.java +++ b/src/org/traccar/protocol/Gps103ProtocolDecoder.java @@ -61,6 +61,7 @@ public class Gps103ProtocolDecoder extends GenericProtocolDecoder { /** * Decode message */ + @Override protected Object decode( ChannelHandlerContext ctx, Channel channel, Object msg) throws Exception { @@ -143,7 +144,7 @@ public class Gps103ProtocolDecoder extends GenericProtocolDecoder { // Speed position.setSpeed(Double.valueOf(parser.group(index++))); - + // Course String course = parser.group(index++); if (course != null) { diff --git a/src/org/traccar/protocol/Gt06ProtocolDecoder.java b/src/org/traccar/protocol/Gt06ProtocolDecoder.java index 1daf6c4ec..0005df19b 100644 --- a/src/org/traccar/protocol/Gt06ProtocolDecoder.java +++ b/src/org/traccar/protocol/Gt06ProtocolDecoder.java @@ -31,7 +31,7 @@ import org.traccar.model.Position; * T55 tracker protocol decoder */ public class Gt06ProtocolDecoder extends GenericProtocolDecoder { - + private Long deviceId; /** @@ -40,7 +40,7 @@ public class Gt06ProtocolDecoder extends GenericProtocolDecoder { public Gt06ProtocolDecoder(DataManager dataManager) { super(dataManager); } - + private String readImei(ChannelBuffer buf) { int b = buf.readUnsignedByte(); StringBuilder imei = new StringBuilder(); @@ -58,9 +58,9 @@ public class Gt06ProtocolDecoder extends GenericProtocolDecoder { private static final int MSG_HEARTBEAT = 0x13; private static final int MSG_STRING = 0x15; private static final int MSG_ALARM = 0x16; - + private static void sendResponse(Channel channel, int type, int index) { - if (channel == null) { + if (channel != null) { ChannelBuffer response = ChannelBuffers.directBuffer(10); response.writeByte(0x78); response.writeByte(0x78); // header response.writeByte(0x05); // size @@ -71,7 +71,7 @@ public class Gt06ProtocolDecoder extends GenericProtocolDecoder { channel.write(response); } } - + /** * Decode message */ @@ -81,12 +81,12 @@ public class Gt06ProtocolDecoder extends GenericProtocolDecoder { throws Exception { ChannelBuffer buf = (ChannelBuffer) msg; - + buf.skipBytes(2); // header buf.readByte(); // size - + int type = buf.readUnsignedByte(); - + if (type == MSG_LOGIN) { String imei = readImei(buf); try { @@ -96,18 +96,18 @@ public class Gt06ProtocolDecoder extends GenericProtocolDecoder { Log.warning("Unknown device - " + imei); } } - + else if (type == MSG_HEARTBEAT) { buf.skipBytes(5); sendResponse(channel, type, buf.readUnsignedShort()); } - + else if (type == MSG_DATA) { // Create new position Position position = new Position(); position.setDeviceId(deviceId); StringBuilder extendedInfo = new StringBuilder("gt06"); - + // Date and time Calendar time = Calendar.getInstance(TimeZone.getTimeZone("UTC")); time.clear(); @@ -118,18 +118,18 @@ public class Gt06ProtocolDecoder extends GenericProtocolDecoder { time.set(Calendar.MINUTE, buf.readUnsignedByte()); time.set(Calendar.SECOND, buf.readUnsignedByte()); position.setTime(time.getTime()); - + // Satellites count extendedInfo.append(""); extendedInfo.append(buf.readUnsignedByte()); extendedInfo.append(""); - + // Latitude double latitude = buf.readUnsignedInt() / (60.0 * 30000.0); - + // Longitude double longitude = buf.readUnsignedInt() / (60.0 * 30000.0); - + // Speed position.setSpeed((double) buf.readUnsignedByte()); @@ -139,11 +139,11 @@ public class Gt06ProtocolDecoder extends GenericProtocolDecoder { position.setValid((union & 0x1000) != 0); if ((union & 0x0400) == 0) latitude = -latitude; if ((union & 0x0800) == 0) longitude = -longitude; - + position.setLatitude(latitude); position.setLongitude(longitude); position.setAltitude(0.0); - + // Cell information extendedInfo.append(""); extendedInfo.append(buf.readUnsignedShort()); @@ -157,7 +157,7 @@ public class Gt06ProtocolDecoder extends GenericProtocolDecoder { extendedInfo.append(""); extendedInfo.append(buf.readUnsignedShort() << 8 + buf.readUnsignedByte()); extendedInfo.append(""); - + // Index position.setId((long) buf.readUnsignedShort()); diff --git a/test/org/traccar/protocol/Gps103ProtocolDecoderTest.java b/test/org/traccar/protocol/Gps103ProtocolDecoderTest.java index 61585d3c1..0433e2e34 100644 --- a/test/org/traccar/protocol/Gps103ProtocolDecoderTest.java +++ b/test/org/traccar/protocol/Gps103ProtocolDecoderTest.java @@ -8,18 +8,24 @@ public class Gps103ProtocolDecoderTest { @Test public void testDecode() throws Exception { - + Gps103ProtocolDecoder decoder = new Gps103ProtocolDecoder(new TestDataManager()); // Log on request //assertNull(decoder.decode(null, null, "##,imei:359586015829802,A")); - + // Heartbeat package //assertNull(decoder.decode(null, null, "359586015829802")); // No GPS signal //assertNull(decoder.decode(null, null, "imei:359586015829802,tracker,000000000,13554900601,L,;")); + assertNotNull(decoder.decode(null, null, + "imei:012497000208821,tracker,1301080525,,F,212511.000,A,2228.5279,S,06855.6328,W,18.62,268.98,")); + + assertNotNull(decoder.decode(null, null, + "imei:012497000208821,tracker,1301072224,,F,142411.077,A,2227.0739,S,06855.2912,,0,0,")); + assertNotNull(decoder.decode(null, null, "imei:012497000431811,tracker,1210260609,,F,220925.000,A,0845.5500,N,07024.7673,W,0.00,,")); @@ -34,7 +40,7 @@ public class Gps103ProtocolDecoderTest { assertNotNull(decoder.decode(null, null, "imei:359587010124900,tracker,0809231929,13554900601,F,112909.397,A,2234.4669,N,11354.3287,E,0.11,321.53,")); - + assertNotNull(decoder.decode(null, null, "imei:353451049926460,tracker,1208042043,123456 99008026,F,124336.000,A,3509.8668,N,03322.7636,E,0.00,,")); @@ -57,13 +63,13 @@ public class Gps103ProtocolDecoderTest { // Over speed alarm assertNotNull(decoder.decode(null, null, "imei:359586015829802,speed,0809231429,13554900601,F,062947.294,A,2234.4026,N,11354.3277,E,0.00,")); - + assertNotNull(decoder.decode(null, null, "imei:863070010423167,tracker,1211051840,,F,104000.000,A,2220.6483,N,11407.6377,,0,0,")); - + assertNotNull(decoder.decode(null, null, "imei:863070010423167,tracker,1211051951,63360926,F,115123.000,A,2220.6322,N,11407.5313,E,0.00,,")); - + assertNotNull(decoder.decode(null, null, "imei:863070010423167,tracker,1211060621,,F,062152.000,A,2220.6914,N,11407.5506,E,15.85,347.84,")); } diff --git a/test/org/traccar/protocol/Gt06ProtocolDecoderTest.java b/test/org/traccar/protocol/Gt06ProtocolDecoderTest.java index f20618061..ae4416435 100644 --- a/test/org/traccar/protocol/Gt06ProtocolDecoderTest.java +++ b/test/org/traccar/protocol/Gt06ProtocolDecoderTest.java @@ -6,17 +6,23 @@ import static org.junit.Assert.assertNull; import org.junit.Test; public class Gt06ProtocolDecoderTest { - + @Test public void testDecode() throws Exception { - + Gt06ProtocolDecoder decoder = new Gt06ProtocolDecoder(new TestDataManager()); - - byte[] buf1 = {0x78,0x78,0x1F,0x12,0x0B,0x08,0x1D,0x11,0x2E,0x10,(byte)0xCC,0x02,0x7A,(byte)0xC7,(byte)0xEB,0x0C,0x46,0x58,0x49,0x00,0x14,(byte)0x8F,0x01,(byte)0xCC,0x00,0x28,0x7D,0x00,0x1F,(byte)0xB8,0x00,0x03,(byte)0x80,(byte)0x81,0x0D,0x0A}; - assertNotNull(decoder.decode(null, null, ChannelBuffers.wrappedBuffer(buf1))); - - byte[] buf2 = {0x78,0x78,0x0D,0x01,0x08,0x64,0x71,0x70,0x03,0x28,0x35,(byte)0x81,0x00,0x09,0x3F,0x04,0x0D,0x0A}; - assertNull(decoder.decode(null, null, ChannelBuffers.wrappedBuffer(buf2))); + + byte[] buf1 = {0x78,0x78,0x0d,0x01,0x03,0x53,0x41,(byte)0x90,0x36,0x06,0x60,0x61,0x00,0x03,(byte)0xc3,(byte)0xdf,0x0d,0x0a}; + assertNull(decoder.decode(null, null, ChannelBuffers.wrappedBuffer(buf1))); + + byte[] buf2 = {0x78,0x78,0x1F,0x12,0x0B,0x08,0x1D,0x11,0x2E,0x10,(byte)0xCC,0x02,0x7A,(byte)0xC7,(byte)0xEB,0x0C,0x46,0x58,0x49,0x00,0x14,(byte)0x8F,0x01,(byte)0xCC,0x00,0x28,0x7D,0x00,0x1F,(byte)0xB8,0x00,0x03,(byte)0x80,(byte)0x81,0x0D,0x0A}; + assertNotNull(decoder.decode(null, null, ChannelBuffers.wrappedBuffer(buf2))); + + byte[] buf3 = {0x78,0x78,0x0D,0x01,0x08,0x64,0x71,0x70,0x03,0x28,0x35,(byte)0x81,0x00,0x09,0x3F,0x04,0x0D,0x0A}; + assertNull(decoder.decode(null, null, ChannelBuffers.wrappedBuffer(buf3))); + + byte[] buf4 = {0x78,0x78,0x0D,0x01,0x01,0x23,0x45,0x67,(byte)0x89,0x01,0x23,0x45,0x00,0x01,(byte)0x8C,(byte)0xDD,0x0D,0x0A}; + assertNull(decoder.decode(null, null, ChannelBuffers.wrappedBuffer(buf4))); } -- cgit v1.2.3