aboutsummaryrefslogtreecommitdiff
path: root/src/org
diff options
context:
space:
mode:
Diffstat (limited to 'src/org')
-rw-r--r--src/org/traccar/TrackerEventHandler.java24
-rw-r--r--src/org/traccar/model/DataManager.java2
-rw-r--r--src/org/traccar/model/DatabaseDataManager.java87
3 files changed, 65 insertions, 48 deletions
diff --git a/src/org/traccar/TrackerEventHandler.java b/src/org/traccar/TrackerEventHandler.java
index 6deeaad9b..5387074dc 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 && lastPostition != 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..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;
+
+ }
+
}