From 0e8057c605e38ecae49c6c69941a46075507302a Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Thu, 18 May 2023 07:55:34 -0700 Subject: Clean up TranSync decoder --- setup/default.xml | 1 + .../org/traccar/protocol/TranSyncProtocol.java | 5 +- .../traccar/protocol/TranSyncProtocolDecoder.java | 227 +++++++-------------- .../protocol/TranSyncProtocolDecoderTest.java | 8 +- 4 files changed, 89 insertions(+), 152 deletions(-) diff --git a/setup/default.xml b/setup/default.xml index ff8ecf589..e8dc7aa79 100644 --- a/setup/default.xml +++ b/setup/default.xml @@ -288,5 +288,6 @@ 5244 5245 5246 + 5247 diff --git a/src/main/java/org/traccar/protocol/TranSyncProtocol.java b/src/main/java/org/traccar/protocol/TranSyncProtocol.java index 5422380b6..fcc02a781 100644 --- a/src/main/java/org/traccar/protocol/TranSyncProtocol.java +++ b/src/main/java/org/traccar/protocol/TranSyncProtocol.java @@ -1,5 +1,5 @@ /* - * Copyright 2013 - 2023 Anton Tananaev (anton@traccar.org) + * Copyright 2023 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. @@ -30,9 +30,10 @@ public class TranSyncProtocol extends BaseProtocol { addServer(new TrackerServer(config, getName(), false) { @Override protected void addProtocolHandlers(PipelineBuilder pipeline, Config config) { - pipeline.addLast(new LengthFieldBasedFrameDecoder(256, 2, 1, 2, 0, true)); + pipeline.addLast(new LengthFieldBasedFrameDecoder(256, 2, 1, 2, 0)); pipeline.addLast(new TranSyncProtocolDecoder(TranSyncProtocol.this)); } }); } + } diff --git a/src/main/java/org/traccar/protocol/TranSyncProtocolDecoder.java b/src/main/java/org/traccar/protocol/TranSyncProtocolDecoder.java index 130c916b1..816b5d2cf 100644 --- a/src/main/java/org/traccar/protocol/TranSyncProtocolDecoder.java +++ b/src/main/java/org/traccar/protocol/TranSyncProtocolDecoder.java @@ -1,5 +1,5 @@ /* - * Copyright 2013 - 2023 Anton Tananaev (anton@traccar.org) + * Copyright 2023 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. @@ -29,118 +29,32 @@ import org.traccar.model.Position; import org.traccar.session.DeviceSession; import java.net.SocketAddress; -import java.util.Arrays; public class TranSyncProtocolDecoder extends BaseProtocolDecoder { - private static final byte[] STX = new byte[]{0x3a, 0x3a}; - - private String getHardwareType(int type) { - - switch (type) { - case 1: - return "basic"; - case 2: - return "asset"; - case 3: - return "bike"; - case 4: - return "serial"; - case 5: - return "obd"; - case 6: - return "l1"; - case 7: - return "ais-140"; - default: - return "unknown"; - } - } - public TranSyncProtocolDecoder(Protocol protocol) { super(protocol); } - protected void decodeAlarm(Position position, int value) { + private String decodeAlarm(int value) { switch (value) { + case 4: + return Position.ALARM_LOW_BATTERY; + case 6: + return Position.ALARM_POWER_RESTORED; case 10: - position.set(Position.KEY_ALARM, Position.ALARM_SOS); - break; - case 11: - position.set(Position.KEY_EVENT, 11); - break; - case 16: - position.set(Position.KEY_EVENT, 16); - break; - case 3: - position.set(Position.KEY_ALARM, 3); - break; - case 22: - position.set(Position.KEY_ALARM, 22); - break; - case 9: - position.set(Position.KEY_EVENT, 9); - case 17: - position.set(Position.KEY_ALARM, Position.ALARM_OVERSPEED); - break; + return Position.ALARM_SOS; case 13: - position.set(Position.KEY_ALARM, Position.ALARM_BRAKING); - break; + return Position.ALARM_BRAKING; case 14: - position.set(Position.KEY_ALARM, Position.ALARM_ACCELERATION); - break; - case 15: - position.set(Position.KEY_EVENT, 15); - break; + return Position.ALARM_ACCELERATION; + case 17: + return Position.ALARM_OVERSPEED; case 23: - position.set(Position.KEY_ALARM, Position.ALARM_ACCIDENT); - break; - case 12: - position.set(Position.KEY_EVENT, 12); - break; - case 6: - position.set(Position.KEY_ALARM, Position.ALARM_POWER_RESTORED); - break; - case 4: - position.set(Position.KEY_ALARM, Position.ALARM_LOW_BATTERY); - break; - case 5: - position.set(Position.KEY_EVENT, 5); - break; + return Position.ALARM_ACCIDENT; default: - position.set(Position.KEY_EVENT, "unknown"); - } - } - private void analizeNegativePosition(Position position, int value) { - - if (!BitUtil.check(value, 1)) { - position.setLatitude(-position.getLatitude()); // 0/1 S/N - } - if (!BitUtil.check(value, 2)) { - position.setLongitude(-position.getLongitude()); // 0/1 W/E - } - } - - private void decodePowerEngineParameters(Position position, int value) { - - position.set(Position.PREFIX_OUT + 1, BitUtil.check(value, 7)); - position.set(Position.PREFIX_OUT + 2, BitUtil.check(value, 6)); - position.set(Position.PREFIX_IN + 3, BitUtil.check(value, 5)); - if (BitUtil.check(value, 4)) { - position.set(Position.KEY_ALARM, Position.ALARM_POWER_OFF); - } - position.set(Position.KEY_IGNITION, BitUtil.check(value, 3)); - position.set("gpsFix", BitUtil.check(value, 0)); - } - - private void decodeTrackerStatusParameters(Position position, int value) { - if (BitUtil.check(value, 7)) { - position.set(Position.KEY_ARCHIVE, true); - } - if (BitUtil.check(value, 5)) { - position.set(Position.KEY_ALARM, Position.ALARM_GPS_ANTENNA_CUT); + return null; } - position.set(Position.KEY_VERSION_HW, getHardwareType(BitUtil.between(value, 0, 3))); } @Override @@ -148,88 +62,105 @@ public class TranSyncProtocolDecoder extends BaseProtocolDecoder { ByteBuf buf = (ByteBuf) msg; - if (Arrays.equals(ByteBufUtil.getBytes(buf, 0, 2), STX)) { - buf.readUnsignedShort(); - } - buf.readByte(); //packetLength - - int locationAreaCode = buf.readUnsignedShort(); - String deviceId = ByteBufUtil.hexDump(buf.readSlice(8)); + buf.readUnsignedShort(); // header + buf.readByte(); // length - buf.readUnsignedShort(); //informationSerialNumber - buf.readUnsignedByte(); //protocolNumber + int lac = buf.readUnsignedShort(); + String deviceId = ByteBufUtil.hexDump(buf.readSlice(8)); DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, deviceId); if (deviceSession == null) { return null; } + Position position = new Position(getProtocolName()); position.setDeviceId(deviceSession.getDeviceId()); - position.setValid(true); + + buf.readUnsignedShort(); // index + buf.readUnsignedByte(); // type + position.setTime(new DateBuilder() .setDate(buf.readUnsignedByte(), buf.readUnsignedByte(), buf.readUnsignedByte()) .setTime(buf.readUnsignedByte(), buf.readUnsignedByte(), buf.readUnsignedByte()) .getDate()); - position.setLatitude(buf.readUnsignedInt() / 1800000.0); - position.setLongitude(buf.readUnsignedInt() / 1800000.0); + + double latitude = buf.readUnsignedInt() / 1800000.0; + double longitude = buf.readUnsignedInt() / 1800000.0; + position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedByte())); position.setCourse(buf.readUnsignedShort()); - int mobileNetworkCode = buf.readUnsignedByte(); - int cellTowerId = buf.readUnsignedShort(); - int statusParameters = buf.readUnsignedByte(); + int mnc = buf.readUnsignedByte(); + int cid = buf.readUnsignedShort(); + int status0 = buf.readUnsignedByte(); - decodePowerEngineParameters(position, statusParameters); - analizeNegativePosition(position, statusParameters); + position.setValid(BitUtil.check(status0, 0)); + position.setLatitude(BitUtil.check(status0, 1) ? latitude : -latitude); + position.setLongitude(BitUtil.check(status0, 2) ? longitude : -longitude); + + position.set(Position.PREFIX_OUT + 1, BitUtil.check(status0, 7)); + position.set(Position.PREFIX_OUT + 2, BitUtil.check(status0, 6)); + position.set(Position.PREFIX_IN + 3, BitUtil.check(status0, 5)); + if (BitUtil.check(status0, 4)) { + position.set(Position.KEY_ALARM, Position.ALARM_POWER_OFF); + } + position.set(Position.KEY_IGNITION, BitUtil.check(status0, 3)); buf.readUnsignedByte(); // reserved - decodeAlarm(position, buf.readUnsignedByte()); + int event = buf.readUnsignedByte(); + position.set(Position.KEY_ALARM, decodeAlarm(event)); + position.set(Position.KEY_EVENT, event); - decodeTrackerStatusParameters(position, buf.readUnsignedByte()); + int status3 = buf.readUnsignedByte(); + if (BitUtil.check(status3, 7)) { + position.set(Position.KEY_ARCHIVE, true); + } + if (BitUtil.check(status3, 5)) { + position.set(Position.KEY_ALARM, Position.ALARM_GPS_ANTENNA_CUT); + } - int gsmSignalStrength = buf.readUnsignedByte(); + int rssi = buf.readUnsignedByte(); + CellTower cellTower = CellTower.fromLacCid(getConfig(), lac, cid); + cellTower.setMobileNetworkCode(mnc); + cellTower.setSignalStrength(rssi); + position.setNetwork(new Network(cellTower)); position.set(Position.KEY_BATTERY, (double) (buf.readUnsignedByte() / 10)); position.set(Position.KEY_SATELLITES, buf.readUnsignedByte()); position.set(Position.KEY_HDOP, buf.readUnsignedByte()); position.set(Position.PREFIX_ADC + 1, (short) buf.readUnsignedShort()); - CellTower cellTower = CellTower.fromLacCid(getConfig(), locationAreaCode, cellTowerId); - cellTower.setMobileNetworkCode(mobileNetworkCode); - cellTower.setSignalStrength(gsmSignalStrength); - - position.setNetwork(new Network(cellTower)); - if (buf.readableBytes() > 5) { - buf.readUnsignedByte(); // odometerIndex - int odometerLength = buf.readUnsignedByte(); - if (odometerLength > 0) { - int odometer = buf.readBytes(odometerLength).readInt(); - position.set(Position.KEY_ODOMETER, odometer); + buf.readUnsignedByte(); // odometer id + int length = buf.readUnsignedByte(); + if (length > 0) { + position.set(Position.KEY_ODOMETER, buf.readBytes(length).readInt()); } - if ((buf.readableBytes() > 5)) { - buf.readUnsignedByte(); // tagIndex - int tagLength = buf.readUnsignedByte(); - if (tagLength > 0) { - position.set("tag", ByteBufUtil.hexDump(buf.readSlice(tagLength))); - } + } + if (buf.readableBytes() > 5) { + buf.readUnsignedByte(); // rfid id + int length = buf.readUnsignedByte(); + if (length > 0) { + position.set(Position.KEY_DRIVER_UNIQUE_ID, ByteBufUtil.hexDump(buf.readSlice(length))); } - if ((buf.readableBytes() > 5)) { - buf.readUnsignedByte(); // adc2Index - int adc2Length = buf.readUnsignedByte(); - if (adc2Length > 0) { - position.set(Position.PREFIX_ADC + 2, buf.readUnsignedShort()); - } + } + if (buf.readableBytes() > 5) { + buf.readUnsignedByte(); // adc2 id + int length = buf.readUnsignedByte(); + if (length > 0) { + position.set(Position.PREFIX_ADC + 2, buf.readUnsignedShort()); } - if ((buf.readableBytes() > 5)) { - buf.readUnsignedByte(); // adc3Index - int adc2Length = buf.readUnsignedByte(); - if (adc2Length > 0 && adc2Length <= buf.readableBytes() - 2) { - position.set(Position.PREFIX_ADC + 3, buf.readUnsignedShort()); - } + } + if (buf.readableBytes() > 5) { + buf.readUnsignedByte(); // adc3 id + int length = buf.readUnsignedByte(); + if (length > 0 && length <= buf.readableBytes() - 2) { + position.set(Position.PREFIX_ADC + 3, buf.readUnsignedShort()); } } + return position; } + } diff --git a/src/test/java/org/traccar/protocol/TranSyncProtocolDecoderTest.java b/src/test/java/org/traccar/protocol/TranSyncProtocolDecoderTest.java index c84ae2e0f..27f53b574 100644 --- a/src/test/java/org/traccar/protocol/TranSyncProtocolDecoderTest.java +++ b/src/test/java/org/traccar/protocol/TranSyncProtocolDecoderTest.java @@ -10,8 +10,12 @@ public class TranSyncProtocolDecoderTest extends ProtocolTest { var decoder = inject(new TranSyncProtocolDecoder(null)); - verifyPosition(decoder, binary("3a3a2b583f086065705154043900801017050b11190f01623ef40887dff00000c25e9ff707000007152a2d0000000105004794916902050000100000050252ee060200822323")); + verifyPosition(decoder, binary( + "3a3a2b583f086065705154043900801017050b11190f01623ef40887dff00000c25e9ff707000007152a2d0000000105004794916902050000100000050252ee060200822323")); + + verifyAttributes(decoder, binary( + "3a3a2b583f086065705154043900801017050b11190f01623ef40887dff00000c25e9ff707000007152a2d0000000105004794916902050000000000050252ee060200822323")); - verifyAttributes(decoder, binary("3a3a2b583f086065705154043900801017050b11190f01623ef40887dff00000c25e9ff707000007152a2d0000000105004794916902050000000000050252ee060200822323")); } + } -- cgit v1.2.3