diff options
Diffstat (limited to 'src/org/traccar/BaseProtocolDecoder.java')
-rw-r--r-- | src/org/traccar/BaseProtocolDecoder.java | 36 |
1 files changed, 35 insertions, 1 deletions
diff --git a/src/org/traccar/BaseProtocolDecoder.java b/src/org/traccar/BaseProtocolDecoder.java index 46832a4ca..d8130c79e 100644 --- a/src/org/traccar/BaseProtocolDecoder.java +++ b/src/org/traccar/BaseProtocolDecoder.java @@ -1,5 +1,5 @@ /* - * Copyright 2012 - 2016 Anton Tananaev (anton.tananaev@gmail.com) + * Copyright 2012 - 2016 Anton Tananaev (anton@traccar.org) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -26,11 +26,27 @@ import java.net.SocketAddress; import java.util.Date; import java.util.HashMap; import java.util.Map; +import java.sql.SQLException; public abstract class BaseProtocolDecoder extends ExtendedObjectDecoder { private final Protocol protocol; + public long addUnknownDevice(String uniqueId) { + Device device = new Device(); + device.setName(uniqueId); + device.setUniqueId(uniqueId); + + try { + Context.getDeviceManager().addDevice(device); + Log.info("Automatically registered device " + uniqueId); + return device.getId(); + } catch (SQLException e) { + Log.warning(e); + return 0; + } + } + public String getProtocolName() { return protocol.getName(); } @@ -55,6 +71,10 @@ public abstract class BaseProtocolDecoder extends ExtendedObjectDecoder { Log.warning(e); } if (deviceId == 0) { + if (Context.getConfig().getBoolean("database.registerUnknown")) { + return addUnknownDevice(uniqueIds[0]); + } + StringBuilder message = new StringBuilder("Unknown device -"); for (String uniqueId : uniqueIds) { message.append(" ").append(uniqueId); @@ -69,6 +89,17 @@ public abstract class BaseProtocolDecoder extends ExtendedObjectDecoder { } public DeviceSession getDeviceSession(Channel channel, SocketAddress remoteAddress, String... uniqueIds) { + if (Context.getConfig().getBoolean("decoder.ignoreSessionCache")) { + long deviceId = findDeviceId(remoteAddress, uniqueIds); + if (deviceId != 0) { + if (Context.getConnectionManager() != null) { + Context.getConnectionManager().addActiveDevice(deviceId, protocol, channel, remoteAddress); + } + return new DeviceSession(deviceId); + } else { + return null; + } + } if (channel instanceof DatagramChannel) { long deviceId = findDeviceId(remoteAddress, uniqueIds); DeviceSession deviceSession = addressDeviceSessions.get(remoteAddress); @@ -129,6 +160,9 @@ public abstract class BaseProtocolDecoder extends ExtendedObjectDecoder { @Override protected void onMessageEvent(Channel channel, SocketAddress remoteAddress, Object msg) { + if (Context.getStatisticsManager() != null) { + Context.getStatisticsManager().registerMessageReceived(); + } DeviceSession deviceSession = getDeviceSession(channel, remoteAddress); if (deviceSession != null) { Context.getConnectionManager().updateDevice(deviceSession.getDeviceId(), Device.STATUS_ONLINE, new Date()); |