aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnton Tananaev <anton.tananaev@gmail.com>2015-07-14 19:17:02 +1200
committerAnton Tananaev <anton.tananaev@gmail.com>2015-07-14 19:17:02 +1200
commite98b18b8b0a8c99d24ac8010c1393978d071baa2 (patch)
tree48d63f067dcfaae9c0bdac34af4f9514a49ab9f5
parent7310ee5f15729ea8517bfb6b0890a46da1217610 (diff)
downloadtrackermap-server-e98b18b8b0a8c99d24ac8010c1393978d071baa2.tar.gz
trackermap-server-e98b18b8b0a8c99d24ac8010c1393978d071baa2.tar.bz2
trackermap-server-e98b18b8b0a8c99d24ac8010c1393978d071baa2.zip
Support forwarding of data (fix #1131)
-rw-r--r--debug.xml2
-rw-r--r--src/org/traccar/DefaultDataHandler.java1
-rw-r--r--src/org/traccar/MainEventHandler.java2
-rw-r--r--src/org/traccar/WebDataHandler.java41
-rw-r--r--src/org/traccar/database/DataManager.java18
-rw-r--r--src/org/traccar/database/IdentityManager.java2
-rw-r--r--test/org/traccar/helper/TestIdentityManager.java5
7 files changed, 61 insertions, 10 deletions
diff --git a/debug.xml b/debug.xml
index 8e3e46f1d..cf40dbc4d 100644
--- a/debug.xml
+++ b/debug.xml
@@ -27,7 +27,7 @@
<entry key='filter.distance'>50</entry>-->
<!--<entry key='forward.enable'>true</entry>
- <entry key='forward.url'>http://localhost:5055?id={deviceId}&amp;timestamp={fixTime}&amp;lat={latitude}&amp;lon={longitude}</entry>-->
+ <entry key='forward.url'>http://example.com:8080/gprmc/Data?id={uniqueId}&amp;code=0x0000&amp;gprmc={gprmc}</entry>-->
<entry key='logger.enable'>true</entry>
<entry key='logger.level'>all</entry>
diff --git a/src/org/traccar/DefaultDataHandler.java b/src/org/traccar/DefaultDataHandler.java
index 430e20776..d75b62dee 100644
--- a/src/org/traccar/DefaultDataHandler.java
+++ b/src/org/traccar/DefaultDataHandler.java
@@ -26,7 +26,6 @@ public class DefaultDataHandler extends BaseDataHandler {
try {
Context.getDataManager().addPosition(position);
Context.getDataManager().updateLatestPosition(position);
- Context.getConnectionManager().update(position);
} catch (Exception error) {
Log.warning(error);
}
diff --git a/src/org/traccar/MainEventHandler.java b/src/org/traccar/MainEventHandler.java
index b32d577de..c28340f98 100644
--- a/src/org/traccar/MainEventHandler.java
+++ b/src/org/traccar/MainEventHandler.java
@@ -43,6 +43,8 @@ public class MainEventHandler extends IdleStateAwareChannelHandler {
s.append("lat: ").append(position.getLatitude()).append(", ");
s.append("lon: ").append(position.getLongitude());
Log.info(s.toString());
+
+ Context.getConnectionManager().update(position);
}
}
}
diff --git a/src/org/traccar/WebDataHandler.java b/src/org/traccar/WebDataHandler.java
index bb1a540a9..fce73c816 100644
--- a/src/org/traccar/WebDataHandler.java
+++ b/src/org/traccar/WebDataHandler.java
@@ -16,6 +16,12 @@
package org.traccar;
import com.ning.http.client.AsyncHttpClient;
+import java.util.Calendar;
+import java.util.Formatter;
+import java.util.Locale;
+import java.util.TimeZone;
+import org.traccar.helper.Crc;
+import org.traccar.model.Device;
import org.traccar.model.Position;
public class WebDataHandler extends BaseDataHandler {
@@ -25,15 +31,44 @@ public class WebDataHandler extends BaseDataHandler {
public WebDataHandler(String url) {
this.url = url;
}
+
+ private static String formatSentence(Position position) {
+
+ StringBuilder s = new StringBuilder("$GPRMC,");
+
+ try (Formatter f = new Formatter(s, Locale.ENGLISH)) {
+
+ Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC"), Locale.ENGLISH);
+ calendar.setTimeInMillis(position.getFixTime().getTime());
+
+ f.format("%1$tH%1$tM%1$tS.%1$tL,A,", calendar);
+
+ double lat = position.getLatitude();
+ double lon = position.getLongitude();
+ f.format("%02d%07.4f,%c,", (int) Math.abs(lat), Math.abs(lat) % 1 * 60, lat < 0 ? 'S' : 'N');
+ f.format("%03d%07.4f,%c,", (int) Math.abs(lon), Math.abs(lon) % 1 * 60, lon < 0 ? 'W' : 'E');
+
+ f.format("%.2f,%.2f,", position.getSpeed(), position.getCourse());
+ f.format("%1$td%1$tm%1$ty,,", calendar);
+ }
+
+ s.append(Crc.nmeaChecksum(s.toString()));
+
+ return s.toString();
+ }
@Override
protected Position handlePosition(Position position) {
+
+ Device device = Context.getIdentityManager().getDeviceById(position.getDeviceId());
+
String request = url.
- //replaceAll("\\{uniqueId}", String.valueOf(position.getDeviceId())).
- replaceAll("\\{deviceId}", String.valueOf(position.getDeviceId())).
+ replaceAll("\\{uniqueId}", device.getUniqueId()).
+ replaceAll("\\{deviceId}", String.valueOf(device.getId())).
replaceAll("\\{fixTime}", String.valueOf(position.getFixTime().getTime())).
replaceAll("\\{latitude}", String.valueOf(position.getLatitude())).
- replaceAll("\\{longitude}", String.valueOf(position.getLongitude()));
+ replaceAll("\\{longitude}", String.valueOf(position.getLongitude())).
+ replaceAll("\\{gprmc}", formatSentence(position));
AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
asyncHttpClient.prepareGet(request).execute();
diff --git a/src/org/traccar/database/DataManager.java b/src/org/traccar/database/DataManager.java
index 48f3228ce..f134f87cb 100644
--- a/src/org/traccar/database/DataManager.java
+++ b/src/org/traccar/database/DataManager.java
@@ -50,7 +50,8 @@ public class DataManager implements IdentityManager {
private DataSource dataSource;
- private final Map<String, Device> devices = new HashMap<>();
+ private final Map<Long, Device> devicesById = new HashMap<>();
+ private final Map<String, Device> devicesByUniqueId = new HashMap<>();
private long devicesLastUpdate;
private final long devicesRefreshDelay;
@@ -107,20 +108,27 @@ public class DataManager implements IdentityManager {
dataSource = ds;
}
}
+
+ @Override
+ public Device getDeviceById(long id) {
+ return devicesById.get(id);
+ }
@Override
public Device getDeviceByUniqueId(String uniqueId) throws SQLException {
- if ((new Date().getTime() - devicesLastUpdate > devicesRefreshDelay) || !devices.containsKey(uniqueId)) {
+ if ((new Date().getTime() - devicesLastUpdate > devicesRefreshDelay) || !devicesByUniqueId.containsKey(uniqueId)) {
- devices.clear();
+ devicesById.clear();
+ devicesByUniqueId.clear();
for (Device device : getAllDevices()) {
- devices.put(device.getUniqueId(), device);
+ devicesById.put(device.getId(), device);
+ devicesByUniqueId.put(device.getUniqueId(), device);
}
devicesLastUpdate = new Date().getTime();
}
- return devices.get(uniqueId);
+ return devicesByUniqueId.get(uniqueId);
}
private String getQuery(String key) {
diff --git a/src/org/traccar/database/IdentityManager.java b/src/org/traccar/database/IdentityManager.java
index c3861ad72..84f246363 100644
--- a/src/org/traccar/database/IdentityManager.java
+++ b/src/org/traccar/database/IdentityManager.java
@@ -19,6 +19,8 @@ import org.traccar.model.Device;
public interface IdentityManager {
+ public Device getDeviceById(long id);
+
public Device getDeviceByUniqueId(String uniqueId) throws Exception;
}
diff --git a/test/org/traccar/helper/TestIdentityManager.java b/test/org/traccar/helper/TestIdentityManager.java
index 0cc1e786c..a4ddeeb09 100644
--- a/test/org/traccar/helper/TestIdentityManager.java
+++ b/test/org/traccar/helper/TestIdentityManager.java
@@ -6,6 +6,11 @@ import org.traccar.model.Device;
public class TestIdentityManager implements IdentityManager {
@Override
+ public Device getDeviceById(long id) {
+ return null;
+ }
+
+ @Override
public Device getDeviceByUniqueId(String imei) {
Device device = new Device();
device.setId(new Long(1));