aboutsummaryrefslogtreecommitdiff
path: root/src/org/traccar/notification
diff options
context:
space:
mode:
Diffstat (limited to 'src/org/traccar/notification')
-rw-r--r--src/org/traccar/notification/EventForwarder.java37
-rw-r--r--src/org/traccar/notification/JsonTypeEventForwarder.java6
-rw-r--r--src/org/traccar/notification/MultiPartEventForwarder.java5
-rw-r--r--src/org/traccar/notification/NotificationFormatter.java7
4 files changed, 36 insertions, 19 deletions
diff --git a/src/org/traccar/notification/EventForwarder.java b/src/org/traccar/notification/EventForwarder.java
index 68c3ea221..9d2181e21 100644
--- a/src/org/traccar/notification/EventForwarder.java
+++ b/src/org/traccar/notification/EventForwarder.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2016 Anton Tananaev (anton@traccar.org)
+ * Copyright 2016 - 2018 Anton Tananaev (anton@traccar.org)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -17,7 +17,7 @@ package org.traccar.notification;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.ning.http.client.AsyncHttpClient.BoundRequestBuilder;
-import java.util.Arrays;
+import java.util.Collections;
import java.util.List;
import org.traccar.Context;
@@ -25,11 +25,14 @@ import org.traccar.helper.Log;
import org.traccar.model.Device;
import org.traccar.model.Event;
import org.traccar.model.Geofence;
+import org.traccar.model.Maintenance;
import org.traccar.model.Position;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
+import java.util.Set;
+
import com.ning.http.client.FluentCaseInsensitiveStringsMap;
public abstract class EventForwarder {
@@ -46,8 +49,10 @@ public abstract class EventForwarder {
private static final String KEY_EVENT = "event";
private static final String KEY_GEOFENCE = "geofence";
private static final String KEY_DEVICE = "device";
+ private static final String KEY_MAINTENANCE = "maintenance";
+ private static final String KEY_USERS = "users";
- public final void forwardEvent(Event event, Position position) {
+ public final void forwardEvent(Event event, Position position, Set<Long> users) {
BoundRequestBuilder requestBuilder = Context.getAsyncHttpClient().preparePost(url);
requestBuilder.setBodyEncoding(StandardCharsets.UTF_8.name());
@@ -60,7 +65,7 @@ public abstract class EventForwarder {
requestBuilder.setHeaders(params);
}
- setContent(event, position, requestBuilder);
+ setContent(event, position, users, requestBuilder);
requestBuilder.execute();
}
@@ -73,30 +78,35 @@ public abstract class EventForwarder {
for (String paramLine: paramsLines) {
splitedLine = paramLine.split(separator, 2);
if (splitedLine.length == 2) {
- paramsMap.put(splitedLine[0].trim(), Arrays.asList(splitedLine[1].trim()));
+ paramsMap.put(splitedLine[0].trim(), Collections.singletonList(splitedLine[1].trim()));
}
}
return paramsMap;
}
- protected String prepareJsonPayload(Event event, Position position) {
+ protected String prepareJsonPayload(Event event, Position position, Set<Long> users) {
Map<String, Object> data = new HashMap<>();
data.put(KEY_EVENT, event);
if (position != null) {
data.put(KEY_POSITION, position);
}
- if (event.getDeviceId() != 0) {
- Device device = Context.getIdentityManager().getById(event.getDeviceId());
- if (device != null) {
- data.put(KEY_DEVICE, device);
- }
+ Device device = Context.getIdentityManager().getById(event.getDeviceId());
+ if (device != null) {
+ data.put(KEY_DEVICE, device);
}
if (event.getGeofenceId() != 0) {
- Geofence geofence = (Geofence) Context.getGeofenceManager().getById(event.getGeofenceId());
+ Geofence geofence = Context.getGeofenceManager().getById(event.getGeofenceId());
if (geofence != null) {
data.put(KEY_GEOFENCE, geofence);
}
}
+ if (event.getMaintenanceId() != 0) {
+ Maintenance maintenance = Context.getMaintenancesManager().getById(event.getMaintenanceId());
+ if (maintenance != null) {
+ data.put(KEY_MAINTENANCE, maintenance);
+ }
+ }
+ data.put(KEY_USERS, Context.getUsersManager().getItems(users));
try {
return Context.getObjectMapper().writeValueAsString(data);
} catch (JsonProcessingException e) {
@@ -106,6 +116,7 @@ public abstract class EventForwarder {
}
protected abstract String getContentType();
- protected abstract void setContent(Event event, Position position, BoundRequestBuilder requestBuilder);
+ protected abstract void setContent(
+ Event event, Position position, Set<Long> users, BoundRequestBuilder requestBuilder);
}
diff --git a/src/org/traccar/notification/JsonTypeEventForwarder.java b/src/org/traccar/notification/JsonTypeEventForwarder.java
index c1e4220d0..27ef61af1 100644
--- a/src/org/traccar/notification/JsonTypeEventForwarder.java
+++ b/src/org/traccar/notification/JsonTypeEventForwarder.java
@@ -1,5 +1,7 @@
package org.traccar.notification;
+import java.util.Set;
+
import org.traccar.model.Event;
import org.traccar.model.Position;
@@ -13,8 +15,8 @@ public class JsonTypeEventForwarder extends EventForwarder {
}
@Override
- protected void setContent(Event event, Position position, BoundRequestBuilder requestBuilder) {
- requestBuilder.setBody(prepareJsonPayload(event, position));
+ protected void setContent(Event event, Position position, Set<Long> users, BoundRequestBuilder requestBuilder) {
+ requestBuilder.setBody(prepareJsonPayload(event, position, users));
}
}
diff --git a/src/org/traccar/notification/MultiPartEventForwarder.java b/src/org/traccar/notification/MultiPartEventForwarder.java
index f4c36d3e4..6227c66cc 100644
--- a/src/org/traccar/notification/MultiPartEventForwarder.java
+++ b/src/org/traccar/notification/MultiPartEventForwarder.java
@@ -3,6 +3,7 @@ package org.traccar.notification;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;
+import java.util.Set;
import java.util.Map.Entry;
import org.traccar.Context;
@@ -28,7 +29,7 @@ public class MultiPartEventForwarder extends EventForwarder {
}
@Override
- protected void setContent(Event event, Position position, BoundRequestBuilder requestBuilder) {
+ protected void setContent(Event event, Position position, Set<Long> users, BoundRequestBuilder requestBuilder) {
if (additionalParams != null && !additionalParams.isEmpty()) {
Map<String, List<String>> paramsToAdd = splitIntoKeyValues(additionalParams, "=");
@@ -41,6 +42,6 @@ public class MultiPartEventForwarder extends EventForwarder {
}
}
requestBuilder.addBodyPart(new StringPart(payloadParamName,
- prepareJsonPayload(event, position), "application/json", StandardCharsets.UTF_8));
+ prepareJsonPayload(event, position, users), "application/json", StandardCharsets.UTF_8));
}
}
diff --git a/src/org/traccar/notification/NotificationFormatter.java b/src/org/traccar/notification/NotificationFormatter.java
index 114825a83..524153721 100644
--- a/src/org/traccar/notification/NotificationFormatter.java
+++ b/src/org/traccar/notification/NotificationFormatter.java
@@ -1,6 +1,6 @@
/*
- * Copyright 2016 - 2017 Anton Tananaev (anton@traccar.org)
- * Copyright 2017 Andrey Kunitsyn (andrey@traccar.org)
+ * Copyright 2016 - 2018 Anton Tananaev (anton@traccar.org)
+ * Copyright 2017 - 2018 Andrey Kunitsyn (andrey@traccar.org)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -57,6 +57,9 @@ public final class NotificationFormatter {
if (event.getGeofenceId() != 0) {
velocityContext.put("geofence", Context.getGeofenceManager().getById(event.getGeofenceId()));
}
+ if (event.getMaintenanceId() != 0) {
+ velocityContext.put("maintenance", Context.getMaintenancesManager().getById(event.getMaintenanceId()));
+ }
String driverUniqueId = event.getString(Position.KEY_DRIVER_UNIQUE_ID);
if (driverUniqueId != null) {
velocityContext.put("driver", Context.getDriversManager().getDriverByUniqueId(driverUniqueId));