From 3c492dc734d79d13ce0ea9d5a8c7667fc82b9377 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Mon, 25 Jun 2018 07:09:17 +1200 Subject: Support Huabao batched locations --- .../traccar/protocol/HuabaoProtocolDecoder.java | 92 ++++++++++++++-------- .../protocol/HuabaoProtocolDecoderTest.java | 3 + 2 files changed, 63 insertions(+), 32 deletions(-) diff --git a/src/org/traccar/protocol/HuabaoProtocolDecoder.java b/src/org/traccar/protocol/HuabaoProtocolDecoder.java index afb29e3c3..b92840dfc 100644 --- a/src/org/traccar/protocol/HuabaoProtocolDecoder.java +++ b/src/org/traccar/protocol/HuabaoProtocolDecoder.java @@ -31,6 +31,8 @@ import org.traccar.model.Position; import java.net.SocketAddress; import java.nio.charset.StandardCharsets; +import java.util.LinkedList; +import java.util.List; import java.util.TimeZone; public class HuabaoProtocolDecoder extends BaseProtocolDecoder { @@ -44,6 +46,7 @@ public class HuabaoProtocolDecoder extends BaseProtocolDecoder { public static final int MSG_TERMINAL_REGISTER_RESPONSE = 0x8100; public static final int MSG_TERMINAL_AUTH = 0x0102; public static final int MSG_LOCATION_REPORT = 0x0200; + public static final int MSG_LOCATION_BATCH = 0x0704; public static final int MSG_OIL_CONTROL = 0XA006; public static final int RESULT_SUCCESS = 0; @@ -134,52 +137,77 @@ public class HuabaoProtocolDecoder extends BaseProtocolDecoder { } else if (type == MSG_LOCATION_REPORT) { - Position position = new Position(getProtocolName()); - position.setDeviceId(deviceSession.getDeviceId()); + return decodeLocation(deviceSession, buf); - position.set(Position.KEY_ALARM, decodeAlarm(buf.readUnsignedInt())); + } else if (type == MSG_LOCATION_BATCH) { - int flags = buf.readInt(); + return decodeLocationBatch(deviceSession, buf); - position.set(Position.KEY_IGNITION, BitUtil.check(flags, 0)); + } - position.setValid(BitUtil.check(flags, 1)); + return null; + } - double lat = buf.readUnsignedInt() * 0.000001; - double lon = buf.readUnsignedInt() * 0.000001; + private Position decodeLocation(DeviceSession deviceSession, ByteBuf buf) { - if (BitUtil.check(flags, 2)) { - position.setLatitude(-lat); - } else { - position.setLatitude(lat); - } + Position position = new Position(getProtocolName()); + position.setDeviceId(deviceSession.getDeviceId()); - if (BitUtil.check(flags, 3)) { - position.setLongitude(-lon); - } else { - position.setLongitude(lon); - } + position.set(Position.KEY_ALARM, decodeAlarm(buf.readUnsignedInt())); - position.setAltitude(buf.readShort()); - position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedShort() * 0.1)); - position.setCourse(buf.readUnsignedShort()); + int flags = buf.readInt(); - DateBuilder dateBuilder = new DateBuilder(TimeZone.getTimeZone("GMT+8")) - .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.set(Position.KEY_IGNITION, BitUtil.check(flags, 0)); - // additional information + position.setValid(BitUtil.check(flags, 1)); - return position; + double lat = buf.readUnsignedInt() * 0.000001; + double lon = buf.readUnsignedInt() * 0.000001; + if (BitUtil.check(flags, 2)) { + position.setLatitude(-lat); + } else { + position.setLatitude(lat); } - return null; + if (BitUtil.check(flags, 3)) { + position.setLongitude(-lon); + } else { + position.setLongitude(lon); + } + + position.setAltitude(buf.readShort()); + position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedShort() * 0.1)); + position.setCourse(buf.readUnsignedShort()); + + DateBuilder dateBuilder = new DateBuilder(TimeZone.getTimeZone("GMT+8")) + .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 + + return position; + } + + private List decodeLocationBatch(DeviceSession deviceSession, ByteBuf buf) { + + List positions = new LinkedList<>(); + + int count = buf.readUnsignedShort(); + buf.readUnsignedByte(); // location type + + for (int i = 0; i < count; i++) { + int endIndex = buf.readUnsignedShort() + buf.readerIndex(); + positions.add(decodeLocation(deviceSession, buf)); + buf.readerIndex(endIndex); + } + + return positions; } } diff --git a/test/org/traccar/protocol/HuabaoProtocolDecoderTest.java b/test/org/traccar/protocol/HuabaoProtocolDecoderTest.java index 9ce1c6caa..95e183460 100644 --- a/test/org/traccar/protocol/HuabaoProtocolDecoderTest.java +++ b/test/org/traccar/protocol/HuabaoProtocolDecoderTest.java @@ -10,6 +10,9 @@ public class HuabaoProtocolDecoderTest extends ProtocolTest { HuabaoProtocolDecoder decoder = new HuabaoProtocolDecoder(new HuabaoProtocol()); + verifyPositions(decoder, binary( + "7E070400F30303000002450064000401003A000000000000000301618AC606C31F20000000000029180514202847010400000000EB16000C00B28986061708003732585700060089FFFFFFFE003A000000000000000301618AE806C31EB800000000009F180514202917010400000000EB16000C00B28986061708003732585700060089FFFFFFFE003A000000000000000301618AE806C31EB800000000009F180514202947010400000000EB16000C00B28986061708003732585700060089FFFFFFFE003A000001000000080301618AE806C31EB800000000009F180514203006010400000000EB16000C00B28986061708003732585700060089FFFFFFFED77E")); + verifyPosition(decoder, binary( "7e02000054093037612710000700000000000000010223aca000dc9dd800000000000017121417122133362a4d30302c34352c31313336393042383030313233303026303030303030303030303030263132333435363738393031323334353623897e")); -- cgit v1.2.3