From 31153730de44a31b87c8131d95de1127d2758508 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Wed, 17 Jun 2015 11:14:32 +1200 Subject: Load latest positions from db --- debug.xml | 4 +++ src/org/traccar/database/DataCache.java | 44 +++++++++++++++++++------------ src/org/traccar/database/DataManager.java | 32 ++++++++++++---------- 3 files changed, 49 insertions(+), 31 deletions(-) diff --git a/debug.xml b/debug.xml index 1074d9eba..698f7bdc9 100644 --- a/debug.xml +++ b/debug.xml @@ -194,6 +194,10 @@ VALUES (:deviceId, NOW(), :time, :time, :valid, :latitude, :longitude, :altitude, :speed, :course, :address, :other); + + SELECT * FROM position WHERE id IN (SELECT positionId FROM device); + + UPDATE device SET positionId = :id WHERE id = :deviceId; diff --git a/src/org/traccar/database/DataCache.java b/src/org/traccar/database/DataCache.java index de3214bd0..390187200 100644 --- a/src/org/traccar/database/DataCache.java +++ b/src/org/traccar/database/DataCache.java @@ -15,6 +15,7 @@ */ package org.traccar.database; +import java.sql.SQLException; import java.util.Collection; import java.util.HashMap; import java.util.HashSet; @@ -22,6 +23,8 @@ import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Set; + +import org.traccar.helper.Log; import org.traccar.model.Event; import org.traccar.model.Position; @@ -31,14 +34,21 @@ public class DataCache { private final Map> listeners = new HashMap>(); public DataCache(DataManager dataManager) { - // TODO: load latest data from datavase + try { + Collection positions = dataManager.getLatestPositions(); + for (Position position : positions) { + this.positions.put(position.getDeviceId(), position); + } + } catch (SQLException error) { + Log.warning(error); + } } public synchronized void update(Position position) { - long device = position.getDeviceId(); - positions.put(device, position); - if (listeners.containsKey(device)) { - for (DataCacheListener listener : listeners.get(device)) { + long deviceId = position.getDeviceId(); + positions.put(deviceId, position); + if (listeners.containsKey(deviceId)) { + for (DataCacheListener listener : listeners.get(deviceId)) { listener.onUpdate(position); } } @@ -62,29 +72,29 @@ public class DataCache { } public void addListener(Collection devices, DataCacheListener listener) { - for (long device : devices) { - addListener(device, listener); + for (long deviceId : devices) { + addListener(deviceId, listener); } } - public synchronized void addListener(long device, DataCacheListener listener) { - if (!listeners.containsKey(device)) { - listeners.put(device, new HashSet()); + public synchronized void addListener(long deviceId, DataCacheListener listener) { + if (!listeners.containsKey(deviceId)) { + listeners.put(deviceId, new HashSet()); } - listeners.get(device).add(listener); + listeners.get(deviceId).add(listener); } public void removeListener(Collection devices, DataCacheListener listener) { - for (long device : devices) { - removeListener(device, listener); + for (long deviceId : devices) { + removeListener(deviceId, listener); } } - public synchronized void removeListener(long device, DataCacheListener listener) { - if (!listeners.containsKey(device)) { - listeners.put(device, new HashSet()); + public synchronized void removeListener(long deviceId, DataCacheListener listener) { + if (!listeners.containsKey(deviceId)) { + listeners.put(deviceId, new HashSet()); } - listeners.get(device).remove(listener); + listeners.get(deviceId).remove(listener); } } diff --git a/src/org/traccar/database/DataManager.java b/src/org/traccar/database/DataManager.java index d8efaaaca..807a75f34 100644 --- a/src/org/traccar/database/DataManager.java +++ b/src/org/traccar/database/DataManager.java @@ -133,17 +133,6 @@ public class DataManager { return devices.get(uniqueId); } - // TODO: possibly remove this method - public void updateLatestPosition(Position position) throws SQLException { - QueryBuilder.create(dataSource, properties.getProperty("database.updateLatestPosition")) - .setObject(position) - .setDate("time", position.getFixTime()) // tmp - .setLong("device_id", position.getDeviceId()) // tmp - .setLong("power", 0) // tmp - .setString("extended_info", position.getOther()) // tmp - .executeUpdate(); - } - private void createDatabaseSchema() throws SQLException { Connection connection = dataSource.getConnection(); @@ -248,7 +237,6 @@ public class DataManager { .setObject(user) .executeUpdate(); } - Context.getPermissionsManager().refresh(); } @@ -318,12 +306,28 @@ public class DataManager { .setString("extended_info", position.getOther()) // tmp .executeUpdate()); } - + + // TODO: possibly remove this method + public void updateLatestPosition(Position position) throws SQLException { + QueryBuilder.create(dataSource, properties.getProperty("database.updateLatestPosition")) + .setObject(position) + .setDate("time", position.getFixTime()) // tmp + .setLong("device_id", position.getDeviceId()) // tmp + .setLong("power", 0) // tmp + .setString("extended_info", position.getOther()) // tmp + .executeUpdate(); + } + + public Collection getLatestPositions() throws SQLException { + return QueryBuilder.create(dataSource, properties.getProperty("database.selectLatestPositions")) + .executeQuery(new Position()); + } + public Server getServer() throws SQLException { return QueryBuilder.create(dataSource, properties.getProperty("database.selectServers")) .executeQuerySingle(new Server()); } - + public void updateServer(Server server) throws SQLException { QueryBuilder.create(dataSource, properties.getProperty("database.updateServer")) .setObject(server) -- cgit v1.2.3