aboutsummaryrefslogtreecommitdiff
path: root/src/org/traccar
diff options
context:
space:
mode:
authorJed <Jed@192.168.1.100>2014-09-08 19:14:20 +0800
committerJed <Jed@192.168.1.100>2014-09-08 19:14:20 +0800
commit6a26740a4e74b2a627ad2de4b764a0c7dfdf6ca2 (patch)
treefa42ea40d2330d5ff0ba2acc2d1657655accf4b8 /src/org/traccar
parent9bad8efca5f64b60d5525b3858f42ac6cd6be272 (diff)
downloadtrackermap-server-6a26740a4e74b2a627ad2de4b764a0c7dfdf6ca2.tar.gz
trackermap-server-6a26740a4e74b2a627ad2de4b764a0c7dfdf6ca2.tar.bz2
trackermap-server-6a26740a4e74b2a627ad2de4b764a0c7dfdf6ca2.zip
Updated TrackerEventHandler to only trigger UpdateLatestPosition on the last record processed and pass the position object to the function
Added database property to Device class Updated DatabaseDataManager to allow the use of placeholder _database_ in queries
Diffstat (limited to 'src/org/traccar')
-rw-r--r--src/org/traccar/TrackerEventHandler.java24
-rw-r--r--src/org/traccar/model/DataManager.java3
-rw-r--r--src/org/traccar/model/DatabaseDataManager.java80
-rw-r--r--src/org/traccar/model/Device.java13
4 files changed, 105 insertions, 15 deletions
diff --git a/src/org/traccar/TrackerEventHandler.java b/src/org/traccar/TrackerEventHandler.java
index 6deeaad9b..f7858fea4 100644
--- a/src/org/traccar/TrackerEventHandler.java
+++ b/src/org/traccar/TrackerEventHandler.java
@@ -39,7 +39,7 @@ public class TrackerEventHandler extends IdleStateAwareChannelHandler {
dataManager = newDataManager;
}
- private void processSinglePosition(Position position) {
+ private Long processSinglePosition(Position position) {
if (position == null) {
Log.info("processSinglePosition null message");
} else {
@@ -52,24 +52,34 @@ public class TrackerEventHandler extends IdleStateAwareChannelHandler {
}
// Write position to database
+ Long id = null;
try {
- Long id = dataManager.addPosition(position);
- if (id != null) {
- dataManager.updateLatestPosition(position.getDeviceId(), id);
- }
+ id = dataManager.addPosition(position);
} catch (Exception error) {
Log.warning(error);
}
+ return id;
}
@Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) {
+ Long id = null;
+ Position lastPostition = null;
if (e.getMessage() instanceof Position) {
- processSinglePosition((Position) e.getMessage());
+ id = processSinglePosition((Position) e.getMessage());
+ lastPostition = (Position) e.getMessage();
} else if (e.getMessage() instanceof List) {
List<Position> positions = (List<Position>) e.getMessage();
for (Position position : positions) {
- processSinglePosition(position);
+ id = processSinglePosition(position);
+ lastPostition = position;
+ }
+ }
+ if (id != null) {
+ try {
+ dataManager.updateLatestPosition(lastPostition, id);
+ } catch (Exception error) {
+ Log.warning(error);
}
}
}
diff --git a/src/org/traccar/model/DataManager.java b/src/org/traccar/model/DataManager.java
index 4a5aaa348..7228409f1 100644
--- a/src/org/traccar/model/DataManager.java
+++ b/src/org/traccar/model/DataManager.java
@@ -27,11 +27,12 @@ public interface DataManager {
*/
public List<Device> getDevices() throws Exception;
public Device getDeviceByImei(String imei) throws Exception;
+ public Device getDeviceById(Long id) throws Exception;
/**
* Manage positions
*/
public Long addPosition(Position position) throws Exception;
- public void updateLatestPosition(Long deviceId, Long positionId) throws Exception;
+ public void updateLatestPosition(Position position, Long positionId) throws Exception;
}
diff --git a/src/org/traccar/model/DatabaseDataManager.java b/src/org/traccar/model/DatabaseDataManager.java
index 3799c5461..5c1b292af 100644
--- a/src/org/traccar/model/DatabaseDataManager.java
+++ b/src/org/traccar/model/DatabaseDataManager.java
@@ -45,6 +45,9 @@ public class DatabaseDataManager implements DataManager {
private NamedParameterStatement queryGetDevices;
private NamedParameterStatement queryAddPosition;
private NamedParameterStatement queryUpdateLatestPosition;
+ private String queryAddPositionTemplate;
+ private String queryUpdateLatestPositionTemplate;
+ private AdvancedConnection globalConnection;
/**
* Initialize database
@@ -58,7 +61,7 @@ public class DatabaseDataManager implements DataManager {
if (driverFile != null) {
URL url = new URL("jar:file:" + new File(driverFile).getAbsolutePath() + "!/");
- URLClassLoader cl = new URLClassLoader(new URL[] { url });
+ URLClassLoader cl = new URLClassLoader(new URL[]{url});
Driver d = (Driver) Class.forName(driver, true, cl).newInstance();
DriverManager.registerDriver(new DriverDelegate(d));
} else {
@@ -71,7 +74,7 @@ public class DatabaseDataManager implements DataManager {
String user = properties.getProperty("database.user");
String password = properties.getProperty("database.password");
AdvancedConnection connection = new AdvancedConnection(url, user, password);
-
+ globalConnection = connection;
// Load statements from configuration
String query;
@@ -81,11 +84,13 @@ public class DatabaseDataManager implements DataManager {
}
query = properties.getProperty("database.insertPosition");
+ queryAddPositionTemplate = query;
if (query != null) {
queryAddPosition = new NamedParameterStatement(connection, query);
}
query = properties.getProperty("database.updateLatestPosition");
+ queryUpdateLatestPositionTemplate = query;
if (query != null) {
queryUpdateLatestPosition = new NamedParameterStatement(connection, query);
}
@@ -103,6 +108,7 @@ public class DatabaseDataManager implements DataManager {
Device device = new Device();
device.setId(result.getLong("id"));
device.setImei(result.getString("imei"));
+ device.setDatabase(result.getString("database"));
deviceList.add(device);
}
}
@@ -114,13 +120,14 @@ public class DatabaseDataManager implements DataManager {
* Devices cache
*/
private Map<String, Device> devices;
+ private Map<Long, Device> devices_id;
@Override
public Device getDeviceByImei(String imei) throws SQLException {
if (devices == null || !devices.containsKey(imei)) {
devices = new HashMap<String, Device>();
- for (Device device: getDevices()) {
+ for (Device device : getDevices()) {
devices.put(device.getImei(), device);
}
}
@@ -129,7 +136,27 @@ public class DatabaseDataManager implements DataManager {
}
@Override
+ public Device getDeviceById(Long id) throws SQLException {
+
+ if (devices_id == null || !devices_id.containsKey(id)) {
+ devices_id = new HashMap<Long, Device>();
+ for (Device device : getDevices()) {
+ devices_id.put(device.getId(), device);
+ }
+ }
+
+ return devices_id.get(id);
+ }
+
+ @Override
public synchronized Long addPosition(Position position) throws SQLException {
+ if (queryAddPositionTemplate.contains("_database_")) {
+ String query = queryAddPositionTemplate;
+ query = query.replace("_database_", this.getDeviceById(position.getDeviceId()).getDatabase());
+ if (query != null) {
+ queryAddPosition = new NamedParameterStatement(globalConnection, query);
+ }
+ }
if (queryAddPosition != null) {
queryAddPosition.prepare(Statement.RETURN_GENERATED_KEYS);
@@ -144,7 +171,7 @@ public class DatabaseDataManager implements DataManager {
queryAddPosition.setDouble("course", position.getCourse());
queryAddPosition.setString("address", position.getAddress());
queryAddPosition.setString("extended_info", position.getExtendedInfo());
-
+
// DELME: Temporary compatibility support
XPath xpath = XPathFactory.newInstance().newXPath();
try {
@@ -180,13 +207,52 @@ public class DatabaseDataManager implements DataManager {
}
@Override
- public void updateLatestPosition(Long deviceId, Long positionId) throws SQLException {
-
+ public void updateLatestPosition(Position position, Long positionId ) throws SQLException {
+ if (queryUpdateLatestPositionTemplate.contains("_database_")) {
+ String query = queryUpdateLatestPositionTemplate;
+ query = query.replace("_database_", this.getDeviceById(position.getDeviceId()).getDatabase());
+ if (query != null) {
+ queryAddPosition = new NamedParameterStatement(globalConnection, query);
+ }
+ }
+
if (queryUpdateLatestPosition != null) {
queryUpdateLatestPosition.prepare();
- queryUpdateLatestPosition.setLong("device_id", deviceId);
+ queryUpdateLatestPosition.setLong("device_id", position.getDeviceId());
queryUpdateLatestPosition.setLong("id", positionId);
+ queryUpdateLatestPosition.setTimestamp("time", position.getTime());
+ queryUpdateLatestPosition.setBoolean("valid", position.getValid());
+ queryUpdateLatestPosition.setDouble("altitude", position.getAltitude());
+ queryUpdateLatestPosition.setDouble("latitude", position.getLatitude());
+ queryUpdateLatestPosition.setDouble("longitude", position.getLongitude());
+ queryUpdateLatestPosition.setDouble("speed", position.getSpeed());
+ queryUpdateLatestPosition.setDouble("course", position.getCourse());
+ queryUpdateLatestPosition.setString("address", position.getAddress());
+ queryUpdateLatestPosition.setString("extended_info", position.getExtendedInfo());
+
+ // DELME: Temporary compatibility support
+ XPath xpath = XPathFactory.newInstance().newXPath();
+ try {
+ InputSource source = new InputSource(new StringReader(position.getExtendedInfo()));
+ String index = xpath.evaluate("/info/index", source);
+ if (!index.isEmpty()) {
+ queryUpdateLatestPosition.setLong("id", Long.valueOf(index));
+ } else {
+ queryUpdateLatestPosition.setLong("id", null);
+ }
+ source = new InputSource(new StringReader(position.getExtendedInfo()));
+ String power = xpath.evaluate("/info/power", source);
+ if (!power.isEmpty()) {
+ queryUpdateLatestPosition.setDouble("power", Double.valueOf(power));
+ } else {
+ queryUpdateLatestPosition.setLong("power", null);
+ }
+ } catch (XPathExpressionException e) {
+ Log.warning("Error in XML: " + position.getExtendedInfo(), e);
+ queryUpdateLatestPosition.setLong("id", null);
+ queryUpdateLatestPosition.setLong("power", null);
+ }
queryUpdateLatestPosition.executeUpdate();
}
diff --git a/src/org/traccar/model/Device.java b/src/org/traccar/model/Device.java
index 1276216fc..8c8c74e28 100644
--- a/src/org/traccar/model/Device.java
+++ b/src/org/traccar/model/Device.java
@@ -72,4 +72,17 @@ public class Device {
this.uniqueId = uniqueId;
}
+ /**
+ * Database (for multi-database systems)
+ */
+ private String database;
+
+ public String getDatabase() {
+ return database;
+ }
+
+ public void setDatabase(String database) {
+ this.database = database;
+ }
+
}