From 9e6506f2df372c9f29d7166da1080ed17c41fcdc Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Tue, 23 Jun 2015 16:13:24 +1200 Subject: Decode MXT GPS information --- src/org/traccar/protocol/MxtProtocolDecoder.java | 96 ++++++++++++---------- .../traccar/protocol/MxtProtocolDecoderTest.java | 10 +-- 2 files changed, 58 insertions(+), 48 deletions(-) diff --git a/src/org/traccar/protocol/MxtProtocolDecoder.java b/src/org/traccar/protocol/MxtProtocolDecoder.java index 014033be4..07a5737c0 100644 --- a/src/org/traccar/protocol/MxtProtocolDecoder.java +++ b/src/org/traccar/protocol/MxtProtocolDecoder.java @@ -24,7 +24,9 @@ import org.traccar.helper.UnitsConverter; import org.traccar.model.Event; import org.traccar.model.Position; +import java.util.Calendar; import java.util.Date; +import java.util.TimeZone; public class MxtProtocolDecoder extends BaseProtocolDecoder { @@ -32,6 +34,10 @@ public class MxtProtocolDecoder extends BaseProtocolDecoder { super(protocol); } + private static final int MSG_ACK = 0x02; + private static final int MSG_NACK = 0x03; + private static final int MSG_POSITION = 0x31; + @Override protected Object decode( ChannelHandlerContext ctx, Channel channel, Object msg) @@ -39,54 +45,58 @@ public class MxtProtocolDecoder extends BaseProtocolDecoder { ChannelBuffer buf = (ChannelBuffer) msg; - /*buf.readByte(); // header - buf.readUnsignedByte(); // version - buf.readUnsignedByte(); // type - - // Create new position - Position position = new Position(); - position.setProtocol(getProtocol()); + buf.readUnsignedByte(); // start + buf.readUnsignedByte(); // device descriptor + int type = buf.readUnsignedByte(); - // Get device id - String imei = ChannelBufferTools.readHexString(buf, 16).substring(1); - if (!identify(imei)) { + String id = String.valueOf(buf.readUnsignedInt()); + if (!identify(id)) { return null; } - position.setDeviceId(getDeviceId()); - - // Time - long seconds = buf.readUnsignedInt() & 0x7fffffffl; - seconds += 946684800l; // 2000-01-01 00:00 - position.setTime(new Date(seconds * 1000)); - - boolean hasLocation = false; - - while (buf.readableBytes() > 3) { - - short type = buf.readUnsignedByte(); - short length = buf.readUnsignedByte(); - - switch (type) { - - case DATA_GPS: - hasLocation = true; - position.setValid(true); - position.setLatitude(buf.readInt() / 1000000.0); - position.setLongitude(buf.readInt() / 1000000.0); - position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedShort())); - position.setCourse(buf.readUnsignedShort()); - position.set(Event.KEY_HDOP, buf.readUnsignedShort()); - break; - - default: - buf.skipBytes(length); - break; - } - } - if (hasLocation) { + if (type == MSG_POSITION) { + + Position position = new Position(); + position.setProtocol(getProtocol()); + position.setDeviceId(getDeviceId()); + + buf.readUnsignedByte(); // protocol + int infoGroups = buf.readUnsignedByte(); + + position.set(Event.KEY_INDEX, buf.readUnsignedShort()); + + // Date and time + Calendar time = Calendar.getInstance(TimeZone.getTimeZone("UTC")); + time.clear(); + time.set(Calendar.YEAR, 2000); + time.set(Calendar.MONTH, 0); + time.set(Calendar.DAY_OF_MONTH, 1); + + long date = buf.readUnsignedInt(); + + long days = date >> (5 + 6 + 6); + long hours = (date >> (6 + 6)) & 0x1f; + long minutes = (date >> 6) & 0x3f; + long seconds = date & 0x3f; + + long millis = time.getTimeInMillis(); + millis += (((days * 24 + hours) * 60 + minutes) * 60 + seconds) * 1000; + + position.setTime(new Date(millis)); + + // Location + position.setLatitude(buf.readInt() / 1000000.0); + position.setLongitude(buf.readInt() / 1000000.0); + + long flags = buf.readUnsignedInt(); + + position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedByte())); + + int inputMask = buf.readUnsignedByte(); + return position; - }*/ + } + return null; } diff --git a/test/org/traccar/protocol/MxtProtocolDecoderTest.java b/test/org/traccar/protocol/MxtProtocolDecoderTest.java index 291bed804..9c0a315d2 100644 --- a/test/org/traccar/protocol/MxtProtocolDecoderTest.java +++ b/test/org/traccar/protocol/MxtProtocolDecoderTest.java @@ -15,12 +15,9 @@ public class MxtProtocolDecoderTest extends ProtocolDecoderTest { public void testDecode() throws Exception { MxtProtocolDecoder decoder = new MxtProtocolDecoder(null); - - /*verify(decoder.decode(null, null, ChannelBuffers.wrappedBuffer(ByteOrder.LITTLE_ENDIAN, ChannelBufferTools.convertHexString( - "01a631e3f97e00087cf40a98151c2cc46898fee0ce3afd1021001030c0000006102116072e003829bb00000036102100001024000000062b0000f42730004b06a6384b4304")))); verify(decoder.decode(null, null, ChannelBuffers.wrappedBuffer(ByteOrder.LITTLE_ENDIAN, ChannelBufferTools.convertHexString( - "01a631e3f97e00087cfa0af3151c2c126798febace3afd1021801030c0000006102122082f003e29bb00000037102100001024000000ab2f0000f42730004b060000488c04")))); + "01a631e3f97e00087cf40a98151c2cc46898fee0ce3afd1021001030c0000006102116072e003829bb00000036102100001024000000062b0000f42730004b06a6384b4304")))); verify(decoder.decode(null, null, ChannelBuffers.wrappedBuffer(ByteOrder.LITTLE_ENDIAN, ChannelBufferTools.convertHexString( "01a63118787d00086468457a466a2bc26a97feac8a3afd10212010308000000000001fe1053d291024000000922f0000f4271021007b17553599bb04")))); @@ -28,11 +25,14 @@ public class MxtProtocolDecoderTest extends ProtocolDecoderTest { verify(decoder.decode(null, null, ChannelBuffers.wrappedBuffer(ByteOrder.LITTLE_ENDIAN, ChannelBufferTools.convertHexString( "01a63118787d0008648645ec486a2bc26a97feac8a3afd1021001030c0000000001419eb05372b1024000000982a0000f4271021007b17000010308c04")))); + verify(decoder.decode(null, null, ChannelBuffers.wrappedBuffer(ByteOrder.LITTLE_ENDIAN, ChannelBufferTools.convertHexString( + "01a631e3f97e00087cfa0af3151c2c126798febace3afd1021801030c0000006102122082f003e29bb00000037102100001024000000ab2f0000f42730004b060000488c04")))); + verify(decoder.decode(null, null, ChannelBuffers.wrappedBuffer(ByteOrder.LITTLE_ENDIAN, ChannelBufferTools.convertHexString( "01a631e3f97e00087cfe0a4b161c2c126798febace3afd1021801030800000071021240731003e2abb00000038102100001024000000c12f0000f42730004b06a638633104")))); verify(decoder.decode(null, null, ChannelBuffers.wrappedBuffer(ByteOrder.LITTLE_ENDIAN, ChannelBufferTools.convertHexString( - "01a63118787d0008648645ec486a2bc26a97feac8a3afd1021001030c0000000001419eb05372b1024000000982a0000f4271021007b17000010308c04"))));*/ + "01a63118787d0008648645ec486a2bc26a97feac8a3afd1021001030c0000000001419eb05372b1024000000982a0000f4271021007b17000010308c04")))); } -- cgit v1.2.3