Skip to content

Commit

Permalink
Add initial code for device status
Browse files Browse the repository at this point in the history
  • Loading branch information
tananaev committed Nov 8, 2015
1 parent f727af1 commit 9ec7166
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 7 deletions.
9 changes: 8 additions & 1 deletion src/org/traccar/BaseProtocolDecoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public boolean identify(String uniqueId, Channel channel, SocketAddress remoteAd
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;
Expand Down Expand Up @@ -95,4 +95,11 @@ public void getLastLocation(Position position, Date deviceTime) {
}
}

@Override
protected void onMessageEvent(Channel channel, SocketAddress remoteAddress, Object msg) {
if (hasDeviceId()) {
Context.getConnectionManager().updateDevice(deviceId, Device.STATUS_ONLINE, new Date());
}
}

}
4 changes: 4 additions & 0 deletions src/org/traccar/ExtendedObjectDecoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public void handleUpstream(
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) {
Expand All @@ -50,6 +51,9 @@ public void handleUpstream(
}
}

protected void onMessageEvent(Channel channel, SocketAddress remoteAddress, Object msg) {
}

protected abstract Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception;

}
2 changes: 1 addition & 1 deletion src/org/traccar/MainEventHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) {
s.append("course: ").append(position.getCourse());
Log.info(s.toString());

Context.getConnectionManager().update(position);
Context.getConnectionManager().updatePosition(position);
}
}

Expand Down
16 changes: 12 additions & 4 deletions src/org/traccar/database/ConnectionManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
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;
Expand All @@ -27,6 +28,7 @@
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 {
Expand All @@ -47,13 +49,14 @@ public ConnectionManager(DataManager dataManager) {
}
}

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;
}
Expand All @@ -64,12 +67,17 @@ public ActiveDevice getActiveDevice(long deviceId) {
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);
}
}
}
Expand All @@ -92,7 +100,7 @@ public synchronized Collection<Position> getInitialState(Collection<Long> device
}

public interface DataCacheListener {
void onUpdate(Position position);
void onUpdatePosition(Position position);
}

public void addListener(Collection<Long> devices, DataCacheListener listener) {
Expand Down
4 changes: 4 additions & 0 deletions src/org/traccar/model/Device.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ public void setUniqueId(String uniqueId) {
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() {
Expand Down
2 changes: 1 addition & 1 deletion src/org/traccar/web/AsyncServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public boolean hasDevice(long deviceId) {

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) {
Expand Down

0 comments on commit 9ec7166

Please sign in to comment.