diff options
author | Anton Tananaev <anton.tananaev@gmail.com> | 2021-06-12 11:25:40 -0700 |
---|---|---|
committer | Anton Tananaev <anton.tananaev@gmail.com> | 2021-06-12 11:25:47 -0700 |
commit | b9ceb322b85493d2b8065b2614b3fda8c73194a1 (patch) | |
tree | 9f6585ef6c2f0fb45738456ce4c97d96eed11e59 | |
parent | 79c10ee538392cbca4a0573919296a98816e98ad (diff) | |
download | trackermap-server-b9ceb322b85493d2b8065b2614b3fda8c73194a1.tar.gz trackermap-server-b9ceb322b85493d2b8065b2614b3fda8c73194a1.tar.bz2 trackermap-server-b9ceb322b85493d2b8065b2614b3fda8c73194a1.zip |
Support Kingwo protocol
-rw-r--r-- | src/main/java/org/traccar/protocol/UproProtocolDecoder.java | 66 | ||||
-rw-r--r-- | src/test/java/org/traccar/protocol/UproProtocolDecoderTest.java | 3 |
2 files changed, 61 insertions, 8 deletions
diff --git a/src/main/java/org/traccar/protocol/UproProtocolDecoder.java b/src/main/java/org/traccar/protocol/UproProtocolDecoder.java index a17003fc8..05e6fd811 100644 --- a/src/main/java/org/traccar/protocol/UproProtocolDecoder.java +++ b/src/main/java/org/traccar/protocol/UproProtocolDecoder.java @@ -1,5 +1,5 @@ /* - * Copyright 2012 - 2019 Anton Tananaev (anton@traccar.org) + * Copyright 2012 - 2021 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. @@ -127,6 +127,7 @@ public class UproProtocolDecoder extends BaseProtocolDecoder { Position position = new Position(getProtocolName()); position.setDeviceId(deviceSession.getDeviceId()); + Network network = new Network(); String type = parser.next(); String subtype = parser.next(); @@ -135,7 +136,7 @@ public class UproProtocolDecoder extends BaseProtocolDecoder { channel.writeAndFlush(new NetworkMessage("*" + head + "Y" + type + subtype + "#", remoteAddress)); } - while (buf.isReadable()) { + while (buf.readableBytes() > 1) { buf.readByte(); // skip delimiter @@ -143,10 +144,15 @@ public class UproProtocolDecoder extends BaseProtocolDecoder { int delimiterIndex = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) '&'); if (delimiterIndex < 0) { - delimiterIndex = buf.writerIndex(); + delimiterIndex = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) '#'); + if (delimiterIndex < 0) { + delimiterIndex = buf.writerIndex(); + } } ByteBuf data = buf.readSlice(delimiterIndex - buf.readerIndex()); + int mcc = 0, mnc = 0, count; + String stringValue; switch (dataType) { case 'A': @@ -171,6 +177,22 @@ public class UproProtocolDecoder extends BaseProtocolDecoder { position.setAltitude( Integer.parseInt(data.readSlice(6).toString(StandardCharsets.US_ASCII)) * 0.1); break; + case 'I': + stringValue = data.toString(StandardCharsets.US_ASCII); + count = Integer.parseInt(stringValue.substring(0, 1)); + if (stringValue.length() == 6 + count * 10) { + mcc = Integer.parseInt(stringValue.substring(1, 4)); + mnc = Integer.parseInt(stringValue.substring(4, 6)); + for (int i = 0; i < count; i++) { + int offset = 6 + i * 10; + network.addCellTower(CellTower.from( + mcc, mnc, + Integer.parseInt(stringValue.substring(offset, offset + 4), 16), + Integer.parseInt(stringValue.substring(offset + 4, offset + 8), 16), + Integer.parseInt(stringValue.substring(offset + 8, offset + 10)))); + } + } + break; case 'J': if (data.readableBytes() == 6) { char index = (char) data.readUnsignedByte(); @@ -215,7 +237,9 @@ public class UproProtocolDecoder extends BaseProtocolDecoder { } break; case 'Q': - position.set("obdPid", ByteBufUtil.hexDump(data)); + if (!head.startsWith("HQ")) { + position.set("obdPid", ByteBufUtil.hexDump(data)); + } break; case 'R': if (head.startsWith("HQ")) { @@ -230,6 +254,12 @@ public class UproProtocolDecoder extends BaseProtocolDecoder { case 'S': position.set("obdTraffic", ByteBufUtil.hexDump(data)); break; + case 'T': + if (data.readableBytes() == 2) { + position.set(Position.KEY_BATTERY_LEVEL, + Integer.parseInt(data.toString(StandardCharsets.US_ASCII))); + } + break; case 'V': position.set(Position.KEY_POWER, Integer.parseInt(data.readSlice(4).toString(StandardCharsets.US_ASCII)) * 0.1); @@ -239,8 +269,6 @@ public class UproProtocolDecoder extends BaseProtocolDecoder { decodeAlarm(Integer.parseInt(data.readSlice(2).toString(StandardCharsets.US_ASCII)))); break; case 'X': - Network network = new Network(); - int mcc = 0, mnc = 0; String[] cells = data.toString(StandardCharsets.US_ASCII).split(";"); if (!cells[0].startsWith("(")) { for (int i = 0; i < cells.length; i++) { @@ -260,8 +288,26 @@ public class UproProtocolDecoder extends BaseProtocolDecoder { } break; case 'Y': - position.set(Position.KEY_POWER, - Integer.parseInt(data.readSlice(5).toString(StandardCharsets.US_ASCII)) * 0.001); + stringValue = data.toString(StandardCharsets.US_ASCII); + count = Integer.parseInt(stringValue.substring(0, 1)); + if (stringValue.length() == 6 + count * 14) { + mcc = Integer.parseInt(stringValue.substring(1, 4)); + mnc = Integer.parseInt(stringValue.substring(4, 6)); + for (int i = 0; i < count; i++) { + int offset = 6 + i * 14; + network.addCellTower(CellTower.from( + mcc, mnc, + Integer.parseInt(stringValue.substring(offset, offset + 4), 16), + Long.parseLong(stringValue.substring(offset + 4, offset + 12), 16), + Integer.parseInt(stringValue.substring(offset + 12, offset + 14)))); + } + } else { + position.set(Position.KEY_POWER, + Integer.parseInt(data.readSlice(5).toString(StandardCharsets.US_ASCII)) * 0.001); + } + break; + case 'b': + position.set("serial", data.toString(StandardCharsets.US_ASCII).substring(3)); break; default: break; @@ -269,6 +315,10 @@ public class UproProtocolDecoder extends BaseProtocolDecoder { } + if (network.getCellTowers() != null || network.getWifiAccessPoints() != null) { + position.setNetwork(network); + } + if (position.getLatitude() == 0 || position.getLongitude() == 0) { if (position.getAttributes().isEmpty()) { return null; diff --git a/src/test/java/org/traccar/protocol/UproProtocolDecoderTest.java b/src/test/java/org/traccar/protocol/UproProtocolDecoderTest.java index 0369f47ed..8c12f48f7 100644 --- a/src/test/java/org/traccar/protocol/UproProtocolDecoderTest.java +++ b/src/test/java/org/traccar/protocol/UproProtocolDecoderTest.java @@ -12,6 +12,9 @@ public class UproProtocolDecoderTest extends ProtocolTest { var decoder = new UproProtocolDecoder(null); verifyPosition(decoder, buffer( + "*HQ201999999,BA&A1656512233362911356523660000230618&B0100060010&C00000<6<&F0000&R2405&V0109&W0000003E&K00100&T65&I54600027A00FCB6227A00FCA5727A00E955327A00E8B5327A00F9748&Y54600027A000000FCB6227A000000FCA5727A000000E955327A000000E8B5327A000000F9748&b00A7E81007607#")); + + verifyPosition(decoder, buffer( "*HQ201861909268000132,BA&A1820223307024309650492530000311019&B0100000000&F0000&V0036&R0500&J000182&M0052&W00000091&I231026027BD39090827BD5ACA04&X(501E0)(B0000)(E0136)(J01E0)(L3)(k8937204016201240376F)&K00200&T85&N01#")); verifyAttribute(decoder, buffer( |