From 9ec7166889b9b0492a18a338e6dc5352743209b8 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Mon, 9 Nov 2015 10:47:43 +1300 Subject: Add initial code for device status --- src/org/traccar/BaseProtocolDecoder.java | 9 ++++++++- src/org/traccar/ExtendedObjectDecoder.java | 4 ++++ src/org/traccar/MainEventHandler.java | 2 +- src/org/traccar/database/ConnectionManager.java | 16 ++++++++++++---- src/org/traccar/model/Device.java | 4 ++++ src/org/traccar/web/AsyncServlet.java | 2 +- 6 files changed, 30 insertions(+), 7 deletions(-) diff --git a/src/org/traccar/BaseProtocolDecoder.java b/src/org/traccar/BaseProtocolDecoder.java index e9c678930..f8abdcc85 100644 --- a/src/org/traccar/BaseProtocolDecoder.java +++ b/src/org/traccar/BaseProtocolDecoder.java @@ -46,7 +46,7 @@ public abstract class BaseProtocolDecoder extends ExtendedObjectDecoder { Device device = Context.getIdentityManager().getDeviceByUniqueId(uniqueId); if (device != null) { deviceId = device.getId(); - Context.getConnectionManager().setActiveDevice(deviceId, protocol, channel, remoteAddress); + Context.getConnectionManager().addActiveDevice(deviceId, protocol, channel, remoteAddress); return true; } else { deviceId = 0; @@ -95,4 +95,11 @@ 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()); + } + } + } diff --git a/src/org/traccar/ExtendedObjectDecoder.java b/src/org/traccar/ExtendedObjectDecoder.java index 382ef869d..ca4561a3f 100644 --- a/src/org/traccar/ExtendedObjectDecoder.java +++ b/src/org/traccar/ExtendedObjectDecoder.java @@ -37,6 +37,7 @@ public abstract class ExtendedObjectDecoder implements ChannelUpstreamHandler { MessageEvent e = (MessageEvent) evt; Object originalMessage = e.getMessage(); Object decodedMessage = decode(e.getChannel(), e.getRemoteAddress(), originalMessage); + onMessageEvent(e.getChannel(), e.getRemoteAddress(), originalMessage); // call after decode if (originalMessage == decodedMessage) { ctx.sendUpstream(evt); } else if (decodedMessage != null) { @@ -50,6 +51,9 @@ public abstract class ExtendedObjectDecoder implements ChannelUpstreamHandler { } } + protected void onMessageEvent(Channel channel, SocketAddress remoteAddress, Object msg) { + } + protected abstract Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception; } diff --git a/src/org/traccar/MainEventHandler.java b/src/org/traccar/MainEventHandler.java index 81376724b..48bfb3fc9 100644 --- a/src/org/traccar/MainEventHandler.java +++ b/src/org/traccar/MainEventHandler.java @@ -46,7 +46,7 @@ public class MainEventHandler extends IdleStateAwareChannelHandler { s.append("course: ").append(position.getCourse()); Log.info(s.toString()); - Context.getConnectionManager().update(position); + Context.getConnectionManager().updatePosition(position); } } diff --git a/src/org/traccar/database/ConnectionManager.java b/src/org/traccar/database/ConnectionManager.java index e45c83651..8ef171673 100644 --- a/src/org/traccar/database/ConnectionManager.java +++ b/src/org/traccar/database/ConnectionManager.java @@ -18,6 +18,7 @@ package org.traccar.database; import java.net.SocketAddress; import java.sql.SQLException; import java.util.Collection; +import java.util.Date; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedList; @@ -27,6 +28,7 @@ import java.util.Set; import org.jboss.netty.channel.Channel; import org.traccar.Protocol; import org.traccar.helper.Log; +import org.traccar.model.Device; import org.traccar.model.Position; public class ConnectionManager { @@ -47,13 +49,14 @@ public class ConnectionManager { } } - public void setActiveDevice(long deviceId, Protocol protocol, Channel channel, SocketAddress remoteAddress) { + public void addActiveDevice(long deviceId, Protocol protocol, Channel channel, SocketAddress remoteAddress) { activeDevices.put(deviceId, new ActiveDevice(deviceId, protocol, channel, remoteAddress)); } public void removeActiveDevice(Channel channel) { for (ActiveDevice activeDevice : activeDevices.values()) { if (activeDevice.getChannel() == channel) { + updateDevice(activeDevice.getDeviceId(), Device.STATUS_OFFLINE, new Date()); activeDevices.remove(activeDevice.getDeviceId()); break; } @@ -64,12 +67,17 @@ public class ConnectionManager { return activeDevices.get(deviceId); } - public synchronized void update(Position position) { + public synchronized void updateDevice(long deviceId, String status, Date time) { + // TODO update cache and call listener + Log.debug(deviceId + " " + status + " " + time); + } + + public synchronized void updatePosition(Position position) { long deviceId = position.getDeviceId(); positions.put(deviceId, position); if (listeners.containsKey(deviceId)) { for (DataCacheListener listener : listeners.get(deviceId)) { - listener.onUpdate(position); + listener.onUpdatePosition(position); } } } @@ -92,7 +100,7 @@ public class ConnectionManager { } public interface DataCacheListener { - void onUpdate(Position position); + void onUpdatePosition(Position position); } public void addListener(Collection devices, DataCacheListener listener) { diff --git a/src/org/traccar/model/Device.java b/src/org/traccar/model/Device.java index 698505983..fd62cc691 100644 --- a/src/org/traccar/model/Device.java +++ b/src/org/traccar/model/Device.java @@ -54,6 +54,10 @@ public class Device implements Factory { this.uniqueId = uniqueId; } + public static final String STATUS_UNKNOWN = "unknown"; + public static final String STATUS_ONLINE = "online"; + public static final String STATUS_OFFLINE = "offline"; + private String status; public String getStatus() { diff --git a/src/org/traccar/web/AsyncServlet.java b/src/org/traccar/web/AsyncServlet.java index a96a64381..63b08ff3e 100644 --- a/src/org/traccar/web/AsyncServlet.java +++ b/src/org/traccar/web/AsyncServlet.java @@ -88,7 +88,7 @@ public class AsyncServlet extends BaseServlet { private final ConnectionManager.DataCacheListener dataListener = new ConnectionManager.DataCacheListener() { @Override - public void onUpdate(Position position) { + public void onUpdatePosition(Position position) { synchronized (AsyncSession.this) { logEvent("onUpdate deviceId: " + position.getDeviceId()); if (!destroyed) { -- cgit v1.2.3