diff options
Diffstat (limited to 'src/main')
-rw-r--r-- | src/main/java/org/traccar/BaseProtocolDecoder.java | 6 | ||||
-rw-r--r-- | src/main/java/org/traccar/protocol/EgtsProtocolDecoder.java | 35 |
2 files changed, 35 insertions, 6 deletions
diff --git a/src/main/java/org/traccar/BaseProtocolDecoder.java b/src/main/java/org/traccar/BaseProtocolDecoder.java index 87d09289f..9dbe78c9d 100644 --- a/src/main/java/org/traccar/BaseProtocolDecoder.java +++ b/src/main/java/org/traccar/BaseProtocolDecoder.java @@ -143,7 +143,13 @@ public abstract class BaseProtocolDecoder extends ExtendedObjectDecoder { } public DeviceSession getDeviceSession(Channel channel, SocketAddress remoteAddress, String... uniqueIds) { + return getDeviceSession(channel, remoteAddress, false, uniqueIds); + } + + public DeviceSession getDeviceSession( + Channel channel, SocketAddress remoteAddress, boolean ignoreCache, String... uniqueIds) { if (channel != null && BasePipelineFactory.getHandler(channel.pipeline(), HttpRequestDecoder.class) != null + || ignoreCache || config.getBoolean("decoder.ignoreSessionCache")) { long deviceId = findDeviceId(remoteAddress, uniqueIds); if (deviceId != 0) { diff --git a/src/main/java/org/traccar/protocol/EgtsProtocolDecoder.java b/src/main/java/org/traccar/protocol/EgtsProtocolDecoder.java index b9fcb2f44..d6a59ea8d 100644 --- a/src/main/java/org/traccar/protocol/EgtsProtocolDecoder.java +++ b/src/main/java/org/traccar/protocol/EgtsProtocolDecoder.java @@ -18,6 +18,8 @@ package org.traccar.protocol; import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; import io.netty.channel.Channel; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.traccar.BaseProtocolDecoder; import org.traccar.DeviceSession; import org.traccar.NetworkMessage; @@ -35,10 +37,14 @@ import java.util.List; public class EgtsProtocolDecoder extends BaseProtocolDecoder { + private static final Logger LOGGER = LoggerFactory.getLogger(EgtsProtocolDecoder.class); + public EgtsProtocolDecoder(Protocol protocol) { super(protocol); } + private boolean useObjectIdAsDeviceId = true; + public static final int PT_RESPONSE = 0; public static final int PT_APPDATA = 1; public static final int PT_SIGNED_APPDATA = 2; @@ -68,7 +74,7 @@ public class EgtsProtocolDecoder extends BaseProtocolDecoder { public static final int MSG_ABS_CNTR_DATA = 25; public static final int MSG_ABS_LOOPIN_DATA = 26; public static final int MSG_LIQUID_LEVEL_SENSOR = 27; - public static final int MSG_PASSENGERS_COUNTERS = 28; + public static final int MSG_PASSENGERS_COUNTERS = 28; private int packetId; @@ -121,11 +127,18 @@ public class EgtsProtocolDecoder extends BaseProtocolDecoder { ByteBuf buf = (ByteBuf) msg; + List<Position> positions = new LinkedList<>(); + + short headerLength = buf.getUnsignedByte(buf.readerIndex() + 3); int index = buf.getUnsignedShort(buf.readerIndex() + 5 + 2); - buf.skipBytes(buf.getUnsignedByte(buf.readerIndex() + 3)); + short packetType = buf.getUnsignedByte(buf.readerIndex() + 5 + 2 + 2); + buf.skipBytes(headerLength); - List<Position> positions = new LinkedList<>(); + if (packetType == PT_RESPONSE) { + return null; + } + long objectId = 0L; while (buf.readableBytes() > 2) { int length = buf.readUnsignedShortLE(); @@ -133,7 +146,7 @@ public class EgtsProtocolDecoder extends BaseProtocolDecoder { int recordFlags = buf.readUnsignedByte(); if (BitUtil.check(recordFlags, 0)) { - buf.readUnsignedIntLE(); // object id + objectId = buf.readUnsignedIntLE(); } if (BitUtil.check(recordFlags, 1)) { @@ -164,6 +177,7 @@ public class EgtsProtocolDecoder extends BaseProtocolDecoder { int end = buf.readUnsignedShortLE() + buf.readerIndex(); if (type == MSG_TERM_IDENTITY) { + useObjectIdAsDeviceId = false; buf.readUnsignedIntLE(); // object id int flags = buf.readUnsignedByte(); @@ -254,8 +268,17 @@ public class EgtsProtocolDecoder extends BaseProtocolDecoder { buf.readerIndex(end); } - if (serviceType == SERVICE_TELEDATA && deviceSession != null) { - positions.add(position); + if (serviceType == SERVICE_TELEDATA && position.getValid()) { + if (useObjectIdAsDeviceId && objectId != 0L) { + LOGGER.debug("[{}] Using objectId as deviceId: {}", channel == null ? "" : channel.id(), objectId); + deviceSession = getDeviceSession(channel, remoteAddress, true, String.valueOf(objectId)); + if (deviceSession != null) { + position.setDeviceId(deviceSession.getDeviceId()); + } + } + if (deviceSession != null) { + positions.add(position); + } } } |