diff options
author | Anton Tananaev <anton.tananaev@gmail.com> | 2015-07-14 19:17:02 +1200 |
---|---|---|
committer | Anton Tananaev <anton.tananaev@gmail.com> | 2015-07-14 19:17:02 +1200 |
commit | e98b18b8b0a8c99d24ac8010c1393978d071baa2 (patch) | |
tree | 48d63f067dcfaae9c0bdac34af4f9514a49ab9f5 /src/org/traccar | |
parent | 7310ee5f15729ea8517bfb6b0890a46da1217610 (diff) | |
download | traccar-server-e98b18b8b0a8c99d24ac8010c1393978d071baa2.tar.gz traccar-server-e98b18b8b0a8c99d24ac8010c1393978d071baa2.tar.bz2 traccar-server-e98b18b8b0a8c99d24ac8010c1393978d071baa2.zip |
Support forwarding of data (fix #1131)
Diffstat (limited to 'src/org/traccar')
-rw-r--r-- | src/org/traccar/DefaultDataHandler.java | 1 | ||||
-rw-r--r-- | src/org/traccar/MainEventHandler.java | 2 | ||||
-rw-r--r-- | src/org/traccar/WebDataHandler.java | 41 | ||||
-rw-r--r-- | src/org/traccar/database/DataManager.java | 18 | ||||
-rw-r--r-- | src/org/traccar/database/IdentityManager.java | 2 |
5 files changed, 55 insertions, 9 deletions
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; } |