From 815c3de43ee8943fddb916f27317c37a7a806d65 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Tue, 19 Jul 2016 15:11:35 +1200 Subject: Move device id to a session --- src/org/traccar/BaseProtocolDecoder.java | 93 +++++++++++++--------- src/org/traccar/DeviceSession.java | 30 +++++++ src/org/traccar/protocol/AdmProtocolDecoder.java | 15 +++- src/org/traccar/protocol/ApelProtocolDecoder.java | 13 ++- .../traccar/protocol/AplicomProtocolDecoder.java | 7 +- .../traccar/protocol/AppelloProtocolDecoder.java | 6 +- .../traccar/protocol/AquilaProtocolDecoder.java | 6 +- .../traccar/protocol/Ardi01ProtocolDecoder.java | 6 +- .../traccar/protocol/ArknavProtocolDecoder.java | 6 +- .../traccar/protocol/ArnaviProtocolDecoder.java | 6 +- src/org/traccar/protocol/AstraProtocolDecoder.java | 6 +- .../traccar/protocol/AtrackProtocolDecoder.java | 6 +- src/org/traccar/protocol/AuroProtocolDecoder.java | 6 +- .../traccar/protocol/AutoFonProtocolDecoder.java | 28 ++++--- .../traccar/protocol/Avl301ProtocolDecoder.java | 17 ++-- src/org/traccar/protocol/BceProtocolDecoder.java | 6 +- .../traccar/protocol/BlackKiteProtocolDecoder.java | 13 ++- src/org/traccar/protocol/BoxProtocolDecoder.java | 12 ++- .../traccar/protocol/CalAmpProtocolDecoder.java | 13 +-- .../traccar/protocol/CarTrackProtocolDecoder.java | 6 +- .../traccar/protocol/CarcellProtocolDecoder.java | 7 +- .../traccar/protocol/CarscopProtocolDecoder.java | 10 ++- .../traccar/protocol/CastelProtocolDecoder.java | 19 +++-- .../protocol/CellocatorProtocolDecoder.java | 6 +- .../traccar/protocol/CityeasyProtocolDecoder.java | 8 +- src/org/traccar/protocol/DishaProtocolDecoder.java | 6 +- .../traccar/protocol/EasyTrackProtocolDecoder.java | 6 +- .../traccar/protocol/EelinkProtocolDecoder.java | 22 +++-- .../traccar/protocol/EnforaProtocolDecoder.java | 6 +- .../traccar/protocol/FlextrackProtocolDecoder.java | 12 ++- src/org/traccar/protocol/FoxProtocolDecoder.java | 6 +- .../traccar/protocol/FreedomProtocolDecoder.java | 6 +- .../traccar/protocol/GalileoProtocolDecoder.java | 11 ++- src/org/traccar/protocol/GatorProtocolDecoder.java | 6 +- src/org/traccar/protocol/Gl100ProtocolDecoder.java | 6 +- src/org/traccar/protocol/Gl200ProtocolDecoder.java | 36 +++++---- .../traccar/protocol/GlobalSatProtocolDecoder.java | 11 ++- src/org/traccar/protocol/GnxProtocolDecoder.java | 6 +- .../traccar/protocol/GoSafeProtocolDecoder.java | 12 +-- src/org/traccar/protocol/GotopProtocolDecoder.java | 6 +- .../traccar/protocol/Gps103ProtocolDecoder.java | 18 +++-- .../traccar/protocol/GpsGateProtocolDecoder.java | 18 +++-- .../traccar/protocol/GpsMarkerProtocolDecoder.java | 6 +- .../traccar/protocol/GpsmtaProtocolDecoder.java | 6 +- .../traccar/protocol/GranitProtocolDecoder.java | 22 +++-- src/org/traccar/protocol/Gt02ProtocolDecoder.java | 6 +- src/org/traccar/protocol/Gt06ProtocolDecoder.java | 44 +++++----- src/org/traccar/protocol/H02ProtocolDecoder.java | 12 ++- .../traccar/protocol/HaicomProtocolDecoder.java | 6 +- .../traccar/protocol/HomtecsProtocolDecoder.java | 6 +- .../traccar/protocol/HuaShengProtocolDecoder.java | 11 ++- .../traccar/protocol/HuabaoProtocolDecoder.java | 6 +- src/org/traccar/protocol/IdplProtocolDecoder.java | 6 +- .../protocol/IntellitracProtocolDecoder.java | 6 +- src/org/traccar/protocol/Jt600ProtocolDecoder.java | 11 ++- src/org/traccar/protocol/KenjiProtocolDecoder.java | 6 +- src/org/traccar/protocol/KhdProtocolDecoder.java | 6 +- .../traccar/protocol/LaipacProtocolDecoder.java | 6 +- src/org/traccar/protocol/M2mProtocolDecoder.java | 12 ++- .../traccar/protocol/ManPowerProtocolDecoder.java | 6 +- .../traccar/protocol/MegastekProtocolDecoder.java | 21 +++-- .../traccar/protocol/MeiligaoProtocolDecoder.java | 18 ++--- .../traccar/protocol/MeitrackProtocolDecoder.java | 14 ++-- .../protocol/MiniFinderProtocolDecoder.java | 12 ++- src/org/traccar/protocol/Mta6ProtocolDecoder.java | 16 ++-- src/org/traccar/protocol/MtxProtocolDecoder.java | 6 +- src/org/traccar/protocol/MxtProtocolDecoder.java | 6 +- .../traccar/protocol/NavigilProtocolDecoder.java | 51 ++++++------ src/org/traccar/protocol/NavisProtocolDecoder.java | 38 ++++----- src/org/traccar/protocol/NoranProtocolDecoder.java | 6 +- src/org/traccar/protocol/NvsProtocolDecoder.java | 16 ++-- src/org/traccar/protocol/OrionProtocolDecoder.java | 7 +- .../traccar/protocol/OsmAndProtocolDecoder.java | 6 +- .../traccar/protocol/PathAwayProtocolDecoder.java | 7 +- .../traccar/protocol/PiligrimProtocolDecoder.java | 7 +- .../traccar/protocol/ProgressProtocolDecoder.java | 12 ++- .../traccar/protocol/Pt3000ProtocolDecoder.java | 6 +- src/org/traccar/protocol/Pt502ProtocolDecoder.java | 6 +- src/org/traccar/protocol/RitiProtocolDecoder.java | 6 +- .../traccar/protocol/RuptelaProtocolDecoder.java | 6 +- src/org/traccar/protocol/SanavProtocolDecoder.java | 6 +- .../traccar/protocol/SkypatrolProtocolDecoder.java | 6 +- .../traccar/protocol/Stl060ProtocolDecoder.java | 6 +- .../traccar/protocol/SuntechProtocolDecoder.java | 6 +- .../traccar/protocol/SupermateProtocolDecoder.java | 6 +- src/org/traccar/protocol/T55ProtocolDecoder.java | 60 ++++++++------ src/org/traccar/protocol/T800xProtocolDecoder.java | 7 +- src/org/traccar/protocol/TaipProtocolDecoder.java | 33 ++------ src/org/traccar/protocol/TelicProtocolDecoder.java | 6 +- .../traccar/protocol/TeltonikaProtocolDecoder.java | 16 ++-- .../traccar/protocol/ThinkRaceProtocolDecoder.java | 13 ++- src/org/traccar/protocol/Tk102ProtocolDecoder.java | 16 ++-- src/org/traccar/protocol/Tk103ProtocolDecoder.java | 16 ++-- src/org/traccar/protocol/Tlt2hProtocolDecoder.java | 6 +- .../protocol/TopflytechProtocolDecoder.java | 6 +- src/org/traccar/protocol/TotemProtocolDecoder.java | 6 +- src/org/traccar/protocol/Tr20ProtocolDecoder.java | 6 +- src/org/traccar/protocol/Tr900ProtocolDecoder.java | 6 +- .../traccar/protocol/TrackboxProtocolDecoder.java | 10 ++- .../traccar/protocol/TramigoProtocolDecoder.java | 6 +- src/org/traccar/protocol/TrvProtocolDecoder.java | 10 ++- .../traccar/protocol/Tt8850ProtocolDecoder.java | 6 +- src/org/traccar/protocol/TytanProtocolDecoder.java | 6 +- src/org/traccar/protocol/TzoneProtocolDecoder.java | 6 +- .../traccar/protocol/UlbotechProtocolDecoder.java | 6 +- src/org/traccar/protocol/UproProtocolDecoder.java | 6 +- src/org/traccar/protocol/V680ProtocolDecoder.java | 12 ++- .../traccar/protocol/VisiontekProtocolDecoder.java | 8 +- src/org/traccar/protocol/WatchProtocolDecoder.java | 8 +- .../traccar/protocol/WialonProtocolDecoder.java | 19 +++-- .../traccar/protocol/WondexProtocolDecoder.java | 14 ++-- src/org/traccar/protocol/XexunProtocolDecoder.java | 6 +- src/org/traccar/protocol/XirgoProtocolDecoder.java | 6 +- src/org/traccar/protocol/Xt013ProtocolDecoder.java | 6 +- src/org/traccar/protocol/YwtProtocolDecoder.java | 6 +- .../protocol/AutoFonProtocolDecoderTest.java | 6 +- .../traccar/protocol/Gt06ProtocolDecoderTest.java | 6 +- .../traccar/protocol/NavisProtocolDecoderTest.java | 6 +- 118 files changed, 874 insertions(+), 508 deletions(-) create mode 100644 src/org/traccar/DeviceSession.java diff --git a/src/org/traccar/BaseProtocolDecoder.java b/src/org/traccar/BaseProtocolDecoder.java index 59205bc6f..f98617caf 100644 --- a/src/org/traccar/BaseProtocolDecoder.java +++ b/src/org/traccar/BaseProtocolDecoder.java @@ -16,6 +16,7 @@ package org.traccar; import org.jboss.netty.channel.Channel; +import org.jboss.netty.channel.socket.DatagramChannel; import org.traccar.helper.Log; import org.traccar.model.Device; import org.traccar.model.Position; @@ -23,6 +24,8 @@ import org.traccar.model.Position; import java.net.InetSocketAddress; import java.net.SocketAddress; import java.util.Date; +import java.util.HashMap; +import java.util.Map; public abstract class BaseProtocolDecoder extends ExtendedObjectDecoder { @@ -32,47 +35,63 @@ public abstract class BaseProtocolDecoder extends ExtendedObjectDecoder { return protocol.getName(); } - private long deviceId; - - public boolean hasDeviceId() { - return deviceId != 0; - } - - public long getDeviceId() { + private DeviceSession channelDeviceSession; // connection-based protocols + private Map addressDeviceSessions = new HashMap<>(); // connectionless protocols + + private long findDeviceId(SocketAddress remoteAddress, String... uniqueIds) { + long deviceId = 0; + if (uniqueIds.length > 0) { + try { + for (String uniqueId : uniqueIds) { + if (uniqueId != null) { + Device device = Context.getIdentityManager().getDeviceByUniqueId(uniqueId); + if (device != null) { + deviceId = device.getId(); + break; + } + } + } + } catch (Exception e) { + Log.warning(e); + } + if (deviceId == 0) { + StringBuilder message = new StringBuilder("Unknown device -"); + for (String uniqueId : uniqueIds) { + message.append(" ").append(uniqueId); + } + if (remoteAddress != null) { + message.append(" (").append(((InetSocketAddress) remoteAddress).getHostString()).append(")"); + } + Log.warning(message.toString()); + } + } return deviceId; } - public boolean identify(String uniqueId, Channel channel, SocketAddress remoteAddress, boolean logWarning) { - try { - Device device = Context.getIdentityManager().getDeviceByUniqueId(uniqueId); - if (device != null) { - deviceId = device.getId(); - if (Context.getConnectionManager() != null) { - Context.getConnectionManager().addActiveDevice(deviceId, protocol, channel, remoteAddress); - } - return true; + public DeviceSession getDeviceSession(Channel channel, SocketAddress remoteAddress, String... uniqueIds) { + if (channel instanceof DatagramChannel) { + long deviceId = findDeviceId(remoteAddress, uniqueIds); + DeviceSession deviceSession = addressDeviceSessions.get(remoteAddress); + if (deviceSession != null && (deviceSession.getDeviceId() == deviceId || uniqueIds.length == 0)) { + return deviceSession; + } else if (deviceId != 0) { + deviceSession = new DeviceSession(deviceId); + addressDeviceSessions.put(remoteAddress, deviceSession); + return deviceSession; } else { - deviceId = 0; - if (logWarning) { - String message = "Unknown device - " + uniqueId; - if (remoteAddress != null) { - message += " (" + ((InetSocketAddress) remoteAddress).getHostString() + ")"; - } - Log.warning(message); + return null; + } + } else { + if (channelDeviceSession == null) { + long deviceId = findDeviceId(remoteAddress, uniqueIds); + if (deviceId != 0) { + channelDeviceSession = new DeviceSession(deviceId); } - return false; } - } catch (Exception error) { - deviceId = 0; - Log.warning(error); - return false; + return channelDeviceSession; } } - public boolean identify(String uniqueId, Channel channel, SocketAddress remoteAddress) { - return identify(uniqueId, channel, remoteAddress, true); - } - public BaseProtocolDecoder(Protocol protocol) { this.protocol = protocol; } @@ -104,17 +123,19 @@ public abstract class BaseProtocolDecoder extends ExtendedObjectDecoder { @Override protected void onMessageEvent(Channel channel, SocketAddress remoteAddress, Object msg) { - if (hasDeviceId()) { - Context.getConnectionManager().updateDevice(deviceId, Device.STATUS_ONLINE, new Date()); + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress); + if (deviceSession != null) { + Context.getConnectionManager().updateDevice(deviceSession.getDeviceId(), Device.STATUS_ONLINE, new Date()); } } @Override protected Object handleEmptyMessage(Channel channel, SocketAddress remoteAddress, Object msg) { - if (Context.getConfig().getBoolean("database.saveEmpty") && hasDeviceId()) { + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress); + if (Context.getConfig().getBoolean("database.saveEmpty") && deviceSession != null) { Position position = new Position(); position.setProtocol(getProtocolName()); - position.setDeviceId(getDeviceId()); + position.setDeviceId(deviceSession.getDeviceId()); getLastLocation(position, null); return position; } else { diff --git a/src/org/traccar/DeviceSession.java b/src/org/traccar/DeviceSession.java new file mode 100644 index 000000000..5e2686f23 --- /dev/null +++ b/src/org/traccar/DeviceSession.java @@ -0,0 +1,30 @@ +/* + * Copyright 2016 Anton Tananaev (anton.tananaev@gmail.com) + * + * 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; + +public class DeviceSession { + + private final long deviceId; + + public DeviceSession(long deviceId) { + this.deviceId = deviceId; + } + + public long getDeviceId() { + return deviceId; + } + +} diff --git a/src/org/traccar/protocol/AdmProtocolDecoder.java b/src/org/traccar/protocol/AdmProtocolDecoder.java index dd089e8e3..e234e3f0b 100644 --- a/src/org/traccar/protocol/AdmProtocolDecoder.java +++ b/src/org/traccar/protocol/AdmProtocolDecoder.java @@ -18,6 +18,7 @@ package org.traccar.protocol; import org.jboss.netty.buffer.ChannelBuffer; import org.jboss.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; +import org.traccar.DeviceSession; import org.traccar.helper.BitUtil; import org.traccar.helper.UnitsConverter; import org.traccar.model.Position; @@ -47,15 +48,23 @@ public class AdmProtocolDecoder extends BaseProtocolDecoder { int type = buf.readUnsignedByte(); + DeviceSession deviceSession; if (type == MSG_IMEI) { - identify(buf.toString(buf.readerIndex(), 15, StandardCharsets.US_ASCII), channel, remoteAddress); + deviceSession = getDeviceSession( + channel, remoteAddress, buf.readBytes(15).toString(StandardCharsets.US_ASCII)); + } else { + deviceSession = getDeviceSession(channel, remoteAddress); } - if (hasDeviceId() && BitUtil.to(type, 2) == 0) { + if (deviceSession == null) { + return null; + } + + if (BitUtil.to(type, 2) == 0) { Position position = new Position(); position.setProtocol(getProtocolName()); - position.setDeviceId(getDeviceId()); + position.setDeviceId(deviceSession.getDeviceId()); buf.readUnsignedByte(); // firmware version buf.readUnsignedShort(); // index diff --git a/src/org/traccar/protocol/ApelProtocolDecoder.java b/src/org/traccar/protocol/ApelProtocolDecoder.java index fb473be1e..f10c4b5b1 100644 --- a/src/org/traccar/protocol/ApelProtocolDecoder.java +++ b/src/org/traccar/protocol/ApelProtocolDecoder.java @@ -19,6 +19,7 @@ import org.jboss.netty.buffer.ChannelBuffer; import org.jboss.netty.buffer.ChannelBuffers; import org.jboss.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; +import org.traccar.DeviceSession; import org.traccar.helper.Checksum; import org.traccar.helper.UnitsConverter; import org.traccar.model.Position; @@ -110,7 +111,7 @@ public class ApelProtocolDecoder extends BaseProtocolDecoder { int length = buf.readUnsignedShort(); buf.skipBytes(length); length = buf.readUnsignedShort(); - identify(buf.readBytes(length).toString(StandardCharsets.US_ASCII), channel, remoteAddress); + getDeviceSession(channel, remoteAddress, buf.readBytes(length).toString(StandardCharsets.US_ASCII)); } else if (type == MSG_LAST_LOG_INDEX) { @@ -120,8 +121,12 @@ public class ApelProtocolDecoder extends BaseProtocolDecoder { requestArchive(channel); } - } else if (hasDeviceId() - && (type == MSG_CURRENT_GPS_DATA || type == MSG_STATE_FULL_INFO_T104 || type == MSG_LOG_RECORDS)) { + } else if (type == MSG_CURRENT_GPS_DATA || type == MSG_STATE_FULL_INFO_T104 || type == MSG_LOG_RECORDS) { + + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress); + if (deviceSession == null) { + return null; + } List positions = new LinkedList<>(); @@ -133,7 +138,7 @@ public class ApelProtocolDecoder extends BaseProtocolDecoder { for (int j = 0; j < recordCount; j++) { Position position = new Position(); position.setProtocol(getProtocolName()); - position.setDeviceId(getDeviceId()); + position.setDeviceId(deviceSession.getDeviceId()); int subtype = type; if (type == MSG_LOG_RECORDS) { diff --git a/src/org/traccar/protocol/AplicomProtocolDecoder.java b/src/org/traccar/protocol/AplicomProtocolDecoder.java index 70e5d7d71..ff5a1aa20 100644 --- a/src/org/traccar/protocol/AplicomProtocolDecoder.java +++ b/src/org/traccar/protocol/AplicomProtocolDecoder.java @@ -20,6 +20,7 @@ import org.jboss.netty.buffer.ChannelBuffers; import org.jboss.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; import org.traccar.Context; +import org.traccar.DeviceSession; import org.traccar.helper.Checksum; import org.traccar.helper.Log; import org.traccar.helper.UnitsConverter; @@ -214,11 +215,11 @@ public class AplicomProtocolDecoder extends BaseProtocolDecoder { Position position = new Position(); position.setProtocol(getProtocolName()); - if (!identify(imei, channel, remoteAddress)) { + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, imei); + if (deviceSession == null) { return null; } - - position.setDeviceId(getDeviceId()); + position.setDeviceId(deviceSession.getDeviceId()); int event = buf.readUnsignedByte(); position.set(Position.KEY_EVENT, event); diff --git a/src/org/traccar/protocol/AppelloProtocolDecoder.java b/src/org/traccar/protocol/AppelloProtocolDecoder.java index bc3e7885d..4535464ed 100755 --- a/src/org/traccar/protocol/AppelloProtocolDecoder.java +++ b/src/org/traccar/protocol/AppelloProtocolDecoder.java @@ -17,6 +17,7 @@ package org.traccar.protocol; import org.jboss.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; +import org.traccar.DeviceSession; import org.traccar.helper.DateBuilder; import org.traccar.helper.Parser; import org.traccar.helper.PatternBuilder; @@ -60,13 +61,14 @@ public class AppelloProtocolDecoder extends BaseProtocolDecoder { } String imei = parser.next(); - if (!identify(imei, channel, remoteAddress)) { + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, imei); + if (deviceSession == null) { return null; } Position position = new Position(); position.setProtocol(getProtocolName()); - position.setDeviceId(getDeviceId()); + position.setDeviceId(deviceSession.getDeviceId()); if (parser.hasNext(6)) { DateBuilder dateBuilder = new DateBuilder() diff --git a/src/org/traccar/protocol/AquilaProtocolDecoder.java b/src/org/traccar/protocol/AquilaProtocolDecoder.java index 52f6c61a4..514448d9b 100644 --- a/src/org/traccar/protocol/AquilaProtocolDecoder.java +++ b/src/org/traccar/protocol/AquilaProtocolDecoder.java @@ -17,6 +17,7 @@ package org.traccar.protocol; import org.jboss.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; +import org.traccar.DeviceSession; import org.traccar.helper.DateBuilder; import org.traccar.helper.Parser; import org.traccar.helper.PatternBuilder; @@ -80,10 +81,11 @@ public class AquilaProtocolDecoder extends BaseProtocolDecoder { Position position = new Position(); position.setProtocol(getProtocolName()); - if (!identify(parser.next(), channel, remoteAddress)) { + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, parser.next()); + if (deviceSession == null) { return null; } - position.setDeviceId(getDeviceId()); + position.setDeviceId(deviceSession.getDeviceId()); position.set(Position.KEY_EVENT, parser.nextInt()); diff --git a/src/org/traccar/protocol/Ardi01ProtocolDecoder.java b/src/org/traccar/protocol/Ardi01ProtocolDecoder.java index 1dcfc51bf..1eaa65d84 100644 --- a/src/org/traccar/protocol/Ardi01ProtocolDecoder.java +++ b/src/org/traccar/protocol/Ardi01ProtocolDecoder.java @@ -17,6 +17,7 @@ package org.traccar.protocol; import org.jboss.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; +import org.traccar.DeviceSession; import org.traccar.helper.DateBuilder; import org.traccar.helper.Parser; import org.traccar.helper.PatternBuilder; @@ -59,10 +60,11 @@ public class Ardi01ProtocolDecoder extends BaseProtocolDecoder { Position position = new Position(); position.setProtocol(getProtocolName()); - if (!identify(parser.next(), channel, remoteAddress)) { + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, parser.next()); + if (deviceSession == null) { return null; } - position.setDeviceId(getDeviceId()); + position.setDeviceId(deviceSession.getDeviceId()); DateBuilder dateBuilder = new DateBuilder() .setDate(parser.nextInt(), parser.nextInt(), parser.nextInt()) diff --git a/src/org/traccar/protocol/ArknavProtocolDecoder.java b/src/org/traccar/protocol/ArknavProtocolDecoder.java index 5e633c198..6c6edc905 100644 --- a/src/org/traccar/protocol/ArknavProtocolDecoder.java +++ b/src/org/traccar/protocol/ArknavProtocolDecoder.java @@ -17,6 +17,7 @@ package org.traccar.protocol; import org.jboss.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; +import org.traccar.DeviceSession; import org.traccar.helper.DateBuilder; import org.traccar.helper.Parser; import org.traccar.helper.PatternBuilder; @@ -61,10 +62,11 @@ public class ArknavProtocolDecoder extends BaseProtocolDecoder { Position position = new Position(); position.setProtocol(getProtocolName()); - if (!identify(parser.next(), channel, remoteAddress)) { + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, parser.next()); + if (deviceSession == null) { return null; } - position.setDeviceId(getDeviceId()); + position.setDeviceId(deviceSession.getDeviceId()); position.setValid(parser.next().equals("A")); position.setLatitude(parser.nextCoordinate()); diff --git a/src/org/traccar/protocol/ArnaviProtocolDecoder.java b/src/org/traccar/protocol/ArnaviProtocolDecoder.java index 46728c53e..f7e146172 100644 --- a/src/org/traccar/protocol/ArnaviProtocolDecoder.java +++ b/src/org/traccar/protocol/ArnaviProtocolDecoder.java @@ -17,6 +17,7 @@ package org.traccar.protocol; import org.jboss.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; +import org.traccar.DeviceSession; import org.traccar.helper.DateBuilder; import org.traccar.helper.Parser; import org.traccar.helper.PatternBuilder; @@ -67,10 +68,11 @@ public class ArnaviProtocolDecoder extends BaseProtocolDecoder { Position position = new Position(); position.setProtocol(getProtocolName()); - if (!identify(parser.next(), channel, remoteAddress)) { + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, parser.next()); + if (deviceSession == null) { return null; } - position.setDeviceId(getDeviceId()); + position.setDeviceId(deviceSession.getDeviceId()); position.set(Position.KEY_INDEX, parser.nextInt()); position.set(Position.KEY_POWER, parser.nextInt() * 0.01); diff --git a/src/org/traccar/protocol/AstraProtocolDecoder.java b/src/org/traccar/protocol/AstraProtocolDecoder.java index f6b4ed559..6880047a9 100644 --- a/src/org/traccar/protocol/AstraProtocolDecoder.java +++ b/src/org/traccar/protocol/AstraProtocolDecoder.java @@ -19,6 +19,7 @@ import org.jboss.netty.buffer.ChannelBuffer; import org.jboss.netty.buffer.ChannelBuffers; import org.jboss.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; +import org.traccar.DeviceSession; import org.traccar.helper.BitUtil; import org.traccar.helper.DateBuilder; import org.traccar.helper.UnitsConverter; @@ -52,7 +53,8 @@ public class AstraProtocolDecoder extends BaseProtocolDecoder { buf.readUnsignedShort(); // length String imei = String.format("%08d", buf.readUnsignedInt()) + String.format("%07d", buf.readUnsignedMedium()); - if (!identify(imei, channel, remoteAddress)) { + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, imei); + if (deviceSession == null) { return null; } @@ -62,7 +64,7 @@ public class AstraProtocolDecoder extends BaseProtocolDecoder { Position position = new Position(); position.setProtocol(getProtocolName()); - position.setDeviceId(getDeviceId()); + position.setDeviceId(deviceSession.getDeviceId()); buf.readUnsignedByte(); // index diff --git a/src/org/traccar/protocol/AtrackProtocolDecoder.java b/src/org/traccar/protocol/AtrackProtocolDecoder.java index 0f1eb5f8e..60d5244a0 100644 --- a/src/org/traccar/protocol/AtrackProtocolDecoder.java +++ b/src/org/traccar/protocol/AtrackProtocolDecoder.java @@ -20,6 +20,7 @@ import org.jboss.netty.buffer.ChannelBuffers; import org.jboss.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; import org.traccar.Context; +import org.traccar.DeviceSession; import org.traccar.helper.DateBuilder; import org.traccar.helper.UnitsConverter; import org.traccar.model.Position; @@ -188,7 +189,8 @@ public class AtrackProtocolDecoder extends BaseProtocolDecoder { int index = buf.readUnsignedShort(); long id = buf.readLong(); - if (!identify(String.valueOf(id), channel, remoteAddress)) { + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, String.valueOf(id)); + if (deviceSession == null) { return null; } @@ -200,7 +202,7 @@ public class AtrackProtocolDecoder extends BaseProtocolDecoder { Position position = new Position(); position.setProtocol(getProtocolName()); - position.setDeviceId(getDeviceId()); + position.setDeviceId(deviceSession.getDeviceId()); if (longDate) { diff --git a/src/org/traccar/protocol/AuroProtocolDecoder.java b/src/org/traccar/protocol/AuroProtocolDecoder.java index ff3ab5b0d..428e83f99 100644 --- a/src/org/traccar/protocol/AuroProtocolDecoder.java +++ b/src/org/traccar/protocol/AuroProtocolDecoder.java @@ -17,6 +17,7 @@ package org.traccar.protocol; import org.jboss.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; +import org.traccar.DeviceSession; import org.traccar.helper.DateBuilder; import org.traccar.helper.Parser; import org.traccar.helper.PatternBuilder; @@ -67,10 +68,11 @@ public class AuroProtocolDecoder extends BaseProtocolDecoder { position.set(Position.KEY_INDEX, parser.nextInt()); - if (!identify(parser.next(), channel, remoteAddress)) { + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, parser.next()); + if (deviceSession == null) { return null; } - position.setDeviceId(getDeviceId()); + position.setDeviceId(deviceSession.getDeviceId()); position.setValid(true); position.setLongitude(parser.nextCoordinate(Parser.CoordinateFormat.HEM_DEG_MIN_MIN)); diff --git a/src/org/traccar/protocol/AutoFonProtocolDecoder.java b/src/org/traccar/protocol/AutoFonProtocolDecoder.java index b80fd6bcb..dd0f54af0 100644 --- a/src/org/traccar/protocol/AutoFonProtocolDecoder.java +++ b/src/org/traccar/protocol/AutoFonProtocolDecoder.java @@ -20,6 +20,7 @@ import org.jboss.netty.buffer.ChannelBuffer; import org.jboss.netty.buffer.ChannelBuffers; import org.jboss.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; +import org.traccar.DeviceSession; import org.traccar.helper.BitUtil; import org.traccar.helper.DateBuilder; import org.traccar.model.Position; @@ -57,11 +58,11 @@ public class AutoFonProtocolDecoder extends BaseProtocolDecoder { } } - private Position decodePosition(ChannelBuffer buf, boolean history) { + private Position decodePosition(DeviceSession deviceSession, ChannelBuffer buf, boolean history) { Position position = new Position(); position.setProtocol(getProtocolName()); - position.setDeviceId(getDeviceId()); + position.setDeviceId(deviceSession.getDeviceId()); if (!history) { buf.readUnsignedByte(); // interval @@ -127,20 +128,27 @@ public class AutoFonProtocolDecoder extends BaseProtocolDecoder { } String imei = ChannelBuffers.hexDump(buf.readBytes(8)).substring(1); - if (!identify(imei, channel, remoteAddress)) { - return null; - } + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, imei); - if (channel != null) { + if (deviceSession != null && channel != null) { ChannelBuffer response = ChannelBuffers.dynamicBuffer(); response.writeBytes("resp_crc=".getBytes(StandardCharsets.US_ASCII)); response.writeByte(buf.getByte(buf.writerIndex() - 1)); channel.write(response); } - } else if (type == MSG_LOCATION) { + return null; + + } + + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress); + if (deviceSession == null) { + return null; + } + + if (type == MSG_LOCATION) { - return decodePosition(buf, false); + return decodePosition(deviceSession, buf, false); } else if (type == MSG_HISTORY) { @@ -149,7 +157,7 @@ public class AutoFonProtocolDecoder extends BaseProtocolDecoder { List positions = new LinkedList<>(); for (int i = 0; i < count; i++) { - positions.add(decodePosition(buf, true)); + positions.add(decodePosition(deviceSession, buf, true)); } return positions; @@ -158,7 +166,7 @@ public class AutoFonProtocolDecoder extends BaseProtocolDecoder { Position position = new Position(); position.setProtocol(getProtocolName()); - position.setDeviceId(getDeviceId()); + position.setDeviceId(deviceSession.getDeviceId()); short status = buf.readUnsignedByte(); position.set(Position.KEY_ALARM, BitUtil.check(status, 7)); diff --git a/src/org/traccar/protocol/Avl301ProtocolDecoder.java b/src/org/traccar/protocol/Avl301ProtocolDecoder.java index 1f7e3470c..4d2dfe126 100644 --- a/src/org/traccar/protocol/Avl301ProtocolDecoder.java +++ b/src/org/traccar/protocol/Avl301ProtocolDecoder.java @@ -19,6 +19,7 @@ import org.jboss.netty.buffer.ChannelBuffer; import org.jboss.netty.buffer.ChannelBuffers; import org.jboss.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; +import org.traccar.DeviceSession; import org.traccar.helper.DateBuilder; import org.traccar.model.Position; @@ -46,7 +47,7 @@ public class Avl301ProtocolDecoder extends BaseProtocolDecoder { public static final int MSG_STATUS = 'H'; public static final int MSG_GPS_LBS_STATUS = '$'; - private static void sendResponse(Channel channel, int type) { + private void sendResponse(Channel channel, int type) { if (channel != null) { ChannelBuffer response = ChannelBuffers.directBuffer(5); response.writeByte('$'); @@ -69,18 +70,24 @@ public class Avl301ProtocolDecoder extends BaseProtocolDecoder { if (type == MSG_LOGIN) { - if (identify(readImei(buf), channel, remoteAddress)) { + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, readImei(buf)); + if (deviceSession == null) { sendResponse(channel, type); } - } else if (hasDeviceId() && type == MSG_STATUS) { + } else if (type == MSG_STATUS) { sendResponse(channel, type); - } else if (hasDeviceId() && type == MSG_GPS_LBS_STATUS) { + } else if (type == MSG_GPS_LBS_STATUS) { + + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress); + if (deviceSession == null) { + return null; + } Position position = new Position(); - position.setDeviceId(getDeviceId()); + position.setDeviceId(deviceSession.getDeviceId()); position.setProtocol(getProtocolName()); DateBuilder dateBuilder = new DateBuilder() diff --git a/src/org/traccar/protocol/BceProtocolDecoder.java b/src/org/traccar/protocol/BceProtocolDecoder.java index 5502de5b2..06290b6b0 100644 --- a/src/org/traccar/protocol/BceProtocolDecoder.java +++ b/src/org/traccar/protocol/BceProtocolDecoder.java @@ -19,6 +19,7 @@ import org.jboss.netty.buffer.ChannelBuffer; import org.jboss.netty.buffer.ChannelBuffers; import org.jboss.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; +import org.traccar.DeviceSession; import org.traccar.helper.BitUtil; import org.traccar.model.Position; @@ -49,7 +50,8 @@ public class BceProtocolDecoder extends BaseProtocolDecoder { ChannelBuffer buf = (ChannelBuffer) msg; String imei = String.format("%015d", buf.readLong()); - if (!identify(imei, channel, remoteAddress)) { + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, imei); + if (deviceSession == null) { return null; } @@ -65,7 +67,7 @@ public class BceProtocolDecoder extends BaseProtocolDecoder { Position position = new Position(); position.setProtocol(getProtocolName()); - position.setDeviceId(getDeviceId()); + position.setDeviceId(deviceSession.getDeviceId()); int structEnd = buf.readUnsignedByte() + buf.readerIndex(); diff --git a/src/org/traccar/protocol/BlackKiteProtocolDecoder.java b/src/org/traccar/protocol/BlackKiteProtocolDecoder.java index 6787140e0..1c9c0a003 100644 --- a/src/org/traccar/protocol/BlackKiteProtocolDecoder.java +++ b/src/org/traccar/protocol/BlackKiteProtocolDecoder.java @@ -1,6 +1,6 @@ /* + * Copyright 2013 - 2016 Anton Tananaev (anton.tananaev@gmail.com) * Copyright 2015 Vijay Kumar (vijaykumar@zilogic.com) - * Copyright 2013 - 2014 Anton Tananaev (anton.tananaev@gmail.com) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,6 +20,7 @@ import org.jboss.netty.buffer.ChannelBuffer; import org.jboss.netty.buffer.ChannelBuffers; import org.jboss.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; +import org.traccar.DeviceSession; import org.traccar.helper.BitUtil; import org.traccar.helper.Log; import org.traccar.model.Position; @@ -96,9 +97,7 @@ public class BlackKiteProtocolDecoder extends BaseProtocolDecoder { switch (tag) { case TAG_IMEI: - String imei = buf.toString(buf.readerIndex(), 15, StandardCharsets.US_ASCII); - buf.skipBytes(imei.length()); - identify(imei, channel, remoteAddress); + getDeviceSession(channel, remoteAddress, buf.readBytes(15).toString(StandardCharsets.US_ASCII)); break; case TAG_DATE: @@ -174,15 +173,15 @@ public class BlackKiteProtocolDecoder extends BaseProtocolDecoder { positions.add(position); } - if (!hasDeviceId()) { - Log.warning("Unknown device"); + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress); + if (deviceSession == null) { return null; } sendReply(channel, buf.readUnsignedShort()); for (Position p : positions) { - p.setDeviceId(getDeviceId()); + p.setDeviceId(deviceSession.getDeviceId()); } if (positions.isEmpty()) { diff --git a/src/org/traccar/protocol/BoxProtocolDecoder.java b/src/org/traccar/protocol/BoxProtocolDecoder.java index 480417607..f0a90ad97 100644 --- a/src/org/traccar/protocol/BoxProtocolDecoder.java +++ b/src/org/traccar/protocol/BoxProtocolDecoder.java @@ -17,6 +17,7 @@ package org.traccar.protocol; import org.jboss.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; +import org.traccar.DeviceSession; import org.traccar.helper.DateBuilder; import org.traccar.helper.Parser; import org.traccar.helper.PatternBuilder; @@ -57,7 +58,7 @@ public class BoxProtocolDecoder extends BaseProtocolDecoder { int index = sentence.indexOf(',', 2) + 1; String id = sentence.substring(index, sentence.indexOf(',', index)); - identify(id, channel, remoteAddress); + getDeviceSession(channel, remoteAddress, id); } else if (sentence.startsWith("E,")) { @@ -65,7 +66,12 @@ public class BoxProtocolDecoder extends BaseProtocolDecoder { channel.write("A," + sentence.substring(2) + "\r"); } - } else if (sentence.startsWith("L,") && hasDeviceId()) { + } else if (sentence.startsWith("L,")) { + + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress); + if (deviceSession == null) { + return null; + } Parser parser = new Parser(PATTERN, sentence); if (!parser.matches()) { @@ -73,7 +79,7 @@ public class BoxProtocolDecoder extends BaseProtocolDecoder { } Position position = new Position(); - position.setDeviceId(getDeviceId()); + position.setDeviceId(deviceSession.getDeviceId()); position.setProtocol(getProtocolName()); DateBuilder dateBuilder = new DateBuilder() diff --git a/src/org/traccar/protocol/CalAmpProtocolDecoder.java b/src/org/traccar/protocol/CalAmpProtocolDecoder.java index 69fca3914..99edb9fb7 100644 --- a/src/org/traccar/protocol/CalAmpProtocolDecoder.java +++ b/src/org/traccar/protocol/CalAmpProtocolDecoder.java @@ -19,6 +19,7 @@ import org.jboss.netty.buffer.ChannelBuffer; import org.jboss.netty.buffer.ChannelBuffers; import org.jboss.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; +import org.traccar.DeviceSession; import org.traccar.helper.BitUtil; import org.traccar.helper.UnitsConverter; import org.traccar.model.Position; @@ -63,10 +64,10 @@ public class CalAmpProtocolDecoder extends BaseProtocolDecoder { } } - private Position decodePosition(int type, ChannelBuffer buf) { + private Position decodePosition(DeviceSession deviceSession, int type, ChannelBuffer buf) { Position position = new Position(); - position.setDeviceId(getDeviceId()); + position.setDeviceId(deviceSession.getDeviceId()); position.setProtocol(getProtocolName()); position.setTime(new Date(buf.readUnsignedInt() * 1000)); @@ -157,7 +158,8 @@ public class CalAmpProtocolDecoder extends BaseProtocolDecoder { } } - identify(String.valueOf(id), channel, remoteAddress); + getDeviceSession(channel, remoteAddress, String.valueOf(id)); + } if (BitUtil.check(content, 1)) { @@ -182,7 +184,8 @@ public class CalAmpProtocolDecoder extends BaseProtocolDecoder { } - if (!hasDeviceId()) { + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress); + if (deviceSession == null) { return null; } @@ -195,7 +198,7 @@ public class CalAmpProtocolDecoder extends BaseProtocolDecoder { } if (type == MSG_EVENT_REPORT || type == MSG_LOCATE_REPORT || type == MSG_MINI_EVENT_REPORT) { - return decodePosition(type, buf); + return decodePosition(deviceSession, type, buf); } return null; diff --git a/src/org/traccar/protocol/CarTrackProtocolDecoder.java b/src/org/traccar/protocol/CarTrackProtocolDecoder.java index ff9d32c49..e0fd60b15 100644 --- a/src/org/traccar/protocol/CarTrackProtocolDecoder.java +++ b/src/org/traccar/protocol/CarTrackProtocolDecoder.java @@ -18,6 +18,7 @@ package org.traccar.protocol; import org.jboss.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; +import org.traccar.DeviceSession; import org.traccar.helper.DateBuilder; import org.traccar.helper.Parser; import org.traccar.helper.PatternBuilder; @@ -67,10 +68,11 @@ public class CarTrackProtocolDecoder extends BaseProtocolDecoder { Position position = new Position(); position.setProtocol(getProtocolName()); - if (!identify(parser.next(), channel, remoteAddress)) { + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, parser.next()); + if (deviceSession == null) { return null; } - position.setDeviceId(getDeviceId()); + position.setDeviceId(deviceSession.getDeviceId()); position.set("command", parser.next()); diff --git a/src/org/traccar/protocol/CarcellProtocolDecoder.java b/src/org/traccar/protocol/CarcellProtocolDecoder.java index 50b294f45..7aacfa4d0 100644 --- a/src/org/traccar/protocol/CarcellProtocolDecoder.java +++ b/src/org/traccar/protocol/CarcellProtocolDecoder.java @@ -20,6 +20,7 @@ import java.util.regex.Pattern; import org.jboss.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; +import org.traccar.DeviceSession; import org.traccar.helper.DateBuilder; import org.traccar.helper.Parser; import org.traccar.helper.Parser.CoordinateFormat; @@ -91,11 +92,11 @@ public class CarcellProtocolDecoder extends BaseProtocolDecoder { position.set(Position.KEY_ARCHIVE, parser.next().equals("%")); position.setValid(true); - if (!identify(parser.next(), channel, remoteAddress)) { + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, parser.next()); + if (deviceSession == null) { return null; } - - position.setDeviceId(getDeviceId()); + position.setDeviceId(deviceSession.getDeviceId()); if (parser.hasNext(8)) { position.setLatitude(parser.nextCoordinate(CoordinateFormat.HEM_DEG_MIN_MIN)); diff --git a/src/org/traccar/protocol/CarscopProtocolDecoder.java b/src/org/traccar/protocol/CarscopProtocolDecoder.java index 4c1aced57..5e1973ae8 100644 --- a/src/org/traccar/protocol/CarscopProtocolDecoder.java +++ b/src/org/traccar/protocol/CarscopProtocolDecoder.java @@ -17,6 +17,7 @@ package org.traccar.protocol; import org.jboss.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; +import org.traccar.DeviceSession; import org.traccar.helper.DateBuilder; import org.traccar.helper.Parser; import org.traccar.helper.PatternBuilder; @@ -53,12 +54,15 @@ public class CarscopProtocolDecoder extends BaseProtocolDecoder { String sentence = (String) msg; + DeviceSession deviceSession; int index = sentence.indexOf("UB05"); if (index != -1) { String imei = sentence.substring(index + 4, index + 4 + 15); - identify(imei, channel, remoteAddress); + deviceSession = getDeviceSession(channel, remoteAddress, imei); + } else { + deviceSession = getDeviceSession(channel, remoteAddress); } - if (!hasDeviceId()) { + if (deviceSession == null) { return null; } @@ -68,7 +72,7 @@ public class CarscopProtocolDecoder extends BaseProtocolDecoder { } Position position = new Position(); - position.setDeviceId(getDeviceId()); + position.setDeviceId(deviceSession.getDeviceId()); position.setProtocol(getProtocolName()); DateBuilder dateBuilder = new DateBuilder() diff --git a/src/org/traccar/protocol/CastelProtocolDecoder.java b/src/org/traccar/protocol/CastelProtocolDecoder.java index e379b6f2d..212096018 100644 --- a/src/org/traccar/protocol/CastelProtocolDecoder.java +++ b/src/org/traccar/protocol/CastelProtocolDecoder.java @@ -19,6 +19,7 @@ import org.jboss.netty.buffer.ChannelBuffer; import org.jboss.netty.buffer.ChannelBuffers; import org.jboss.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; +import org.traccar.DeviceSession; import org.traccar.helper.Checksum; import org.traccar.helper.DateBuilder; import org.traccar.helper.UnitsConverter; @@ -52,11 +53,11 @@ public class CastelProtocolDecoder extends BaseProtocolDecoder { private static final short MSG_CC_HEARTBEAT = 0x4206; private static final short MSG_CC_HEARTBEAT_RESPONSE = (short) 0x8206; - private Position readPosition(ChannelBuffer buf) { + private Position readPosition(DeviceSession deviceSession, ChannelBuffer buf) { Position position = new Position(); position.setProtocol(getProtocolName()); - position.setDeviceId(getDeviceId()); + position.setDeviceId(deviceSession.getDeviceId()); DateBuilder dateBuilder = new DateBuilder() .setDateReverse(buf.readUnsignedByte(), buf.readUnsignedByte(), buf.readUnsignedByte()) @@ -148,7 +149,9 @@ public class CastelProtocolDecoder extends BaseProtocolDecoder { ChannelBuffer id = buf.readBytes(20); int type = ChannelBuffers.swapShort(buf.readShort()); - if (!identify(id.toString(StandardCharsets.US_ASCII).trim(), channel, remoteAddress)) { + DeviceSession deviceSession = getDeviceSession( + channel, remoteAddress, id.toString(StandardCharsets.US_ASCII).trim()); + if (deviceSession == null) { return null; } @@ -162,7 +165,7 @@ public class CastelProtocolDecoder extends BaseProtocolDecoder { buf.readUnsignedInt(); // unix time buf.readUnsignedByte(); - return readPosition(buf); + return readPosition(deviceSession, buf); } @@ -205,7 +208,7 @@ public class CastelProtocolDecoder extends BaseProtocolDecoder { List positions = new LinkedList<>(); for (int i = 0; i < count; i++) { - Position position = readPosition(buf); + Position position = readPosition(deviceSession, buf); position.set(Position.KEY_ODOMETER, odometer); position.set(Position.KEY_STATUS, status); positions.add(position); @@ -217,7 +220,7 @@ public class CastelProtocolDecoder extends BaseProtocolDecoder { } else if (type == MSG_SC_GPS_SLEEP || type == MSG_SC_AGPS_REQUEST) { - return readPosition(buf); + return readPosition(deviceSession, buf); } @@ -233,7 +236,7 @@ public class CastelProtocolDecoder extends BaseProtocolDecoder { List positions = new LinkedList<>(); for (int i = 0; i < count; i++) { - Position position = readPosition(buf); + Position position = readPosition(deviceSession, buf); position.set(Position.KEY_STATUS, buf.readUnsignedInt()); position.set(Position.KEY_BATTERY, buf.readUnsignedByte()); @@ -255,7 +258,7 @@ public class CastelProtocolDecoder extends BaseProtocolDecoder { sendResponse(channel, remoteAddress, version, id, MSG_CC_LOGIN_RESPONSE, null); - Position position = readPosition(buf); + Position position = readPosition(deviceSession, buf); position.set(Position.KEY_STATUS, buf.readUnsignedInt()); position.set(Position.KEY_BATTERY, buf.readUnsignedByte()); diff --git a/src/org/traccar/protocol/CellocatorProtocolDecoder.java b/src/org/traccar/protocol/CellocatorProtocolDecoder.java index f5a77e387..f074cb5d3 100644 --- a/src/org/traccar/protocol/CellocatorProtocolDecoder.java +++ b/src/org/traccar/protocol/CellocatorProtocolDecoder.java @@ -19,6 +19,7 @@ import org.jboss.netty.buffer.ChannelBuffer; import org.jboss.netty.buffer.ChannelBuffers; import org.jboss.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; +import org.traccar.DeviceSession; import org.traccar.helper.DateBuilder; import org.traccar.helper.UnitsConverter; import org.traccar.model.Position; @@ -89,10 +90,11 @@ public class CellocatorProtocolDecoder extends BaseProtocolDecoder { Position position = new Position(); position.setProtocol(getProtocolName()); - if (!identify(String.valueOf(deviceUniqueId), channel, remoteAddress)) { + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, String.valueOf(deviceUniqueId)); + if (deviceSession == null) { return null; } - position.setDeviceId(getDeviceId()); + position.setDeviceId(deviceSession.getDeviceId()); buf.readUnsignedByte(); // hardware version buf.readUnsignedByte(); // software version diff --git a/src/org/traccar/protocol/CityeasyProtocolDecoder.java b/src/org/traccar/protocol/CityeasyProtocolDecoder.java index 77d7570dc..5cdbe525d 100644 --- a/src/org/traccar/protocol/CityeasyProtocolDecoder.java +++ b/src/org/traccar/protocol/CityeasyProtocolDecoder.java @@ -19,6 +19,7 @@ import org.jboss.netty.buffer.ChannelBuffer; import org.jboss.netty.buffer.ChannelBuffers; import org.jboss.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; +import org.traccar.DeviceSession; import org.traccar.helper.Checksum; import org.traccar.helper.DateBuilder; import org.traccar.helper.Parser; @@ -73,8 +74,9 @@ public class CityeasyProtocolDecoder extends BaseProtocolDecoder { buf.readUnsignedShort(); // length String imei = ChannelBuffers.hexDump(buf.readBytes(7)); - if (!identify(imei, channel, remoteAddress, false) - && !identify(imei + Checksum.luhn(Long.parseLong(imei)), channel, remoteAddress)) { + DeviceSession deviceSession = getDeviceSession( + channel, remoteAddress, imei, imei + Checksum.luhn(Long.parseLong(imei))); + if (deviceSession == null) { return null; } @@ -90,7 +92,7 @@ public class CityeasyProtocolDecoder extends BaseProtocolDecoder { Position position = new Position(); position.setProtocol(getProtocolName()); - position.setDeviceId(getDeviceId()); + position.setDeviceId(deviceSession.getDeviceId()); if (parser.hasNext(15)) { diff --git a/src/org/traccar/protocol/DishaProtocolDecoder.java b/src/org/traccar/protocol/DishaProtocolDecoder.java index 02f92d017..fb540c8d3 100644 --- a/src/org/traccar/protocol/DishaProtocolDecoder.java +++ b/src/org/traccar/protocol/DishaProtocolDecoder.java @@ -17,6 +17,7 @@ package org.traccar.protocol; import org.jboss.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; +import org.traccar.DeviceSession; import org.traccar.helper.DateBuilder; import org.traccar.helper.Parser; import org.traccar.helper.PatternBuilder; @@ -68,10 +69,11 @@ public class DishaProtocolDecoder extends BaseProtocolDecoder { Position position = new Position(); position.setProtocol(getProtocolName()); - if (!identify(parser.next(), channel, remoteAddress)) { + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, parser.next()); + if (deviceSession == null) { return null; } - position.setDeviceId(getDeviceId()); + position.setDeviceId(deviceSession.getDeviceId()); position.setValid(parser.next().equals("A")); diff --git a/src/org/traccar/protocol/EasyTrackProtocolDecoder.java b/src/org/traccar/protocol/EasyTrackProtocolDecoder.java index 013b9cc43..fa792d7b5 100644 --- a/src/org/traccar/protocol/EasyTrackProtocolDecoder.java +++ b/src/org/traccar/protocol/EasyTrackProtocolDecoder.java @@ -17,6 +17,7 @@ package org.traccar.protocol; import org.jboss.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; +import org.traccar.DeviceSession; import org.traccar.helper.BitUtil; import org.traccar.helper.DateBuilder; import org.traccar.helper.Parser; @@ -70,10 +71,11 @@ public class EasyTrackProtocolDecoder extends BaseProtocolDecoder { Position position = new Position(); position.setProtocol(getProtocolName()); - if (!identify(parser.next(), channel, remoteAddress)) { + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, parser.next()); + if (deviceSession == null) { return null; } - position.setDeviceId(getDeviceId()); + position.setDeviceId(deviceSession.getDeviceId()); position.set("command", parser.next()); diff --git a/src/org/traccar/protocol/EelinkProtocolDecoder.java b/src/org/traccar/protocol/EelinkProtocolDecoder.java index e9de73dd5..85f231a17 100644 --- a/src/org/traccar/protocol/EelinkProtocolDecoder.java +++ b/src/org/traccar/protocol/EelinkProtocolDecoder.java @@ -19,6 +19,7 @@ import org.jboss.netty.buffer.ChannelBuffer; import org.jboss.netty.buffer.ChannelBuffers; import org.jboss.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; +import org.traccar.DeviceSession; import org.traccar.helper.BitUtil; import org.traccar.helper.UnitsConverter; import org.traccar.model.Position; @@ -63,10 +64,10 @@ public class EelinkProtocolDecoder extends BaseProtocolDecoder { } } - private Position decodeOld(ChannelBuffer buf, int type, int index) { + private Position decodeOld(DeviceSession deviceSession, ChannelBuffer buf, int type, int index) { Position position = new Position(); - position.setDeviceId(getDeviceId()); + position.setDeviceId(deviceSession.getDeviceId()); position.setProtocol(getProtocolName()); position.set(Position.KEY_INDEX, index); @@ -95,10 +96,10 @@ public class EelinkProtocolDecoder extends BaseProtocolDecoder { return position; } - private Position decodeNew(ChannelBuffer buf, int type, int index) { + private Position decodeNew(DeviceSession deviceSession, ChannelBuffer buf, int type, int index) { Position position = new Position(); - position.setDeviceId(getDeviceId()); + position.setDeviceId(deviceSession.getDeviceId()); position.setProtocol(getProtocolName()); position.set(Position.KEY_INDEX, index); @@ -164,13 +165,18 @@ public class EelinkProtocolDecoder extends BaseProtocolDecoder { if (type == MSG_LOGIN) { - identify(ChannelBuffers.hexDump(buf.readBytes(8)).substring(1), channel, remoteAddress); + getDeviceSession(channel, remoteAddress, ChannelBuffers.hexDump(buf.readBytes(8)).substring(1)); + + } else { + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress); + if (deviceSession == null) { + return null; + } - } else if (hasDeviceId()) { if (type == MSG_GPS || type == MSG_ALARM || type == MSG_STATE || type == MSG_SMS) { - return decodeOld(buf, type, index); + return decodeOld(deviceSession, buf, type, index); } else if (type >= MSG_NORMAL && type <= MSG_OBD_CODE) { - return decodeNew(buf, type, index); + return decodeNew(deviceSession, buf, type, index); } } diff --git a/src/org/traccar/protocol/EnforaProtocolDecoder.java b/src/org/traccar/protocol/EnforaProtocolDecoder.java index 23cfb0093..e9a32da2d 100644 --- a/src/org/traccar/protocol/EnforaProtocolDecoder.java +++ b/src/org/traccar/protocol/EnforaProtocolDecoder.java @@ -19,6 +19,7 @@ import org.jboss.netty.buffer.ChannelBuffer; import org.jboss.netty.buffer.ChannelBufferIndexFinder; import org.jboss.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; +import org.traccar.DeviceSession; import org.traccar.helper.DateBuilder; import org.traccar.helper.Parser; import org.traccar.helper.PatternBuilder; @@ -77,7 +78,8 @@ public class EnforaProtocolDecoder extends BaseProtocolDecoder { } String imei = buf.toString(index, IMEI_LENGTH, StandardCharsets.US_ASCII); - if (!identify(imei, channel, remoteAddress)) { + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, imei); + if (deviceSession == null) { return null; } @@ -95,7 +97,7 @@ public class EnforaProtocolDecoder extends BaseProtocolDecoder { Position position = new Position(); position.setProtocol(getProtocolName()); - position.setDeviceId(getDeviceId()); + position.setDeviceId(deviceSession.getDeviceId()); DateBuilder dateBuilder = new DateBuilder() .setTime(parser.nextInt(), parser.nextInt(), parser.nextInt(), parser.nextInt()); diff --git a/src/org/traccar/protocol/FlextrackProtocolDecoder.java b/src/org/traccar/protocol/FlextrackProtocolDecoder.java index a0f151ac2..0ce2ab780 100644 --- a/src/org/traccar/protocol/FlextrackProtocolDecoder.java +++ b/src/org/traccar/protocol/FlextrackProtocolDecoder.java @@ -17,6 +17,7 @@ package org.traccar.protocol; import org.jboss.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; +import org.traccar.DeviceSession; import org.traccar.helper.DateBuilder; import org.traccar.helper.Parser; import org.traccar.helper.PatternBuilder; @@ -87,12 +88,15 @@ public class FlextrackProtocolDecoder extends BaseProtocolDecoder { String id = parser.next(); String iccid = parser.next(); - if (!identify(iccid, channel, remoteAddress, false) && !identify(id, channel, remoteAddress)) { + getDeviceSession(channel, remoteAddress, iccid, id); + + } else if (sentence.contains("UNITSTAT")) { + + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress); + if (deviceSession == null) { return null; } - } else if (sentence.contains("UNITSTAT") && hasDeviceId()) { - Parser parser = new Parser(PATTERN, sentence); if (!parser.matches()) { return null; @@ -100,7 +104,7 @@ public class FlextrackProtocolDecoder extends BaseProtocolDecoder { Position position = new Position(); position.setProtocol(getProtocolName()); - position.setDeviceId(getDeviceId()); + position.setDeviceId(deviceSession.getDeviceId()); sendAcknowledgement(channel, parser.next()); diff --git a/src/org/traccar/protocol/FoxProtocolDecoder.java b/src/org/traccar/protocol/FoxProtocolDecoder.java index ac267f567..fa14e7029 100644 --- a/src/org/traccar/protocol/FoxProtocolDecoder.java +++ b/src/org/traccar/protocol/FoxProtocolDecoder.java @@ -17,6 +17,7 @@ package org.traccar.protocol; import org.jboss.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; +import org.traccar.DeviceSession; import org.traccar.helper.DateBuilder; import org.traccar.helper.Parser; import org.traccar.helper.PatternBuilder; @@ -78,7 +79,8 @@ public class FoxProtocolDecoder extends BaseProtocolDecoder { if (id != null && data != null) { - if (!identify(id, channel, remoteAddress)) { + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, id); + if (deviceSession == null) { return null; } @@ -89,7 +91,7 @@ public class FoxProtocolDecoder extends BaseProtocolDecoder { Position position = new Position(); position.setProtocol(getProtocolName()); - position.setDeviceId(getDeviceId()); + position.setDeviceId(deviceSession.getDeviceId()); position.set(Position.KEY_STATUS, parser.nextInt()); diff --git a/src/org/traccar/protocol/FreedomProtocolDecoder.java b/src/org/traccar/protocol/FreedomProtocolDecoder.java index 03ab894c5..3a05486eb 100644 --- a/src/org/traccar/protocol/FreedomProtocolDecoder.java +++ b/src/org/traccar/protocol/FreedomProtocolDecoder.java @@ -17,6 +17,7 @@ package org.traccar.protocol; import org.jboss.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; +import org.traccar.DeviceSession; import org.traccar.helper.DateBuilder; import org.traccar.helper.Parser; import org.traccar.helper.PatternBuilder; @@ -56,10 +57,11 @@ public class FreedomProtocolDecoder extends BaseProtocolDecoder { Position position = new Position(); position.setProtocol(getProtocolName()); - if (!identify(parser.next(), channel, remoteAddress)) { + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, parser.next()); + if (deviceSession == null) { return null; } - position.setDeviceId(getDeviceId()); + position.setDeviceId(deviceSession.getDeviceId()); position.setValid(true); diff --git a/src/org/traccar/protocol/GalileoProtocolDecoder.java b/src/org/traccar/protocol/GalileoProtocolDecoder.java index 1b03f842c..eb863b442 100644 --- a/src/org/traccar/protocol/GalileoProtocolDecoder.java +++ b/src/org/traccar/protocol/GalileoProtocolDecoder.java @@ -19,6 +19,7 @@ import org.jboss.netty.buffer.ChannelBuffer; import org.jboss.netty.buffer.ChannelBuffers; import org.jboss.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; +import org.traccar.DeviceSession; import org.traccar.helper.Log; import org.traccar.model.Position; @@ -146,9 +147,7 @@ public class GalileoProtocolDecoder extends BaseProtocolDecoder { switch (tag) { case TAG_IMEI: - String imei = buf.toString(buf.readerIndex(), 15, StandardCharsets.US_ASCII); - buf.skipBytes(imei.length()); - identify(imei, channel, remoteAddress); + getDeviceSession(channel, remoteAddress, buf.readBytes(15).toString(StandardCharsets.US_ASCII)); break; case TAG_DATE: @@ -219,8 +218,8 @@ public class GalileoProtocolDecoder extends BaseProtocolDecoder { positions.add(position); } - if (!hasDeviceId()) { - Log.warning("Unknown device"); + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress); + if (deviceSession == null) { return null; } @@ -228,7 +227,7 @@ public class GalileoProtocolDecoder extends BaseProtocolDecoder { for (Position p : positions) { p.setProtocol(getProtocolName()); - p.setDeviceId(getDeviceId()); + p.setDeviceId(deviceSession.getDeviceId()); } if (positions.isEmpty()) { diff --git a/src/org/traccar/protocol/GatorProtocolDecoder.java b/src/org/traccar/protocol/GatorProtocolDecoder.java index ade27a56a..88da9a37f 100644 --- a/src/org/traccar/protocol/GatorProtocolDecoder.java +++ b/src/org/traccar/protocol/GatorProtocolDecoder.java @@ -19,6 +19,7 @@ import org.jboss.netty.buffer.ChannelBuffer; import org.jboss.netty.buffer.ChannelBuffers; import org.jboss.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; +import org.traccar.DeviceSession; import org.traccar.helper.BcdUtil; import org.traccar.helper.DateBuilder; import org.traccar.helper.UnitsConverter; @@ -90,10 +91,11 @@ public class GatorProtocolDecoder extends BaseProtocolDecoder { Position position = new Position(); position.setProtocol(getProtocolName()); - if (!identify("1" + id, channel, remoteAddress, false) && !identify(id, channel, remoteAddress)) { + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, "1" + id, id); + if (deviceSession == null) { return null; } - position.setDeviceId(getDeviceId()); + position.setDeviceId(deviceSession.getDeviceId()); DateBuilder dateBuilder = new DateBuilder() .setYear(BcdUtil.readInteger(buf, 2)) diff --git a/src/org/traccar/protocol/Gl100ProtocolDecoder.java b/src/org/traccar/protocol/Gl100ProtocolDecoder.java index 31102a676..d9e5bc6e6 100644 --- a/src/org/traccar/protocol/Gl100ProtocolDecoder.java +++ b/src/org/traccar/protocol/Gl100ProtocolDecoder.java @@ -17,6 +17,7 @@ package org.traccar.protocol; import org.jboss.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; +import org.traccar.DeviceSession; import org.traccar.helper.DateBuilder; import org.traccar.helper.Parser; import org.traccar.helper.PatternBuilder; @@ -75,10 +76,11 @@ public class Gl100ProtocolDecoder extends BaseProtocolDecoder { Position position = new Position(); position.setProtocol(getProtocolName()); - if (!identify(parser.next(), channel, remoteAddress)) { + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, parser.next()); + if (deviceSession == null) { return null; } - position.setDeviceId(getDeviceId()); + position.setDeviceId(deviceSession.getDeviceId()); position.setValid(parser.nextInt() == 0); position.setSpeed(parser.nextDouble()); diff --git a/src/org/traccar/protocol/Gl200ProtocolDecoder.java b/src/org/traccar/protocol/Gl200ProtocolDecoder.java index 2793e123a..670b9fa96 100644 --- a/src/org/traccar/protocol/Gl200ProtocolDecoder.java +++ b/src/org/traccar/protocol/Gl200ProtocolDecoder.java @@ -18,6 +18,7 @@ package org.traccar.protocol; import org.jboss.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; import org.traccar.Context; +import org.traccar.DeviceSession; import org.traccar.helper.BitUtil; import org.traccar.helper.DateBuilder; import org.traccar.helper.Parser; @@ -258,10 +259,11 @@ public class Gl200ProtocolDecoder extends BaseProtocolDecoder { Position position = new Position(); position.setProtocol(getProtocolName()); - if (!identify(parser.next(), channel, remoteAddress)) { + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, parser.next()); + if (deviceSession == null) { return null; } - position.setDeviceId(getDeviceId()); + position.setDeviceId(deviceSession.getDeviceId()); position.set(Position.KEY_STATUS, parser.next()); position.set(Position.KEY_POWER, parser.next()); @@ -318,10 +320,11 @@ public class Gl200ProtocolDecoder extends BaseProtocolDecoder { Position position = new Position(); position.setProtocol(getProtocolName()); - if (!identify(parser.next(), channel, remoteAddress)) { + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, parser.next()); + if (deviceSession == null) { return null; } - position.setDeviceId(getDeviceId()); + position.setDeviceId(deviceSession.getDeviceId()); position.set(Position.KEY_RPM, parser.next()); position.set(Position.KEY_OBD_SPEED, parser.next()); @@ -359,7 +362,8 @@ public class Gl200ProtocolDecoder extends BaseProtocolDecoder { LinkedList positions = new LinkedList<>(); - if (!identify(parser.next(), channel, remoteAddress)) { + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, parser.next()); + if (deviceSession == null) { return null; } @@ -370,7 +374,7 @@ public class Gl200ProtocolDecoder extends BaseProtocolDecoder { while (itemParser.find()) { Position position = new Position(); position.setProtocol(getProtocolName()); - position.setDeviceId(getDeviceId()); + position.setDeviceId(deviceSession.getDeviceId()); position.set(Position.KEY_VIN, vin); @@ -433,10 +437,11 @@ public class Gl200ProtocolDecoder extends BaseProtocolDecoder { Position position = new Position(); position.setProtocol(getProtocolName()); - if (!identify(parser.next(), channel, remoteAddress)) { + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, parser.next()); + if (deviceSession == null) { return null; } - position.setDeviceId(getDeviceId()); + position.setDeviceId(deviceSession.getDeviceId()); decodeLocation(position, parser); @@ -464,10 +469,11 @@ public class Gl200ProtocolDecoder extends BaseProtocolDecoder { Position position = new Position(); position.setProtocol(getProtocolName()); - if (!identify(parser.next(), channel, remoteAddress)) { + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, parser.next()); + if (deviceSession == null) { return null; } - position.setDeviceId(getDeviceId()); + position.setDeviceId(deviceSession.getDeviceId()); position.set(Position.KEY_RFID, parser.next()); @@ -496,10 +502,11 @@ public class Gl200ProtocolDecoder extends BaseProtocolDecoder { Position position = new Position(); position.setProtocol(getProtocolName()); - if (!identify(parser.next(), channel, remoteAddress)) { + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, parser.next()); + if (deviceSession == null) { return null; } - position.setDeviceId(getDeviceId()); + position.setDeviceId(deviceSession.getDeviceId()); int reportType = parser.nextInt(); if (type.equals("NMR")) { @@ -539,10 +546,11 @@ public class Gl200ProtocolDecoder extends BaseProtocolDecoder { Position position = new Position(); position.setProtocol(getProtocolName()); - if (!identify(parser.next(), channel, remoteAddress)) { + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, parser.next()); + if (deviceSession == null) { return null; } - position.setDeviceId(getDeviceId()); + position.setDeviceId(deviceSession.getDeviceId()); position.setSpeed(UnitsConverter.knotsFromKph(parser.nextDouble())); position.setCourse(parser.nextDouble()); diff --git a/src/org/traccar/protocol/GlobalSatProtocolDecoder.java b/src/org/traccar/protocol/GlobalSatProtocolDecoder.java index b8bb742e4..084279732 100644 --- a/src/org/traccar/protocol/GlobalSatProtocolDecoder.java +++ b/src/org/traccar/protocol/GlobalSatProtocolDecoder.java @@ -18,6 +18,7 @@ package org.traccar.protocol; import org.jboss.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; import org.traccar.Context; +import org.traccar.DeviceSession; import org.traccar.helper.DateBuilder; import org.traccar.helper.Parser; import org.traccar.helper.PatternBuilder; @@ -84,10 +85,11 @@ public class GlobalSatProtocolDecoder extends BaseProtocolDecoder { switch (format.charAt(formatIndex)) { case 'S': - if (!identify(value, channel, remoteAddress)) { + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, value); + if (deviceSession == null) { return null; } - position.setDeviceId(getDeviceId()); + position.setDeviceId(deviceSession.getDeviceId()); break; case 'A': if (value.isEmpty()) { @@ -203,10 +205,11 @@ public class GlobalSatProtocolDecoder extends BaseProtocolDecoder { Position position = new Position(); position.setProtocol(getProtocolName()); - if (!identify(parser.next(), channel, remoteAddress)) { + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, parser.next()); + if (deviceSession == null) { return null; } - position.setDeviceId(getDeviceId()); + position.setDeviceId(deviceSession.getDeviceId()); position.setValid(!parser.next().equals("1")); diff --git a/src/org/traccar/protocol/GnxProtocolDecoder.java b/src/org/traccar/protocol/GnxProtocolDecoder.java index 3b1cccfc4..a2ee5ae48 100644 --- a/src/org/traccar/protocol/GnxProtocolDecoder.java +++ b/src/org/traccar/protocol/GnxProtocolDecoder.java @@ -17,6 +17,7 @@ package org.traccar.protocol; import org.jboss.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; +import org.traccar.DeviceSession; import org.traccar.helper.DateBuilder; import org.traccar.helper.Parser; import org.traccar.helper.PatternBuilder; @@ -87,10 +88,11 @@ public class GnxProtocolDecoder extends BaseProtocolDecoder { Position position = new Position(); position.setProtocol(getProtocolName()); - if (!identify(parser.next(), channel, remoteAddress)) { + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, parser.next()); + if (deviceSession == null) { return null; } - position.setDeviceId(getDeviceId()); + position.setDeviceId(deviceSession.getDeviceId()); if (parser.nextInt() == 1) { position.set(Position.KEY_ARCHIVE, true); diff --git a/src/org/traccar/protocol/GoSafeProtocolDecoder.java b/src/org/traccar/protocol/GoSafeProtocolDecoder.java index 50e39d67f..8ce53cc40 100644 --- a/src/org/traccar/protocol/GoSafeProtocolDecoder.java +++ b/src/org/traccar/protocol/GoSafeProtocolDecoder.java @@ -17,6 +17,7 @@ package org.traccar.protocol; import org.jboss.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; +import org.traccar.DeviceSession; import org.traccar.helper.BitUtil; import org.traccar.helper.DateBuilder; import org.traccar.helper.Parser; @@ -126,11 +127,11 @@ public class GoSafeProtocolDecoder extends BaseProtocolDecoder { .any() .compile(); - private Position decodePosition(Parser parser, Date time) { + private Position decodePosition(DeviceSession deviceSession, Parser parser, Date time) { Position position = new Position(); position.setProtocol(getProtocolName()); - position.setDeviceId(getDeviceId()); + position.setDeviceId(deviceSession.getDeviceId()); if (time != null) { position.setTime(time); @@ -192,7 +193,8 @@ public class GoSafeProtocolDecoder extends BaseProtocolDecoder { return null; } - if (!identify(parser.next(), channel, remoteAddress)) { + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, parser.next()); + if (deviceSession == null) { return null; } @@ -200,7 +202,7 @@ public class GoSafeProtocolDecoder extends BaseProtocolDecoder { Position position = new Position(); position.setProtocol(getProtocolName()); - position.setDeviceId(getDeviceId()); + position.setDeviceId(deviceSession.getDeviceId()); DateBuilder dateBuilder = new DateBuilder() .setTime(parser.nextInt(), parser.nextInt(), parser.nextInt()); @@ -231,7 +233,7 @@ public class GoSafeProtocolDecoder extends BaseProtocolDecoder { List positions = new LinkedList<>(); Parser itemParser = new Parser(PATTERN_ITEM, parser.next()); while (itemParser.find()) { - positions.add(decodePosition(itemParser, time)); + positions.add(decodePosition(deviceSession, itemParser, time)); } return positions; diff --git a/src/org/traccar/protocol/GotopProtocolDecoder.java b/src/org/traccar/protocol/GotopProtocolDecoder.java index a62cfc623..0dcae3c8d 100644 --- a/src/org/traccar/protocol/GotopProtocolDecoder.java +++ b/src/org/traccar/protocol/GotopProtocolDecoder.java @@ -17,6 +17,7 @@ package org.traccar.protocol; import org.jboss.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; +import org.traccar.DeviceSession; import org.traccar.helper.DateBuilder; import org.traccar.helper.Parser; import org.traccar.helper.PatternBuilder; @@ -58,10 +59,11 @@ public class GotopProtocolDecoder extends BaseProtocolDecoder { Position position = new Position(); position.setProtocol(getProtocolName()); - if (!identify(parser.next(), channel, remoteAddress)) { + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, parser.next()); + if (deviceSession == null) { return null; } - position.setDeviceId(getDeviceId()); + position.setDeviceId(deviceSession.getDeviceId()); position.setValid(parser.next().equals("A")); diff --git a/src/org/traccar/protocol/Gps103ProtocolDecoder.java b/src/org/traccar/protocol/Gps103ProtocolDecoder.java index f8256a5ab..821cf2d21 100644 --- a/src/org/traccar/protocol/Gps103ProtocolDecoder.java +++ b/src/org/traccar/protocol/Gps103ProtocolDecoder.java @@ -17,6 +17,7 @@ package org.traccar.protocol; import org.jboss.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; +import org.traccar.DeviceSession; import org.traccar.helper.DateBuilder; import org.traccar.helper.Parser; import org.traccar.helper.PatternBuilder; @@ -113,7 +114,7 @@ public class Gps103ProtocolDecoder extends BaseProtocolDecoder { channel.write("LOAD", remoteAddress); Parser handshakeParser = new Parser(PATTERN_HANDSHAKE, sentence); if (handshakeParser.matches()) { - identify(handshakeParser.next(), channel, remoteAddress); + getDeviceSession(channel, remoteAddress, handshakeParser.next()); } } return null; @@ -138,10 +139,11 @@ public class Gps103ProtocolDecoder extends BaseProtocolDecoder { Parser parser = new Parser(PATTERN_NETWORK, sentence); if (parser.matches()) { - if (!identify(parser.next(), channel, remoteAddress)) { + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, parser.next()); + if (deviceSession == null) { return null; } - position.setDeviceId(getDeviceId()); + position.setDeviceId(deviceSession.getDeviceId()); getLastLocation(position, null); @@ -155,10 +157,11 @@ public class Gps103ProtocolDecoder extends BaseProtocolDecoder { parser = new Parser(PATTERN_OBD, sentence); if (parser.matches()) { - if (!identify(parser.next(), channel, remoteAddress)) { + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, parser.next()); + if (deviceSession == null) { return null; } - position.setDeviceId(getDeviceId()); + position.setDeviceId(deviceSession.getDeviceId()); DateBuilder dateBuilder = new DateBuilder() .setDate(parser.nextInt(), parser.nextInt(), parser.nextInt()) @@ -185,10 +188,11 @@ public class Gps103ProtocolDecoder extends BaseProtocolDecoder { } String imei = parser.next(); - if (!identify(imei, channel, remoteAddress)) { + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, imei); + if (deviceSession == null) { return null; } - position.setDeviceId(getDeviceId()); + position.setDeviceId(deviceSession.getDeviceId()); String alarm = parser.next(); position.set(Position.KEY_ALARM, alarm); diff --git a/src/org/traccar/protocol/GpsGateProtocolDecoder.java b/src/org/traccar/protocol/GpsGateProtocolDecoder.java index 127c986a3..1cb0ddfde 100644 --- a/src/org/traccar/protocol/GpsGateProtocolDecoder.java +++ b/src/org/traccar/protocol/GpsGateProtocolDecoder.java @@ -17,6 +17,7 @@ package org.traccar.protocol; import org.jboss.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; +import org.traccar.DeviceSession; import org.traccar.helper.Checksum; import org.traccar.helper.DateBuilder; import org.traccar.helper.Parser; @@ -85,7 +86,8 @@ public class GpsGateProtocolDecoder extends BaseProtocolDecoder { int endIndex = sentence.indexOf(',', beginIndex); if (endIndex != -1) { String imei = sentence.substring(beginIndex, endIndex); - if (identify(imei, channel, remoteAddress)) { + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, imei); + if (deviceSession != null) { if (channel != null) { send(channel, "$FRSES," + channel.getId()); } @@ -106,14 +108,19 @@ public class GpsGateProtocolDecoder extends BaseProtocolDecoder { } else if (sentence.startsWith("$GPRMC,")) { + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress); + if (deviceSession == null) { + return null; + } + Parser parser = new Parser(PATTERN_GPRMC, sentence); - if (!parser.matches() || !hasDeviceId()) { + if (!parser.matches()) { return null; } Position position = new Position(); position.setProtocol(getProtocolName()); - position.setDeviceId(getDeviceId()); + position.setDeviceId(deviceSession.getDeviceId()); DateBuilder dateBuilder = new DateBuilder() .setTime(parser.nextInt(), parser.nextInt(), parser.nextInt(), parser.nextInt()); @@ -139,10 +146,11 @@ public class GpsGateProtocolDecoder extends BaseProtocolDecoder { Position position = new Position(); position.setProtocol(getProtocolName()); - if (!identify(parser.next(), channel, remoteAddress)) { + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, parser.next()); + if (deviceSession == null) { return null; } - position.setDeviceId(getDeviceId()); + position.setDeviceId(deviceSession.getDeviceId()); position.setLatitude(parser.nextCoordinate()); position.setLongitude(parser.nextCoordinate()); diff --git a/src/org/traccar/protocol/GpsMarkerProtocolDecoder.java b/src/org/traccar/protocol/GpsMarkerProtocolDecoder.java index 515958339..4f6faa6ba 100644 --- a/src/org/traccar/protocol/GpsMarkerProtocolDecoder.java +++ b/src/org/traccar/protocol/GpsMarkerProtocolDecoder.java @@ -17,6 +17,7 @@ package org.traccar.protocol; import org.jboss.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; +import org.traccar.DeviceSession; import org.traccar.helper.DateBuilder; import org.traccar.helper.Parser; import org.traccar.helper.PatternBuilder; @@ -64,10 +65,11 @@ public class GpsMarkerProtocolDecoder extends BaseProtocolDecoder { Position position = new Position(); position.setProtocol(getProtocolName()); - if (!identify(parser.next(), channel, remoteAddress)) { + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, parser.next()); + if (deviceSession == null) { return null; } - position.setDeviceId(getDeviceId()); + position.setDeviceId(deviceSession.getDeviceId()); DateBuilder dateBuilder = new DateBuilder() .setDateReverse(parser.nextInt(), parser.nextInt(), parser.nextInt()) diff --git a/src/org/traccar/protocol/GpsmtaProtocolDecoder.java b/src/org/traccar/protocol/GpsmtaProtocolDecoder.java index b46d4c6f2..f10b3771c 100644 --- a/src/org/traccar/protocol/GpsmtaProtocolDecoder.java +++ b/src/org/traccar/protocol/GpsmtaProtocolDecoder.java @@ -17,6 +17,7 @@ package org.traccar.protocol; import org.jboss.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; +import org.traccar.DeviceSession; import org.traccar.helper.Parser; import org.traccar.helper.PatternBuilder; import org.traccar.model.Position; @@ -59,10 +60,11 @@ public class GpsmtaProtocolDecoder extends BaseProtocolDecoder { Position position = new Position(); position.setProtocol(getProtocolName()); - if (!identify(parser.next(), channel, remoteAddress)) { + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, parser.next()); + if (deviceSession == null) { return null; } - position.setDeviceId(getDeviceId()); + position.setDeviceId(deviceSession.getDeviceId()); String time = parser.next(); position.setTime(new Date(Long.parseLong(time) * 1000)); diff --git a/src/org/traccar/protocol/GranitProtocolDecoder.java b/src/org/traccar/protocol/GranitProtocolDecoder.java index 7432961b0..0c7ea4d1d 100644 --- a/src/org/traccar/protocol/GranitProtocolDecoder.java +++ b/src/org/traccar/protocol/GranitProtocolDecoder.java @@ -19,6 +19,7 @@ import org.jboss.netty.buffer.ChannelBuffer; import org.jboss.netty.buffer.ChannelBuffers; import org.jboss.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; +import org.traccar.DeviceSession; import org.traccar.helper.BitUtil; import org.traccar.helper.Checksum; import org.traccar.helper.StringFinder; @@ -136,11 +137,13 @@ public class GranitProtocolDecoder extends BaseProtocolDecoder { int indexTilde = buf.indexOf(buf.readerIndex(), buf.writerIndex(), new StringFinder("~")); - if (hasDeviceId() && indexTilde == -1) { + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress); + + if (deviceSession != null && indexTilde == -1) { String bufString = buf.toString(StandardCharsets.US_ASCII); Position position = new Position(); position.setProtocol(getProtocolName()); - position.setDeviceId(getDeviceId()); + position.setDeviceId(deviceSession.getDeviceId()); position.setTime(new Date()); getLastLocation(position, new Date()); @@ -155,9 +158,11 @@ public class GranitProtocolDecoder extends BaseProtocolDecoder { String header = buf.readBytes(HEADER_LENGTH).toString(StandardCharsets.US_ASCII); if (header.equals("+RRCB~")) { + buf.skipBytes(2); //binary length 26 int deviceId = buf.readUnsignedShort(); - if (!identify(String.valueOf(deviceId), channel, remoteAddress)) { + deviceSession = getDeviceSession(channel, remoteAddress, String.valueOf(deviceId)); + if (deviceSession == null) { return null; } long unixTime = buf.readUnsignedInt(); @@ -166,7 +171,7 @@ public class GranitProtocolDecoder extends BaseProtocolDecoder { } Position position = new Position(); position.setProtocol(getProtocolName()); - position.setDeviceId(getDeviceId()); + position.setDeviceId(deviceSession.getDeviceId()); position.setTime(new Date(unixTime * 1000)); @@ -174,9 +179,11 @@ public class GranitProtocolDecoder extends BaseProtocolDecoder { return position; } else if (header.equals("+DDAT~")) { + buf.skipBytes(2); //binary length int deviceId = buf.readUnsignedShort(); - if (!identify(String.valueOf(deviceId), channel, remoteAddress)) { + deviceSession = getDeviceSession(channel, remoteAddress, String.valueOf(deviceId)); + if (deviceSession == null) { return null; } byte format = buf.readByte(); @@ -197,7 +204,7 @@ public class GranitProtocolDecoder extends BaseProtocolDecoder { if (buf.getUnsignedByte(buf.readerIndex()) != 0xFE) { Position position = new Position(); position.setProtocol(getProtocolName()); - position.setDeviceId(getDeviceId()); + position.setDeviceId(deviceSession.getDeviceId()); position.setTime(new Date((unixTime + i * timeIncrement) * 1000)); decodeStructure(buf, position); positions.add(position); @@ -208,7 +215,10 @@ public class GranitProtocolDecoder extends BaseProtocolDecoder { buf.skipBytes(2); // increment } return positions; + } + return null; } + } diff --git a/src/org/traccar/protocol/Gt02ProtocolDecoder.java b/src/org/traccar/protocol/Gt02ProtocolDecoder.java index 2cc31bb30..d93b93b83 100644 --- a/src/org/traccar/protocol/Gt02ProtocolDecoder.java +++ b/src/org/traccar/protocol/Gt02ProtocolDecoder.java @@ -19,6 +19,7 @@ import org.jboss.netty.buffer.ChannelBuffer; import org.jboss.netty.buffer.ChannelBuffers; import org.jboss.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; +import org.traccar.DeviceSession; import org.traccar.helper.BitUtil; import org.traccar.helper.DateBuilder; import org.traccar.helper.UnitsConverter; @@ -52,10 +53,11 @@ public class Gt02ProtocolDecoder extends BaseProtocolDecoder { int gsm = buf.readUnsignedByte(); String imei = ChannelBuffers.hexDump(buf.readBytes(8)).substring(1); - if (!identify(imei, channel, remoteAddress)) { + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, imei); + if (deviceSession == null) { return null; } - position.setDeviceId(getDeviceId()); + position.setDeviceId(deviceSession.getDeviceId()); position.set(Position.KEY_INDEX, buf.readUnsignedShort()); diff --git a/src/org/traccar/protocol/Gt06ProtocolDecoder.java b/src/org/traccar/protocol/Gt06ProtocolDecoder.java index 57e2d64b7..f89d20fce 100644 --- a/src/org/traccar/protocol/Gt06ProtocolDecoder.java +++ b/src/org/traccar/protocol/Gt06ProtocolDecoder.java @@ -20,6 +20,7 @@ import org.jboss.netty.buffer.ChannelBuffers; import org.jboss.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; import org.traccar.Context; +import org.traccar.DeviceSession; import org.traccar.helper.BitUtil; import org.traccar.helper.Checksum; import org.traccar.helper.DateBuilder; @@ -175,7 +176,6 @@ public class Gt06ProtocolDecoder extends BaseProtocolDecoder { int length = buf.readUnsignedByte(); int dataLength = length - 5; - int type = buf.readUnsignedByte(); if (type == MSG_LOGIN) { @@ -197,17 +197,23 @@ public class Gt06ProtocolDecoder extends BaseProtocolDecoder { } } - if (identify(imei, channel, remoteAddress)) { + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, imei); + if (deviceSession != null) { buf.skipBytes(buf.readableBytes() - 6); sendResponse(channel, type, buf.readUnsignedShort()); } - } else if (hasDeviceId()) { + } else { + + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress); + if (deviceSession == null) { + return null; + } if (type == MSG_STRING) { Position position = new Position(); - position.setDeviceId(getDeviceId()); + position.setDeviceId(deviceSession.getDeviceId()); position.setProtocol(getProtocolName()); getLastLocation(position, null); @@ -228,7 +234,7 @@ public class Gt06ProtocolDecoder extends BaseProtocolDecoder { } else if (isSupported(type)) { Position position = new Position(); - position.setDeviceId(getDeviceId()); + position.setDeviceId(deviceSession.getDeviceId()); position.setProtocol(getProtocolName()); if (hasGps(type)) { @@ -271,39 +277,31 @@ public class Gt06ProtocolDecoder extends BaseProtocolDecoder { } else if (header == 0x7979) { + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress); + if (deviceSession == null) { + return null; + } + buf.readUnsignedShort(); // length int type = buf.readUnsignedByte(); if (type == MSG_INFO) { int subType = buf.readUnsignedByte(); - if (subType == 0x00) { + Position position = new Position(); + position.setDeviceId(deviceSession.getDeviceId()); + position.setProtocol(getProtocolName()); - Position position = new Position(); - position.setDeviceId(getDeviceId()); - position.setProtocol(getProtocolName()); - - getLastLocation(position, null); + getLastLocation(position, null); + if (subType == 0x00) { position.set(Position.KEY_POWER, buf.readUnsignedShort() * 0.01); - return position; - } else if (subType == 0x05) { - - Position position = new Position(); - position.setDeviceId(getDeviceId()); - position.setProtocol(getProtocolName()); - - getLastLocation(position, null); - int flags = buf.readUnsignedByte(); - position.set("door", BitUtil.check(flags, 0)); position.set(Position.PREFIX_IO + 1, BitUtil.check(flags, 2)); - return position; - } } diff --git a/src/org/traccar/protocol/H02ProtocolDecoder.java b/src/org/traccar/protocol/H02ProtocolDecoder.java index ec5d3adef..b340973e6 100644 --- a/src/org/traccar/protocol/H02ProtocolDecoder.java +++ b/src/org/traccar/protocol/H02ProtocolDecoder.java @@ -19,6 +19,7 @@ import org.jboss.netty.buffer.ChannelBuffer; import org.jboss.netty.buffer.ChannelBuffers; import org.jboss.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; +import org.traccar.DeviceSession; import org.traccar.helper.BcdUtil; import org.traccar.helper.BitUtil; import org.traccar.helper.DateBuilder; @@ -78,10 +79,12 @@ public class H02ProtocolDecoder extends BaseProtocolDecoder { buf.readByte(); // marker - if (!identify(ChannelBuffers.hexDump(buf.readBytes(5)), channel, remoteAddress)) { + DeviceSession deviceSession = getDeviceSession( + channel, remoteAddress, ChannelBuffers.hexDump(buf.readBytes(5))); + if (deviceSession == null) { return null; } - position.setDeviceId(getDeviceId()); + position.setDeviceId(deviceSession.getDeviceId()); DateBuilder dateBuilder = new DateBuilder() .setHour(BcdUtil.readInteger(buf, 2)) @@ -152,10 +155,11 @@ public class H02ProtocolDecoder extends BaseProtocolDecoder { Position position = new Position(); position.setProtocol(getProtocolName()); - if (!identify(parser.next(), channel, remoteAddress)) { + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, parser.next()); + if (deviceSession == null) { return null; } - position.setDeviceId(getDeviceId()); + position.setDeviceId(deviceSession.getDeviceId()); DateBuilder dateBuilder = new DateBuilder(); if (parser.hasNext(3)) { diff --git a/src/org/traccar/protocol/HaicomProtocolDecoder.java b/src/org/traccar/protocol/HaicomProtocolDecoder.java index f8509d63a..647477d50 100644 --- a/src/org/traccar/protocol/HaicomProtocolDecoder.java +++ b/src/org/traccar/protocol/HaicomProtocolDecoder.java @@ -17,6 +17,7 @@ package org.traccar.protocol; import org.jboss.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; +import org.traccar.DeviceSession; import org.traccar.helper.BitUtil; import org.traccar.helper.DateBuilder; import org.traccar.helper.Parser; @@ -65,10 +66,11 @@ public class HaicomProtocolDecoder extends BaseProtocolDecoder { Position position = new Position(); position.setProtocol(getProtocolName()); - if (!identify(parser.next(), channel, remoteAddress)) { + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, parser.next()); + if (deviceSession == null) { return null; } - position.setDeviceId(getDeviceId()); + position.setDeviceId(deviceSession.getDeviceId()); position.set(Position.KEY_VERSION, parser.next()); diff --git a/src/org/traccar/protocol/HomtecsProtocolDecoder.java b/src/org/traccar/protocol/HomtecsProtocolDecoder.java index c09ef75b1..21eb2c0b5 100644 --- a/src/org/traccar/protocol/HomtecsProtocolDecoder.java +++ b/src/org/traccar/protocol/HomtecsProtocolDecoder.java @@ -17,6 +17,7 @@ package org.traccar.protocol; import org.jboss.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; +import org.traccar.DeviceSession; import org.traccar.helper.DateBuilder; import org.traccar.helper.Parser; import org.traccar.helper.PatternBuilder; @@ -57,10 +58,11 @@ public class HomtecsProtocolDecoder extends BaseProtocolDecoder { Position position = new Position(); position.setProtocol(getProtocolName()); - if (!identify(parser.next(), channel, remoteAddress)) { + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, parser.next()); + if (deviceSession == null) { return null; } - position.setDeviceId(getDeviceId()); + position.setDeviceId(deviceSession.getDeviceId()); DateBuilder dateBuilder = new DateBuilder() .setDate(parser.nextInt(), parser.nextInt(), parser.nextInt()) diff --git a/src/org/traccar/protocol/HuaShengProtocolDecoder.java b/src/org/traccar/protocol/HuaShengProtocolDecoder.java index 50517045b..6901f37fb 100644 --- a/src/org/traccar/protocol/HuaShengProtocolDecoder.java +++ b/src/org/traccar/protocol/HuaShengProtocolDecoder.java @@ -19,6 +19,7 @@ import org.jboss.netty.buffer.ChannelBuffer; import org.jboss.netty.buffer.ChannelBuffers; import org.jboss.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; +import org.traccar.DeviceSession; import org.traccar.helper.DateBuilder; import org.traccar.helper.UnitsConverter; import org.traccar.model.Position; @@ -75,7 +76,8 @@ public class HuaShengProtocolDecoder extends BaseProtocolDecoder { int length = buf.readUnsignedShort() - 4; if (subtype == 0x0003) { String imei = buf.readBytes(length).toString(StandardCharsets.US_ASCII); - if (identify(imei, channel, remoteAddress) && channel != null) { + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, imei); + if (deviceSession != null && channel != null) { ChannelBuffer content = ChannelBuffers.dynamicBuffer(); content.writeByte(0); // success sendResponse(channel, MSG_LOGIN_RSP, content); @@ -87,9 +89,14 @@ public class HuaShengProtocolDecoder extends BaseProtocolDecoder { } else if (type == MSG_POSITION) { + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress); + if (deviceSession == null) { + return null; + } + Position position = new Position(); position.setProtocol(getProtocolName()); - position.setDeviceId(getDeviceId()); + position.setDeviceId(deviceSession.getDeviceId()); position.set(Position.KEY_STATUS, buf.readUnsignedShort()); position.set(Position.KEY_EVENT, buf.readUnsignedShort()); diff --git a/src/org/traccar/protocol/HuabaoProtocolDecoder.java b/src/org/traccar/protocol/HuabaoProtocolDecoder.java index 55473e180..e4c9a9860 100644 --- a/src/org/traccar/protocol/HuabaoProtocolDecoder.java +++ b/src/org/traccar/protocol/HuabaoProtocolDecoder.java @@ -19,6 +19,7 @@ import org.jboss.netty.buffer.ChannelBuffer; import org.jboss.netty.buffer.ChannelBuffers; import org.jboss.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; +import org.traccar.DeviceSession; import org.traccar.helper.BcdUtil; import org.traccar.helper.BitUtil; import org.traccar.helper.Checksum; @@ -81,7 +82,8 @@ public class HuabaoProtocolDecoder extends BaseProtocolDecoder { ChannelBuffer id = buf.readBytes(6); // phone number int index = buf.readUnsignedShort(); - if (!identify(ChannelBuffers.hexDump(id), channel, remoteAddress)) { + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, ChannelBuffers.hexDump(id)); + if (deviceSession == null) { return null; } @@ -101,7 +103,7 @@ public class HuabaoProtocolDecoder extends BaseProtocolDecoder { Position position = new Position(); position.setProtocol(getProtocolName()); - position.setDeviceId(getDeviceId()); + position.setDeviceId(deviceSession.getDeviceId()); position.set(Position.KEY_ALARM, buf.readUnsignedInt()); diff --git a/src/org/traccar/protocol/IdplProtocolDecoder.java b/src/org/traccar/protocol/IdplProtocolDecoder.java index 322a1e9fb..b0d331e3e 100644 --- a/src/org/traccar/protocol/IdplProtocolDecoder.java +++ b/src/org/traccar/protocol/IdplProtocolDecoder.java @@ -20,6 +20,7 @@ import java.util.regex.Pattern; import org.jboss.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; +import org.traccar.DeviceSession; import org.traccar.Protocol; import org.traccar.helper.DateBuilder; import org.traccar.helper.Parser; @@ -74,10 +75,11 @@ public class IdplProtocolDecoder extends BaseProtocolDecoder { position.set(Position.KEY_TYPE, parser.nextInt()); - if (!identify(parser.next(), channel, remoteAddress)) { + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, parser.next()); + if (deviceSession == null) { return null; } - position.setDeviceId(getDeviceId()); + position.setDeviceId(deviceSession.getDeviceId()); DateBuilder dateBuilder = new DateBuilder() .setDateReverse(parser.nextInt(), parser.nextInt(), parser.nextInt()) diff --git a/src/org/traccar/protocol/IntellitracProtocolDecoder.java b/src/org/traccar/protocol/IntellitracProtocolDecoder.java index 98ba90aa7..06d6476e6 100644 --- a/src/org/traccar/protocol/IntellitracProtocolDecoder.java +++ b/src/org/traccar/protocol/IntellitracProtocolDecoder.java @@ -17,6 +17,7 @@ package org.traccar.protocol; import org.jboss.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; +import org.traccar.DeviceSession; import org.traccar.helper.DateBuilder; import org.traccar.helper.Parser; import org.traccar.helper.PatternBuilder; @@ -75,10 +76,11 @@ public class IntellitracProtocolDecoder extends BaseProtocolDecoder { Position position = new Position(); position.setProtocol(getProtocolName()); - if (!identify(parser.next(), channel, remoteAddress)) { + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, parser.next()); + if (deviceSession == null) { return null; } - position.setDeviceId(getDeviceId()); + position.setDeviceId(deviceSession.getDeviceId()); DateBuilder dateBuilder = new DateBuilder() .setDate(parser.nextInt(), parser.nextInt(), parser.nextInt()) diff --git a/src/org/traccar/protocol/Jt600ProtocolDecoder.java b/src/org/traccar/protocol/Jt600ProtocolDecoder.java index 6529ff124..7e0b54456 100644 --- a/src/org/traccar/protocol/Jt600ProtocolDecoder.java +++ b/src/org/traccar/protocol/Jt600ProtocolDecoder.java @@ -19,6 +19,7 @@ import org.jboss.netty.buffer.ChannelBuffer; import org.jboss.netty.buffer.ChannelBuffers; import org.jboss.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; +import org.traccar.DeviceSession; import org.traccar.helper.BcdUtil; import org.traccar.helper.DateBuilder; import org.traccar.helper.Parser; @@ -50,10 +51,11 @@ public class Jt600ProtocolDecoder extends BaseProtocolDecoder { buf.readByte(); // header String id = String.valueOf(Long.parseLong(ChannelBuffers.hexDump(buf.readBytes(5)))); - if (!identify(id, channel, remoteAddress)) { + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, id); + if (deviceSession == null) { return null; } - position.setDeviceId(getDeviceId()); + position.setDeviceId(deviceSession.getDeviceId()); int version = BcdUtil.readInteger(buf, 1); buf.readUnsignedByte(); // type @@ -151,10 +153,11 @@ public class Jt600ProtocolDecoder extends BaseProtocolDecoder { position.set(Position.KEY_ALARM, true); - if (!identify(parser.next(), channel, remoteAddress)) { + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, parser.next()); + if (deviceSession == null) { return null; } - position.setDeviceId(getDeviceId()); + position.setDeviceId(deviceSession.getDeviceId()); position.setLongitude(parser.nextCoordinate()); position.setLatitude(parser.nextCoordinate()); diff --git a/src/org/traccar/protocol/KenjiProtocolDecoder.java b/src/org/traccar/protocol/KenjiProtocolDecoder.java index 2756cb1ec..2a95717ee 100755 --- a/src/org/traccar/protocol/KenjiProtocolDecoder.java +++ b/src/org/traccar/protocol/KenjiProtocolDecoder.java @@ -17,6 +17,7 @@ package org.traccar.protocol; import org.jboss.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; +import org.traccar.DeviceSession; import org.traccar.helper.DateBuilder; import org.traccar.helper.Parser; import org.traccar.helper.PatternBuilder; @@ -60,10 +61,11 @@ public class KenjiProtocolDecoder extends BaseProtocolDecoder { Position position = new Position(); position.setProtocol(getProtocolName()); - if (!identify(parser.next(), channel, remoteAddress)) { + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, parser.next()); + if (deviceSession == null) { return null; } - position.setDeviceId(getDeviceId()); + position.setDeviceId(deviceSession.getDeviceId()); position.set(Position.KEY_ALARM, parser.nextInt(16)); position.set(Position.KEY_OUTPUT, parser.nextInt(16)); diff --git a/src/org/traccar/protocol/KhdProtocolDecoder.java b/src/org/traccar/protocol/KhdProtocolDecoder.java index f4350d2cc..e34a24b40 100644 --- a/src/org/traccar/protocol/KhdProtocolDecoder.java +++ b/src/org/traccar/protocol/KhdProtocolDecoder.java @@ -19,6 +19,7 @@ import org.jboss.netty.buffer.ChannelBuffer; import org.jboss.netty.buffer.ChannelBuffers; import org.jboss.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; +import org.traccar.DeviceSession; import org.traccar.helper.BcdUtil; import org.traccar.helper.Checksum; import org.traccar.helper.DateBuilder; @@ -73,10 +74,11 @@ public class KhdProtocolDecoder extends BaseProtocolDecoder { Position position = new Position(); position.setProtocol(getProtocolName()); - if (!identify(readSerialNumber(buf), channel, remoteAddress)) { + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, readSerialNumber(buf)); + if (deviceSession == null) { return null; } - position.setDeviceId(getDeviceId()); + position.setDeviceId(deviceSession.getDeviceId()); DateBuilder dateBuilder = new DateBuilder() .setYear(BcdUtil.readInteger(buf, 2)) diff --git a/src/org/traccar/protocol/LaipacProtocolDecoder.java b/src/org/traccar/protocol/LaipacProtocolDecoder.java index ba0dad4b4..ebca443a1 100644 --- a/src/org/traccar/protocol/LaipacProtocolDecoder.java +++ b/src/org/traccar/protocol/LaipacProtocolDecoder.java @@ -17,6 +17,7 @@ package org.traccar.protocol; import org.jboss.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; +import org.traccar.DeviceSession; import org.traccar.helper.Checksum; import org.traccar.helper.DateBuilder; import org.traccar.helper.Parser; @@ -68,10 +69,11 @@ public class LaipacProtocolDecoder extends BaseProtocolDecoder { Position position = new Position(); position.setProtocol(getProtocolName()); - if (!identify(parser.next(), channel, remoteAddress)) { + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, parser.next()); + if (deviceSession == null) { return null; } - position.setDeviceId(getDeviceId()); + position.setDeviceId(deviceSession.getDeviceId()); DateBuilder dateBuilder = new DateBuilder() .setTime(parser.nextInt(), parser.nextInt(), parser.nextInt()); diff --git a/src/org/traccar/protocol/M2mProtocolDecoder.java b/src/org/traccar/protocol/M2mProtocolDecoder.java index 9a328472c..601b6d191 100644 --- a/src/org/traccar/protocol/M2mProtocolDecoder.java +++ b/src/org/traccar/protocol/M2mProtocolDecoder.java @@ -18,6 +18,7 @@ package org.traccar.protocol; import org.jboss.netty.buffer.ChannelBuffer; import org.jboss.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; +import org.traccar.DeviceSession; import org.traccar.helper.DateBuilder; import org.traccar.model.Position; @@ -58,13 +59,18 @@ public class M2mProtocolDecoder extends BaseProtocolDecoder { imei.append(b % 10); } - identify(imei.toString(), channel, remoteAddress); + getDeviceSession(channel, remoteAddress, imei.toString()); - } else if (hasDeviceId()) { + } else { + + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress); + if (deviceSession == null) { + return null; + } Position position = new Position(); position.setProtocol(getProtocolName()); - position.setDeviceId(getDeviceId()); + position.setDeviceId(deviceSession.getDeviceId()); DateBuilder dateBuilder = new DateBuilder() .setDay(buf.readUnsignedByte() & 0x3f) diff --git a/src/org/traccar/protocol/ManPowerProtocolDecoder.java b/src/org/traccar/protocol/ManPowerProtocolDecoder.java index 5ff111927..a2ede32b8 100644 --- a/src/org/traccar/protocol/ManPowerProtocolDecoder.java +++ b/src/org/traccar/protocol/ManPowerProtocolDecoder.java @@ -17,6 +17,7 @@ package org.traccar.protocol; import org.jboss.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; +import org.traccar.DeviceSession; import org.traccar.helper.DateBuilder; import org.traccar.helper.Parser; import org.traccar.helper.PatternBuilder; @@ -60,10 +61,11 @@ public class ManPowerProtocolDecoder extends BaseProtocolDecoder { Position position = new Position(); position.setProtocol(getProtocolName()); - if (!identify(parser.next(), channel, remoteAddress)) { + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, parser.next()); + if (deviceSession == null) { return null; } - position.setDeviceId(getDeviceId()); + position.setDeviceId(deviceSession.getDeviceId()); position.set(Position.KEY_STATUS, parser.next()); diff --git a/src/org/traccar/protocol/MegastekProtocolDecoder.java b/src/org/traccar/protocol/MegastekProtocolDecoder.java index ca554aa48..1ff7add1a 100644 --- a/src/org/traccar/protocol/MegastekProtocolDecoder.java +++ b/src/org/traccar/protocol/MegastekProtocolDecoder.java @@ -17,6 +17,7 @@ package org.traccar.protocol; import org.jboss.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; +import org.traccar.DeviceSession; import org.traccar.helper.DateBuilder; import org.traccar.helper.Parser; import org.traccar.helper.PatternBuilder; @@ -157,10 +158,11 @@ public class MegastekProtocolDecoder extends BaseProtocolDecoder { position.set(Position.KEY_ALARM, parser.next()); - if (!identify(parser.next(), channel, remoteAddress, false) && !identify(id, channel, remoteAddress)) { + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, parser.next(), id); + if (deviceSession == null) { return null; } - position.setDeviceId(getDeviceId()); + position.setDeviceId(deviceSession.getDeviceId()); position.set(Position.KEY_SATELLITES, parser.next()); @@ -182,10 +184,11 @@ public class MegastekProtocolDecoder extends BaseProtocolDecoder { } else { - if (!identify(id, channel, remoteAddress)) { + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, id); + if (deviceSession == null) { return null; } - position.setDeviceId(getDeviceId()); + position.setDeviceId(deviceSession.getDeviceId()); } @@ -194,10 +197,11 @@ public class MegastekProtocolDecoder extends BaseProtocolDecoder { Parser parser = new Parser(PATTERN_ALTERNATIVE, status); if (parser.matches()) { - if (!identify(id, channel, remoteAddress)) { + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, id); + if (deviceSession == null) { return null; } - position.setDeviceId(getDeviceId()); + position.setDeviceId(deviceSession.getDeviceId()); position.set(Position.KEY_MCC, parser.nextInt()); position.set(Position.KEY_MNC, parser.nextInt()); @@ -275,10 +279,11 @@ public class MegastekProtocolDecoder extends BaseProtocolDecoder { Position position = new Position(); position.setProtocol(getProtocolName()); - if (!identify(parser.next(), channel, remoteAddress)) { + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, parser.next()); + if (deviceSession == null) { return null; } - position.setDeviceId(getDeviceId()); + position.setDeviceId(deviceSession.getDeviceId()); if (parser.next().equals("S")) { position.set(Position.KEY_ARCHIVE, true); diff --git a/src/org/traccar/protocol/MeiligaoProtocolDecoder.java b/src/org/traccar/protocol/MeiligaoProtocolDecoder.java index cdbf3850a..9c7e9ea23 100644 --- a/src/org/traccar/protocol/MeiligaoProtocolDecoder.java +++ b/src/org/traccar/protocol/MeiligaoProtocolDecoder.java @@ -20,6 +20,7 @@ import org.jboss.netty.buffer.ChannelBuffers; import org.jboss.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; import org.traccar.Context; +import org.traccar.DeviceSession; import org.traccar.helper.Checksum; import org.traccar.helper.DateBuilder; import org.traccar.helper.Parser; @@ -90,7 +91,7 @@ public class MeiligaoProtocolDecoder extends BaseProtocolDecoder { public static final int MSG_RFID = 0x9966; - private boolean identify(ChannelBuffer buf, Channel channel, SocketAddress remoteAddress) { + private DeviceSession identify(ChannelBuffer buf, Channel channel, SocketAddress remoteAddress) { StringBuilder builder = new StringBuilder(); for (int i = 0; i < 7; i++) { @@ -113,13 +114,11 @@ public class MeiligaoProtocolDecoder extends BaseProtocolDecoder { String id = builder.toString(); - // Try to recreate full IMEI number - // Sometimes first digit is cut, so this won't work - if (id.length() == 14 && identify(id + Checksum.luhn(Long.parseLong(id)), channel, remoteAddress, false)) { - return true; + if (id.length() == 14) { + return getDeviceSession(channel, remoteAddress, id, id + Checksum.luhn(Long.parseLong(id))); + } else { + return getDeviceSession(channel, remoteAddress, id); } - - return identify(id, channel, remoteAddress); } private static void sendResponse( @@ -202,10 +201,11 @@ public class MeiligaoProtocolDecoder extends BaseProtocolDecoder { buf.skipBytes(6); } - if (!identify(id, channel, remoteAddress)) { + DeviceSession deviceSession = identify(id, channel, remoteAddress); + if (deviceSession == null) { return null; } - position.setDeviceId(getDeviceId()); + position.setDeviceId(deviceSession.getDeviceId()); if (command == MSG_RFID) { for (int i = 0; i < 15; i++) { diff --git a/src/org/traccar/protocol/MeitrackProtocolDecoder.java b/src/org/traccar/protocol/MeitrackProtocolDecoder.java index a7fca89fc..81b6b8f76 100644 --- a/src/org/traccar/protocol/MeitrackProtocolDecoder.java +++ b/src/org/traccar/protocol/MeitrackProtocolDecoder.java @@ -19,6 +19,7 @@ import org.jboss.netty.buffer.ChannelBuffer; import org.jboss.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; import org.traccar.Context; +import org.traccar.DeviceSession; import org.traccar.helper.DateBuilder; import org.traccar.helper.Parser; import org.traccar.helper.PatternBuilder; @@ -92,10 +93,11 @@ public class MeitrackProtocolDecoder extends BaseProtocolDecoder { Position position = new Position(); position.setProtocol(getProtocolName()); - if (!identify(parser.next(), channel, remoteAddress)) { + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, parser.next()); + if (deviceSession == null) { return null; } - position.setDeviceId(getDeviceId()); + position.setDeviceId(deviceSession.getDeviceId()); int event = parser.nextInt(); position.set(Position.KEY_EVENT, event); @@ -173,7 +175,8 @@ public class MeitrackProtocolDecoder extends BaseProtocolDecoder { int index = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) ','); String imei = buf.toString(index + 1, 15, StandardCharsets.US_ASCII); - if (!identify(imei, channel, remoteAddress)) { + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, imei); + if (deviceSession == null) { return null; } @@ -183,7 +186,7 @@ public class MeitrackProtocolDecoder extends BaseProtocolDecoder { Position position = new Position(); position.setProtocol(getProtocolName()); - position.setDeviceId(getDeviceId()); + position.setDeviceId(deviceSession.getDeviceId()); position.set(Position.KEY_EVENT, buf.readUnsignedByte()); @@ -251,7 +254,8 @@ public class MeitrackProtocolDecoder extends BaseProtocolDecoder { switch (type) { case "D03": if (channel != null) { - String imei = Context.getIdentityManager().getDeviceById(getDeviceId()).getUniqueId(); + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress); + String imei = Context.getIdentityManager().getDeviceById(deviceSession.getDeviceId()).getUniqueId(); channel.write("@@O46," + imei + ",D00,camera_picture.jpg,0*00\r\n"); } return null; diff --git a/src/org/traccar/protocol/MiniFinderProtocolDecoder.java b/src/org/traccar/protocol/MiniFinderProtocolDecoder.java index 96eaaf0fa..3eb559cc4 100644 --- a/src/org/traccar/protocol/MiniFinderProtocolDecoder.java +++ b/src/org/traccar/protocol/MiniFinderProtocolDecoder.java @@ -17,6 +17,7 @@ package org.traccar.protocol; import org.jboss.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; +import org.traccar.DeviceSession; import org.traccar.helper.BitUtil; import org.traccar.helper.DateBuilder; import org.traccar.helper.Parser; @@ -60,9 +61,14 @@ public class MiniFinderProtocolDecoder extends BaseProtocolDecoder { if (sentence.startsWith("!1")) { - identify(sentence.substring(3, sentence.length()), channel, remoteAddress); + getDeviceSession(channel, remoteAddress, sentence.substring(3, sentence.length())); - } else if (sentence.matches("![A-D].*") && hasDeviceId()) { + } else if (sentence.matches("![A-D].*")) { + + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress); + if (deviceSession == null) { + return null; + } Parser parser = new Parser(PATTERN, sentence); if (!parser.matches()) { @@ -71,7 +77,7 @@ public class MiniFinderProtocolDecoder extends BaseProtocolDecoder { Position position = new Position(); position.setProtocol(getProtocolName()); - position.setDeviceId(getDeviceId()); + position.setDeviceId(deviceSession.getDeviceId()); DateBuilder dateBuilder = new DateBuilder() .setDateReverse(parser.nextInt(), parser.nextInt(), parser.nextInt()) diff --git a/src/org/traccar/protocol/Mta6ProtocolDecoder.java b/src/org/traccar/protocol/Mta6ProtocolDecoder.java index 791a1acab..609f0b63e 100644 --- a/src/org/traccar/protocol/Mta6ProtocolDecoder.java +++ b/src/org/traccar/protocol/Mta6ProtocolDecoder.java @@ -24,6 +24,7 @@ import org.jboss.netty.handler.codec.http.HttpResponse; import org.jboss.netty.handler.codec.http.HttpResponseStatus; import org.jboss.netty.handler.codec.http.HttpVersion; import org.traccar.BaseProtocolDecoder; +import org.traccar.DeviceSession; import org.traccar.Protocol; import org.traccar.helper.BitUtil; import org.traccar.helper.DateBuilder; @@ -110,7 +111,7 @@ public class Mta6ProtocolDecoder extends BaseProtocolDecoder { } - private List parseFormatA(ChannelBuffer buf) { + private List parseFormatA(DeviceSession deviceSession, ChannelBuffer buf) { List positions = new LinkedList<>(); FloatReader latitudeReader = new FloatReader(); @@ -120,8 +121,8 @@ public class Mta6ProtocolDecoder extends BaseProtocolDecoder { try { while (buf.readable()) { Position position = new Position(); - position.setDeviceId(getDeviceId()); position.setProtocol(getProtocolName()); + position.setDeviceId(deviceSession.getDeviceId()); short flags = buf.readUnsignedByte(); @@ -197,9 +198,9 @@ public class Mta6ProtocolDecoder extends BaseProtocolDecoder { return positions; } - private Position parseFormatA1(ChannelBuffer buf) { + private Position parseFormatA1(DeviceSession deviceSession, ChannelBuffer buf) { Position position = new Position(); - position.setDeviceId(getDeviceId()); + position.setDeviceId(deviceSession.getDeviceId()); position.setProtocol(getProtocolName()); short flags = buf.readUnsignedByte(); @@ -283,7 +284,8 @@ public class Mta6ProtocolDecoder extends BaseProtocolDecoder { buf.skipBytes("id=".length()); int index = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) '&'); String uniqueId = buf.toString(buf.readerIndex(), index - buf.readerIndex(), StandardCharsets.US_ASCII); - if (!identify(uniqueId, channel, remoteAddress)) { + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, uniqueId); + if (deviceSession == null) { return null; } buf.skipBytes(uniqueId.length()); @@ -303,9 +305,9 @@ public class Mta6ProtocolDecoder extends BaseProtocolDecoder { if (packetId == 0x31 || packetId == 0x32 || packetId == 0x36) { if (simple) { - return parseFormatA1(buf); + return parseFormatA1(deviceSession, buf); } else { - return parseFormatA(buf); + return parseFormatA(deviceSession, buf); } } //else if (0x34 0x38 0x4F 0x59) diff --git a/src/org/traccar/protocol/MtxProtocolDecoder.java b/src/org/traccar/protocol/MtxProtocolDecoder.java index 8af10c7d8..e39c73d17 100644 --- a/src/org/traccar/protocol/MtxProtocolDecoder.java +++ b/src/org/traccar/protocol/MtxProtocolDecoder.java @@ -17,6 +17,7 @@ package org.traccar.protocol; import org.jboss.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; +import org.traccar.DeviceSession; import org.traccar.helper.DateBuilder; import org.traccar.helper.Parser; import org.traccar.helper.PatternBuilder; @@ -71,10 +72,11 @@ public class MtxProtocolDecoder extends BaseProtocolDecoder { Position position = new Position(); position.setProtocol(getProtocolName()); - if (!identify(parser.next(), channel, remoteAddress)) { + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, parser.next()); + if (deviceSession == null) { return null; } - position.setDeviceId(getDeviceId()); + position.setDeviceId(deviceSession.getDeviceId()); DateBuilder dateBuilder = new DateBuilder() .setDate(parser.nextInt(), parser.nextInt(), parser.nextInt()) diff --git a/src/org/traccar/protocol/MxtProtocolDecoder.java b/src/org/traccar/protocol/MxtProtocolDecoder.java index 92e941e6d..56024057e 100644 --- a/src/org/traccar/protocol/MxtProtocolDecoder.java +++ b/src/org/traccar/protocol/MxtProtocolDecoder.java @@ -18,6 +18,7 @@ package org.traccar.protocol; import org.jboss.netty.buffer.ChannelBuffer; import org.jboss.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; +import org.traccar.DeviceSession; import org.traccar.helper.BitUtil; import org.traccar.helper.DateBuilder; import org.traccar.helper.UnitsConverter; @@ -46,7 +47,8 @@ public class MxtProtocolDecoder extends BaseProtocolDecoder { int type = buf.readUnsignedByte(); String id = String.valueOf(buf.readUnsignedInt()); - if (!identify(id, channel, remoteAddress)) { + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, id); + if (deviceSession == null) { return null; } @@ -54,7 +56,7 @@ public class MxtProtocolDecoder extends BaseProtocolDecoder { Position position = new Position(); position.setProtocol(getProtocolName()); - position.setDeviceId(getDeviceId()); + position.setDeviceId(deviceSession.getDeviceId()); buf.readUnsignedByte(); // protocol int infoGroups = buf.readUnsignedByte(); diff --git a/src/org/traccar/protocol/NavigilProtocolDecoder.java b/src/org/traccar/protocol/NavigilProtocolDecoder.java index 8dd4e140b..e2afdf8d3 100644 --- a/src/org/traccar/protocol/NavigilProtocolDecoder.java +++ b/src/org/traccar/protocol/NavigilProtocolDecoder.java @@ -19,6 +19,7 @@ import org.jboss.netty.buffer.ChannelBuffer; import org.jboss.netty.buffer.ChannelBuffers; import org.jboss.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; +import org.traccar.DeviceSession; import org.traccar.helper.Checksum; import org.traccar.helper.Log; import org.traccar.helper.UnitsConverter; @@ -78,13 +79,14 @@ public class NavigilProtocolDecoder extends BaseProtocolDecoder { } } - private Position parseUnitReport(ChannelBuffer buf, int sequenceNumber) { + private Position parseUnitReport( + DeviceSession deviceSession, ChannelBuffer buf, int sequenceNumber) { Position position = new Position(); position.setProtocol(getProtocolName()); position.setValid(true); position.set(Position.KEY_INDEX, sequenceNumber); - position.setDeviceId(getDeviceId()); + position.setDeviceId(deviceSession.getDeviceId()); buf.readUnsignedShort(); // report trigger buf.readUnsignedShort(); // flags @@ -112,13 +114,14 @@ public class NavigilProtocolDecoder extends BaseProtocolDecoder { return position; } - private Position parseTg2Report(ChannelBuffer buf, int sequenceNumber) { + private Position parseTg2Report( + DeviceSession deviceSession, ChannelBuffer buf, int sequenceNumber) { Position position = new Position(); position.setProtocol(getProtocolName()); position.setValid(true); position.set(Position.KEY_INDEX, sequenceNumber); - position.setDeviceId(getDeviceId()); + position.setDeviceId(deviceSession.getDeviceId()); buf.readUnsignedShort(); // report trigger buf.readUnsignedByte(); // reserved @@ -148,12 +151,13 @@ public class NavigilProtocolDecoder extends BaseProtocolDecoder { return position; } - private Position parsePositionReport(ChannelBuffer buf, int sequenceNumber, long timestamp) { + private Position parsePositionReport( + DeviceSession deviceSession, ChannelBuffer buf, int sequenceNumber, long timestamp) { Position position = new Position(); position.setProtocol(getProtocolName()); position.set(Position.KEY_INDEX, sequenceNumber); - position.setDeviceId(getDeviceId()); + position.setDeviceId(deviceSession.getDeviceId()); position.setTime(convertTimestamp(timestamp)); position.setLatitude(buf.readMedium() * 0.00002); @@ -169,12 +173,13 @@ public class NavigilProtocolDecoder extends BaseProtocolDecoder { return position; } - private Position parsePositionReport2(ChannelBuffer buf, int sequenceNumber, long timestamp) { + private Position parsePositionReport2( + DeviceSession deviceSession, ChannelBuffer buf, int sequenceNumber, long timestamp) { Position position = new Position(); position.setProtocol(getProtocolName()); position.set(Position.KEY_INDEX, sequenceNumber); - position.setDeviceId(getDeviceId()); + position.setDeviceId(deviceSession.getDeviceId()); position.setTime(convertTimestamp(timestamp)); position.setLatitude(buf.readInt() * 0.0000001); @@ -192,12 +197,13 @@ public class NavigilProtocolDecoder extends BaseProtocolDecoder { return position; } - private Position parseSnapshot4(ChannelBuffer buf, int sequenceNumber) { + private Position parseSnapshot4( + DeviceSession deviceSession, ChannelBuffer buf, int sequenceNumber) { Position position = new Position(); position.setProtocol(getProtocolName()); position.set(Position.KEY_INDEX, sequenceNumber); - position.setDeviceId(getDeviceId()); + position.setDeviceId(deviceSession.getDeviceId()); buf.readUnsignedByte(); // report trigger buf.readUnsignedByte(); // position fix source @@ -230,12 +236,13 @@ public class NavigilProtocolDecoder extends BaseProtocolDecoder { return position; } - private Position parseTrackingData(ChannelBuffer buf, int sequenceNumber, long timestamp) { + private Position parseTrackingData( + DeviceSession deviceSession, ChannelBuffer buf, int sequenceNumber, long timestamp) { Position position = new Position(); position.setProtocol(getProtocolName()); position.set(Position.KEY_INDEX, sequenceNumber); - position.setDeviceId(getDeviceId()); + position.setDeviceId(deviceSession.getDeviceId()); position.setTime(convertTimestamp(timestamp)); buf.readUnsignedByte(); // tracking mode @@ -273,32 +280,30 @@ public class NavigilProtocolDecoder extends BaseProtocolDecoder { int flags = buf.readUnsignedShort(); buf.readUnsignedShort(); // checksum - // Get device identifier - if (!identify(String.valueOf(buf.readUnsignedInt()), channel, remoteAddress)) { + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, String.valueOf(buf.readUnsignedInt())); + if (deviceSession == null) { return null; } - long timestamp = buf.readUnsignedInt(); // message timestamp + long timestamp = buf.readUnsignedInt(); - // Acknowledgment if ((flags & 0x1) == 0x0) { sendAcknowledgment(channel, sequenceNumber); } - // Parse messages switch (messageId) { case MSG_UNIT_REPORT: - return parseUnitReport(buf, sequenceNumber); + return parseUnitReport(deviceSession, buf, sequenceNumber); case MSG_TG2_REPORT: - return parseTg2Report(buf, sequenceNumber); + return parseTg2Report(deviceSession, buf, sequenceNumber); case MSG_POSITION_REPORT: - return parsePositionReport(buf, sequenceNumber, timestamp); + return parsePositionReport(deviceSession, buf, sequenceNumber, timestamp); case MSG_POSITION_REPORT_2: - return parsePositionReport2(buf, sequenceNumber, timestamp); + return parsePositionReport2(deviceSession, buf, sequenceNumber, timestamp); case MSG_SNAPSHOT4: - return parseSnapshot4(buf, sequenceNumber); + return parseSnapshot4(deviceSession, buf, sequenceNumber); case MSG_TRACKING_DATA: - return parseTrackingData(buf, sequenceNumber, timestamp); + return parseTrackingData(deviceSession, buf, sequenceNumber, timestamp); default: Log.warning(new UnsupportedOperationException(String.valueOf(messageId))); break; diff --git a/src/org/traccar/protocol/NavisProtocolDecoder.java b/src/org/traccar/protocol/NavisProtocolDecoder.java index fbd704355..fad24301c 100644 --- a/src/org/traccar/protocol/NavisProtocolDecoder.java +++ b/src/org/traccar/protocol/NavisProtocolDecoder.java @@ -19,9 +19,9 @@ import org.jboss.netty.buffer.ChannelBuffer; import org.jboss.netty.buffer.ChannelBuffers; import org.jboss.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; +import org.traccar.DeviceSession; import org.traccar.helper.BitUtil; import org.traccar.helper.DateBuilder; -import org.traccar.helper.Log; import org.traccar.model.Position; import java.net.SocketAddress; @@ -39,7 +39,6 @@ public class NavisProtocolDecoder extends BaseProtocolDecoder { super(protocol); } - // Format types public static final int F10 = 0x01; public static final int F20 = 0x02; public static final int F30 = 0x03; @@ -75,11 +74,11 @@ public class NavisProtocolDecoder extends BaseProtocolDecoder { } } - private ParseResult parsePosition(ChannelBuffer buf) { + private ParseResult parsePosition(DeviceSession deviceSession, ChannelBuffer buf) { Position position = new Position(); position.setProtocol(getProtocolName()); - position.setDeviceId(getDeviceId()); + position.setDeviceId(deviceSession.getDeviceId()); int format; if (buf.getUnsignedByte(buf.readerIndex()) == 0) { @@ -177,8 +176,8 @@ public class NavisProtocolDecoder extends BaseProtocolDecoder { return new ParseResult(index, position); } - private Object processSingle(Channel channel, ChannelBuffer buf) { - ParseResult result = parsePosition(buf); + private Object processSingle(DeviceSession deviceSession, Channel channel, ChannelBuffer buf) { + ParseResult result = parsePosition(deviceSession, buf); ChannelBuffer response = ChannelBuffers.dynamicBuffer(ByteOrder.LITTLE_ENDIAN, 8); response.writeBytes(ChannelBuffers.copiedBuffer(ByteOrder.LITTLE_ENDIAN, "* positions = new LinkedList<>(); int count = buf.readUnsignedByte(); for (int i = 0; i < count; i++) { - Position position = parsePosition(buf).getPosition(); + Position position = parsePosition(deviceSession, buf).getPosition(); if (position.getFixTime() != null) { positions.add(position); } @@ -217,7 +216,7 @@ public class NavisProtocolDecoder extends BaseProtocolDecoder { private Object processHandshake(Channel channel, SocketAddress remoteAddress, ChannelBuffer buf) { buf.readByte(); // semicolon symbol - if (identify(buf.toString(StandardCharsets.US_ASCII), channel, remoteAddress)) { + if (getDeviceSession(channel, remoteAddress, buf.toString(StandardCharsets.US_ASCII)) != null) { sendReply(channel, ChannelBuffers.copiedBuffer(ByteOrder.LITTLE_ENDIAN, "*T": - return processSingle(channel, buf); - case "*>A": - return processArray(channel, buf); - case "*>S": - return processHandshake(channel, remoteAddress, buf); - default: - Log.warning(new UnsupportedOperationException(type)); - break; + if (type.equals("*>S")) { + return processHandshake(channel, remoteAddress, buf); + } else { + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress); + if (deviceSession != null) { + if (type.equals("*>T")) { + return processSingle(deviceSession, channel, buf); + } else if (type.equals("*>A")) { + return processArray(deviceSession, channel, buf); + } + } } return null; diff --git a/src/org/traccar/protocol/NoranProtocolDecoder.java b/src/org/traccar/protocol/NoranProtocolDecoder.java index 2fe680560..c2161d9c9 100644 --- a/src/org/traccar/protocol/NoranProtocolDecoder.java +++ b/src/org/traccar/protocol/NoranProtocolDecoder.java @@ -19,6 +19,7 @@ import org.jboss.netty.buffer.ChannelBuffer; import org.jboss.netty.buffer.ChannelBuffers; import org.jboss.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; +import org.traccar.DeviceSession; import org.traccar.helper.BitUtil; import org.traccar.helper.DateBuilder; import org.traccar.helper.UnitsConverter; @@ -120,10 +121,11 @@ public class NoranProtocolDecoder extends BaseProtocolDecoder { rawId = buf.readBytes(11); } String id = rawId.toString(StandardCharsets.US_ASCII).replaceAll("[^\\p{Print}]", ""); - if (!identify(id, channel, remoteAddress)) { + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, id); + if (deviceSession == null) { return null; } - position.setDeviceId(getDeviceId()); + position.setDeviceId(deviceSession.getDeviceId()); if (newFormat) { DateFormat dateFormat = new SimpleDateFormat("yy-MM-dd HH:mm:ss"); diff --git a/src/org/traccar/protocol/NvsProtocolDecoder.java b/src/org/traccar/protocol/NvsProtocolDecoder.java index 2a195c599..c32501d56 100644 --- a/src/org/traccar/protocol/NvsProtocolDecoder.java +++ b/src/org/traccar/protocol/NvsProtocolDecoder.java @@ -19,6 +19,7 @@ import org.jboss.netty.buffer.ChannelBuffer; import org.jboss.netty.buffer.ChannelBuffers; import org.jboss.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; +import org.traccar.DeviceSession; import org.traccar.helper.UnitsConverter; import org.traccar.model.Position; @@ -53,13 +54,18 @@ public class NvsProtocolDecoder extends BaseProtocolDecoder { String imei = buf.toString(buf.readerIndex(), 15, StandardCharsets.US_ASCII); - if (!identify(imei, channel, remoteAddress)) { - sendResponse(channel, "NO01"); - } else { + if (getDeviceSession(channel, remoteAddress, imei) != null) { sendResponse(channel, "OK"); + } else { + sendResponse(channel, "NO01"); } - } else if (hasDeviceId()) { + } else { + + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress); + if (deviceSession == null) { + return null; + } List positions = new LinkedList<>(); @@ -72,7 +78,7 @@ public class NvsProtocolDecoder extends BaseProtocolDecoder { for (int i = 0; i < count; i++) { Position position = new Position(); position.setProtocol(getProtocolName()); - position.setDeviceId(getDeviceId()); + position.setDeviceId(deviceSession.getDeviceId()); position.setTime(new Date(buf.readUnsignedInt() * 1000)); diff --git a/src/org/traccar/protocol/OrionProtocolDecoder.java b/src/org/traccar/protocol/OrionProtocolDecoder.java index 0f6d88203..c65e534f4 100644 --- a/src/org/traccar/protocol/OrionProtocolDecoder.java +++ b/src/org/traccar/protocol/OrionProtocolDecoder.java @@ -19,6 +19,7 @@ import org.jboss.netty.buffer.ChannelBuffer; import org.jboss.netty.buffer.ChannelBuffers; import org.jboss.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; +import org.traccar.DeviceSession; import org.traccar.helper.DateBuilder; import org.traccar.model.Position; @@ -68,7 +69,9 @@ public class OrionProtocolDecoder extends BaseProtocolDecoder { sendResponse(channel, buf); } - if (!identify(String.valueOf(buf.readUnsignedInt()), channel, remoteAddress)) { + DeviceSession deviceSession = getDeviceSession( + channel, remoteAddress, String.valueOf(buf.readUnsignedInt())); + if (deviceSession == null) { return null; } @@ -77,7 +80,7 @@ public class OrionProtocolDecoder extends BaseProtocolDecoder { for (int i = 0; i < (header & 0x0f); i++) { Position position = new Position(); - position.setDeviceId(getDeviceId()); + position.setDeviceId(deviceSession.getDeviceId()); position.setProtocol(getProtocolName()); position.set(Position.KEY_EVENT, buf.readUnsignedByte()); diff --git a/src/org/traccar/protocol/OsmAndProtocolDecoder.java b/src/org/traccar/protocol/OsmAndProtocolDecoder.java index cea2e6b56..cfa8990dd 100644 --- a/src/org/traccar/protocol/OsmAndProtocolDecoder.java +++ b/src/org/traccar/protocol/OsmAndProtocolDecoder.java @@ -25,6 +25,7 @@ import org.jboss.netty.handler.codec.http.HttpVersion; import org.jboss.netty.handler.codec.http.QueryStringDecoder; import org.joda.time.format.ISODateTimeFormat; import org.traccar.BaseProtocolDecoder; +import org.traccar.DeviceSession; import org.traccar.model.Position; import java.net.SocketAddress; @@ -63,10 +64,11 @@ public class OsmAndProtocolDecoder extends BaseProtocolDecoder { switch (entry.getKey()) { case "id": case "deviceid": - if (!identify(value, channel, remoteAddress)) { + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, value); + if (deviceSession == null) { return null; } - position.setDeviceId(getDeviceId()); + position.setDeviceId(deviceSession.getDeviceId()); break; case "valid": position.setValid(Boolean.parseBoolean(value)); diff --git a/src/org/traccar/protocol/PathAwayProtocolDecoder.java b/src/org/traccar/protocol/PathAwayProtocolDecoder.java index bc2f48094..aa2de5704 100644 --- a/src/org/traccar/protocol/PathAwayProtocolDecoder.java +++ b/src/org/traccar/protocol/PathAwayProtocolDecoder.java @@ -24,6 +24,7 @@ import org.jboss.netty.handler.codec.http.HttpResponseStatus; import org.jboss.netty.handler.codec.http.HttpVersion; import org.jboss.netty.handler.codec.http.QueryStringDecoder; import org.traccar.BaseProtocolDecoder; +import org.traccar.DeviceSession; import org.traccar.helper.DateBuilder; import org.traccar.helper.Parser; import org.traccar.helper.PatternBuilder; @@ -61,7 +62,9 @@ public class PathAwayProtocolDecoder extends BaseProtocolDecoder { HttpRequest request = (HttpRequest) msg; QueryStringDecoder decoder = new QueryStringDecoder(request.getUri()); - if (!identify(decoder.getParameters().get("UserName").get(0), channel, remoteAddress)) { + DeviceSession deviceSession = getDeviceSession( + channel, remoteAddress, decoder.getParameters().get("UserName").get(0)); + if (deviceSession == null) { return null; } @@ -72,7 +75,7 @@ public class PathAwayProtocolDecoder extends BaseProtocolDecoder { Position position = new Position(); position.setProtocol(getProtocolName()); - position.setDeviceId(getDeviceId()); + position.setDeviceId(deviceSession.getDeviceId()); DateBuilder dateBuilder = new DateBuilder() .setDateReverse(parser.nextInt(), parser.nextInt(), parser.nextInt()) diff --git a/src/org/traccar/protocol/PiligrimProtocolDecoder.java b/src/org/traccar/protocol/PiligrimProtocolDecoder.java index 48e694b19..47f73fe8f 100644 --- a/src/org/traccar/protocol/PiligrimProtocolDecoder.java +++ b/src/org/traccar/protocol/PiligrimProtocolDecoder.java @@ -25,6 +25,7 @@ import org.jboss.netty.handler.codec.http.HttpResponseStatus; import org.jboss.netty.handler.codec.http.HttpVersion; import org.jboss.netty.handler.codec.http.QueryStringDecoder; import org.traccar.BaseProtocolDecoder; +import org.traccar.DeviceSession; import org.traccar.helper.BitUtil; import org.traccar.helper.DateBuilder; import org.traccar.model.Position; @@ -79,7 +80,9 @@ public class PiligrimProtocolDecoder extends BaseProtocolDecoder { sendResponse(channel, "BINGPS: OK"); QueryStringDecoder decoder = new QueryStringDecoder(request.getUri()); - if (!identify(decoder.getParameters().get("imei").get(0), channel, remoteAddress)) { + DeviceSession deviceSession = getDeviceSession( + channel, remoteAddress, decoder.getParameters().get("imei").get(0)); + if (deviceSession == null) { return null; } @@ -96,7 +99,7 @@ public class PiligrimProtocolDecoder extends BaseProtocolDecoder { Position position = new Position(); position.setProtocol(getProtocolName()); - position.setDeviceId(getDeviceId()); + position.setDeviceId(deviceSession.getDeviceId()); DateBuilder dateBuilder = new DateBuilder() .setDay(buf.readUnsignedByte()) diff --git a/src/org/traccar/protocol/ProgressProtocolDecoder.java b/src/org/traccar/protocol/ProgressProtocolDecoder.java index 0439c99db..e524626fa 100644 --- a/src/org/traccar/protocol/ProgressProtocolDecoder.java +++ b/src/org/traccar/protocol/ProgressProtocolDecoder.java @@ -19,6 +19,7 @@ import org.jboss.netty.buffer.ChannelBuffer; import org.jboss.netty.buffer.ChannelBuffers; import org.jboss.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; +import org.traccar.DeviceSession; import org.traccar.helper.BitUtil; import org.traccar.model.Position; @@ -78,9 +79,14 @@ public class ProgressProtocolDecoder extends BaseProtocolDecoder { buf.skipBytes(length); length = buf.readUnsignedShort(); String imei = buf.readBytes(length).toString(StandardCharsets.US_ASCII); - identify(imei, channel, remoteAddress); + getDeviceSession(channel, remoteAddress, imei); - } else if (hasDeviceId() && (type == MSG_POINT || type == MSG_ALARM || type == MSG_LOGMSG)) { + } else if (type == MSG_POINT || type == MSG_ALARM || type == MSG_LOGMSG) { + + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress); + if (deviceSession == null) { + return null; + } List positions = new LinkedList<>(); @@ -92,7 +98,7 @@ public class ProgressProtocolDecoder extends BaseProtocolDecoder { for (int j = 0; j < recordCount; j++) { Position position = new Position(); position.setProtocol(getProtocolName()); - position.setDeviceId(getDeviceId()); + position.setDeviceId(deviceSession.getDeviceId()); if (type == MSG_LOGMSG) { position.set(Position.KEY_ARCHIVE, true); diff --git a/src/org/traccar/protocol/Pt3000ProtocolDecoder.java b/src/org/traccar/protocol/Pt3000ProtocolDecoder.java index 146e3f0a4..88ffb0616 100644 --- a/src/org/traccar/protocol/Pt3000ProtocolDecoder.java +++ b/src/org/traccar/protocol/Pt3000ProtocolDecoder.java @@ -17,6 +17,7 @@ package org.traccar.protocol; import org.jboss.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; +import org.traccar.DeviceSession; import org.traccar.helper.DateBuilder; import org.traccar.helper.Parser; import org.traccar.helper.PatternBuilder; @@ -58,10 +59,11 @@ public class Pt3000ProtocolDecoder extends BaseProtocolDecoder { Position position = new Position(); position.setProtocol(getProtocolName()); - if (!identify(parser.next(), channel, remoteAddress)) { + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, parser.next()); + if (deviceSession == null) { return null; } - position.setDeviceId(getDeviceId()); + position.setDeviceId(deviceSession.getDeviceId()); DateBuilder dateBuilder = new DateBuilder() .setTime(parser.nextInt(), parser.nextInt(), parser.nextInt()); diff --git a/src/org/traccar/protocol/Pt502ProtocolDecoder.java b/src/org/traccar/protocol/Pt502ProtocolDecoder.java index 424d491ae..1436eb5ac 100644 --- a/src/org/traccar/protocol/Pt502ProtocolDecoder.java +++ b/src/org/traccar/protocol/Pt502ProtocolDecoder.java @@ -18,6 +18,7 @@ package org.traccar.protocol; import org.jboss.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; +import org.traccar.DeviceSession; import org.traccar.helper.DateBuilder; import org.traccar.helper.Parser; import org.traccar.helper.PatternBuilder; @@ -77,10 +78,11 @@ public class Pt502ProtocolDecoder extends BaseProtocolDecoder { Position position = new Position(); position.setProtocol(getProtocolName()); - if (!identify(parser.next(), channel, remoteAddress)) { + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, parser.next()); + if (deviceSession == null) { return null; } - position.setDeviceId(getDeviceId()); + position.setDeviceId(deviceSession.getDeviceId()); DateBuilder dateBuilder = new DateBuilder() .setTime(parser.nextInt(), parser.nextInt(), parser.nextInt(), parser.nextInt()); diff --git a/src/org/traccar/protocol/RitiProtocolDecoder.java b/src/org/traccar/protocol/RitiProtocolDecoder.java index b509367bb..bf041554f 100644 --- a/src/org/traccar/protocol/RitiProtocolDecoder.java +++ b/src/org/traccar/protocol/RitiProtocolDecoder.java @@ -18,6 +18,7 @@ package org.traccar.protocol; import org.jboss.netty.buffer.ChannelBuffer; import org.jboss.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; +import org.traccar.DeviceSession; import org.traccar.helper.DateBuilder; import org.traccar.helper.Parser; import org.traccar.helper.PatternBuilder; @@ -58,10 +59,11 @@ public class RitiProtocolDecoder extends BaseProtocolDecoder { Position position = new Position(); position.setProtocol(getProtocolName()); - if (!identify(String.valueOf(buf.readUnsignedShort()), channel, remoteAddress)) { + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, String.valueOf(buf.readUnsignedShort())); + if (deviceSession == null) { return null; } - position.setDeviceId(getDeviceId()); + position.setDeviceId(deviceSession.getDeviceId()); position.set("mode", buf.readUnsignedByte()); position.set("command", buf.readUnsignedByte()); diff --git a/src/org/traccar/protocol/RuptelaProtocolDecoder.java b/src/org/traccar/protocol/RuptelaProtocolDecoder.java index 0e1ab3d22..a9cea8c64 100644 --- a/src/org/traccar/protocol/RuptelaProtocolDecoder.java +++ b/src/org/traccar/protocol/RuptelaProtocolDecoder.java @@ -19,6 +19,7 @@ import org.jboss.netty.buffer.ChannelBuffer; import org.jboss.netty.buffer.ChannelBuffers; import org.jboss.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; +import org.traccar.DeviceSession; import org.traccar.helper.UnitsConverter; import org.traccar.model.Position; @@ -44,7 +45,8 @@ public class RuptelaProtocolDecoder extends BaseProtocolDecoder { buf.readUnsignedShort(); // data length String imei = String.format("%015d", buf.readLong()); - if (!identify(imei, channel, remoteAddress)) { + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, imei); + if (deviceSession == null) { return null; } @@ -59,7 +61,7 @@ public class RuptelaProtocolDecoder extends BaseProtocolDecoder { for (int i = 0; i < count; i++) { Position position = new Position(); position.setProtocol(getProtocolName()); - position.setDeviceId(getDeviceId()); + position.setDeviceId(deviceSession.getDeviceId()); position.setTime(new Date(buf.readUnsignedInt() * 1000)); buf.readUnsignedByte(); // timestamp extension diff --git a/src/org/traccar/protocol/SanavProtocolDecoder.java b/src/org/traccar/protocol/SanavProtocolDecoder.java index 156e730a9..103b2d395 100644 --- a/src/org/traccar/protocol/SanavProtocolDecoder.java +++ b/src/org/traccar/protocol/SanavProtocolDecoder.java @@ -17,6 +17,7 @@ package org.traccar.protocol; import org.jboss.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; +import org.traccar.DeviceSession; import org.traccar.helper.DateBuilder; import org.traccar.helper.Parser; import org.traccar.helper.PatternBuilder; @@ -61,10 +62,11 @@ public class SanavProtocolDecoder extends BaseProtocolDecoder { Position position = new Position(); position.setProtocol(getProtocolName()); - if (!identify(parser.next(), channel, remoteAddress)) { + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, parser.next()); + if (deviceSession == null) { return null; } - position.setDeviceId(getDeviceId()); + position.setDeviceId(deviceSession.getDeviceId()); DateBuilder dateBuilder = new DateBuilder() .setTime(parser.nextInt(), parser.nextInt(), parser.nextInt(), parser.nextInt()); diff --git a/src/org/traccar/protocol/SkypatrolProtocolDecoder.java b/src/org/traccar/protocol/SkypatrolProtocolDecoder.java index 13e036f0a..d07bb152b 100644 --- a/src/org/traccar/protocol/SkypatrolProtocolDecoder.java +++ b/src/org/traccar/protocol/SkypatrolProtocolDecoder.java @@ -19,6 +19,7 @@ import org.jboss.netty.buffer.ChannelBuffer; import org.jboss.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; import org.traccar.Context; +import org.traccar.DeviceSession; import org.traccar.helper.BitUtil; import org.traccar.helper.DateBuilder; import org.traccar.helper.Log; @@ -84,10 +85,11 @@ public class SkypatrolProtocolDecoder extends BaseProtocolDecoder { Log.warning("No device id field"); return null; } - if (!identify(id, channel, remoteAddress)) { + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, id); + if (deviceSession == null) { return null; } - position.setDeviceId(getDeviceId()); + position.setDeviceId(deviceSession.getDeviceId()); if (BitUtil.check(mask, 3)) { buf.readUnsignedShort(); // io data diff --git a/src/org/traccar/protocol/Stl060ProtocolDecoder.java b/src/org/traccar/protocol/Stl060ProtocolDecoder.java index a966dfea1..b8eb5ed3d 100644 --- a/src/org/traccar/protocol/Stl060ProtocolDecoder.java +++ b/src/org/traccar/protocol/Stl060ProtocolDecoder.java @@ -17,6 +17,7 @@ package org.traccar.protocol; import org.jboss.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; +import org.traccar.DeviceSession; import org.traccar.helper.DateBuilder; import org.traccar.helper.Parser; import org.traccar.helper.PatternBuilder; @@ -78,10 +79,11 @@ public class Stl060ProtocolDecoder extends BaseProtocolDecoder { Position position = new Position(); position.setProtocol(getProtocolName()); - if (!identify(parser.next(), channel, remoteAddress)) { + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, parser.next()); + if (deviceSession == null) { return null; } - position.setDeviceId(getDeviceId()); + position.setDeviceId(deviceSession.getDeviceId()); DateBuilder dateBuilder = new DateBuilder() .setDateReverse(parser.nextInt(), parser.nextInt(), parser.nextInt()) diff --git a/src/org/traccar/protocol/SuntechProtocolDecoder.java b/src/org/traccar/protocol/SuntechProtocolDecoder.java index 5f18fd3ee..5169e7c67 100644 --- a/src/org/traccar/protocol/SuntechProtocolDecoder.java +++ b/src/org/traccar/protocol/SuntechProtocolDecoder.java @@ -17,6 +17,7 @@ package org.traccar.protocol; import org.jboss.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; +import org.traccar.DeviceSession; import org.traccar.helper.DateBuilder; import org.traccar.helper.Parser; import org.traccar.helper.PatternBuilder; @@ -71,10 +72,11 @@ public class SuntechProtocolDecoder extends BaseProtocolDecoder { } } - if (!identify(parser.next(), channel, remoteAddress)) { + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, parser.next()); + if (deviceSession == null) { return null; } - position.setDeviceId(getDeviceId()); + position.setDeviceId(deviceSession.getDeviceId()); position.set(Position.KEY_VERSION, parser.next()); diff --git a/src/org/traccar/protocol/SupermateProtocolDecoder.java b/src/org/traccar/protocol/SupermateProtocolDecoder.java index bcbd02eb0..f673bff40 100755 --- a/src/org/traccar/protocol/SupermateProtocolDecoder.java +++ b/src/org/traccar/protocol/SupermateProtocolDecoder.java @@ -18,6 +18,7 @@ package org.traccar.protocol; import org.jboss.netty.buffer.ChannelBuffers; import org.jboss.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; +import org.traccar.DeviceSession; import org.traccar.helper.DateBuilder; import org.traccar.helper.Parser; import org.traccar.helper.PatternBuilder; @@ -74,10 +75,11 @@ public class SupermateProtocolDecoder extends BaseProtocolDecoder { position.setProtocol(getProtocolName()); String imei = parser.next(); - if (!identify(imei, channel, remoteAddress)) { + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, imei); + if (deviceSession == null) { return null; } - position.setDeviceId(getDeviceId()); + position.setDeviceId(deviceSession.getDeviceId()); position.set("commandId", parser.next()); position.set("command", parser.next()); diff --git a/src/org/traccar/protocol/T55ProtocolDecoder.java b/src/org/traccar/protocol/T55ProtocolDecoder.java index 87c7a60c3..34efc376f 100644 --- a/src/org/traccar/protocol/T55ProtocolDecoder.java +++ b/src/org/traccar/protocol/T55ProtocolDecoder.java @@ -18,6 +18,7 @@ package org.traccar.protocol; import org.jboss.netty.channel.Channel; import org.jboss.netty.channel.socket.DatagramChannel; import org.traccar.BaseProtocolDecoder; +import org.traccar.DeviceSession; import org.traccar.helper.DateBuilder; import org.traccar.helper.Parser; import org.traccar.helper.PatternBuilder; @@ -91,7 +92,8 @@ public class T55ProtocolDecoder extends BaseProtocolDecoder { private Position position = null; - private Position decodeGprmc(String sentence, SocketAddress remoteAddress, Channel channel) { + private Position decodeGprmc( + DeviceSession deviceSession, String sentence, SocketAddress remoteAddress, Channel channel) { if (channel != null && !(channel instanceof DatagramChannel)) { channel.write("OK1\r\n"); @@ -105,8 +107,8 @@ public class T55ProtocolDecoder extends BaseProtocolDecoder { Position position = new Position(); position.setProtocol(getProtocolName()); - if (hasDeviceId()) { - position.setDeviceId(getDeviceId()); + if (deviceSession != null) { + position.setDeviceId(deviceSession.getDeviceId()); } DateBuilder dateBuilder = new DateBuilder() @@ -123,13 +125,14 @@ public class T55ProtocolDecoder extends BaseProtocolDecoder { if (parser.hasNext(3)) { position.set(Position.KEY_SATELLITES, parser.next()); - if (!identify(parser.next(), channel, remoteAddress)) { + deviceSession = getDeviceSession(channel, remoteAddress, parser.next()); + if (deviceSession == null) { return null; } - position.setDeviceId(getDeviceId()); + position.setDeviceId(deviceSession.getDeviceId()); } - if (hasDeviceId()) { + if (deviceSession != null) { return position; } else { this.position = position; // save position @@ -137,7 +140,7 @@ public class T55ProtocolDecoder extends BaseProtocolDecoder { } } - private Position decodeGpgga(String sentence) { + private Position decodeGpgga(DeviceSession deviceSession, String sentence) { Parser parser = new Parser(PATTERN_GPGGA, sentence); if (!parser.matches()) { @@ -146,7 +149,7 @@ public class T55ProtocolDecoder extends BaseProtocolDecoder { Position position = new Position(); position.setProtocol(getProtocolName()); - position.setDeviceId(getDeviceId()); + position.setDeviceId(deviceSession.getDeviceId()); DateBuilder dateBuilder = new DateBuilder() .setCurrentDate() @@ -160,7 +163,7 @@ public class T55ProtocolDecoder extends BaseProtocolDecoder { return position; } - private Position decodeGprma(String sentence) { + private Position decodeGprma(DeviceSession deviceSession, String sentence) { Parser parser = new Parser(PATTERN_GPRMA, sentence); if (!parser.matches()) { @@ -169,7 +172,7 @@ public class T55ProtocolDecoder extends BaseProtocolDecoder { Position position = new Position(); position.setProtocol(getProtocolName()); - position.setDeviceId(getDeviceId()); + position.setDeviceId(deviceSession.getDeviceId()); position.setTime(new Date()); position.setValid(parser.next().equals("A")); @@ -181,7 +184,7 @@ public class T55ProtocolDecoder extends BaseProtocolDecoder { return position; } - private Position decodeTrccr(String sentence) { + private Position decodeTrccr(DeviceSession deviceSession, String sentence) { Parser parser = new Parser(PATTERN_TRCCR, sentence); if (!parser.matches()) { @@ -190,7 +193,7 @@ public class T55ProtocolDecoder extends BaseProtocolDecoder { Position position = new Position(); position.setProtocol(getProtocolName()); - position.setDeviceId(getDeviceId()); + position.setDeviceId(deviceSession.getDeviceId()); DateBuilder dateBuilder = new DateBuilder() .setDate(parser.nextInt(), parser.nextInt(), parser.nextInt()) @@ -215,6 +218,8 @@ public class T55ProtocolDecoder extends BaseProtocolDecoder { String sentence = (String) msg; + DeviceSession deviceSession; + if (!sentence.startsWith("$") && sentence.contains("$")) { int index = sentence.indexOf("$"); String id = sentence.substring(0, index); @@ -223,33 +228,36 @@ public class T55ProtocolDecoder extends BaseProtocolDecoder { } else if (id.endsWith("/")) { id = id.substring(id.indexOf('/') + 1, id.length() - 1); } - identify(id, channel, remoteAddress); + deviceSession = getDeviceSession(channel, remoteAddress, id); sentence = sentence.substring(index); + } else { + deviceSession = getDeviceSession(channel, remoteAddress); } if (sentence.startsWith("$PGID")) { - identify(sentence.substring(6, sentence.length() - 3), channel, remoteAddress); + getDeviceSession(channel, remoteAddress, sentence.substring(6, sentence.length() - 3)); } else if (sentence.startsWith("$PCPTI")) { - identify(sentence.substring(7, sentence.indexOf(",", 7)), channel, remoteAddress); + getDeviceSession(channel, remoteAddress, sentence.substring(7, sentence.indexOf(",", 7))); } else if (sentence.startsWith("IMEI")) { - identify(sentence.substring(5, sentence.length()), channel, remoteAddress); + getDeviceSession(channel, remoteAddress, sentence.substring(5, sentence.length())); } else if (sentence.startsWith("$GPFID")) { - if (identify(sentence.substring(7, sentence.length()), channel, remoteAddress) && position != null) { + deviceSession = getDeviceSession(channel, remoteAddress, sentence.substring(7, sentence.length())); + if (deviceSession != null && position != null) { Position position = this.position; - position.setDeviceId(getDeviceId()); + position.setDeviceId(deviceSession.getDeviceId()); this.position = null; return position; } } else if (sentence.matches("^[0-9A-F]+$")) { - identify(sentence, channel, remoteAddress); + getDeviceSession(channel, remoteAddress, sentence); } else if (sentence.startsWith("$GPRMC")) { - return decodeGprmc(sentence, remoteAddress, channel); - } else if (sentence.startsWith("$GPGGA") && hasDeviceId()) { - return decodeGpgga(sentence); - } else if (sentence.startsWith("$GPRMA") && hasDeviceId()) { - return decodeGprma(sentence); - } else if (sentence.startsWith("$TRCCR") && hasDeviceId()) { - return decodeTrccr(sentence); + return decodeGprmc(deviceSession, sentence, remoteAddress, channel); + } else if (sentence.startsWith("$GPGGA") && deviceSession != null) { + return decodeGpgga(deviceSession, sentence); + } else if (sentence.startsWith("$GPRMA") && deviceSession != null) { + return decodeGprma(deviceSession, sentence); + } else if (sentence.startsWith("$TRCCR") && deviceSession != null) { + return decodeTrccr(deviceSession, sentence); } return null; diff --git a/src/org/traccar/protocol/T800xProtocolDecoder.java b/src/org/traccar/protocol/T800xProtocolDecoder.java index dcf45bb06..de966f901 100644 --- a/src/org/traccar/protocol/T800xProtocolDecoder.java +++ b/src/org/traccar/protocol/T800xProtocolDecoder.java @@ -19,6 +19,7 @@ import org.jboss.netty.buffer.ChannelBuffer; import org.jboss.netty.buffer.ChannelBuffers; import org.jboss.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; +import org.traccar.DeviceSession; import org.traccar.helper.BcdUtil; import org.traccar.helper.BitUtil; import org.traccar.helper.DateBuilder; @@ -71,7 +72,9 @@ public class T800xProtocolDecoder extends BaseProtocolDecoder { int index = buf.readUnsignedShort(); ChannelBuffer imei = buf.readBytes(8); - if (!identify(ChannelBuffers.hexDump(imei).substring(1), channel, remoteAddress)) { + DeviceSession deviceSession = getDeviceSession( + channel, remoteAddress, ChannelBuffers.hexDump(imei).substring(1)); + if (deviceSession == null) { return null; } @@ -83,7 +86,7 @@ public class T800xProtocolDecoder extends BaseProtocolDecoder { Position position = new Position(); position.setProtocol(getProtocolName()); - position.setDeviceId(getDeviceId()); + position.setDeviceId(deviceSession.getDeviceId()); position.set(Position.KEY_INDEX, index); diff --git a/src/org/traccar/protocol/TaipProtocolDecoder.java b/src/org/traccar/protocol/TaipProtocolDecoder.java index f44b9bcbe..811db774c 100644 --- a/src/org/traccar/protocol/TaipProtocolDecoder.java +++ b/src/org/traccar/protocol/TaipProtocolDecoder.java @@ -17,6 +17,7 @@ package org.traccar.protocol; import org.jboss.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; +import org.traccar.DeviceSession; import org.traccar.helper.DateBuilder; import org.traccar.helper.DateUtil; import org.traccar.helper.Parser; @@ -84,27 +85,6 @@ public class TaipProtocolDecoder extends BaseProtocolDecoder { sentence = sentence.substring(beginIndex + 1); } - // Find device identifier - beginIndex = sentence.indexOf(";ID="); - if (beginIndex != -1) { - beginIndex += 4; - int endIndex = sentence.indexOf(';', beginIndex); - if (endIndex == -1) { - endIndex = sentence.length(); - } - - String id = sentence.substring(beginIndex, endIndex); - if (!identify(id, channel, remoteAddress)) { - return null; - } - - if (sendResponse && channel != null) { - channel.write(id); - } - } else { - return null; - } - Parser parser = new Parser(PATTERN, sentence); if (!parser.matches()) { return null; @@ -112,7 +92,6 @@ public class TaipProtocolDecoder extends BaseProtocolDecoder { Position position = new Position(); position.setProtocol(getProtocolName()); - position.setDeviceId(getDeviceId()); String week = parser.next(); String day = parser.next(); @@ -157,8 +136,9 @@ public class TaipProtocolDecoder extends BaseProtocolDecoder { switch (key) { case "id": - if (!identify(value, channel, remoteAddress)) { - return null; + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, value); + if (deviceSession != null) { + position.setDeviceId(deviceSession.getDeviceId()); } if (sendResponse && channel != null) { channel.write(value); @@ -186,7 +166,10 @@ public class TaipProtocolDecoder extends BaseProtocolDecoder { } } - return position; + if (position.getDeviceId() != 0) { + return position; + } + return null; } } diff --git a/src/org/traccar/protocol/TelicProtocolDecoder.java b/src/org/traccar/protocol/TelicProtocolDecoder.java index be4c80b84..818390f60 100644 --- a/src/org/traccar/protocol/TelicProtocolDecoder.java +++ b/src/org/traccar/protocol/TelicProtocolDecoder.java @@ -17,6 +17,7 @@ package org.traccar.protocol; import org.jboss.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; +import org.traccar.DeviceSession; import org.traccar.helper.DateBuilder; import org.traccar.helper.Parser; import org.traccar.helper.PatternBuilder; @@ -69,10 +70,11 @@ public class TelicProtocolDecoder extends BaseProtocolDecoder { Position position = new Position(); position.setProtocol(getProtocolName()); - if (!identify(parser.next(), channel, remoteAddress)) { + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, parser.next()); + if (deviceSession == null) { return null; } - position.setDeviceId(getDeviceId()); + position.setDeviceId(deviceSession.getDeviceId()); position.set(Position.KEY_TYPE, parser.next()); diff --git a/src/org/traccar/protocol/TeltonikaProtocolDecoder.java b/src/org/traccar/protocol/TeltonikaProtocolDecoder.java index eb77cf350..d982b07b3 100644 --- a/src/org/traccar/protocol/TeltonikaProtocolDecoder.java +++ b/src/org/traccar/protocol/TeltonikaProtocolDecoder.java @@ -19,6 +19,7 @@ import org.jboss.netty.buffer.ChannelBuffer; import org.jboss.netty.buffer.ChannelBuffers; import org.jboss.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; +import org.traccar.DeviceSession; import org.traccar.helper.BitUtil; import org.traccar.helper.UnitsConverter; import org.traccar.model.Position; @@ -39,11 +40,11 @@ public class TeltonikaProtocolDecoder extends BaseProtocolDecoder { int length = buf.readUnsignedShort(); String imei = buf.toString(buf.readerIndex(), length, StandardCharsets.US_ASCII); - boolean result = identify(imei, channel, remoteAddress); + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, imei); if (channel != null) { ChannelBuffer response = ChannelBuffers.directBuffer(1); - if (result) { + if (deviceSession != null) { response.writeByte(1); } else { response.writeByte(0); @@ -187,7 +188,7 @@ public class TeltonikaProtocolDecoder extends BaseProtocolDecoder { } - private List parseData(Channel channel, ChannelBuffer buf) { + private List parseData(Channel channel, SocketAddress remoteAddress, ChannelBuffer buf) { List positions = new LinkedList<>(); buf.skipBytes(4); // marker @@ -195,11 +196,16 @@ public class TeltonikaProtocolDecoder extends BaseProtocolDecoder { int codec = buf.readUnsignedByte(); int count = buf.readUnsignedByte(); + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress); + if (deviceSession == null) { + return null; + } + for (int i = 0; i < count; i++) { Position position = new Position(); position.setProtocol(getProtocolName()); - position.setDeviceId(getDeviceId()); + position.setDeviceId(deviceSession.getDeviceId()); if (codec == CODEC_12) { decodeSerial(position, buf); @@ -228,7 +234,7 @@ public class TeltonikaProtocolDecoder extends BaseProtocolDecoder { if (buf.getUnsignedShort(0) > 0) { parseIdentification(channel, remoteAddress, buf); } else { - return parseData(channel, buf); + return parseData(channel, remoteAddress, buf); } return null; diff --git a/src/org/traccar/protocol/ThinkRaceProtocolDecoder.java b/src/org/traccar/protocol/ThinkRaceProtocolDecoder.java index c7236dc6d..2c49cccaf 100644 --- a/src/org/traccar/protocol/ThinkRaceProtocolDecoder.java +++ b/src/org/traccar/protocol/ThinkRaceProtocolDecoder.java @@ -19,6 +19,7 @@ import org.jboss.netty.buffer.ChannelBuffer; import org.jboss.netty.buffer.ChannelBuffers; import org.jboss.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; +import org.traccar.DeviceSession; import org.traccar.helper.BitUtil; import org.traccar.model.Position; @@ -63,7 +64,8 @@ public class ThinkRaceProtocolDecoder extends BaseProtocolDecoder { if (command == 0x01) { String imei = buf.toString(buf.readerIndex(), 15, StandardCharsets.US_ASCII); - if (identify(imei, channel, remoteAddress) && channel != null) { + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, imei); + if (deviceSession != null && channel != null) { ChannelBuffer response = ChannelBuffers.dynamicBuffer(); response.writeByte(0x48); response.writeByte(0x52); // header response.writeBytes(id); @@ -76,11 +78,16 @@ public class ThinkRaceProtocolDecoder extends BaseProtocolDecoder { } } - } else if (hasDeviceId() && type == MSG_GPS) { + } else if (type == MSG_GPS) { + + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress); + if (deviceSession == null) { + return null; + } Position position = new Position(); position.setProtocol(getProtocolName()); - position.setDeviceId(getDeviceId()); + position.setDeviceId(deviceSession.getDeviceId()); position.setTime(new Date(buf.readUnsignedInt() * 1000)); diff --git a/src/org/traccar/protocol/Tk102ProtocolDecoder.java b/src/org/traccar/protocol/Tk102ProtocolDecoder.java index 8323d6a3d..bb7510eb1 100644 --- a/src/org/traccar/protocol/Tk102ProtocolDecoder.java +++ b/src/org/traccar/protocol/Tk102ProtocolDecoder.java @@ -17,6 +17,7 @@ package org.traccar.protocol; import org.jboss.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; +import org.traccar.DeviceSession; import org.traccar.helper.DateBuilder; import org.traccar.helper.Parser; import org.traccar.helper.PatternBuilder; @@ -58,14 +59,17 @@ public class Tk102ProtocolDecoder extends BaseProtocolDecoder { if (sentence.startsWith("[!")) { - if (!identify(sentence.substring(14, 14 + 15), channel, remoteAddress)) { - return null; - } - if (channel != null) { + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, sentence.substring(14, 14 + 15)); + if (deviceSession != null && channel != null) { channel.write("[”0000000001" + sentence.substring(13) + "]"); } - } else if (hasDeviceId()) { + } else { + + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress); + if (deviceSession == null) { + return null; + } Parser parser = new Parser(PATTERN, sentence); if (!parser.matches()) { @@ -74,7 +78,7 @@ public class Tk102ProtocolDecoder extends BaseProtocolDecoder { Position position = new Position(); position.setProtocol(getProtocolName()); - position.setDeviceId(getDeviceId()); + position.setDeviceId(deviceSession.getDeviceId()); DateBuilder dateBuilder = new DateBuilder() .setTime(parser.nextInt(), parser.nextInt(), parser.nextInt()); diff --git a/src/org/traccar/protocol/Tk103ProtocolDecoder.java b/src/org/traccar/protocol/Tk103ProtocolDecoder.java index a22eaa4ba..a0998d50d 100644 --- a/src/org/traccar/protocol/Tk103ProtocolDecoder.java +++ b/src/org/traccar/protocol/Tk103ProtocolDecoder.java @@ -18,6 +18,7 @@ package org.traccar.protocol; import org.jboss.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; import org.traccar.Context; +import org.traccar.DeviceSession; import org.traccar.helper.BitUtil; import org.traccar.helper.DateBuilder; import org.traccar.helper.Parser; @@ -103,10 +104,11 @@ public class Tk103ProtocolDecoder extends BaseProtocolDecoder { Parser parser = new Parser(PATTERN_BATTERY, sentence); if (parser.matches()) { - if (!identify(parser.next(), channel, remoteAddress)) { + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, parser.next()); + if (deviceSession == null) { return null; } - position.setDeviceId(getDeviceId()); + position.setDeviceId(deviceSession.getDeviceId()); DateBuilder dateBuilder = new DateBuilder() .setDateReverse(parser.nextInt(), parser.nextInt(), parser.nextInt()) @@ -129,10 +131,11 @@ public class Tk103ProtocolDecoder extends BaseProtocolDecoder { parser = new Parser(PATTERN_NETWORK, sentence); if (parser.matches()) { - if (!identify(parser.next(), channel, remoteAddress)) { + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, parser.next()); + if (deviceSession == null) { return null; } - position.setDeviceId(getDeviceId()); + position.setDeviceId(deviceSession.getDeviceId()); getLastLocation(position, null); @@ -149,10 +152,11 @@ public class Tk103ProtocolDecoder extends BaseProtocolDecoder { return null; } - if (!identify(parser.next(), channel, remoteAddress)) { + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, parser.next()); + if (deviceSession == null) { return null; } - position.setDeviceId(getDeviceId()); + position.setDeviceId(deviceSession.getDeviceId()); int alarm = sentence.indexOf("BO01"); if (alarm != -1) { diff --git a/src/org/traccar/protocol/Tlt2hProtocolDecoder.java b/src/org/traccar/protocol/Tlt2hProtocolDecoder.java index d7cc74e84..6a58d6dcb 100644 --- a/src/org/traccar/protocol/Tlt2hProtocolDecoder.java +++ b/src/org/traccar/protocol/Tlt2hProtocolDecoder.java @@ -17,6 +17,7 @@ package org.traccar.protocol; import org.jboss.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; +import org.traccar.DeviceSession; import org.traccar.helper.DateBuilder; import org.traccar.helper.Parser; import org.traccar.helper.PatternBuilder; @@ -69,7 +70,8 @@ public class Tlt2hProtocolDecoder extends BaseProtocolDecoder { return null; } - if (!identify(parser.next(), channel, remoteAddress)) { + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, parser.next()); + if (deviceSession == null) { return null; } @@ -84,7 +86,7 @@ public class Tlt2hProtocolDecoder extends BaseProtocolDecoder { Position position = new Position(); position.setProtocol(getProtocolName()); - position.setDeviceId(getDeviceId()); + position.setDeviceId(deviceSession.getDeviceId()); parser.next(); // base station info diff --git a/src/org/traccar/protocol/TopflytechProtocolDecoder.java b/src/org/traccar/protocol/TopflytechProtocolDecoder.java index 7333c4c86..04f379557 100644 --- a/src/org/traccar/protocol/TopflytechProtocolDecoder.java +++ b/src/org/traccar/protocol/TopflytechProtocolDecoder.java @@ -17,6 +17,7 @@ package org.traccar.protocol; import org.jboss.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; +import org.traccar.DeviceSession; import org.traccar.helper.DateBuilder; import org.traccar.helper.Parser; import org.traccar.helper.PatternBuilder; @@ -56,10 +57,11 @@ public class TopflytechProtocolDecoder extends BaseProtocolDecoder { Position position = new Position(); position.setProtocol(getProtocolName()); - if (!identify(parser.next(), channel, remoteAddress)) { + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, parser.next()); + if (deviceSession == null) { return null; } - position.setDeviceId(getDeviceId()); + position.setDeviceId(deviceSession.getDeviceId()); DateBuilder dateBuilder = new DateBuilder() .setDate(parser.nextInt(), parser.nextInt(), parser.nextInt()) diff --git a/src/org/traccar/protocol/TotemProtocolDecoder.java b/src/org/traccar/protocol/TotemProtocolDecoder.java index 1e07e7ed5..7b196f5ad 100644 --- a/src/org/traccar/protocol/TotemProtocolDecoder.java +++ b/src/org/traccar/protocol/TotemProtocolDecoder.java @@ -17,6 +17,7 @@ package org.traccar.protocol; import org.jboss.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; +import org.traccar.DeviceSession; import org.traccar.helper.DateBuilder; import org.traccar.helper.Parser; import org.traccar.helper.PatternBuilder; @@ -185,10 +186,11 @@ public class TotemProtocolDecoder extends BaseProtocolDecoder { Position position = new Position(); position.setProtocol(getProtocolName()); - if (!identify(parser.next(), channel, remoteAddress)) { + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, parser.next()); + if (deviceSession == null) { return null; } - position.setDeviceId(getDeviceId()); + position.setDeviceId(deviceSession.getDeviceId()); if (pattern == PATTERN1 || pattern == PATTERN2) { diff --git a/src/org/traccar/protocol/Tr20ProtocolDecoder.java b/src/org/traccar/protocol/Tr20ProtocolDecoder.java index de16fafb6..54b303b5d 100644 --- a/src/org/traccar/protocol/Tr20ProtocolDecoder.java +++ b/src/org/traccar/protocol/Tr20ProtocolDecoder.java @@ -17,6 +17,7 @@ package org.traccar.protocol; import org.jboss.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; +import org.traccar.DeviceSession; import org.traccar.helper.DateBuilder; import org.traccar.helper.Parser; import org.traccar.helper.PatternBuilder; @@ -73,10 +74,11 @@ public class Tr20ProtocolDecoder extends BaseProtocolDecoder { Position position = new Position(); position.setProtocol(getProtocolName()); - if (!identify(parser.next(), channel, remoteAddress)) { + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, parser.next()); + if (deviceSession == null) { return null; } - position.setDeviceId(getDeviceId()); + position.setDeviceId(deviceSession.getDeviceId()); position.setValid(parser.next().equals("A")); diff --git a/src/org/traccar/protocol/Tr900ProtocolDecoder.java b/src/org/traccar/protocol/Tr900ProtocolDecoder.java index 8659861d8..8dfff9e41 100644 --- a/src/org/traccar/protocol/Tr900ProtocolDecoder.java +++ b/src/org/traccar/protocol/Tr900ProtocolDecoder.java @@ -17,6 +17,7 @@ package org.traccar.protocol; import org.jboss.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; +import org.traccar.DeviceSession; import org.traccar.helper.DateBuilder; import org.traccar.helper.Parser; import org.traccar.helper.PatternBuilder; @@ -66,10 +67,11 @@ public class Tr900ProtocolDecoder extends BaseProtocolDecoder { Position position = new Position(); position.setProtocol(getProtocolName()); - if (!identify(parser.next(), channel, remoteAddress)) { + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, parser.next()); + if (deviceSession == null) { return null; } - position.setDeviceId(getDeviceId()); + position.setDeviceId(deviceSession.getDeviceId()); position.setValid(parser.nextInt() == 1); diff --git a/src/org/traccar/protocol/TrackboxProtocolDecoder.java b/src/org/traccar/protocol/TrackboxProtocolDecoder.java index 369409fc9..89253b891 100644 --- a/src/org/traccar/protocol/TrackboxProtocolDecoder.java +++ b/src/org/traccar/protocol/TrackboxProtocolDecoder.java @@ -17,6 +17,7 @@ package org.traccar.protocol; import org.jboss.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; +import org.traccar.DeviceSession; import org.traccar.helper.DateBuilder; import org.traccar.helper.Parser; import org.traccar.helper.PatternBuilder; @@ -59,12 +60,17 @@ public class TrackboxProtocolDecoder extends BaseProtocolDecoder { if (sentence.startsWith("a=connect")) { String id = sentence.substring(sentence.indexOf("i=") + 2); - if (identify(id, channel, remoteAddress)) { + if (getDeviceSession(channel, remoteAddress, id) != null) { sendResponse(channel); } return null; } + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress); + if (deviceSession == null) { + return null; + } + Parser parser = new Parser(PATTERN, sentence); if (!parser.matches()) { return null; @@ -72,8 +78,8 @@ public class TrackboxProtocolDecoder extends BaseProtocolDecoder { sendResponse(channel); Position position = new Position(); - position.setDeviceId(getDeviceId()); position.setProtocol(getProtocolName()); + position.setDeviceId(deviceSession.getDeviceId()); DateBuilder dateBuilder = new DateBuilder() .setTime(parser.nextInt(), parser.nextInt(), parser.nextInt(), parser.nextInt()); diff --git a/src/org/traccar/protocol/TramigoProtocolDecoder.java b/src/org/traccar/protocol/TramigoProtocolDecoder.java index be276a5f7..af81616d8 100644 --- a/src/org/traccar/protocol/TramigoProtocolDecoder.java +++ b/src/org/traccar/protocol/TramigoProtocolDecoder.java @@ -19,6 +19,7 @@ import org.jboss.netty.buffer.ChannelBuffer; import org.jboss.netty.buffer.ChannelBuffers; import org.jboss.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; +import org.traccar.DeviceSession; import org.traccar.helper.DateUtil; import org.traccar.helper.UnitsConverter; import org.traccar.model.Position; @@ -63,10 +64,11 @@ public class TramigoProtocolDecoder extends BaseProtocolDecoder { position.set(Position.KEY_INDEX, index); position.setValid(true); - if (!identify(String.valueOf(id), channel, remoteAddress)) { + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, String.valueOf(id)); + if (deviceSession == null) { return null; } - position.setDeviceId(getDeviceId()); + position.setDeviceId(deviceSession.getDeviceId()); if (protocol == 0x01 && (type == MSG_COMPACT || type == MSG_FULL)) { diff --git a/src/org/traccar/protocol/TrvProtocolDecoder.java b/src/org/traccar/protocol/TrvProtocolDecoder.java index 8b35805a4..42f8d313c 100644 --- a/src/org/traccar/protocol/TrvProtocolDecoder.java +++ b/src/org/traccar/protocol/TrvProtocolDecoder.java @@ -17,6 +17,7 @@ package org.traccar.protocol; import org.jboss.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; +import org.traccar.DeviceSession; import org.traccar.helper.DateBuilder; import org.traccar.helper.Parser; import org.traccar.helper.PatternBuilder; @@ -81,11 +82,12 @@ public class TrvProtocolDecoder extends BaseProtocolDecoder { } if (type.equals("AP00")) { - identify(sentence.substring(7), channel, remoteAddress); + getDeviceSession(channel, remoteAddress, sentence.substring(7)); return null; } - if (!hasDeviceId()) { + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress); + if (deviceSession == null) { return null; } @@ -98,7 +100,7 @@ public class TrvProtocolDecoder extends BaseProtocolDecoder { Position position = new Position(); position.setProtocol(getProtocolName()); - position.setDeviceId(getDeviceId()); + position.setDeviceId(deviceSession.getDeviceId()); getLastLocation(position, null); @@ -121,7 +123,7 @@ public class TrvProtocolDecoder extends BaseProtocolDecoder { Position position = new Position(); position.setProtocol(getProtocolName()); - position.setDeviceId(getDeviceId()); + position.setDeviceId(deviceSession.getDeviceId()); DateBuilder dateBuilder = new DateBuilder() .setDate(parser.nextInt(), parser.nextInt(), parser.nextInt()); diff --git a/src/org/traccar/protocol/Tt8850ProtocolDecoder.java b/src/org/traccar/protocol/Tt8850ProtocolDecoder.java index 614522f2b..39fedc138 100644 --- a/src/org/traccar/protocol/Tt8850ProtocolDecoder.java +++ b/src/org/traccar/protocol/Tt8850ProtocolDecoder.java @@ -17,6 +17,7 @@ package org.traccar.protocol; import org.jboss.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; +import org.traccar.DeviceSession; import org.traccar.helper.DateBuilder; import org.traccar.helper.Parser; import org.traccar.helper.PatternBuilder; @@ -70,10 +71,11 @@ public class Tt8850ProtocolDecoder extends BaseProtocolDecoder { Position position = new Position(); position.setProtocol(getProtocolName()); - if (!identify(parser.next(), channel, remoteAddress)) { + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, parser.next()); + if (deviceSession == null) { return null; } - position.setDeviceId(getDeviceId()); + position.setDeviceId(deviceSession.getDeviceId()); position.setValid(parser.nextInt() < 20); position.setSpeed(UnitsConverter.knotsFromKph(parser.nextDouble())); diff --git a/src/org/traccar/protocol/TytanProtocolDecoder.java b/src/org/traccar/protocol/TytanProtocolDecoder.java index b657ae029..092ba431a 100644 --- a/src/org/traccar/protocol/TytanProtocolDecoder.java +++ b/src/org/traccar/protocol/TytanProtocolDecoder.java @@ -19,6 +19,7 @@ import org.jboss.netty.buffer.ChannelBuffer; import org.jboss.netty.buffer.ChannelBuffers; import org.jboss.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; +import org.traccar.DeviceSession; import org.traccar.helper.BitUtil; import org.traccar.helper.UnitsConverter; import org.traccar.model.Position; @@ -136,7 +137,8 @@ public class TytanProtocolDecoder extends BaseProtocolDecoder { } String id = String.valueOf(buf.readUnsignedInt()); - if (!identify(id, channel, remoteAddress)) { + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, id); + if (deviceSession == null) { return null; } @@ -146,7 +148,7 @@ public class TytanProtocolDecoder extends BaseProtocolDecoder { Position position = new Position(); position.setProtocol(getProtocolName()); - position.setDeviceId(getDeviceId()); + position.setDeviceId(deviceSession.getDeviceId()); int end = buf.readerIndex() + buf.readUnsignedByte(); diff --git a/src/org/traccar/protocol/TzoneProtocolDecoder.java b/src/org/traccar/protocol/TzoneProtocolDecoder.java index f4d6c68f3..be4919dc2 100644 --- a/src/org/traccar/protocol/TzoneProtocolDecoder.java +++ b/src/org/traccar/protocol/TzoneProtocolDecoder.java @@ -19,6 +19,7 @@ import org.jboss.netty.buffer.ChannelBuffer; import org.jboss.netty.buffer.ChannelBuffers; import org.jboss.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; +import org.traccar.DeviceSession; import org.traccar.helper.BitUtil; import org.traccar.helper.DateBuilder; import org.traccar.model.Position; @@ -46,7 +47,8 @@ public class TzoneProtocolDecoder extends BaseProtocolDecoder { buf.readUnsignedInt(); // firmware String imei = ChannelBuffers.hexDump(buf.readBytes(8)).substring(1); - if (!identify(imei, channel, remoteAddress)) { + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, imei); + if (deviceSession == null) { return null; } @@ -54,7 +56,7 @@ public class TzoneProtocolDecoder extends BaseProtocolDecoder { Position position = new Position(); position.setProtocol(getProtocolName()); - position.setDeviceId(getDeviceId()); + position.setDeviceId(deviceSession.getDeviceId()); // GPS info diff --git a/src/org/traccar/protocol/UlbotechProtocolDecoder.java b/src/org/traccar/protocol/UlbotechProtocolDecoder.java index 2fd7d8f74..c17341b5f 100644 --- a/src/org/traccar/protocol/UlbotechProtocolDecoder.java +++ b/src/org/traccar/protocol/UlbotechProtocolDecoder.java @@ -20,6 +20,7 @@ import org.jboss.netty.buffer.ChannelBuffers; import org.jboss.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; import org.traccar.Context; +import org.traccar.DeviceSession; import org.traccar.helper.BitUtil; import org.traccar.helper.ObdDecoder; import org.traccar.helper.UnitsConverter; @@ -131,10 +132,11 @@ public class UlbotechProtocolDecoder extends BaseProtocolDecoder { position.setProtocol(getProtocolName()); String imei = ChannelBuffers.hexDump(buf.readBytes(8)).substring(1); - if (!identify(imei, channel, remoteAddress)) { + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, imei); + if (deviceSession == null) { return null; } - position.setDeviceId(getDeviceId()); + position.setDeviceId(deviceSession.getDeviceId()); long seconds = buf.readUnsignedInt() & 0x7fffffffL; seconds += 946684800L; // 2000-01-01 00:00 diff --git a/src/org/traccar/protocol/UproProtocolDecoder.java b/src/org/traccar/protocol/UproProtocolDecoder.java index f7166e589..b4000c37a 100644 --- a/src/org/traccar/protocol/UproProtocolDecoder.java +++ b/src/org/traccar/protocol/UproProtocolDecoder.java @@ -17,6 +17,7 @@ package org.traccar.protocol; import org.jboss.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; +import org.traccar.DeviceSession; import org.traccar.helper.DateBuilder; import org.traccar.helper.Parser; import org.traccar.helper.PatternBuilder; @@ -56,10 +57,11 @@ public class UproProtocolDecoder extends BaseProtocolDecoder { Position position = new Position(); position.setProtocol(getProtocolName()); - if (!identify(parser.next(), channel, remoteAddress)) { + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, parser.next()); + if (deviceSession == null) { return null; } - position.setDeviceId(getDeviceId()); + position.setDeviceId(deviceSession.getDeviceId()); DateBuilder dateBuilder = new DateBuilder() .setTime(parser.nextInt(), parser.nextInt(), parser.nextInt()); diff --git a/src/org/traccar/protocol/V680ProtocolDecoder.java b/src/org/traccar/protocol/V680ProtocolDecoder.java index 35bd3780b..29ac4fd90 100644 --- a/src/org/traccar/protocol/V680ProtocolDecoder.java +++ b/src/org/traccar/protocol/V680ProtocolDecoder.java @@ -17,6 +17,7 @@ package org.traccar.protocol; import org.jboss.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; +import org.traccar.DeviceSession; import org.traccar.helper.DateBuilder; import org.traccar.helper.Parser; import org.traccar.helper.PatternBuilder; @@ -62,7 +63,7 @@ public class V680ProtocolDecoder extends BaseProtocolDecoder { if (sentence.length() == 16) { - identify(sentence.substring(1, sentence.length()), channel, remoteAddress); + getDeviceSession(channel, remoteAddress, sentence.substring(1, sentence.length())); } else { @@ -74,13 +75,16 @@ public class V680ProtocolDecoder extends BaseProtocolDecoder { Position position = new Position(); position.setProtocol(getProtocolName()); + DeviceSession deviceSession; if (parser.hasNext()) { - identify(parser.next(), channel, remoteAddress); + deviceSession = getDeviceSession(channel, remoteAddress, parser.next()); + } else { + deviceSession = getDeviceSession(channel, remoteAddress); } - if (!hasDeviceId()) { + if (deviceSession == null) { return null; } - position.setDeviceId(getDeviceId()); + position.setDeviceId(deviceSession.getDeviceId()); position.set("user", parser.next()); position.setValid(parser.nextInt() > 0); diff --git a/src/org/traccar/protocol/VisiontekProtocolDecoder.java b/src/org/traccar/protocol/VisiontekProtocolDecoder.java index 5d9b905f1..bfd4a78fa 100644 --- a/src/org/traccar/protocol/VisiontekProtocolDecoder.java +++ b/src/org/traccar/protocol/VisiontekProtocolDecoder.java @@ -17,6 +17,7 @@ package org.traccar.protocol; import org.jboss.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; +import org.traccar.DeviceSession; import org.traccar.helper.DateBuilder; import org.traccar.helper.Parser; import org.traccar.helper.PatternBuilder; @@ -80,12 +81,11 @@ public class VisiontekProtocolDecoder extends BaseProtocolDecoder { Position position = new Position(); position.setProtocol(getProtocolName()); - String id = parser.next(); - String imei = parser.next(); - if (!identify(id, channel, remoteAddress, false) && (imei == null || !identify(imei, channel, remoteAddress))) { + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, parser.next(), parser.next()); + if (deviceSession == null) { return null; } - position.setDeviceId(getDeviceId()); + position.setDeviceId(deviceSession.getDeviceId()); DateBuilder dateBuilder = new DateBuilder() .setDateReverse(parser.nextInt(), parser.nextInt(), parser.nextInt()) diff --git a/src/org/traccar/protocol/WatchProtocolDecoder.java b/src/org/traccar/protocol/WatchProtocolDecoder.java index b9b42332f..ca63e701e 100644 --- a/src/org/traccar/protocol/WatchProtocolDecoder.java +++ b/src/org/traccar/protocol/WatchProtocolDecoder.java @@ -17,6 +17,7 @@ package org.traccar.protocol; import org.jboss.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; +import org.traccar.DeviceSession; import org.traccar.helper.DateBuilder; import org.traccar.helper.Parser; import org.traccar.helper.PatternBuilder; @@ -79,7 +80,8 @@ public class WatchProtocolDecoder extends BaseProtocolDecoder { String manufacturer = parser.next(); String id = parser.next(); - if (!identify(id, channel, remoteAddress)) { + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, id); + if (deviceSession == null) { return null; } @@ -95,7 +97,7 @@ public class WatchProtocolDecoder extends BaseProtocolDecoder { if (values.length >= 4) { Position position = new Position(); position.setProtocol(getProtocolName()); - position.setDeviceId(getDeviceId()); + position.setDeviceId(deviceSession.getDeviceId()); getLastLocation(position, null); @@ -118,7 +120,7 @@ public class WatchProtocolDecoder extends BaseProtocolDecoder { Position position = new Position(); position.setProtocol(getProtocolName()); - position.setDeviceId(getDeviceId()); + position.setDeviceId(deviceSession.getDeviceId()); DateBuilder dateBuilder = new DateBuilder() .setDateReverse(parser.nextInt(), parser.nextInt(), parser.nextInt()) diff --git a/src/org/traccar/protocol/WialonProtocolDecoder.java b/src/org/traccar/protocol/WialonProtocolDecoder.java index 1de2f9dbf..627b93b1c 100644 --- a/src/org/traccar/protocol/WialonProtocolDecoder.java +++ b/src/org/traccar/protocol/WialonProtocolDecoder.java @@ -17,6 +17,7 @@ package org.traccar.protocol; import org.jboss.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; +import org.traccar.DeviceSession; import org.traccar.helper.DateBuilder; import org.traccar.helper.Parser; import org.traccar.helper.PatternBuilder; @@ -66,16 +67,21 @@ public class WialonProtocolDecoder extends BaseProtocolDecoder { } } - private Position decodePosition(String substring) { + private Position decodePosition(Channel channel, SocketAddress remoteAddress, String substring) { + + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress); + if (deviceSession == null) { + return null; + } Parser parser = new Parser(PATTERN, substring); - if (!hasDeviceId() || !parser.matches()) { + if (!parser.matches()) { return null; } Position position = new Position(); position.setProtocol(getProtocolName()); - position.setDeviceId(getDeviceId()); + position.setDeviceId(deviceSession.getDeviceId()); DateBuilder dateBuilder = new DateBuilder() .setDateReverse(parser.nextInt(), parser.nextInt(), parser.nextInt()) @@ -129,7 +135,8 @@ public class WialonProtocolDecoder extends BaseProtocolDecoder { if (sentence.startsWith("#L#")) { String imei = sentence.substring(3, sentence.indexOf(';')); - if (identify(imei, channel, remoteAddress)) { + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, imei); + if (deviceSession != null) { sendResponse(channel, "#AL#", 1); } @@ -140,7 +147,7 @@ public class WialonProtocolDecoder extends BaseProtocolDecoder { } else if (sentence.startsWith("#SD#") || sentence.startsWith("#D#")) { Position position = decodePosition( - sentence.substring(sentence.indexOf('#', 1) + 1)); + channel, remoteAddress, sentence.substring(sentence.indexOf('#', 1) + 1)); if (position != null) { sendResponse(channel, "#AD#", 1); @@ -153,7 +160,7 @@ public class WialonProtocolDecoder extends BaseProtocolDecoder { List positions = new LinkedList<>(); for (String message : messages) { - Position position = decodePosition(message); + Position position = decodePosition(channel, remoteAddress, message); if (position != null) { positions.add(position); } diff --git a/src/org/traccar/protocol/WondexProtocolDecoder.java b/src/org/traccar/protocol/WondexProtocolDecoder.java index 81aeff54e..d048b9b31 100644 --- a/src/org/traccar/protocol/WondexProtocolDecoder.java +++ b/src/org/traccar/protocol/WondexProtocolDecoder.java @@ -18,6 +18,7 @@ package org.traccar.protocol; import org.jboss.netty.buffer.ChannelBuffer; import org.jboss.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; +import org.traccar.DeviceSession; import org.traccar.helper.DateBuilder; import org.traccar.helper.Parser; import org.traccar.helper.PatternBuilder; @@ -65,15 +66,17 @@ public class WondexProtocolDecoder extends BaseProtocolDecoder { if (buf.getUnsignedByte(0) == 0xD0) { long deviceId = ((Long.reverseBytes(buf.getLong(0))) >> 32) & 0xFFFFFFFFL; - identify(String.valueOf(deviceId), channel, remoteAddress); + getDeviceSession(channel, remoteAddress, String.valueOf(deviceId)); return null; } else if (buf.toString(StandardCharsets.US_ASCII).startsWith("$OK:") - || buf.toString(StandardCharsets.US_ASCII).startsWith("$ERR:")) { + || buf.toString(StandardCharsets.US_ASCII).startsWith("$ERR:")) { + + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress); Position position = new Position(); position.setProtocol(getProtocolName()); - position.setDeviceId(getDeviceId()); + position.setDeviceId(deviceSession.getDeviceId()); getLastLocation(position, new Date()); position.setValid(false); position.set(Position.KEY_RESULT, buf.toString(StandardCharsets.US_ASCII)); @@ -89,10 +92,11 @@ public class WondexProtocolDecoder extends BaseProtocolDecoder { Position position = new Position(); position.setProtocol(getProtocolName()); - if (!identify(parser.next(), channel, remoteAddress)) { + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, parser.next()); + if (deviceSession == null) { return null; } - position.setDeviceId(getDeviceId()); + position.setDeviceId(deviceSession.getDeviceId()); DateBuilder dateBuilder = new DateBuilder() .setDate(parser.nextInt(), parser.nextInt(), parser.nextInt()) diff --git a/src/org/traccar/protocol/XexunProtocolDecoder.java b/src/org/traccar/protocol/XexunProtocolDecoder.java index 82dc16d11..7b6a84f1f 100644 --- a/src/org/traccar/protocol/XexunProtocolDecoder.java +++ b/src/org/traccar/protocol/XexunProtocolDecoder.java @@ -17,6 +17,7 @@ package org.traccar.protocol; import org.jboss.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; +import org.traccar.DeviceSession; import org.traccar.helper.DateBuilder; import org.traccar.helper.Parser; import org.traccar.helper.PatternBuilder; @@ -100,10 +101,11 @@ public class XexunProtocolDecoder extends BaseProtocolDecoder { position.set("signal", parser.next()); position.set(Position.KEY_ALARM, parser.next()); - if (!identify(parser.next(), channel, remoteAddress)) { + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, parser.next()); + if (deviceSession == null) { return null; } - position.setDeviceId(getDeviceId()); + position.setDeviceId(deviceSession.getDeviceId()); if (full) { position.set(Position.KEY_SATELLITES, parser.next().replaceFirst("^0*(?![\\.$])", "")); diff --git a/src/org/traccar/protocol/XirgoProtocolDecoder.java b/src/org/traccar/protocol/XirgoProtocolDecoder.java index 415d8dee3..5006102e6 100644 --- a/src/org/traccar/protocol/XirgoProtocolDecoder.java +++ b/src/org/traccar/protocol/XirgoProtocolDecoder.java @@ -17,6 +17,7 @@ package org.traccar.protocol; import org.jboss.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; +import org.traccar.DeviceSession; import org.traccar.helper.DateBuilder; import org.traccar.helper.Parser; import org.traccar.helper.PatternBuilder; @@ -109,10 +110,11 @@ public class XirgoProtocolDecoder extends BaseProtocolDecoder { Position position = new Position(); position.setProtocol(getProtocolName()); - if (!identify(parser.next(), channel, remoteAddress)) { + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, parser.next()); + if (deviceSession == null) { return null; } - position.setDeviceId(getDeviceId()); + position.setDeviceId(deviceSession.getDeviceId()); position.set(Position.KEY_EVENT, parser.next()); diff --git a/src/org/traccar/protocol/Xt013ProtocolDecoder.java b/src/org/traccar/protocol/Xt013ProtocolDecoder.java index 1c0726d8c..b5c222ba6 100644 --- a/src/org/traccar/protocol/Xt013ProtocolDecoder.java +++ b/src/org/traccar/protocol/Xt013ProtocolDecoder.java @@ -17,6 +17,7 @@ package org.traccar.protocol; import org.jboss.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; +import org.traccar.DeviceSession; import org.traccar.helper.DateBuilder; import org.traccar.helper.Parser; import org.traccar.helper.PatternBuilder; @@ -68,10 +69,11 @@ public class Xt013ProtocolDecoder extends BaseProtocolDecoder { Position position = new Position(); position.setProtocol(getProtocolName()); - if (!identify(parser.next(), channel, remoteAddress)) { + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, parser.next()); + if (deviceSession == null) { return null; } - position.setDeviceId(getDeviceId()); + position.setDeviceId(deviceSession.getDeviceId()); DateBuilder dateBuilder = new DateBuilder() .setDate(parser.nextInt(), parser.nextInt(), parser.nextInt()) diff --git a/src/org/traccar/protocol/YwtProtocolDecoder.java b/src/org/traccar/protocol/YwtProtocolDecoder.java index b653c1ec4..da5a86175 100644 --- a/src/org/traccar/protocol/YwtProtocolDecoder.java +++ b/src/org/traccar/protocol/YwtProtocolDecoder.java @@ -17,6 +17,7 @@ package org.traccar.protocol; import org.jboss.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; +import org.traccar.DeviceSession; import org.traccar.helper.DateBuilder; import org.traccar.helper.Parser; import org.traccar.helper.PatternBuilder; @@ -81,10 +82,11 @@ public class YwtProtocolDecoder extends BaseProtocolDecoder { String type = parser.next(); - if (!identify(parser.next(), channel, remoteAddress)) { + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, parser.next()); + if (deviceSession == null) { return null; } - position.setDeviceId(getDeviceId()); + position.setDeviceId(deviceSession.getDeviceId()); DateBuilder dateBuilder = new DateBuilder() .setDate(parser.nextInt(), parser.nextInt(), parser.nextInt()) diff --git a/test/org/traccar/protocol/AutoFonProtocolDecoderTest.java b/test/org/traccar/protocol/AutoFonProtocolDecoderTest.java index f84873c6e..a229695a2 100644 --- a/test/org/traccar/protocol/AutoFonProtocolDecoderTest.java +++ b/test/org/traccar/protocol/AutoFonProtocolDecoderTest.java @@ -11,12 +11,12 @@ public class AutoFonProtocolDecoderTest extends ProtocolTest { AutoFonProtocolDecoder decoder = new AutoFonProtocolDecoder(new AutoFonProtocol()); - verifyPosition(decoder, binary( - "02080000251848470afa010262daa690013aa4046da83745f8812560df010001126a")); - verifyNothing(decoder, binary( "10556103592310314825728F")); + verifyPosition(decoder, binary( + "02080000251848470afa010262daa690013aa4046da83745f8812560df010001126a")); + verifyPosition(decoder, binary( "111E00000000000000000100007101010B0C020302010B0C0005A053FFFFFFFF02010B0C00276047FFFFFFFF1F5600FA000176F218C7850C0B0B0C203A033DBD46035783EF009E00320014FFFF45")); diff --git a/test/org/traccar/protocol/Gt06ProtocolDecoderTest.java b/test/org/traccar/protocol/Gt06ProtocolDecoderTest.java index 9201caac6..3343f72e2 100644 --- a/test/org/traccar/protocol/Gt06ProtocolDecoderTest.java +++ b/test/org/traccar/protocol/Gt06ProtocolDecoderTest.java @@ -13,12 +13,12 @@ public class Gt06ProtocolDecoderTest extends ProtocolTest { verifyNothing(decoder, binary( "787805120099abec0d0a")); - verifyAttributes(decoder, binary( - "797900089400000002e852d70d0a")); - verifyNothing(decoder, binary( "78780D01086471700328358100093F040D0A")); + verifyAttributes(decoder, binary( + "797900089400000002e852d70d0a")); + verifyAttributes(decoder, binary( "7979000794050000c9b63e0d0a")); diff --git a/test/org/traccar/protocol/NavisProtocolDecoderTest.java b/test/org/traccar/protocol/NavisProtocolDecoderTest.java index 116984b52..4b612e941 100644 --- a/test/org/traccar/protocol/NavisProtocolDecoderTest.java +++ b/test/org/traccar/protocol/NavisProtocolDecoderTest.java @@ -13,12 +13,12 @@ public class NavisProtocolDecoderTest extends ProtocolTest { NavisProtocolDecoder decoder = new NavisProtocolDecoder(new NavisProtocol()); - verifyPositions(decoder, binary(ByteOrder.LITTLE_ENDIAN, - "404e544300000000040000005a00c6812a3e410125e3a60700011705071503011030210c0000fa200910e6000000000000000000000001082106150010ae97643f88a39f3f0000000090001fcc6c450000000000000000000000000000000000000000000000f6808080")); - verifyNothing(decoder, binary(ByteOrder.LITTLE_ENDIAN, "404E5443010000007B000000130044342A3E533A383631373835303035323035303739")); + verifyPositions(decoder, binary(ByteOrder.LITTLE_ENDIAN, + "404e544300000000040000005a00c6812a3e410125e3a60700011705071503011030210c0000fa200910e6000000000000000000000001082106150010ae97643f88a39f3f0000000090001fcc6c450000000000000000000000000000000000000000000000f6808080")); + verifyPositions(decoder, binary(ByteOrder.LITTLE_ENDIAN, "404e544301000000000000005a002e6c2a3e410125d7540100001512233a0b0a0f08026300000a000b000b00020000000000000000000c12233b0b0a0f03fd6d3f0fde603f00000000ba0051e0c845000000000000000000000000000000000000000000000080808080")); -- cgit v1.2.3 From b6373da0a0ef4436f48d35256c3cd3373f22d8d7 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Tue, 19 Jul 2016 15:15:56 +1200 Subject: Remove unused imports --- src/org/traccar/protocol/BlackKiteProtocolDecoder.java | 1 - src/org/traccar/protocol/GalileoProtocolDecoder.java | 1 - 2 files changed, 2 deletions(-) diff --git a/src/org/traccar/protocol/BlackKiteProtocolDecoder.java b/src/org/traccar/protocol/BlackKiteProtocolDecoder.java index 1c9c0a003..92179072a 100644 --- a/src/org/traccar/protocol/BlackKiteProtocolDecoder.java +++ b/src/org/traccar/protocol/BlackKiteProtocolDecoder.java @@ -22,7 +22,6 @@ import org.jboss.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; import org.traccar.DeviceSession; import org.traccar.helper.BitUtil; -import org.traccar.helper.Log; import org.traccar.model.Position; import java.net.SocketAddress; diff --git a/src/org/traccar/protocol/GalileoProtocolDecoder.java b/src/org/traccar/protocol/GalileoProtocolDecoder.java index eb863b442..12b35333d 100644 --- a/src/org/traccar/protocol/GalileoProtocolDecoder.java +++ b/src/org/traccar/protocol/GalileoProtocolDecoder.java @@ -20,7 +20,6 @@ import org.jboss.netty.buffer.ChannelBuffers; import org.jboss.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; import org.traccar.DeviceSession; -import org.traccar.helper.Log; import org.traccar.model.Position; import java.net.SocketAddress; -- cgit v1.2.3 From 1a0eae22fc58d7241e2c60bb593535a17d6c629b Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Tue, 19 Jul 2016 18:45:34 +1200 Subject: Correctly add device to active --- src/org/traccar/BaseProtocolDecoder.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/org/traccar/BaseProtocolDecoder.java b/src/org/traccar/BaseProtocolDecoder.java index f98617caf..46832a4ca 100644 --- a/src/org/traccar/BaseProtocolDecoder.java +++ b/src/org/traccar/BaseProtocolDecoder.java @@ -77,6 +77,9 @@ public abstract class BaseProtocolDecoder extends ExtendedObjectDecoder { } else if (deviceId != 0) { deviceSession = new DeviceSession(deviceId); addressDeviceSessions.put(remoteAddress, deviceSession); + if (Context.getConnectionManager() != null) { + Context.getConnectionManager().addActiveDevice(deviceId, protocol, channel, remoteAddress); + } return deviceSession; } else { return null; @@ -86,6 +89,9 @@ public abstract class BaseProtocolDecoder extends ExtendedObjectDecoder { long deviceId = findDeviceId(remoteAddress, uniqueIds); if (deviceId != 0) { channelDeviceSession = new DeviceSession(deviceId); + if (Context.getConnectionManager() != null) { + Context.getConnectionManager().addActiveDevice(deviceId, protocol, channel, remoteAddress); + } } } return channelDeviceSession; -- cgit v1.2.3