From 6dae98179630a15c5f62899b32ed093e293822e2 Mon Sep 17 00:00:00 2001 From: Nikolay Vlahovski Date: Mon, 15 May 2023 19:42:22 +0300 Subject: Add [Protocol]TranSyncProtocol Add TranSyncProtocol.java Add TranSyncProtocolDecoder.java Add TranSyncProtocolDecoderTest.java --- .../org/traccar/protocol/TranSyncProtocol.java | 22 ++ .../traccar/protocol/TranSyncProtocolDecoder.java | 353 +++++++++++++++++++++ 2 files changed, 375 insertions(+) create mode 100644 src/main/java/org/traccar/protocol/TranSyncProtocol.java create mode 100644 src/main/java/org/traccar/protocol/TranSyncProtocolDecoder.java (limited to 'src/main/java/org/traccar') diff --git a/src/main/java/org/traccar/protocol/TranSyncProtocol.java b/src/main/java/org/traccar/protocol/TranSyncProtocol.java new file mode 100644 index 000000000..0634642df --- /dev/null +++ b/src/main/java/org/traccar/protocol/TranSyncProtocol.java @@ -0,0 +1,22 @@ +package org.traccar.protocol; + +import org.traccar.BaseProtocol; +import org.traccar.PipelineBuilder; +import org.traccar.TrackerServer; +import org.traccar.config.Config; + +import javax.inject.Inject; + +public class TranSyncProtocol extends BaseProtocol { + + @Inject + public TranSyncProtocol(Config config) { + addServer(new TrackerServer(config, getName(), false) { + @Override + protected void addProtocolHandlers(PipelineBuilder pipeline, Config config) { + 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 new file mode 100644 index 000000000..7cafc09ee --- /dev/null +++ b/src/main/java/org/traccar/protocol/TranSyncProtocolDecoder.java @@ -0,0 +1,353 @@ +package org.traccar.protocol; + +import io.netty.buffer.ByteBuf; +import io.netty.buffer.ByteBufUtil; +import io.netty.channel.Channel; +import org.traccar.BaseProtocolDecoder; +import org.traccar.Protocol; +import org.traccar.helper.BitUtil; +import org.traccar.helper.UnitsConverter; +import org.traccar.model.CellTower; +import org.traccar.model.Network; +import org.traccar.model.Position; +import org.traccar.session.DeviceSession; + +import java.net.SocketAddress; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.TimeZone; + +public class TranSyncProtocolDecoder extends BaseProtocolDecoder { + + public TranSyncProtocolDecoder(Protocol protocol) { + super(protocol); + } + + private boolean isOptionalParameters; + private boolean extended; + private final String STX = "3A3A"; + private final String ETX = "2323"; + private String lac; + private String deviceId; + private Date datePacket; + private double latitude; + private double longitude; + private double speed; + private int course; + private int mobileNetworkCode; + private int gsmSignalStrength; + private double batteryVoltage; + private int satellitesNumber; + private int hdopProtocol; + private short adcVoltageInMilliVolts; + private String rfidTagName; + private int adc2; + private int cellIdInt; + private int lacInt; + private int odometer; + private boolean isGpsFix; + private boolean isLiveData; + private boolean hasGpsAlert; + private boolean isIgnitionOff; + private boolean isPowerOff; + private int deviceAlert; + private String gpsTrackerModel; + private boolean isOutPutTwo; + private boolean isOutPutOne; + private boolean isInPutThree; + + + + public String getLac() { + return lac; + } + + public void setLac(String lac) { + this.lac = lac; + } + + public String getDeviceId() { + return deviceId; + } + + public void setDeviceId(String deviceId) { + this.deviceId = deviceId; + } + + public void setDatePacket(Date datePacket) { + this.datePacket = datePacket; + } + + public double getLatitude() { + return latitude; + } + + public void setLatitude(double latitude) { + this.latitude = latitude; + } + + public double getLongitude() { + return longitude; + } + + public void setLongitude(double longitude) { + this.longitude = longitude; + } + + public int getCourse() { + return course; + } + + public void setCourse(int course) { + this.course = course; + } + + public void setCellId(String cellId) { + } + + public int getAdc2() { + return adc2; + } + + public void setAdc2(String adc2) { + this.adc2 = Integer.parseInt(adc2); + } + + public double getSpeed() { + return speed; + } + + public void setSpeed(double speed) { + this.speed = speed; + } + + public void setTimestampPacket(int year, int month, int day, int hours, int minutes, int seconds) { + + String dataString = ""+year + "-" + month + "-" + day + " " + hours + ":" + minutes + ":" + seconds; + SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yy-MM-dd HH:mm:ss"); + simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC")); + try { + Date deviceTime = simpleDateFormat.parse(dataString); + long timestampPacket = deviceTime.toInstant().toEpochMilli(); + this.setDatePacket(deviceTime); + + } catch (ParseException e) { + e.printStackTrace(); + } + } + + protected void setDeviceAlert(Position position, int alert) { + switch (alert) { + case 10: + position.set(Position.ALARM_SOS, true); + break; + case 11: + position.set(Position.ALARM_SOS, false); + break; + case 16: + position.set(Position.KEY_EVENT, 16); + break; + case 3: + position.set("distressCutOff", true); + break; + case 22: + position.set("tilt", true); + break; + case 9: + position.set(Position.KEY_EVENT, 9); + case 17: + position.set(Position.ALARM_OVERSPEED, true); + break; + case 13: + position.set(Position.ALARM_BRAKING, true); + break; + case 14: + position.set(Position.ALARM_ACCELERATION, true); + break; + case 15: + position.set(Position.KEY_EVENT, 15); + break; + case 23: + position.set(Position.ALARM_ACCIDENT, true); + break; + case 12: + position.set(Position.KEY_EVENT, 12); + break; + case 6: + position.set(Position.ALARM_POWER_RESTORED, true); + break; + case 4: + position.set(Position.ALARM_LOW_BATTERY, true); + break; + case 5: + position.set(Position.KEY_EVENT, 5); + break; + + } + + } + + protected void setInstanceParameters(Object msg) throws Exception { + ByteBuf buf = (ByteBuf) msg; + + if (ByteBufUtil.hexDump(buf, 0, 2).toUpperCase().equals(this.STX)) { + buf.readUnsignedShort(); + } + int packetLength = buf.readByte(); + this.lacInt = buf.readUnsignedShort(); + this.deviceId = ByteBufUtil.hexDump(buf, buf.readerIndex(), 8).toUpperCase().trim().replaceFirst("^0+(?!$)", ""); + buf.readBytes(8); + int informationSerialNumber = buf.readUnsignedShort(); + int protocolNumber = buf.readUnsignedByte(); + // Information Content + int year = buf.readUnsignedByte(); + int month = buf.readUnsignedByte(); + int day = buf.readUnsignedByte(); + int hour = buf.readUnsignedByte(); + int minute = buf.readUnsignedByte(); + int second = buf.readUnsignedByte(); + this.setTimestampPacket(year, month, day, hour, minute, second); + this.latitude = (long) buf.readUnsignedInt() / 1800000.0; + this.longitude = (long) buf.readUnsignedInt() / 1800000.0; + this.speed = UnitsConverter.knotsFromKph(buf.readUnsignedByte()); + this.course = buf.readUnsignedShort(); + this.mobileNetworkCode = buf.readUnsignedByte(); + this.cellIdInt = buf.readUnsignedShort(); + // Status Bytes + int zeroByte = buf.readUnsignedByte(); + this.isOutPutTwo = BitUtil.check(zeroByte, 0); + this.isOutPutOne = BitUtil.check(zeroByte, 1); + this.isInPutThree = BitUtil.check(zeroByte, 2); + this.isPowerOff = BitUtil.check(zeroByte, 3); + this.isIgnitionOff = BitUtil.check(zeroByte, 4); + boolean isgpsFixed = BitUtil.check(zeroByte, 7); + + + this.isGpsFix = BitUtil.check(zeroByte, 0); + int oneByte = buf.readUnsignedByte(); + this.deviceAlert = buf.readUnsignedByte(); + int threeByte = buf.readUnsignedByte(); + this.isLiveData = (threeByte & 0b10000000) == 0; + this.hasGpsAlert = (threeByte & 0b00100000) != 0; + int trackerType = threeByte & 0b00001111; + + switch (trackerType) { + case 1: + this.gpsTrackerModel = "basic"; + break; + case 2: + this.gpsTrackerModel = "asset"; + break; + case 3: + this.gpsTrackerModel = "bike"; + break; + case 4: + this.gpsTrackerModel = "serial"; + break; + case 5: + this.gpsTrackerModel = "obd"; + break; + case 6: + this.gpsTrackerModel = "l1"; + break; + case 7: + this.gpsTrackerModel = "ais"; + break; + default: + this.gpsTrackerModel = "unknown"; + break; + } + // Additional Info + this.gsmSignalStrength = buf.readUnsignedByte(); + this.batteryVoltage = (double) (buf.readUnsignedByte() / 10); + this.satellitesNumber = buf.readUnsignedByte(); + this.hdopProtocol = buf.readUnsignedByte(); + this.adcVoltageInMilliVolts = (short) buf.readUnsignedShort(); + this.isOptionalParameters = (boolean) (buf.readableBytes() > 2); + // Optional parameters + if (this.isOptionalParameters) { // Always True + int odometerIndex = buf.readUnsignedByte(); + int odometerLength = buf.readUnsignedByte(); + if (odometerLength > 0) { + String odometerReading = ByteBufUtil.hexDump(buf, buf.readerIndex(), odometerLength).toUpperCase().trim().replaceFirst("^0+(?!$)", ""); + this.odometer = Integer.parseInt(odometerReading); + buf.readBytes(odometerLength); + } + if ((buf.readableBytes() > 2)) { + int rfidIndex = buf.readUnsignedByte(); + int rfidLength = buf.readUnsignedByte(); + if(rfidLength > 0) { + this.rfidTagName = ByteBufUtil.hexDump(buf, buf.readerIndex(), rfidLength).toUpperCase().trim(); + buf.readBytes(rfidLength); + } + } + if ((buf.readableBytes() > 5)) { + int adcTwoIndex = buf.readUnsignedByte(); + int adcTwoLength = buf.readUnsignedByte(); + if (adcTwoLength > 0){ + this.adc2 = buf.readUnsignedShort(); + } + } + } + + } + + private Position positionHandler(Channel channel, SocketAddress remoteAddress) { + + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, this.deviceId); + if (deviceSession == null) { + return null; + } + Position position = new Position(getProtocolName()); + long devicesessionId = deviceSession.getDeviceId(); + position.setDeviceId(devicesessionId) ; + position.setValid(true); + position.setTime(this.datePacket); + position.setLatitude(this.latitude); + position.setLongitude(this.longitude); + position.setSpeed(this.speed); + position.setCourse(this.course); + position.set("model", this.gpsTrackerModel); + position.set(Position.KEY_HDOP, this.hdopProtocol); + position.set(Position.KEY_BATTERY, this.batteryVoltage); + position.set(Position.KEY_ODOMETER, this.odometer); + position.set(Position.PREFIX_ADC + 1, this.adcVoltageInMilliVolts); + position.set(Position.PREFIX_ADC + 2, this.adc2); + position.set("tag", this.rfidTagName); + position.set(Position.KEY_SATELLITES, this.satellitesNumber); + CellTower cellTower = CellTower.fromLacCid(getConfig(), this.lacInt, this.cellIdInt); + cellTower.setMobileNetworkCode(this.mobileNetworkCode); + cellTower.setSignalStrength(this.gsmSignalStrength); + position.setNetwork(new Network(cellTower)); + if (this.isOutPutOne){ position.set(Position.PREFIX_OUT + 1, this.isOutPutOne);} + if (this.isOutPutTwo){ position.set(Position.PREFIX_OUT + 2, this.isOutPutTwo);} + if (this.isInPutThree){ position.set(Position.PREFIX_IN + 2, this.isInPutThree);} + if (this.isGpsFix) { position.set("gpsFix", this.isGpsFix); } + if (!this.isLiveData) { position.set("restored", true); } + if (this.hasGpsAlert) { + position.set(Position.ALARM_GPS_ANTENNA_CUT, true); + position.set("gpsAlert", true); + } + position.set(Position.KEY_IGNITION, this.isIgnitionOff); + if (isPowerOff){ position.set(Position.ALARM_POWER_OFF, this.isPowerOff );} + position.set(Position.KEY_GPS, true); + if (this.deviceAlert > 0) { setDeviceAlert(position, this.deviceAlert); } + + return position; + } + + @Override + protected Object decode( Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { + + try { + setInstanceParameters(msg); + } + catch (Exception e) { + return null; + } + + return positionHandler(channel, remoteAddress); + } + +} -- cgit v1.2.3 From cd092d7e2733881266a3b864743df61e6ac76ce4 Mon Sep 17 00:00:00 2001 From: Nikolay Vlahovski Date: Mon, 15 May 2023 21:55:20 +0300 Subject: Remove Instance variables from TranSyncProtocolDecoder.java Add parseDate function Add getTrackerModel function Add setNetwork function Rename setTimestampPacket function -> getDatefromIntegerParameters --- .../traccar/protocol/TranSyncProtocolDecoder.java | 303 ++++++--------------- 1 file changed, 84 insertions(+), 219 deletions(-) (limited to 'src/main/java/org/traccar') diff --git a/src/main/java/org/traccar/protocol/TranSyncProtocolDecoder.java b/src/main/java/org/traccar/protocol/TranSyncProtocolDecoder.java index 7cafc09ee..d5a69dbe2 100644 --- a/src/main/java/org/traccar/protocol/TranSyncProtocolDecoder.java +++ b/src/main/java/org/traccar/protocol/TranSyncProtocolDecoder.java @@ -24,117 +24,18 @@ public class TranSyncProtocolDecoder extends BaseProtocolDecoder { super(protocol); } - private boolean isOptionalParameters; - private boolean extended; - private final String STX = "3A3A"; - private final String ETX = "2323"; - private String lac; - private String deviceId; - private Date datePacket; - private double latitude; - private double longitude; - private double speed; - private int course; - private int mobileNetworkCode; - private int gsmSignalStrength; - private double batteryVoltage; - private int satellitesNumber; - private int hdopProtocol; - private short adcVoltageInMilliVolts; - private String rfidTagName; - private int adc2; - private int cellIdInt; - private int lacInt; - private int odometer; - private boolean isGpsFix; - private boolean isLiveData; - private boolean hasGpsAlert; - private boolean isIgnitionOff; - private boolean isPowerOff; - private int deviceAlert; - private String gpsTrackerModel; - private boolean isOutPutTwo; - private boolean isOutPutOne; - private boolean isInPutThree; - - - - public String getLac() { - return lac; - } - - public void setLac(String lac) { - this.lac = lac; - } - - public String getDeviceId() { - return deviceId; - } - - public void setDeviceId(String deviceId) { - this.deviceId = deviceId; - } - - public void setDatePacket(Date datePacket) { - this.datePacket = datePacket; - } - - public double getLatitude() { - return latitude; - } - - public void setLatitude(double latitude) { - this.latitude = latitude; - } - - public double getLongitude() { - return longitude; - } - - public void setLongitude(double longitude) { - this.longitude = longitude; - } - - public int getCourse() { - return course; - } - - public void setCourse(int course) { - this.course = course; - } - - public void setCellId(String cellId) { - } - - public int getAdc2() { - return adc2; - } - - public void setAdc2(String adc2) { - this.adc2 = Integer.parseInt(adc2); - } - - public double getSpeed() { - return speed; - } - - public void setSpeed(double speed) { - this.speed = speed; - } - - public void setTimestampPacket(int year, int month, int day, int hours, int minutes, int seconds) { + public Date getDatefromIntegerParameters(int year, int month, int day, int hours, int minutes, int seconds) { String dataString = ""+year + "-" + month + "-" + day + " " + hours + ":" + minutes + ":" + seconds; SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yy-MM-dd HH:mm:ss"); simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC")); try { - Date deviceTime = simpleDateFormat.parse(dataString); - long timestampPacket = deviceTime.toInstant().toEpochMilli(); - this.setDatePacket(deviceTime); + return simpleDateFormat.parse(dataString); } catch (ParseException e) { e.printStackTrace(); } + return null; } protected void setDeviceAlert(Position position, int alert) { @@ -188,166 +89,130 @@ public class TranSyncProtocolDecoder extends BaseProtocolDecoder { } - protected void setInstanceParameters(Object msg) throws Exception { - ByteBuf buf = (ByteBuf) msg; + private void setNetwork(Position position, int lacInt, int cellIdInt, int mobileNetworkCode, int gsmSignalStrength){ + CellTower cellTower = CellTower.fromLacCid(getConfig(), lacInt, cellIdInt); + cellTower.setMobileNetworkCode(mobileNetworkCode); + cellTower.setSignalStrength(gsmSignalStrength); + position.setNetwork(new Network(cellTower)); + + } + + private String getTrackerModel(int mask){ + + switch (mask) { + 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"; + default: + return "unknown"; + } + + } - if (ByteBufUtil.hexDump(buf, 0, 2).toUpperCase().equals(this.STX)) { + private Position parseData(Channel channel, SocketAddress remoteAddress, ByteBuf buf) { + if (ByteBufUtil.hexDump(buf, 0, 2).equalsIgnoreCase("3A3A")) { buf.readUnsignedShort(); } int packetLength = buf.readByte(); - this.lacInt = buf.readUnsignedShort(); - this.deviceId = ByteBufUtil.hexDump(buf, buf.readerIndex(), 8).toUpperCase().trim().replaceFirst("^0+(?!$)", ""); + int lacInt = buf.readUnsignedShort(); + String deviceId = ByteBufUtil.hexDump(buf, buf.readerIndex(), 8).toUpperCase().trim().replaceFirst("^0+(?!$)", ""); buf.readBytes(8); int informationSerialNumber = buf.readUnsignedShort(); int protocolNumber = buf.readUnsignedByte(); - // Information Content + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, deviceId); + if (deviceSession == null) { + return null; + } + + Position position = new Position(getProtocolName()); + long devicesessionId = deviceSession.getDeviceId(); + + position.setDeviceId(devicesessionId) ; + position.setValid(true); int year = buf.readUnsignedByte(); int month = buf.readUnsignedByte(); int day = buf.readUnsignedByte(); int hour = buf.readUnsignedByte(); int minute = buf.readUnsignedByte(); int second = buf.readUnsignedByte(); - this.setTimestampPacket(year, month, day, hour, minute, second); - this.latitude = (long) buf.readUnsignedInt() / 1800000.0; - this.longitude = (long) buf.readUnsignedInt() / 1800000.0; - this.speed = UnitsConverter.knotsFromKph(buf.readUnsignedByte()); - this.course = buf.readUnsignedShort(); - this.mobileNetworkCode = buf.readUnsignedByte(); - this.cellIdInt = buf.readUnsignedShort(); - // Status Bytes + position.setTime(getDatefromIntegerParameters(year, month, day, hour, minute, second)); + position.setLatitude(buf.readUnsignedInt() / 1800000.0); + position.setLongitude(buf.readUnsignedInt() / 1800000.0); + position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedByte())); + position.setCourse(buf.readUnsignedShort()); + int mobileNetworkCode = buf.readUnsignedByte(); + int cellIdInt = buf.readUnsignedShort(); int zeroByte = buf.readUnsignedByte(); - this.isOutPutTwo = BitUtil.check(zeroByte, 0); - this.isOutPutOne = BitUtil.check(zeroByte, 1); - this.isInPutThree = BitUtil.check(zeroByte, 2); - this.isPowerOff = BitUtil.check(zeroByte, 3); - this.isIgnitionOff = BitUtil.check(zeroByte, 4); - boolean isgpsFixed = BitUtil.check(zeroByte, 7); - - - this.isGpsFix = BitUtil.check(zeroByte, 0); + position.set(Position.PREFIX_OUT + 1, BitUtil.check(zeroByte, 0)); + position.set(Position.PREFIX_OUT + 2, BitUtil.check(zeroByte, 1)); + position.set(Position.PREFIX_IN + 3, BitUtil.check(zeroByte, 2)); + if (BitUtil.check(zeroByte, 3)) position.set(Position.ALARM_POWER_OFF, true); + position.set(Position.KEY_IGNITION, BitUtil.check(zeroByte, 4)); + position.set("gpsFix", BitUtil.check(zeroByte, 7)); int oneByte = buf.readUnsignedByte(); - this.deviceAlert = buf.readUnsignedByte(); + int deviceAlert = buf.readUnsignedByte(); + if (deviceAlert > 0) { setDeviceAlert(position, deviceAlert); } int threeByte = buf.readUnsignedByte(); - this.isLiveData = (threeByte & 0b10000000) == 0; - this.hasGpsAlert = (threeByte & 0b00100000) != 0; - int trackerType = threeByte & 0b00001111; - - switch (trackerType) { - case 1: - this.gpsTrackerModel = "basic"; - break; - case 2: - this.gpsTrackerModel = "asset"; - break; - case 3: - this.gpsTrackerModel = "bike"; - break; - case 4: - this.gpsTrackerModel = "serial"; - break; - case 5: - this.gpsTrackerModel = "obd"; - break; - case 6: - this.gpsTrackerModel = "l1"; - break; - case 7: - this.gpsTrackerModel = "ais"; - break; - default: - this.gpsTrackerModel = "unknown"; - break; + if (!((threeByte & 0b10000000) == 0)) position.set("restored", true); + if ((threeByte & 0b00100000) != 0) { + position.set(Position.ALARM_GPS_ANTENNA_CUT, true); + position.set("gpsAlert", true); } - // Additional Info - this.gsmSignalStrength = buf.readUnsignedByte(); - this.batteryVoltage = (double) (buf.readUnsignedByte() / 10); - this.satellitesNumber = buf.readUnsignedByte(); - this.hdopProtocol = buf.readUnsignedByte(); - this.adcVoltageInMilliVolts = (short) buf.readUnsignedShort(); - this.isOptionalParameters = (boolean) (buf.readableBytes() > 2); - // Optional parameters - if (this.isOptionalParameters) { // Always True + position.set("model", getTrackerModel(threeByte & 0b00001111)); + int gsmSignalStrength = buf.readUnsignedByte(); + 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()); + boolean isOptionalParameters = (buf.readableBytes() > 2); + setNetwork(position, lacInt, cellIdInt, mobileNetworkCode, gsmSignalStrength); + if (isOptionalParameters) { // Always True int odometerIndex = buf.readUnsignedByte(); int odometerLength = buf.readUnsignedByte(); if (odometerLength > 0) { String odometerReading = ByteBufUtil.hexDump(buf, buf.readerIndex(), odometerLength).toUpperCase().trim().replaceFirst("^0+(?!$)", ""); - this.odometer = Integer.parseInt(odometerReading); + int odometer = Integer.parseInt(odometerReading); buf.readBytes(odometerLength); + position.set(Position.KEY_ODOMETER, odometer); } if ((buf.readableBytes() > 2)) { int rfidIndex = buf.readUnsignedByte(); int rfidLength = buf.readUnsignedByte(); if(rfidLength > 0) { - this.rfidTagName = ByteBufUtil.hexDump(buf, buf.readerIndex(), rfidLength).toUpperCase().trim(); + String rfidTagName = ByteBufUtil.hexDump(buf, buf.readerIndex(), rfidLength).toUpperCase().trim(); buf.readBytes(rfidLength); + position.set("tag", rfidTagName); } } if ((buf.readableBytes() > 5)) { int adcTwoIndex = buf.readUnsignedByte(); int adcTwoLength = buf.readUnsignedByte(); if (adcTwoLength > 0){ - this.adc2 = buf.readUnsignedShort(); + position.set(Position.PREFIX_ADC + 2, buf.readUnsignedShort()); } } } - } - - private Position positionHandler(Channel channel, SocketAddress remoteAddress) { - - DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, this.deviceId); - if (deviceSession == null) { - return null; - } - Position position = new Position(getProtocolName()); - long devicesessionId = deviceSession.getDeviceId(); - position.setDeviceId(devicesessionId) ; - position.setValid(true); - position.setTime(this.datePacket); - position.setLatitude(this.latitude); - position.setLongitude(this.longitude); - position.setSpeed(this.speed); - position.setCourse(this.course); - position.set("model", this.gpsTrackerModel); - position.set(Position.KEY_HDOP, this.hdopProtocol); - position.set(Position.KEY_BATTERY, this.batteryVoltage); - position.set(Position.KEY_ODOMETER, this.odometer); - position.set(Position.PREFIX_ADC + 1, this.adcVoltageInMilliVolts); - position.set(Position.PREFIX_ADC + 2, this.adc2); - position.set("tag", this.rfidTagName); - position.set(Position.KEY_SATELLITES, this.satellitesNumber); - CellTower cellTower = CellTower.fromLacCid(getConfig(), this.lacInt, this.cellIdInt); - cellTower.setMobileNetworkCode(this.mobileNetworkCode); - cellTower.setSignalStrength(this.gsmSignalStrength); - position.setNetwork(new Network(cellTower)); - if (this.isOutPutOne){ position.set(Position.PREFIX_OUT + 1, this.isOutPutOne);} - if (this.isOutPutTwo){ position.set(Position.PREFIX_OUT + 2, this.isOutPutTwo);} - if (this.isInPutThree){ position.set(Position.PREFIX_IN + 2, this.isInPutThree);} - if (this.isGpsFix) { position.set("gpsFix", this.isGpsFix); } - if (!this.isLiveData) { position.set("restored", true); } - if (this.hasGpsAlert) { - position.set(Position.ALARM_GPS_ANTENNA_CUT, true); - position.set("gpsAlert", true); - } - position.set(Position.KEY_IGNITION, this.isIgnitionOff); - if (isPowerOff){ position.set(Position.ALARM_POWER_OFF, this.isPowerOff );} - position.set(Position.KEY_GPS, true); - if (this.deviceAlert > 0) { setDeviceAlert(position, this.deviceAlert); } - return position; } @Override protected Object decode( Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { - - try { - setInstanceParameters(msg); - } - catch (Exception e) { - return null; - } - - return positionHandler(channel, remoteAddress); + ByteBuf buffer = (ByteBuf) msg; + return parseData(channel, remoteAddress, buffer); } } -- cgit v1.2.3 From 8ee05feeafbf7c6356e1afd65e140823c717aef8 Mon Sep 17 00:00:00 2001 From: Nikolay Vlahovski Date: Tue, 16 May 2023 16:32:54 +0300 Subject: Reafactoring --- .../traccar/protocol/TranSyncProtocolDecoder.java | 216 +++++++++------------ .../protocol/TranSyncProtocolDecoderTest.java | 7 +- 2 files changed, 98 insertions(+), 125 deletions(-) (limited to 'src/main/java/org/traccar') diff --git a/src/main/java/org/traccar/protocol/TranSyncProtocolDecoder.java b/src/main/java/org/traccar/protocol/TranSyncProtocolDecoder.java index d5a69dbe2..d07b70365 100644 --- a/src/main/java/org/traccar/protocol/TranSyncProtocolDecoder.java +++ b/src/main/java/org/traccar/protocol/TranSyncProtocolDecoder.java @@ -1,3 +1,18 @@ +/* + * Copyright 2013 - 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package org.traccar.protocol; import io.netty.buffer.ByteBuf; @@ -6,6 +21,7 @@ import io.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; import org.traccar.Protocol; import org.traccar.helper.BitUtil; +import org.traccar.helper.DateBuilder; import org.traccar.helper.UnitsConverter; import org.traccar.model.CellTower; import org.traccar.model.Network; @@ -13,206 +29,166 @@ import org.traccar.model.Position; import org.traccar.session.DeviceSession; import java.net.SocketAddress; -import java.text.ParseException; -import java.text.SimpleDateFormat; -import java.util.Date; -import java.util.TimeZone; +import java.util.Arrays; public class TranSyncProtocolDecoder extends BaseProtocolDecoder { + private static final byte[] STX = new byte[]{0x3a, 0x3a}; + public TranSyncProtocolDecoder(Protocol protocol) { super(protocol); } - public Date getDatefromIntegerParameters(int year, int month, int day, int hours, int minutes, int seconds) { - - String dataString = ""+year + "-" + month + "-" + day + " " + hours + ":" + minutes + ":" + seconds; - SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yy-MM-dd HH:mm:ss"); - simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC")); - try { - return simpleDateFormat.parse(dataString); - - } catch (ParseException e) { - e.printStackTrace(); - } - return null; - } - - protected void setDeviceAlert(Position position, int alert) { - switch (alert) { + protected void decodeAlarm(Position position, int value) { + switch (value) { case 10: - position.set(Position.ALARM_SOS, true); + position.set(Position.KEY_ALARM, Position.ALARM_SOS); break; case 11: - position.set(Position.ALARM_SOS, false); + position.set(Position.KEY_EVENT, 11); break; case 16: position.set(Position.KEY_EVENT, 16); break; case 3: - position.set("distressCutOff", true); + position.set(Position.KEY_ALARM, 3); break; case 22: - position.set("tilt", true); + position.set(Position.KEY_ALARM, 22); break; case 9: position.set(Position.KEY_EVENT, 9); case 17: - position.set(Position.ALARM_OVERSPEED, true); + position.set(Position.KEY_ALARM, Position.ALARM_OVERSPEED); break; case 13: - position.set(Position.ALARM_BRAKING, true); + position.set(Position.KEY_ALARM, Position.ALARM_BRAKING); break; case 14: - position.set(Position.ALARM_ACCELERATION, true); + position.set(Position.KEY_ALARM, Position.ALARM_ACCELERATION); break; case 15: position.set(Position.KEY_EVENT, 15); break; case 23: - position.set(Position.ALARM_ACCIDENT, true); + position.set(Position.KEY_ALARM, Position.ALARM_ACCIDENT); break; case 12: position.set(Position.KEY_EVENT, 12); break; case 6: - position.set(Position.ALARM_POWER_RESTORED, true); + position.set(Position.KEY_ALARM, Position.ALARM_POWER_RESTORED); break; case 4: - position.set(Position.ALARM_LOW_BATTERY, true); + position.set(Position.KEY_ALARM, Position.ALARM_LOW_BATTERY); break; case 5: position.set(Position.KEY_EVENT, 5); break; - } - - } - - private void setNetwork(Position position, int lacInt, int cellIdInt, int mobileNetworkCode, int gsmSignalStrength){ - CellTower cellTower = CellTower.fromLacCid(getConfig(), lacInt, cellIdInt); - cellTower.setMobileNetworkCode(mobileNetworkCode); - cellTower.setSignalStrength(gsmSignalStrength); - position.setNetwork(new Network(cellTower)); - } - private String getTrackerModel(int mask){ + private void decodePowerEngineParameters(Position position, int value){ - switch (mask) { - case 1: - return "basic"; - case 2: - return "asset"; - case 3: - return "bike"; + 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)); - case 4: - return "serial"; + } - case 5: - return "obd"; - case 6: - return "l1"; - case 7: - return "ais"; - default: - return "unknown"; + private void decodeTrackerStatusParameters(Position position, int value){ + if (BitUtil.check(value, 7)) position.set("restored", true); + if (BitUtil.check(value, 5)) { + position.set(Position.KEY_ALARM, Position.ALARM_GPS_ANTENNA_CUT); } - } - private Position parseData(Channel channel, SocketAddress remoteAddress, ByteBuf buf) { - if (ByteBufUtil.hexDump(buf, 0, 2).equalsIgnoreCase("3A3A")) { + @Override + protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { + ByteBuf buf = (ByteBuf) msg; + if (Arrays.equals(ByteBufUtil.getBytes(buf, 0, 2), STX)) { buf.readUnsignedShort(); } - int packetLength = buf.readByte(); - int lacInt = buf.readUnsignedShort(); - String deviceId = ByteBufUtil.hexDump(buf, buf.readerIndex(), 8).toUpperCase().trim().replaceFirst("^0+(?!$)", ""); - buf.readBytes(8); - int informationSerialNumber = buf.readUnsignedShort(); - int protocolNumber = buf.readUnsignedByte(); + buf.readByte(); //packetLength + + int locationAreaCode = buf.readUnsignedShort(); + String deviceId = ByteBufUtil.hexDump(buf.readSlice(8)); + + buf.readUnsignedShort(); //informationSerialNumber + buf.readUnsignedByte(); //protocolNumber + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, deviceId); if (deviceSession == null) { return null; } - Position position = new Position(getProtocolName()); - long devicesessionId = deviceSession.getDeviceId(); - - position.setDeviceId(devicesessionId) ; + position.setDeviceId(deviceSession.getDeviceId()); position.setValid(true); - int year = buf.readUnsignedByte(); - int month = buf.readUnsignedByte(); - int day = buf.readUnsignedByte(); - int hour = buf.readUnsignedByte(); - int minute = buf.readUnsignedByte(); - int second = buf.readUnsignedByte(); - position.setTime(getDatefromIntegerParameters(year, month, day, hour, minute, second)); + 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); position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedByte())); position.setCourse(buf.readUnsignedShort()); + int mobileNetworkCode = buf.readUnsignedByte(); - int cellIdInt = buf.readUnsignedShort(); - int zeroByte = buf.readUnsignedByte(); - position.set(Position.PREFIX_OUT + 1, BitUtil.check(zeroByte, 0)); - position.set(Position.PREFIX_OUT + 2, BitUtil.check(zeroByte, 1)); - position.set(Position.PREFIX_IN + 3, BitUtil.check(zeroByte, 2)); - if (BitUtil.check(zeroByte, 3)) position.set(Position.ALARM_POWER_OFF, true); - position.set(Position.KEY_IGNITION, BitUtil.check(zeroByte, 4)); - position.set("gpsFix", BitUtil.check(zeroByte, 7)); - int oneByte = buf.readUnsignedByte(); - int deviceAlert = buf.readUnsignedByte(); - if (deviceAlert > 0) { setDeviceAlert(position, deviceAlert); } - int threeByte = buf.readUnsignedByte(); - if (!((threeByte & 0b10000000) == 0)) position.set("restored", true); - if ((threeByte & 0b00100000) != 0) { - position.set(Position.ALARM_GPS_ANTENNA_CUT, true); - position.set("gpsAlert", true); - } - position.set("model", getTrackerModel(threeByte & 0b00001111)); + int cellTowerId = buf.readUnsignedShort(); + + decodePowerEngineParameters(position, buf.readUnsignedByte()); + + buf.readUnsignedByte(); // not in use + + decodeAlarm(position, buf.readUnsignedByte()); + + decodeTrackerStatusParameters(position, buf.readUnsignedByte()); + int gsmSignalStrength = buf.readUnsignedByte(); + 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()); - boolean isOptionalParameters = (buf.readableBytes() > 2); - setNetwork(position, lacInt, cellIdInt, mobileNetworkCode, gsmSignalStrength); - if (isOptionalParameters) { // Always True - int odometerIndex = buf.readUnsignedByte(); + + CellTower cellTower = CellTower.fromLacCid(getConfig(), locationAreaCode, cellTowerId); + cellTower.setMobileNetworkCode(mobileNetworkCode); + cellTower.setSignalStrength(gsmSignalStrength); + + position.setNetwork(new Network(cellTower)); + + if (buf.readableBytes() > 2) { + buf.readUnsignedByte(); // odometerIndex int odometerLength = buf.readUnsignedByte(); if (odometerLength > 0) { - String odometerReading = ByteBufUtil.hexDump(buf, buf.readerIndex(), odometerLength).toUpperCase().trim().replaceFirst("^0+(?!$)", ""); - int odometer = Integer.parseInt(odometerReading); - buf.readBytes(odometerLength); + int odometer = buf.readBytes(odometerLength).readInt(); position.set(Position.KEY_ODOMETER, odometer); } if ((buf.readableBytes() > 2)) { - int rfidIndex = buf.readUnsignedByte(); - int rfidLength = buf.readUnsignedByte(); - if(rfidLength > 0) { - String rfidTagName = ByteBufUtil.hexDump(buf, buf.readerIndex(), rfidLength).toUpperCase().trim(); - buf.readBytes(rfidLength); - position.set("tag", rfidTagName); + buf.readUnsignedByte(); // tagIndex + int tagLength = buf.readUnsignedByte(); + if (tagLength > 0) { + position.set("tag", ByteBufUtil.hexDump(buf.readSlice(tagLength))); } } if ((buf.readableBytes() > 5)) { - int adcTwoIndex = buf.readUnsignedByte(); - int adcTwoLength = buf.readUnsignedByte(); - if (adcTwoLength > 0){ + buf.readUnsignedByte(); // adc2Index + int adc2Length = buf.readUnsignedByte(); + if (adc2Length > 0) { position.set(Position.PREFIX_ADC + 2, buf.readUnsignedShort()); } } + if ((buf.readableBytes() > 5)) { + buf.readUnsignedByte(); // adc2Index + int adc2Length = buf.readUnsignedByte(); + if (adc2Length > 0 && adc2Length <= buf.readableBytes() - 2) { + position.set(Position.PREFIX_ADC + 3, buf.readUnsignedShort()); + } + } } - return position; } - - @Override - protected Object decode( Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { - ByteBuf buffer = (ByteBuf) msg; - return parseData(channel, remoteAddress, buffer); - } - } diff --git a/src/test/java/org/traccar/protocol/TranSyncProtocolDecoderTest.java b/src/test/java/org/traccar/protocol/TranSyncProtocolDecoderTest.java index 1b90566d4..6cf2d0c1f 100644 --- a/src/test/java/org/traccar/protocol/TranSyncProtocolDecoderTest.java +++ b/src/test/java/org/traccar/protocol/TranSyncProtocolDecoderTest.java @@ -1,6 +1,5 @@ package org.traccar.protocol; - import org.junit.jupiter.api.Test; import org.traccar.ProtocolTest; @@ -11,12 +10,10 @@ public class TranSyncProtocolDecoderTest extends ProtocolTest { var decoder = inject(new TranSyncProtocolDecoder(null)); - verifyPosition(decoder, binary( - "3a3a2b583f086065705154043900801017050b11190f01623ef40887dff00000c25e9ff707000007152a2d0000000105004794916902050000000000050252ee060200822323")); + "3a3a2b583f086065705154043900801017050b11190f01623ef40887dff00000c25e9ff707000007152a2d0000000105004794916902050000100000050252ee060200822323")); verifyAttributes(decoder, binary( - "3a3a2b583f086065705154043900801017050b11190f01623ef40887dff00000c25e9ff707000007152a2d0000000105004794916902050000000000050252ee060200822323") - ); + "3a3a2b583f086065705154043900801017050b11190f01623ef40887dff00000c25e9ff707000007152a2d0000000105004794916902050000000000050252ee060200822323")); } } -- cgit v1.2.3 From 7e403cf979d3c01acb5b3dfe0ee37dfa83cba4ff Mon Sep 17 00:00:00 2001 From: Nikolay Vlahovski Date: Wed, 17 May 2023 11:00:43 +0300 Subject: Add LengthFieldBasedFrameDecoder to TranSyncProtocol --- .../java/org/traccar/protocol/TranSyncProtocol.java | 19 ++++++++++++++++++- .../org/traccar/protocol/TranSyncProtocolDecoder.java | 15 +++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) (limited to 'src/main/java/org/traccar') diff --git a/src/main/java/org/traccar/protocol/TranSyncProtocol.java b/src/main/java/org/traccar/protocol/TranSyncProtocol.java index 0634642df..39db67585 100644 --- a/src/main/java/org/traccar/protocol/TranSyncProtocol.java +++ b/src/main/java/org/traccar/protocol/TranSyncProtocol.java @@ -1,5 +1,22 @@ +/* + * Copyright 2013 - 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package org.traccar.protocol; +import io.netty.handler.codec.LengthFieldBasedFrameDecoder; +import io.netty.handler.codec.LineBasedFrameDecoder; import org.traccar.BaseProtocol; import org.traccar.PipelineBuilder; import org.traccar.TrackerServer; @@ -14,9 +31,9 @@ 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 TranSyncProtocolDecoder(TranSyncProtocol.this)); } }); } - } diff --git a/src/main/java/org/traccar/protocol/TranSyncProtocolDecoder.java b/src/main/java/org/traccar/protocol/TranSyncProtocolDecoder.java index d07b70365..bb8c9bbf9 100644 --- a/src/main/java/org/traccar/protocol/TranSyncProtocolDecoder.java +++ b/src/main/java/org/traccar/protocol/TranSyncProtocolDecoder.java @@ -17,8 +17,10 @@ package org.traccar.protocol; import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBufUtil; +import io.netty.buffer.Unpooled; import io.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; +import org.traccar.NetworkMessage; import org.traccar.Protocol; import org.traccar.helper.BitUtil; import org.traccar.helper.DateBuilder; @@ -106,6 +108,18 @@ public class TranSyncProtocolDecoder extends BaseProtocolDecoder { } } + private void sendResponse(Channel channel) { + if (channel != null) { + ByteBuf response = Unpooled.buffer(5); + response.writeByte(22); + response.writeByte(1); + response.writeShort(response.capacity()); // length + response.writeByte(4); + + channel.writeAndFlush(new NetworkMessage(response, channel.remoteAddress())); + } + } + @Override protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { ByteBuf buf = (ByteBuf) msg; @@ -189,6 +203,7 @@ public class TranSyncProtocolDecoder extends BaseProtocolDecoder { } } } + sendResponse(channel); return position; } } -- cgit v1.2.3 From 9fa4b44281e96ab56f3a0d27745d89265ea417fd Mon Sep 17 00:00:00 2001 From: Nikolay Vlahovski Date: Wed, 17 May 2023 11:13:43 +0300 Subject: Remove sendResponse function from TranSyncProtocolDecoder.java --- .../java/org/traccar/protocol/TranSyncProtocolDecoder.java | 13 ------------- 1 file changed, 13 deletions(-) (limited to 'src/main/java/org/traccar') diff --git a/src/main/java/org/traccar/protocol/TranSyncProtocolDecoder.java b/src/main/java/org/traccar/protocol/TranSyncProtocolDecoder.java index bb8c9bbf9..e9247ddf8 100644 --- a/src/main/java/org/traccar/protocol/TranSyncProtocolDecoder.java +++ b/src/main/java/org/traccar/protocol/TranSyncProtocolDecoder.java @@ -108,18 +108,6 @@ public class TranSyncProtocolDecoder extends BaseProtocolDecoder { } } - private void sendResponse(Channel channel) { - if (channel != null) { - ByteBuf response = Unpooled.buffer(5); - response.writeByte(22); - response.writeByte(1); - response.writeShort(response.capacity()); // length - response.writeByte(4); - - channel.writeAndFlush(new NetworkMessage(response, channel.remoteAddress())); - } - } - @Override protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { ByteBuf buf = (ByteBuf) msg; @@ -203,7 +191,6 @@ public class TranSyncProtocolDecoder extends BaseProtocolDecoder { } } } - sendResponse(channel); return position; } } -- cgit v1.2.3 From b7d46d78adc763eea3aae838f4e056e12e2dc487 Mon Sep 17 00:00:00 2001 From: Nikolay Vlahovski Date: Wed, 17 May 2023 11:43:53 +0300 Subject: Add Position.KEY_ARCHIVE position parameter in decodeTrackerStatusParameters in TranSyncProtocol Add Position.KEY_VERSION_HW position parameter in decodeTrackerStatusParameters in TranSyncProtocol --- .../org/traccar/protocol/TranSyncProtocol.java | 3 +-- .../traccar/protocol/TranSyncProtocolDecoder.java | 29 +++++++++++++++++++--- 2 files changed, 26 insertions(+), 6 deletions(-) (limited to 'src/main/java/org/traccar') diff --git a/src/main/java/org/traccar/protocol/TranSyncProtocol.java b/src/main/java/org/traccar/protocol/TranSyncProtocol.java index 39db67585..3abe05de8 100644 --- a/src/main/java/org/traccar/protocol/TranSyncProtocol.java +++ b/src/main/java/org/traccar/protocol/TranSyncProtocol.java @@ -16,7 +16,6 @@ package org.traccar.protocol; import io.netty.handler.codec.LengthFieldBasedFrameDecoder; -import io.netty.handler.codec.LineBasedFrameDecoder; import org.traccar.BaseProtocol; import org.traccar.PipelineBuilder; import org.traccar.TrackerServer; @@ -36,4 +35,4 @@ public class TranSyncProtocol extends BaseProtocol { } }); } -} +} \ No newline at end of file diff --git a/src/main/java/org/traccar/protocol/TranSyncProtocolDecoder.java b/src/main/java/org/traccar/protocol/TranSyncProtocolDecoder.java index e9247ddf8..759c012b8 100644 --- a/src/main/java/org/traccar/protocol/TranSyncProtocolDecoder.java +++ b/src/main/java/org/traccar/protocol/TranSyncProtocolDecoder.java @@ -17,10 +17,8 @@ package org.traccar.protocol; import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBufUtil; -import io.netty.buffer.Unpooled; import io.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; -import org.traccar.NetworkMessage; import org.traccar.Protocol; import org.traccar.helper.BitUtil; import org.traccar.helper.DateBuilder; @@ -37,6 +35,28 @@ 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); } @@ -102,10 +122,11 @@ public class TranSyncProtocolDecoder extends BaseProtocolDecoder { } private void decodeTrackerStatusParameters(Position position, int value){ - if (BitUtil.check(value, 7)) position.set("restored", true); + 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); } + position.set(Position.KEY_VERSION_HW, getHardwareType(BitUtil.between(value, 0, 3))); } @Override @@ -143,7 +164,7 @@ public class TranSyncProtocolDecoder extends BaseProtocolDecoder { decodePowerEngineParameters(position, buf.readUnsignedByte()); - buf.readUnsignedByte(); // not in use + buf.readUnsignedByte(); // reserved decodeAlarm(position, buf.readUnsignedByte()); -- cgit v1.2.3 From 34f6dfbf1d2c48ae932fa7565dd30d2817d2c1f9 Mon Sep 17 00:00:00 2001 From: Nikolay Vlahovski Date: Wed, 17 May 2023 12:10:17 +0300 Subject: remove extra empty lines --- src/main/java/org/traccar/protocol/TranSyncProtocolDecoder.java | 8 +++++--- .../java/org/traccar/protocol/TranSyncProtocolDecoderTest.java | 6 ++---- 2 files changed, 7 insertions(+), 7 deletions(-) (limited to 'src/main/java/org/traccar') diff --git a/src/main/java/org/traccar/protocol/TranSyncProtocolDecoder.java b/src/main/java/org/traccar/protocol/TranSyncProtocolDecoder.java index 759c012b8..acb46ec48 100644 --- a/src/main/java/org/traccar/protocol/TranSyncProtocolDecoder.java +++ b/src/main/java/org/traccar/protocol/TranSyncProtocolDecoder.java @@ -131,7 +131,9 @@ public class TranSyncProtocolDecoder extends BaseProtocolDecoder { @Override protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { + ByteBuf buf = (ByteBuf) msg; + if (Arrays.equals(ByteBufUtil.getBytes(buf, 0, 2), STX)) { buf.readUnsignedShort(); } @@ -183,14 +185,14 @@ public class TranSyncProtocolDecoder extends BaseProtocolDecoder { position.setNetwork(new Network(cellTower)); - if (buf.readableBytes() > 2) { + 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); } - if ((buf.readableBytes() > 2)) { + if ((buf.readableBytes() > 5)) { buf.readUnsignedByte(); // tagIndex int tagLength = buf.readUnsignedByte(); if (tagLength > 0) { @@ -205,7 +207,7 @@ public class TranSyncProtocolDecoder extends BaseProtocolDecoder { } } if ((buf.readableBytes() > 5)) { - buf.readUnsignedByte(); // adc2Index + buf.readUnsignedByte(); // adc3Index int adc2Length = buf.readUnsignedByte(); if (adc2Length > 0 && adc2Length <= buf.readableBytes() - 2) { position.set(Position.PREFIX_ADC + 3, buf.readUnsignedShort()); diff --git a/src/test/java/org/traccar/protocol/TranSyncProtocolDecoderTest.java b/src/test/java/org/traccar/protocol/TranSyncProtocolDecoderTest.java index 6cf2d0c1f..c84ae2e0f 100644 --- a/src/test/java/org/traccar/protocol/TranSyncProtocolDecoderTest.java +++ b/src/test/java/org/traccar/protocol/TranSyncProtocolDecoderTest.java @@ -10,10 +10,8 @@ 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 From 074dc016d21f9848140d125c2812d8abf25e8d53 Mon Sep 17 00:00:00 2001 From: Nikolay Vlahovski Date: Wed, 17 May 2023 20:29:37 +0300 Subject: Fix Pythonic ussues --- src/main/java/org/traccar/protocol/TranSyncProtocol.java | 2 +- .../org/traccar/protocol/TranSyncProtocolDecoder.java | 16 +++++++++++----- 2 files changed, 12 insertions(+), 6 deletions(-) (limited to 'src/main/java/org/traccar') diff --git a/src/main/java/org/traccar/protocol/TranSyncProtocol.java b/src/main/java/org/traccar/protocol/TranSyncProtocol.java index 3abe05de8..5422380b6 100644 --- a/src/main/java/org/traccar/protocol/TranSyncProtocol.java +++ b/src/main/java/org/traccar/protocol/TranSyncProtocol.java @@ -35,4 +35,4 @@ public class TranSyncProtocol extends BaseProtocol { } }); } -} \ No newline at end of file +} diff --git a/src/main/java/org/traccar/protocol/TranSyncProtocolDecoder.java b/src/main/java/org/traccar/protocol/TranSyncProtocolDecoder.java index acb46ec48..1b7c9539c 100644 --- a/src/main/java/org/traccar/protocol/TranSyncProtocolDecoder.java +++ b/src/main/java/org/traccar/protocol/TranSyncProtocolDecoder.java @@ -35,7 +35,7 @@ public class TranSyncProtocolDecoder extends BaseProtocolDecoder { private static final byte[] STX = new byte[]{0x3a, 0x3a}; - private String getHardwareType(int type){ + private String getHardwareType(int type) { switch (type) { case 1: @@ -107,22 +107,28 @@ public class TranSyncProtocolDecoder extends BaseProtocolDecoder { case 5: position.set(Position.KEY_EVENT, 5); break; + default: + position.set(Position.KEY_EVENT, "unknown"); } } - private void decodePowerEngineParameters(Position position, int value){ + 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); + 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); + 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); } -- cgit v1.2.3 From 05a73b06f94e451239ebc61ebb2b8528705bbc61 Mon Sep 17 00:00:00 2001 From: Nikolay Vlahovski Date: Thu, 18 May 2023 12:47:40 +0300 Subject: Fix Latitude/Longitude Formats for negative support Add analizeNegativePosition function in TranSyncProtocolDecoder.java Add statusParameters variable for byte --- .../java/org/traccar/protocol/TranSyncProtocolDecoder.java | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) (limited to 'src/main/java/org/traccar') diff --git a/src/main/java/org/traccar/protocol/TranSyncProtocolDecoder.java b/src/main/java/org/traccar/protocol/TranSyncProtocolDecoder.java index 1b7c9539c..130c916b1 100644 --- a/src/main/java/org/traccar/protocol/TranSyncProtocolDecoder.java +++ b/src/main/java/org/traccar/protocol/TranSyncProtocolDecoder.java @@ -111,6 +111,15 @@ public class TranSyncProtocolDecoder extends BaseProtocolDecoder { 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) { @@ -122,7 +131,6 @@ public class TranSyncProtocolDecoder extends BaseProtocolDecoder { } position.set(Position.KEY_IGNITION, BitUtil.check(value, 3)); position.set("gpsFix", BitUtil.check(value, 0)); - } private void decodeTrackerStatusParameters(Position position, int value) { @@ -169,8 +177,10 @@ public class TranSyncProtocolDecoder extends BaseProtocolDecoder { int mobileNetworkCode = buf.readUnsignedByte(); int cellTowerId = buf.readUnsignedShort(); + int statusParameters = buf.readUnsignedByte(); - decodePowerEngineParameters(position, buf.readUnsignedByte()); + decodePowerEngineParameters(position, statusParameters); + analizeNegativePosition(position, statusParameters); buf.readUnsignedByte(); // reserved -- cgit v1.2.3