aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/traccar/protocol/TranSyncProtocolDecoder.java
diff options
context:
space:
mode:
authorAnton Tananaev <anton@traccar.org>2023-05-18 07:55:34 -0700
committerAnton Tananaev <anton@traccar.org>2023-05-18 07:55:34 -0700
commit0e8057c605e38ecae49c6c69941a46075507302a (patch)
tree2bff0ddfb32d6ee43b3fb24397e7a3c97c069db2 /src/main/java/org/traccar/protocol/TranSyncProtocolDecoder.java
parent22277c84db8b19fdda252aa21db3a2bf62d6cba2 (diff)
downloadtrackermap-server-0e8057c605e38ecae49c6c69941a46075507302a.tar.gz
trackermap-server-0e8057c605e38ecae49c6c69941a46075507302a.tar.bz2
trackermap-server-0e8057c605e38ecae49c6c69941a46075507302a.zip
Clean up TranSync decoder
Diffstat (limited to 'src/main/java/org/traccar/protocol/TranSyncProtocolDecoder.java')
-rw-r--r--src/main/java/org/traccar/protocol/TranSyncProtocolDecoder.java227
1 files changed, 79 insertions, 148 deletions
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;
}
+
}