diff options
author | Anton Tananaev <anton.tananaev@gmail.com> | 2016-07-14 08:56:45 +1200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-07-14 08:56:45 +1200 |
commit | 82fd4d09aa7ac37bf2eb1857b12efbd3bc773a94 (patch) | |
tree | 59e55fd0c4f9bffa4c140982c7f040bb8636ad9b /src | |
parent | d154cb277fe2f7b5dfe6bdaee1a013729bbf22eb (diff) | |
parent | 7ce8c4ecf82828920926f81f55cbc4fb833ef259 (diff) | |
download | trackermap-server-82fd4d09aa7ac37bf2eb1857b12efbd3bc773a94.tar.gz trackermap-server-82fd4d09aa7ac37bf2eb1857b12efbd3bc773a94.tar.bz2 trackermap-server-82fd4d09aa7ac37bf2eb1857b12efbd3bc773a94.zip |
Merge pull request #2110 from Abyss777/master
Added possibility to forward all events
Diffstat (limited to 'src')
-rw-r--r-- | src/org/traccar/Context.java | 11 | ||||
-rw-r--r-- | src/org/traccar/api/resource/UserResource.java | 8 | ||||
-rw-r--r-- | src/org/traccar/database/DataManager.java | 14 | ||||
-rw-r--r-- | src/org/traccar/database/NotificationManager.java | 3 | ||||
-rw-r--r-- | src/org/traccar/notification/EventForwarder.java | 75 |
5 files changed, 99 insertions, 12 deletions
diff --git a/src/org/traccar/Context.java b/src/org/traccar/Context.java index ebe700221..0bb18b675 100644 --- a/src/org/traccar/Context.java +++ b/src/org/traccar/Context.java @@ -35,6 +35,7 @@ import org.traccar.helper.Log; import org.traccar.location.LocationProvider; import org.traccar.location.MozillaLocationProvider; import org.traccar.location.OpenCellIdLocationProvider; +import org.traccar.notification.EventForwarder; import org.traccar.web.WebServer; public final class Context { @@ -120,6 +121,12 @@ public final class Context { return ASYNC_HTTP_CLIENT; } + private static EventForwarder eventForwarder; + + public static EventForwarder getEventForvarder() { + return eventForwarder; + } + public static void init(String[] arguments) throws Exception { config = new Config(); @@ -210,6 +217,10 @@ public final class Context { serverManager = new ServerManager(); + if (config.getBoolean("event.forward.enable")) { + eventForwarder = new EventForwarder(); + } + } public static void init(IdentityManager testIdentityManager) { diff --git a/src/org/traccar/api/resource/UserResource.java b/src/org/traccar/api/resource/UserResource.java index cfe338b56..da72c7f47 100644 --- a/src/org/traccar/api/resource/UserResource.java +++ b/src/org/traccar/api/resource/UserResource.java @@ -52,9 +52,6 @@ public class UserResource extends BaseResource { } Context.getDataManager().addUser(entity); Context.getPermissionsManager().refresh(); - if (Context.getGeofenceManager() != null) { - Context.getGeofenceManager().refresh(); - } if (Context.getNotificationManager() != null) { Context.getNotificationManager().refresh(); } @@ -71,9 +68,6 @@ public class UserResource extends BaseResource { } Context.getDataManager().updateUser(entity); Context.getPermissionsManager().refresh(); - if (Context.getGeofenceManager() != null) { - Context.getGeofenceManager().refresh(); - } if (Context.getNotificationManager() != null) { Context.getNotificationManager().refresh(); } @@ -87,7 +81,7 @@ public class UserResource extends BaseResource { Context.getDataManager().removeUser(id); Context.getPermissionsManager().refresh(); if (Context.getGeofenceManager() != null) { - Context.getGeofenceManager().refresh(); + Context.getGeofenceManager().refreshUserGeofences(); } if (Context.getNotificationManager() != null) { Context.getNotificationManager().refresh(); diff --git a/src/org/traccar/database/DataManager.java b/src/org/traccar/database/DataManager.java index b3f24383f..0dc3c5c52 100644 --- a/src/org/traccar/database/DataManager.java +++ b/src/org/traccar/database/DataManager.java @@ -401,9 +401,11 @@ public class DataManager implements IdentityManager { QueryBuilder.create(dataSource, getQuery("database.updateDeviceStatus")) .setObject(device) .executeUpdate(); - Device cachedDevice = getDeviceById(device.getId()); - cachedDevice.setStatus(device.getStatus()); - cachedDevice.setMotion(device.getMotion()); + if (devicesById.containsKey(device.getId())) { + Device cachedDevice = devicesById.get(device.getId()); + cachedDevice.setStatus(device.getStatus()); + cachedDevice.setMotion(device.getMotion()); + } } public void removeDevice(long deviceId) throws SQLException { @@ -512,8 +514,10 @@ public class DataManager implements IdentityManager { .setDate("now", new Date()) .setObject(position) .executeUpdate(); - Device device = getDeviceById(position.getDeviceId()); - device.setPositionId(position.getId()); + if (devicesById.containsKey(position.getDeviceId())) { + Device cachedDevice = devicesById.get(position.getDeviceId()); + cachedDevice.setPositionId(position.getId()); + } } public Collection<Position> getLatestPositions() throws SQLException { diff --git a/src/org/traccar/database/NotificationManager.java b/src/org/traccar/database/NotificationManager.java index e618d7ed3..911c5d2dc 100644 --- a/src/org/traccar/database/NotificationManager.java +++ b/src/org/traccar/database/NotificationManager.java @@ -68,6 +68,9 @@ public class NotificationManager { } } } + if (Context.getEventForvarder() != null) { + Context.getEventForvarder().forwardEvent(event, position); + } } public void updateEvents(Collection<Event> events, Position position) { diff --git a/src/org/traccar/notification/EventForwarder.java b/src/org/traccar/notification/EventForwarder.java new file mode 100644 index 000000000..096026541 --- /dev/null +++ b/src/org/traccar/notification/EventForwarder.java @@ -0,0 +1,75 @@ +package org.traccar.notification; + +import java.nio.charset.StandardCharsets; + +import javax.json.Json; +import javax.json.JsonObjectBuilder; + +import org.traccar.Context; +import org.traccar.model.Device; +import org.traccar.model.Event; +import org.traccar.model.Geofence; +import org.traccar.model.Position; +import org.traccar.web.JsonConverter; + +import com.ning.http.client.AsyncHttpClient.BoundRequestBuilder; + +public final class EventForwarder { + + private String url; + private String header; + + public EventForwarder() { + url = Context.getConfig().getString("event.forward.url", "http://localhost/"); + header = Context.getConfig().getString("event.forward.header", ""); + } + + private static final String USER_AGENT = "Traccar Server"; + + private static final String KEY_POSITION = "position"; + private static final String KEY_EVENT = "event"; + private static final String KEY_GEOFENCE = "geofence"; + private static final String KEY_DEVICE = "device"; + + public void forwardEvent(Event event, Position position) { + + + BoundRequestBuilder requestBuilder = Context.getAsyncHttpClient().preparePost(url); + + requestBuilder.addHeader("Content-Type", "application/json; charset=utf-8"); + requestBuilder.addHeader("User-Agent", USER_AGENT); + if (!header.equals("")) { + String[] headerLines = header.split("\\r?\\n"); + for (String headerLine: headerLines) { + String[] splitedLine = headerLine.split(":", 2); + if (splitedLine.length == 2) { + requestBuilder.addHeader(splitedLine[0].trim(), splitedLine[1].trim()); + } + } + } + + requestBuilder.setBody(preparePayload(event, position)); + requestBuilder.execute(); + } + + private byte[] preparePayload(Event event, Position position) { + JsonObjectBuilder json = Json.createObjectBuilder(); + json.add(KEY_EVENT, JsonConverter.objectToJson(event)); + if (position != null) { + json.add(KEY_POSITION, JsonConverter.objectToJson(position)); + } + if (event.getDeviceId() != 0) { + Device device = Context.getIdentityManager().getDeviceById(event.getDeviceId()); + if (device != null) { + json.add(KEY_DEVICE, JsonConverter.objectToJson(device)); + } + } + if (event.getGeofenceId() != 0) { + Geofence geofence = Context.getGeofenceManager().getGeofence(event.getGeofenceId()); + if (geofence != null) { + json.add(KEY_GEOFENCE, JsonConverter.objectToJson(geofence)); + } + } + return json.build().toString().getBytes(StandardCharsets.UTF_8); + } +} |