diff options
author | Anton Tananaev <anton.tananaev@gmail.com> | 2016-06-01 15:28:54 +1200 |
---|---|---|
committer | Anton Tananaev <anton.tananaev@gmail.com> | 2016-06-01 15:28:54 +1200 |
commit | 3db183a5535a986b015566c6df929da8c6ca9630 (patch) | |
tree | 3183f44e2b9b8ccfa2c460221b1d04fa74f0e9e9 /src/org/traccar/database | |
parent | cce427a783cbe0c2eca6a278d7327294002847e5 (diff) | |
parent | 41c46575a92f746622c61b07004b7a0832712a54 (diff) | |
download | trackermap-server-3db183a5535a986b015566c6df929da8c6ca9630.tar.gz trackermap-server-3db183a5535a986b015566c6df929da8c6ca9630.tar.bz2 trackermap-server-3db183a5535a986b015566c6df929da8c6ca9630.zip |
Merge pull request #1981 from Abyss777/master
Implement events subsystem
Diffstat (limited to 'src/org/traccar/database')
-rw-r--r-- | src/org/traccar/database/ConnectionManager.java | 42 | ||||
-rw-r--r-- | src/org/traccar/database/DataManager.java | 36 |
2 files changed, 72 insertions, 6 deletions
diff --git a/src/org/traccar/database/ConnectionManager.java b/src/org/traccar/database/ConnectionManager.java index bb9958232..9e0da8485 100644 --- a/src/org/traccar/database/ConnectionManager.java +++ b/src/org/traccar/database/ConnectionManager.java @@ -23,6 +23,7 @@ import org.traccar.GlobalTimer; import org.traccar.Protocol; import org.traccar.helper.Log; import org.traccar.model.Device; +import org.traccar.model.Event; import org.traccar.model.Position; import java.net.SocketAddress; @@ -85,14 +86,26 @@ public class ConnectionManager { return; } - device.setStatus(status); - if (time != null) { - device.setLastUpdate(time); + if (status.equals(Device.STATUS_MOVING) || status.equals(Device.STATUS_STOPPED)) { + device.setMotion(status); + } else { + if (!status.equals(device.getStatus())) { + Event event = new Event(Event.TYPE_DEVICE_OFFLINE, deviceId); + if (status.equals(Device.STATUS_ONLINE)) { + event.setType(Event.TYPE_DEVICE_ONLINE); + } + updateEvent(event); + } + device.setStatus(status); + + Timeout timeout = timeouts.remove(deviceId); + if (timeout != null) { + timeout.cancel(); + } } - Timeout timeout = timeouts.remove(deviceId); - if (timeout != null) { - timeout.cancel(); + if (time != null) { + device.setLastUpdate(time); } if (status.equals(Device.STATUS_ONLINE)) { @@ -134,6 +147,22 @@ public class ConnectionManager { } } + public synchronized void updateEvent(Event event) { + long deviceId = event.getDeviceId(); + try { + Context.getDataManager().addEvent(event); + } catch (SQLException error) { + Log.warning(error); + } + for (long userId : Context.getPermissionsManager().getDeviceUsers(deviceId)) { + if (listeners.containsKey(userId)) { + for (UpdateListener listener : listeners.get(userId)) { + listener.onUpdateEvent(event); + } + } + } + } + public Position getLastPosition(long deviceId) { return positions.get(deviceId); } @@ -154,6 +183,7 @@ public class ConnectionManager { public interface UpdateListener { void onUpdateDevice(Device device); void onUpdatePosition(Position position); + void onUpdateEvent(Event event); } public synchronized void addListener(long userId, UpdateListener listener) { diff --git a/src/org/traccar/database/DataManager.java b/src/org/traccar/database/DataManager.java index ac3be45ac..dffb8937c 100644 --- a/src/org/traccar/database/DataManager.java +++ b/src/org/traccar/database/DataManager.java @@ -27,6 +27,7 @@ import org.traccar.Config; import org.traccar.Context; import org.traccar.helper.Log; import org.traccar.model.Device; +import org.traccar.model.Event; import org.traccar.model.DevicePermission; import org.traccar.model.Group; import org.traccar.model.GroupPermission; @@ -42,6 +43,7 @@ import java.net.URL; import java.net.URLClassLoader; import java.sql.SQLException; import java.util.ArrayList; +import java.util.Calendar; import java.util.Collection; import java.util.Date; import java.util.HashMap; @@ -357,6 +359,9 @@ public class DataManager implements IdentityManager { QueryBuilder.create(dataSource, getQuery("database.updateDeviceStatus")) .setObject(device) .executeUpdate(); + Device cachedDevice = getDeviceById(device.getId()); + cachedDevice.setStatus(device.getStatus()); + cachedDevice.setMotion(device.getMotion()); } public void removeDevice(long deviceId) throws SQLException { @@ -465,6 +470,8 @@ public class DataManager implements IdentityManager { .setDate("now", new Date()) .setObject(position) .executeUpdate(); + Device device = getDeviceById(position.getDeviceId()); + device.setPositionId(position.getId()); } public Collection<Position> getLatestPositions() throws SQLException { @@ -482,4 +489,33 @@ public class DataManager implements IdentityManager { .setObject(server) .executeUpdate(); } + + public Event getEvent(long eventId) throws SQLException { + return QueryBuilder.create(dataSource, getQuery("database.selectEvent")) + .setLong("id", eventId) + .executeQuerySingle(Event.class); + } + + public void addEvent(Event event) throws SQLException { + event.setId(QueryBuilder.create(dataSource, getQuery("database.insertEvent"), true) + .setObject(event) + .executeUpdate()); + } + + public Collection<Event> getEvents(long deviceId, String type, Date from, Date to) throws SQLException { + return QueryBuilder.create(dataSource, getQuery("database.selectEvents")) + .setLong("deviceId", deviceId) + .setString("type", type) + .setDate("from", from) + .setDate("to", to) + .executeQuery(Event.class); + } + + public Collection<Event> getLastEvents(long deviceId, String type, int interval) throws SQLException { + Calendar calendar = Calendar.getInstance(); + calendar.add(Calendar.SECOND, -interval); + Date to = calendar.getTime(); + return getEvents(deviceId, type, new Date(), to); + } + } |