From efd9e261791942334dbf4a911883a1b2b791f282 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Mon, 19 Feb 2024 11:53:08 -0800 Subject: Streamline model name access --- src/main/java/org/traccar/BaseProtocolDecoder.java | 10 ++++++++++ src/main/java/org/traccar/BaseProtocolEncoder.java | 11 +++++++++++ .../java/org/traccar/protocol/DualcamProtocolDecoder.java | 3 +-- src/main/java/org/traccar/protocol/Gt06ProtocolEncoder.java | 7 +++---- src/main/java/org/traccar/protocol/LaipacProtocolDecoder.java | 3 +-- .../java/org/traccar/protocol/MeiligaoProtocolEncoder.java | 3 +-- .../java/org/traccar/protocol/MeitrackProtocolDecoder.java | 8 ++------ .../java/org/traccar/protocol/Minifinder2ProtocolEncoder.java | 4 +--- src/main/java/org/traccar/protocol/SigfoxProtocolDecoder.java | 5 +---- .../java/org/traccar/protocol/TeltonikaProtocolDecoder.java | 4 +--- src/main/java/org/traccar/session/ConnectionManager.java | 2 +- src/main/java/org/traccar/session/DeviceSession.java | 9 ++++++++- 12 files changed, 41 insertions(+), 28 deletions(-) (limited to 'src/main/java/org/traccar') diff --git a/src/main/java/org/traccar/BaseProtocolDecoder.java b/src/main/java/org/traccar/BaseProtocolDecoder.java index 4d4086c3c..495a866c0 100644 --- a/src/main/java/org/traccar/BaseProtocolDecoder.java +++ b/src/main/java/org/traccar/BaseProtocolDecoder.java @@ -51,6 +51,8 @@ public abstract class BaseProtocolDecoder extends ExtendedObjectDecoder { private MediaManager mediaManager; private CommandsManager commandsManager; + private String modelOverride; + public BaseProtocolDecoder(Protocol protocol) { this.protocol = protocol; } @@ -141,6 +143,14 @@ public abstract class BaseProtocolDecoder extends ExtendedObjectDecoder { } } + public void setModelOverride(String modelOverride) { + this.modelOverride = modelOverride; + } + + public String getDeviceModel(DeviceSession deviceSession) { + return modelOverride != null ? modelOverride : deviceSession.getModel(); + } + public void getLastLocation(Position position, Date deviceTime) { if (position.getDeviceId() != 0) { position.setOutdated(true); diff --git a/src/main/java/org/traccar/BaseProtocolEncoder.java b/src/main/java/org/traccar/BaseProtocolEncoder.java index b9ca16838..e357c27dc 100644 --- a/src/main/java/org/traccar/BaseProtocolEncoder.java +++ b/src/main/java/org/traccar/BaseProtocolEncoder.java @@ -39,6 +39,8 @@ public abstract class BaseProtocolEncoder extends ChannelOutboundHandlerAdapter private CacheManager cacheManager; + private String modelOverride; + public BaseProtocolEncoder(Protocol protocol) { this.protocol = protocol; } @@ -68,6 +70,15 @@ public abstract class BaseProtocolEncoder extends ChannelOutboundHandlerAdapter } } + public void setModelOverride(String modelOverride) { + this.modelOverride = modelOverride; + } + + public String getDeviceModel(long deviceId) { + String model = getCacheManager().getObject(Device.class, deviceId).getModel(); + return modelOverride != null ? modelOverride : model; + } + @Override public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception { diff --git a/src/main/java/org/traccar/protocol/DualcamProtocolDecoder.java b/src/main/java/org/traccar/protocol/DualcamProtocolDecoder.java index 75cd52384..411e2b9d7 100644 --- a/src/main/java/org/traccar/protocol/DualcamProtocolDecoder.java +++ b/src/main/java/org/traccar/protocol/DualcamProtocolDecoder.java @@ -19,7 +19,6 @@ import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; import io.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; -import org.traccar.model.Device; import org.traccar.session.DeviceSession; import org.traccar.NetworkMessage; import org.traccar.Protocol; @@ -77,7 +76,7 @@ public class DualcamProtocolDecoder extends BaseProtocolDecoder { deviceSession = getDeviceSession(channel, remoteAddress, uniqueId); long settings = buf.readUnsignedInt(); if (channel != null && deviceSession != null) { - model = getCacheManager().getObject(Device.class, deviceSession.getDeviceId()).getModel(); + model = getDeviceModel(deviceSession); ByteBuf response = Unpooled.buffer(); if (BitUtil.check(settings, 25)) { response.writeShort(MSG_PATH_REQUEST); diff --git a/src/main/java/org/traccar/protocol/Gt06ProtocolEncoder.java b/src/main/java/org/traccar/protocol/Gt06ProtocolEncoder.java index dc5dd446f..fd6bb8451 100644 --- a/src/main/java/org/traccar/protocol/Gt06ProtocolEncoder.java +++ b/src/main/java/org/traccar/protocol/Gt06ProtocolEncoder.java @@ -23,7 +23,6 @@ import org.traccar.config.Keys; import org.traccar.helper.Checksum; import org.traccar.helper.model.AttributeUtil; import org.traccar.model.Command; -import org.traccar.model.Device; import java.nio.charset.StandardCharsets; @@ -74,11 +73,11 @@ public class Gt06ProtocolEncoder extends BaseProtocolEncoder { String password = AttributeUtil.getDevicePassword( getCacheManager(), command.getDeviceId(), getProtocolName(), "123456"); - Device device = getCacheManager().getObject(Device.class, command.getDeviceId()); + String model = getDeviceModel(command.getDeviceId()); switch (command.getType()) { case Command.TYPE_ENGINE_STOP: - if ("G109".equals(device.getModel())) { + if ("G109".equals(model)) { return encodeContent(command.getDeviceId(), "DYD#"); } else if (alternative) { return encodeContent(command.getDeviceId(), "DYD," + password + "#"); @@ -86,7 +85,7 @@ public class Gt06ProtocolEncoder extends BaseProtocolEncoder { return encodeContent(command.getDeviceId(), "Relay,1#"); } case Command.TYPE_ENGINE_RESUME: - if ("G109".equals(device.getModel())) { + if ("G109".equals(model)) { return encodeContent(command.getDeviceId(), "HFYD#"); } else if (alternative) { return encodeContent(command.getDeviceId(), "HFYD," + password + "#"); diff --git a/src/main/java/org/traccar/protocol/LaipacProtocolDecoder.java b/src/main/java/org/traccar/protocol/LaipacProtocolDecoder.java index 5745909c7..343d42e09 100644 --- a/src/main/java/org/traccar/protocol/LaipacProtocolDecoder.java +++ b/src/main/java/org/traccar/protocol/LaipacProtocolDecoder.java @@ -28,7 +28,6 @@ import org.traccar.helper.PatternBuilder; import org.traccar.model.CellTower; import org.traccar.model.Network; import org.traccar.model.Position; -import org.traccar.model.Device; import org.traccar.helper.BitUtil; import java.net.SocketAddress; @@ -226,7 +225,7 @@ public class LaipacProtocolDecoder extends BaseProtocolDecoder { return null; } - String model = getCacheManager().getObject(Device.class, deviceSession.getDeviceId()).getModel(); + String model = getDeviceModel(deviceSession); Position position = new Position(getProtocolName()); diff --git a/src/main/java/org/traccar/protocol/MeiligaoProtocolEncoder.java b/src/main/java/org/traccar/protocol/MeiligaoProtocolEncoder.java index 5859d91ce..6d3b4f7e9 100644 --- a/src/main/java/org/traccar/protocol/MeiligaoProtocolEncoder.java +++ b/src/main/java/org/traccar/protocol/MeiligaoProtocolEncoder.java @@ -24,7 +24,6 @@ import org.traccar.helper.Checksum; import org.traccar.helper.DataConverter; import org.traccar.helper.model.AttributeUtil; import org.traccar.model.Command; -import org.traccar.model.Device; import java.nio.charset.StandardCharsets; import java.util.Set; @@ -64,7 +63,7 @@ public class MeiligaoProtocolEncoder extends BaseProtocolEncoder { int outputCount; int outputType; - String model = getCacheManager().getObject(Device.class, deviceId).getModel(); + String model = getDeviceModel(deviceId); if (model != null && Set.of("TK510", "GT08", "TK208", "TK228", "MT05").contains(model)) { outputCount = 5; diff --git a/src/main/java/org/traccar/protocol/MeitrackProtocolDecoder.java b/src/main/java/org/traccar/protocol/MeitrackProtocolDecoder.java index 965c8ee0d..88b6380a5 100644 --- a/src/main/java/org/traccar/protocol/MeitrackProtocolDecoder.java +++ b/src/main/java/org/traccar/protocol/MeitrackProtocolDecoder.java @@ -20,7 +20,6 @@ import io.netty.buffer.ByteBufUtil; import io.netty.buffer.Unpooled; import io.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; -import org.traccar.model.Device; import org.traccar.session.DeviceSession; import org.traccar.NetworkMessage; import org.traccar.Protocol; @@ -38,6 +37,7 @@ import java.nio.charset.StandardCharsets; import java.util.Date; import java.util.LinkedList; import java.util.List; +import java.util.Objects; import java.util.regex.Pattern; public class MeitrackProtocolDecoder extends BaseProtocolDecoder { @@ -206,11 +206,7 @@ public class MeitrackProtocolDecoder extends BaseProtocolDecoder { position.set(Position.PREFIX_ADC + i, parser.nextHexInt()); } - String model = getCacheManager().getObject(Device.class, deviceSession.getDeviceId()).getModel(); - if (model == null) { - model = ""; - } - switch (model.toUpperCase()) { + switch (Objects.requireNonNullElse(getDeviceModel(deviceSession), "").toUpperCase()) { case "MVT340": case "MVT380": position.set(Position.KEY_BATTERY, parser.nextHexInt() * 3.0 * 2.0 / 1024.0); diff --git a/src/main/java/org/traccar/protocol/Minifinder2ProtocolEncoder.java b/src/main/java/org/traccar/protocol/Minifinder2ProtocolEncoder.java index 72ac9db4e..6e330a4dd 100644 --- a/src/main/java/org/traccar/protocol/Minifinder2ProtocolEncoder.java +++ b/src/main/java/org/traccar/protocol/Minifinder2ProtocolEncoder.java @@ -21,7 +21,6 @@ import org.traccar.BaseProtocolEncoder; import org.traccar.Protocol; import org.traccar.helper.Checksum; import org.traccar.model.Command; -import org.traccar.model.Device; import java.nio.charset.StandardCharsets; @@ -55,8 +54,7 @@ public class Minifinder2ProtocolEncoder extends BaseProtocolEncoder { content.writeByte(0xF0); // type } - Device device = getCacheManager().getObject(Device.class, command.getDeviceId()); - if ("Nano".equalsIgnoreCase(device.getModel())) { + if ("Nano".equalsIgnoreCase(getDeviceModel(command.getDeviceId()))) { ByteBuf content = Unpooled.buffer(); if (command.getType().equals(Command.TYPE_FIRMWARE_UPDATE)) { String url = command.getString(Command.KEY_DATA); diff --git a/src/main/java/org/traccar/protocol/SigfoxProtocolDecoder.java b/src/main/java/org/traccar/protocol/SigfoxProtocolDecoder.java index 9380d2327..e0dfab9b2 100644 --- a/src/main/java/org/traccar/protocol/SigfoxProtocolDecoder.java +++ b/src/main/java/org/traccar/protocol/SigfoxProtocolDecoder.java @@ -23,7 +23,6 @@ import io.netty.handler.codec.http.FullHttpRequest; import io.netty.handler.codec.http.HttpResponseStatus; import org.traccar.BaseHttpProtocolDecoder; import org.traccar.helper.BufferUtil; -import org.traccar.model.Device; import org.traccar.session.DeviceSession; import org.traccar.Protocol; import org.traccar.helper.BitUtil; @@ -155,12 +154,10 @@ public class SigfoxProtocolDecoder extends BaseHttpProtocolDecoder { } else if (jsonContains(json, "data")) { - String model = getCacheManager().getObject(Device.class, deviceSession.getDeviceId()).getModel(); - ByteBuf buf = Unpooled.wrappedBuffer(DataConverter.parseHex(json.getString("data"))); try { int header = buf.readUnsignedByte(); - if ("Amber".equals(model)) { + if ("Amber".equals(getDeviceModel(deviceSession))) { int flags = buf.readUnsignedByte(); position.set(Position.KEY_MOTION, BitUtil.check(flags, 1)); diff --git a/src/main/java/org/traccar/protocol/TeltonikaProtocolDecoder.java b/src/main/java/org/traccar/protocol/TeltonikaProtocolDecoder.java index 2f0874797..6197c6c13 100644 --- a/src/main/java/org/traccar/protocol/TeltonikaProtocolDecoder.java +++ b/src/main/java/org/traccar/protocol/TeltonikaProtocolDecoder.java @@ -21,7 +21,6 @@ import io.netty.buffer.Unpooled; import io.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; import org.traccar.helper.BufferUtil; -import org.traccar.model.Device; import org.traccar.session.DeviceSession; import org.traccar.NetworkMessage; import org.traccar.Protocol; @@ -654,7 +653,6 @@ public class TeltonikaProtocolDecoder extends BaseProtocolDecoder { if (deviceSession == null) { return null; } - String model = getCacheManager().getObject(Device.class, deviceSession.getDeviceId()).getModel(); for (int i = 0; i < count; i++) { Position position = new Position(getProtocolName()); @@ -680,7 +678,7 @@ public class TeltonikaProtocolDecoder extends BaseProtocolDecoder { } else if (codec == CODEC_12) { decodeSerial(channel, remoteAddress, deviceSession, position, buf); } else { - decodeLocation(position, buf, codec, model); + decodeLocation(position, buf, codec, getDeviceModel(deviceSession)); } if (!position.getOutdated() || !position.getAttributes().isEmpty()) { diff --git a/src/main/java/org/traccar/session/ConnectionManager.java b/src/main/java/org/traccar/session/ConnectionManager.java index 121b876c5..42dcf5ce9 100644 --- a/src/main/java/org/traccar/session/ConnectionManager.java +++ b/src/main/java/org/traccar/session/ConnectionManager.java @@ -147,7 +147,7 @@ public class ConnectionManager implements BroadcastInterface { } DeviceSession deviceSession = new DeviceSession( - device.getId(), device.getUniqueId(), protocol, channel, remoteAddress); + device.getId(), device.getUniqueId(), device.getModel(), protocol, channel, remoteAddress); endpointSessions.put(device.getUniqueId(), deviceSession); sessionsByEndpoint.put(remoteAddress, endpointSessions); sessionsByDeviceId.put(device.getId(), deviceSession); diff --git a/src/main/java/org/traccar/session/DeviceSession.java b/src/main/java/org/traccar/session/DeviceSession.java index 009f90f5a..f124ca7f9 100644 --- a/src/main/java/org/traccar/session/DeviceSession.java +++ b/src/main/java/org/traccar/session/DeviceSession.java @@ -29,14 +29,17 @@ public class DeviceSession { private final long deviceId; private final String uniqueId; + private final String model; private final Protocol protocol; private final Channel channel; private final SocketAddress remoteAddress; public DeviceSession( - long deviceId, String uniqueId, Protocol protocol, Channel channel, SocketAddress remoteAddress) { + long deviceId, String uniqueId, String model, + Protocol protocol, Channel channel, SocketAddress remoteAddress) { this.deviceId = deviceId; this.uniqueId = uniqueId; + this.model = model; this.protocol = protocol; this.channel = channel; this.remoteAddress = remoteAddress; @@ -50,6 +53,10 @@ public class DeviceSession { return uniqueId; } + public String getModel() { + return model; + } + public Channel getChannel() { return channel; } -- cgit v1.2.3