Skip to content

Commit

Permalink
Support Huabao batched locations
Browse files Browse the repository at this point in the history
  • Loading branch information
tananaev committed Jun 24, 2018
1 parent 05139e4 commit 3c492dc
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 32 deletions.
92 changes: 60 additions & 32 deletions src/org/traccar/protocol/HuabaoProtocolDecoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@

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 {
Expand All @@ -44,6 +46,7 @@ public HuabaoProtocolDecoder(HuabaoProtocol protocol) {
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;
Expand Down Expand Up @@ -134,52 +137,77 @@ protected Object decode(

} 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<Position> decodeLocationBatch(DeviceSession deviceSession, ByteBuf buf) {

List<Position> 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;
}

}
3 changes: 3 additions & 0 deletions test/org/traccar/protocol/HuabaoProtocolDecoderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ public void testDecode() throws Exception {

HuabaoProtocolDecoder decoder = new HuabaoProtocolDecoder(new HuabaoProtocol());

verifyPositions(decoder, binary(
"7E070400F30303000002450064000401003A000000000000000301618AC606C31F20000000000029180514202847010400000000EB16000C00B28986061708003732585700060089FFFFFFFE003A000000000000000301618AE806C31EB800000000009F180514202917010400000000EB16000C00B28986061708003732585700060089FFFFFFFE003A000000000000000301618AE806C31EB800000000009F180514202947010400000000EB16000C00B28986061708003732585700060089FFFFFFFE003A000001000000080301618AE806C31EB800000000009F180514203006010400000000EB16000C00B28986061708003732585700060089FFFFFFFED77E"));

verifyPosition(decoder, binary(
"7e02000054093037612710000700000000000000010223aca000dc9dd800000000000017121417122133362a4d30302c34352c31313336393042383030313233303026303030303030303030303030263132333435363738393031323334353623897e"));

Expand Down

0 comments on commit 3c492dc

Please sign in to comment.