aboutsummaryrefslogtreecommitdiff
path: root/src/org/traccar/model
diff options
context:
space:
mode:
authorAnton Tananaev <anton.tananaev@gmail.com>2014-09-10 09:12:28 +1200
committerAnton Tananaev <anton.tananaev@gmail.com>2014-09-10 09:12:28 +1200
commit026f089951f03931138a1c6171699fcb9156da9c (patch)
tree2a961b6d72b0508a1abf7236bcf7cec68d55bb38 /src/org/traccar/model
parent9bad8efca5f64b60d5525b3858f42ac6cd6be272 (diff)
parent6e20a5d0846ae05349f1332f61928ac6aa3d559c (diff)
downloadtrackermap-server-026f089951f03931138a1c6171699fcb9156da9c.tar.gz
trackermap-server-026f089951f03931138a1c6171699fcb9156da9c.tar.bz2
trackermap-server-026f089951f03931138a1c6171699fcb9156da9c.zip
Merge pull request #864 from jedjones-uk/master
Update to updateLatestPosition call timing
Diffstat (limited to 'src/org/traccar/model')
-rw-r--r--src/org/traccar/model/DataManager.java2
-rw-r--r--src/org/traccar/model/DatabaseDataManager.java87
2 files changed, 48 insertions, 41 deletions
diff --git a/src/org/traccar/model/DataManager.java b/src/org/traccar/model/DataManager.java
index 4a5aaa348..f0bfdb50d 100644
--- a/src/org/traccar/model/DataManager.java
+++ b/src/org/traccar/model/DataManager.java
@@ -32,6 +32,6 @@ public interface DataManager {
* 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..f8d8806a7 100644
--- a/src/org/traccar/model/DatabaseDataManager.java
+++ b/src/org/traccar/model/DatabaseDataManager.java
@@ -58,7 +58,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 {
@@ -120,7 +120,7 @@ public class DatabaseDataManager implements DataManager {
if (devices == null || !devices.containsKey(imei)) {
devices = new HashMap<String, Device>();
- for (Device device: getDevices()) {
+ for (Device device : getDevices()) {
devices.put(device.getImei(), device);
}
}
@@ -134,39 +134,7 @@ public class DatabaseDataManager implements DataManager {
if (queryAddPosition != null) {
queryAddPosition.prepare(Statement.RETURN_GENERATED_KEYS);
- queryAddPosition.setLong("device_id", position.getDeviceId());
- queryAddPosition.setTimestamp("time", position.getTime());
- queryAddPosition.setBoolean("valid", position.getValid());
- queryAddPosition.setDouble("altitude", position.getAltitude());
- queryAddPosition.setDouble("latitude", position.getLatitude());
- queryAddPosition.setDouble("longitude", position.getLongitude());
- queryAddPosition.setDouble("speed", position.getSpeed());
- 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 {
- InputSource source = new InputSource(new StringReader(position.getExtendedInfo()));
- String index = xpath.evaluate("/info/index", source);
- if (!index.isEmpty()) {
- queryAddPosition.setLong("id", Long.valueOf(index));
- } else {
- queryAddPosition.setLong("id", null);
- }
- source = new InputSource(new StringReader(position.getExtendedInfo()));
- String power = xpath.evaluate("/info/power", source);
- if (!power.isEmpty()) {
- queryAddPosition.setDouble("power", Double.valueOf(power));
- } else {
- queryAddPosition.setLong("power", null);
- }
- } catch (XPathExpressionException e) {
- Log.warning("Error in XML: " + position.getExtendedInfo(), e);
- queryAddPosition.setLong("id", null);
- queryAddPosition.setLong("power", null);
- }
+ queryAddPosition = assignVariables(queryAddPosition, position);
queryAddPosition.executeUpdate();
@@ -180,16 +148,55 @@ 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 (queryUpdateLatestPosition != null) {
queryUpdateLatestPosition.prepare();
-
- queryUpdateLatestPosition.setLong("device_id", deviceId);
+
+ queryUpdateLatestPosition = assignVariables(queryUpdateLatestPosition, position);
queryUpdateLatestPosition.setLong("id", positionId);
-
+
queryUpdateLatestPosition.executeUpdate();
}
}
+ private NamedParameterStatement assignVariables(NamedParameterStatement preparedStatement, Position position) throws SQLException {
+
+ preparedStatement.setLong("device_id", position.getDeviceId());
+ preparedStatement.setTimestamp("time", position.getTime());
+ preparedStatement.setBoolean("valid", position.getValid());
+ preparedStatement.setDouble("altitude", position.getAltitude());
+ preparedStatement.setDouble("latitude", position.getLatitude());
+ preparedStatement.setDouble("longitude", position.getLongitude());
+ preparedStatement.setDouble("speed", position.getSpeed());
+ preparedStatement.setDouble("course", position.getCourse());
+ preparedStatement.setString("address", position.getAddress());
+ preparedStatement.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()) {
+ preparedStatement.setLong("id", Long.valueOf(index));
+ } else {
+ preparedStatement.setLong("id", null);
+ }
+ source = new InputSource(new StringReader(position.getExtendedInfo()));
+ String power = xpath.evaluate("/info/power", source);
+ if (!power.isEmpty()) {
+ preparedStatement.setDouble("power", Double.valueOf(power));
+ } else {
+ preparedStatement.setLong("power", null);
+ }
+ } catch (XPathExpressionException e) {
+ Log.warning("Error in XML: " + position.getExtendedInfo(), e);
+ preparedStatement.setLong("id", null);
+ preparedStatement.setLong("power", null);
+ }
+
+ return preparedStatement;
+
+ }
+
}