diff options
Diffstat (limited to 'src/org/traccar')
-rw-r--r-- | src/org/traccar/database/DataManager.java | 75 | ||||
-rw-r--r-- | src/org/traccar/model/Device.java | 51 |
2 files changed, 85 insertions, 41 deletions
diff --git a/src/org/traccar/database/DataManager.java b/src/org/traccar/database/DataManager.java index c7c4d2b7f..2f333b38d 100644 --- a/src/org/traccar/database/DataManager.java +++ b/src/org/traccar/database/DataManager.java @@ -118,7 +118,7 @@ public class DataManager { public Device processNextRow(ResultSet rs) throws SQLException { Device device = new Device(); device.setId(rs.getLong("id")); - device.setImei(rs.getString("imei")); + device.setUniqueId(rs.getString("imei")); return device; } }; @@ -146,7 +146,7 @@ public class DataManager { devices = new HashMap<String, Device>(); for (Device device : getDevices()) { - devices.put(device.getImei(), device); + devices.put(device.getUniqueId(), device); } devicesLastUpdate = Calendar.getInstance(); } @@ -285,7 +285,7 @@ public class DataManager { "course DOUBLE NOT NULL," + "address VARCHAR(1024)," + "other VARCHAR(8192) NOT NULL," + - "FOREIGN KEY (device_id) REFERENCES device(id));" + + "FOREIGN KEY (device_id) REFERENCES device(id) ON DELETE CASCADE);" + "CREATE TABLE data (" + "id INT PRIMARY KEY AUTO_INCREMENT," + @@ -297,17 +297,17 @@ public class DataManager { "ALTER TABLE device ADD " + "FOREIGN KEY (position_id) REFERENCES position(id);" + - + "ALTER TABLE device ADD " + "FOREIGN KEY (data_id) REFERENCES data(id);" + - + "CREATE TABLE server (" + "id INT PRIMARY KEY AUTO_INCREMENT," + "registration BOOLEAN NOT NULL," + "latitude DOUBLE NOT NULL," + "longitude DOUBLE NOT NULL," + "zoom INT NOT NULL);" + - + "CREATE TABLE traccar1 (" + "id INT PRIMARY KEY AUTO_INCREMENT);"); @@ -408,5 +408,68 @@ public class DataManager { connection.close(); } } + + public void addDevice(Device device) throws SQLException { + + Connection connection = dataSource.getConnection(); + try { + PreparedStatement statement = connection.prepareStatement( + "INSERT INTO device (name, unique_id) VALUES (?, ?);", + Statement.RETURN_GENERATED_KEYS); + try { + statement.setString(1, device.getName()); + statement.setString(2, device.getUniqueId()); + + statement.execute(); + + ResultSet result = statement.getGeneratedKeys(); + if (result.next()) { + device.setId(result.getLong(1)); + } + } finally { + statement.close(); + } + } finally { + connection.close(); + } + } + + public void updateDevice(Device device) throws SQLException { + + Connection connection = dataSource.getConnection(); + try { + PreparedStatement statement = connection.prepareStatement( + "UPDATE device SET name = ?, unique_id = ? WHERE id = ?;"); + try { + statement.setString(1, device.getName()); + statement.setString(2, device.getUniqueId()); + statement.setLong(3, device.getId()); + + statement.execute(); + } finally { + statement.close(); + } + } finally { + connection.close(); + } + } + + public void removeDevice(Device device) throws SQLException { + + Connection connection = dataSource.getConnection(); + try { + PreparedStatement statement = connection.prepareStatement( + "DELETE FROM device WHERE id = ?;"); + try { + statement.setLong(1, device.getId()); + + statement.execute(); + } finally { + statement.close(); + } + } finally { + connection.close(); + } + } } diff --git a/src/org/traccar/model/Device.java b/src/org/traccar/model/Device.java index 1276216fc..cd1985e37 100644 --- a/src/org/traccar/model/Device.java +++ b/src/org/traccar/model/Device.java @@ -1,5 +1,5 @@ /* - * Copyright 2012 Anton Tananaev (anton.tananaev@gmail.com) + * Copyright 2012 - 2015 Anton Tananaev (anton.tananaev@gmail.com) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -15,53 +15,30 @@ */ package org.traccar.model; -/** - * Device - */ +import java.util.Date; + public class Device { - /** - * Id - */ - private Long id; + private long id; - public Long getId() { + public long getId() { return id; } - public void setId(Long id) { + public void setId(long id) { this.id = id; } + + private String name; - /** - * International mobile equipment identity (IMEI) - */ - private String imei; - - public String getImei() { - return imei; - } - - public void setImei(String imei) { - this.imei = imei; - } - - /** - * Phone number - */ - private String phoneNumber; - - public String getPhoneNumber() { - return phoneNumber; + public String getName() { + return uniqueId; } - public void setPhoneNumber(String phoneNumber) { - this.phoneNumber = phoneNumber; + public void setName(String name) { + this.name = name; } - /** - * Unique id (for some trackers) - */ private String uniqueId; public String getUniqueId() { @@ -71,5 +48,9 @@ public class Device { public void setUniqueId(String uniqueId) { this.uniqueId = uniqueId; } + + private String status; + + private Date lastUpdate; } |