diff options
Diffstat (limited to 'src')
4 files changed, 94 insertions, 23 deletions
diff --git a/src/main/java/org/traccar/protocol/HuabaoFrameDecoder.java b/src/main/java/org/traccar/protocol/HuabaoFrameDecoder.java index b520f6be9..c99d42a59 100644 --- a/src/main/java/org/traccar/protocol/HuabaoFrameDecoder.java +++ b/src/main/java/org/traccar/protocol/HuabaoFrameDecoder.java @@ -1,5 +1,5 @@ /* - * Copyright 2015 - 2018 Anton Tananaev (anton@traccar.org) + * Copyright 2015 - 2020 Anton Tananaev (anton@traccar.org) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -31,25 +31,36 @@ public class HuabaoFrameDecoder extends BaseFrameDecoder { return null; } - int index = buf.indexOf(buf.readerIndex() + 1, buf.writerIndex(), (byte) 0x7e); - if (index != -1) { - ByteBuf result = Unpooled.buffer(index + 1 - buf.readerIndex()); - - while (buf.readerIndex() <= index) { - int b = buf.readUnsignedByte(); - if (b == 0x7d) { - int ext = buf.readUnsignedByte(); - if (ext == 0x01) { - result.writeByte(0x7d); - } else if (ext == 0x02) { - result.writeByte(0x7e); + if (buf.getByte(buf.readerIndex()) == '(') { + + int index = buf.indexOf(buf.readerIndex() + 1, buf.writerIndex(), (byte) ')'); + if (index >= 0) { + return buf.readRetainedSlice(index + 1); + } + + } else { + + int index = buf.indexOf(buf.readerIndex() + 1, buf.writerIndex(), (byte) 0x7e); + if (index >= 0) { + ByteBuf result = Unpooled.buffer(index + 1 - buf.readerIndex()); + + while (buf.readerIndex() <= index) { + int b = buf.readUnsignedByte(); + if (b == 0x7d) { + int ext = buf.readUnsignedByte(); + if (ext == 0x01) { + result.writeByte(0x7d); + } else if (ext == 0x02) { + result.writeByte(0x7e); + } + } else { + result.writeByte(b); } - } else { - result.writeByte(b); } + + return result; } - return result; } return null; diff --git a/src/main/java/org/traccar/protocol/HuabaoProtocolDecoder.java b/src/main/java/org/traccar/protocol/HuabaoProtocolDecoder.java index a502755fd..d89bcdbe8 100644 --- a/src/main/java/org/traccar/protocol/HuabaoProtocolDecoder.java +++ b/src/main/java/org/traccar/protocol/HuabaoProtocolDecoder.java @@ -28,6 +28,8 @@ import org.traccar.helper.BitUtil; import org.traccar.helper.Checksum; import org.traccar.helper.DateBuilder; import org.traccar.helper.UnitsConverter; +import org.traccar.model.CellTower; +import org.traccar.model.Network; import org.traccar.model.Position; import java.net.SocketAddress; @@ -48,6 +50,7 @@ public class HuabaoProtocolDecoder extends BaseProtocolDecoder { public static final int MSG_TERMINAL_AUTH = 0x0102; public static final int MSG_LOCATION_REPORT = 0x0200; public static final int MSG_LOCATION_REPORT_2 = 0x5501; + public static final int MSG_LOCATION_REPORT_BLIND = 0x5502; public static final int MSG_LOCATION_BATCH = 0x0704; public static final int MSG_OIL_CONTROL = 0XA006; @@ -114,12 +117,16 @@ public class HuabaoProtocolDecoder extends BaseProtocolDecoder { ByteBuf buf = (ByteBuf) msg; + if (buf.getByte(buf.readerIndex()) == '(') { + return decodeResult(channel, remoteAddress, buf.toString(StandardCharsets.US_ASCII)); + } + buf.readUnsignedByte(); // start marker int type = buf.readUnsignedShort(); buf.readUnsignedShort(); // body length ByteBuf id = buf.readSlice(6); // phone number int index; - if (type == MSG_LOCATION_REPORT_2) { + if (type == MSG_LOCATION_REPORT_2 || type == MSG_LOCATION_REPORT_BLIND) { index = buf.readUnsignedByte(); } else { index = buf.readUnsignedShort(); @@ -153,9 +160,9 @@ public class HuabaoProtocolDecoder extends BaseProtocolDecoder { return decodeLocation(deviceSession, buf); - } else if (type == MSG_LOCATION_REPORT_2) { + } else if (type == MSG_LOCATION_REPORT_2 || type == MSG_LOCATION_REPORT_BLIND) { - return decodeLocation2(deviceSession, buf); + return decodeLocation2(deviceSession, buf, type); } else if (type == MSG_LOCATION_BATCH) { @@ -166,6 +173,18 @@ public class HuabaoProtocolDecoder extends BaseProtocolDecoder { return null; } + private Position decodeResult(Channel channel, SocketAddress remoteAddress, String sentence) { + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress); + if (deviceSession != null) { + Position position = new Position(getProtocolName()); + position.setDeviceId(deviceSession.getDeviceId()); + getLastLocation(position, null); + position.set(Position.KEY_RESULT, sentence); + return position; + } + return null; + } + private Position decodeLocation(DeviceSession deviceSession, ByteBuf buf) { Position position = new Position(getProtocolName()); @@ -289,12 +308,44 @@ public class HuabaoProtocolDecoder extends BaseProtocolDecoder { return position; } - private Position decodeLocation2(DeviceSession deviceSession, ByteBuf buf) { + private Position decodeLocation2(DeviceSession deviceSession, ByteBuf buf, int type) { Position position = new Position(getProtocolName()); position.setDeviceId(deviceSession.getDeviceId()); Jt600ProtocolDecoder.decodeBinaryLocation(buf, position); + position.setValid(type != MSG_LOCATION_REPORT_BLIND); + + position.set(Position.KEY_RSSI, buf.readUnsignedByte()); + position.set(Position.KEY_SATELLITES, buf.readUnsignedByte()); + position.set(Position.KEY_ODOMETER, buf.readUnsignedInt() * 1000L); + position.set(Position.KEY_BATTERY_LEVEL, buf.readUnsignedByte()); + position.set(Position.KEY_CHARGE, buf.readUnsignedByte() == 0xAA); + + position.setNetwork(new Network(CellTower.fromCidLac(buf.readUnsignedInt(), buf.readUnsignedShort()))); + + int product = buf.readUnsignedByte(); + int status = buf.readUnsignedShort(); + int alarm = buf.readUnsignedShort(); + + if (product == 1 || product == 2) { + if (BitUtil.check(alarm, 0)) { + position.set(Position.KEY_ALARM, Position.ALARM_LOW_POWER); + } + } if (product == 3) { + position.set(Position.KEY_BLOCKED, BitUtil.check(status, 5)); + if (BitUtil.check(alarm, 1)) { + position.set(Position.KEY_ALARM, Position.ALARM_LOW_POWER); + } + if (BitUtil.check(alarm, 2)) { + position.set(Position.KEY_ALARM, Position.ALARM_VIBRATION); + } + if (BitUtil.check(alarm, 3)) { + position.set(Position.KEY_ALARM, Position.ALARM_LOW_BATTERY); + } + } + + position.set(Position.KEY_STATUS, status); return position; } diff --git a/src/test/java/org/traccar/protocol/HuabaoFrameDecoderTest.java b/src/test/java/org/traccar/protocol/HuabaoFrameDecoderTest.java index 2d3937903..3b32be8dd 100644 --- a/src/test/java/org/traccar/protocol/HuabaoFrameDecoderTest.java +++ b/src/test/java/org/traccar/protocol/HuabaoFrameDecoderTest.java @@ -3,8 +3,6 @@ package org.traccar.protocol; import org.junit.Test; import org.traccar.ProtocolTest; -import static org.junit.Assert.assertEquals; - public class HuabaoFrameDecoderTest extends ProtocolTest { @Test @@ -12,7 +10,11 @@ public class HuabaoFrameDecoderTest extends ProtocolTest { HuabaoFrameDecoder decoder = new HuabaoFrameDecoder(); - assertEquals( + verifyFrame( + binary("283734303139303331313138352c312c3030312c454c4f434b2c332c35323934333929"), + decoder.decode(null, null, binary("283734303139303331313138352c312c3030312c454c4f434b2c332c35323934333929"))); + + verifyFrame( binary("7e307e087d557e"), decoder.decode(null, null, binary("7e307d02087d01557e"))); diff --git a/src/test/java/org/traccar/protocol/HuabaoProtocolDecoderTest.java b/src/test/java/org/traccar/protocol/HuabaoProtocolDecoderTest.java index 97a77eb28..8c4a0278d 100644 --- a/src/test/java/org/traccar/protocol/HuabaoProtocolDecoderTest.java +++ b/src/test/java/org/traccar/protocol/HuabaoProtocolDecoderTest.java @@ -14,6 +14,13 @@ public class HuabaoProtocolDecoderTest extends ProtocolTest { verifyNull(decoder, binary( "7E01000021013345678906000F002C012F373031313142534A2D4D3742203030303030303001D4C1423838383838B47E")); + verifyAttribute(decoder, binary( + "283734303139303331313138352c312c3030312c454c4f434b2c332c35323934333929"), + Position.KEY_RESULT, "(740190311185,1,001,ELOCK,3,529439)"); + + verifyPosition(decoder, binary( + "7e55028436740190311185091607200239270613212607536108630000170a000000014600005ded05e203000000000b010d0c06005b003e00ab0a0302dc65100100a37e")); + verifyPosition(decoder, binary( "7E550100287001608180000127061008045322332880113555602E050031010000000608000010931435010002000300030100051B7E")); |