diff options
author | Anton Tananaev <anton.tananaev@gmail.com> | 2012-05-06 08:56:39 +0000 |
---|---|---|
committer | Anton Tananaev <anton.tananaev@gmail.com> | 2012-05-06 08:56:39 +0000 |
commit | 131152712d834b39f44ae94ab5d2db04520d3125 (patch) | |
tree | 93a224f4cafba45d668153499a5d607382f46d9b /src/org/traccar/model | |
parent | 0218464141fc64f060699c6e98ed5d4f0814e139 (diff) | |
download | traccar-server-131152712d834b39f44ae94ab5d2db04520d3125.tar.gz traccar-server-131152712d834b39f44ae94ab5d2db04520d3125.tar.bz2 traccar-server-131152712d834b39f44ae94ab5d2db04520d3125.zip |
Diffstat (limited to 'src/org/traccar/model')
-rw-r--r-- | src/org/traccar/model/DataManager.java | 4 | ||||
-rw-r--r-- | src/org/traccar/model/DatabaseDataManager.java | 101 |
2 files changed, 81 insertions, 24 deletions
diff --git a/src/org/traccar/model/DataManager.java b/src/org/traccar/model/DataManager.java index d0f290962..70cf63380 100644 --- a/src/org/traccar/model/DataManager.java +++ b/src/org/traccar/model/DataManager.java @@ -27,8 +27,8 @@ public interface DataManager { */ public List<Device> getDevices() throws Exception; public void addDevice(Device device) throws Exception; - public void addUpdate(Device device) throws Exception; - public void addRemove(Device device) throws Exception; + public void updateDevice(Device device) throws Exception; + public void removeDevice(Device device) throws Exception; public Device getDeviceByImei(String imei) throws Exception; /** diff --git a/src/org/traccar/model/DatabaseDataManager.java b/src/org/traccar/model/DatabaseDataManager.java index 7d90ac194..c5492bc8a 100644 --- a/src/org/traccar/model/DatabaseDataManager.java +++ b/src/org/traccar/model/DatabaseDataManager.java @@ -35,16 +35,13 @@ public class DatabaseDataManager implements DataManager { * Database statements */ private NamedParameterStatement queryGetDevices; + private NamedParameterStatement queryAddDevice; + private NamedParameterStatement queryUpdateDevice; + private NamedParameterStatement queryRemoveDevice; + private NamedParameterStatement queryGetPositions; private NamedParameterStatement queryAddPosition; /** - * Devices cache - */ - private Map devices; - private Calendar devicesLastUpdate; - private Long devicesRefreshDelay; - - /** * Initialize database */ private void initDatabase(Properties properties) @@ -70,9 +67,24 @@ public class DatabaseDataManager implements DataManager { String password = properties.getProperty("database.password"); AdvancedConnection connection = new AdvancedConnection(url, user, password); - String query = properties.getProperty("database.selectDevice"); + // Load statements from configuration + String query; + + query = properties.getProperty("database.selectDevice"); queryGetDevices = new NamedParameterStatement(connection, query); + query = properties.getProperty("database.insertDevice"); + queryAddDevice = new NamedParameterStatement(connection, query); + + query = properties.getProperty("database.updateDevice"); + queryUpdateDevice = new NamedParameterStatement(connection, query); + + query = properties.getProperty("database.deleteDevice"); + queryRemoveDevice = new NamedParameterStatement(connection, query); + + query = properties.getProperty("database.selectPosition"); + queryGetPositions = new NamedParameterStatement(connection, query); + query = properties.getProperty("database.insertPosition"); queryAddPosition = new NamedParameterStatement(connection, query); } @@ -93,9 +105,46 @@ public class DatabaseDataManager implements DataManager { return deviceList; } - public void addDevice(Device device) {} // TODO: implement - public void addUpdate(Device device) {} // TODO: implement - public void addRemove(Device device) {} // TODO: implement + public synchronized void addDevice(Device device) throws SQLException { + + queryAddDevice.prepare(); + queryAddDevice.setString("imei", device.getImei()); + queryAddDevice.executeUpdate(); + + // Find generated id + ResultSet result = queryAddDevice.getGeneratedKeys(); + if (result.next()) { + device.setId(result.getLong(1)); + } + + devices = null; + } + + public synchronized void updateDevice(Device device) throws SQLException { + + queryUpdateDevice.prepare(); + queryUpdateDevice.setLong("id", device.getId()); + queryUpdateDevice.setString("imei", device.getImei()); + queryUpdateDevice.executeUpdate(); + + devices = null; + } + + public synchronized void removeDevice(Device device) throws SQLException { + + queryRemoveDevice.prepare(); + queryRemoveDevice.setLong("id", device.getId()); + queryRemoveDevice.executeUpdate(); + + devices = null; + } + + /** + * Devices cache + */ + private Map devices; + private Calendar devicesLastUpdate; + private Long devicesRefreshDelay; public Device getDeviceByImei(String imei) throws SQLException { @@ -110,20 +159,26 @@ public class DatabaseDataManager implements DataManager { return (Device) devices.get(imei); } - public List<Position> getPositions(Long deviceId) { // TODO: implement + public synchronized List<Position> getPositions(Long deviceId) throws SQLException { List<Position> positionList = new LinkedList(); - Position p = new Position(); - p.setDeviceId(new Long(1)); - p.setTime(new Date()); - p.setValid(true); - p.setLatitude(1.0); - p.setLongitude(1.0); - p.setSpeed(1.0); - p.setCourse(1.0); - - positionList.add(p); + queryGetPositions.prepare(); + queryGetPositions.setLong("device_id", deviceId); + ResultSet result = queryGetPositions.executeQuery(); + while (result.next()) { + // TODO: include other parameters + Position position = new Position(); + position.setDeviceId(result.getLong("device_id")); + position.setTime(result.getTimestamp("time")); + position.setValid(result.getBoolean("valid")); + position.setLatitude(result.getDouble("latitude")); + position.setLongitude(result.getDouble("longitude")); + position.setSpeed(result.getDouble("speed")); + position.setCourse(result.getDouble("course")); + position.setPower(result.getDouble("power")); + positionList.add(position); + } return positionList; } @@ -144,6 +199,8 @@ public class DatabaseDataManager implements DataManager { queryAddPosition.setString("extended_info", position.getExtendedInfo()); queryAddPosition.executeUpdate(); + + // TODO: probably return row id } } |