diff options
author | Anton Tananaev <anton.tananaev@gmail.com> | 2015-11-09 10:47:43 +1300 |
---|---|---|
committer | Anton Tananaev <anton.tananaev@gmail.com> | 2015-11-09 10:47:43 +1300 |
commit | 9ec7166889b9b0492a18a338e6dc5352743209b8 (patch) | |
tree | 455782947763d7d6e4828276b8b7843222356ef3 /src/org | |
parent | f727af19cb994517ca3685239e506ff6beeaaa76 (diff) | |
download | traccar-server-9ec7166889b9b0492a18a338e6dc5352743209b8.tar.gz traccar-server-9ec7166889b9b0492a18a338e6dc5352743209b8.tar.bz2 traccar-server-9ec7166889b9b0492a18a338e6dc5352743209b8.zip |
Add initial code for device status
Diffstat (limited to 'src/org')
-rw-r--r-- | src/org/traccar/BaseProtocolDecoder.java | 9 | ||||
-rw-r--r-- | src/org/traccar/ExtendedObjectDecoder.java | 4 | ||||
-rw-r--r-- | src/org/traccar/MainEventHandler.java | 2 | ||||
-rw-r--r-- | src/org/traccar/database/ConnectionManager.java | 16 | ||||
-rw-r--r-- | src/org/traccar/model/Device.java | 4 | ||||
-rw-r--r-- | 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<Long> 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) { |