aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/org/traccar/api/resource/DevicePermissionResource.java8
-rw-r--r--src/org/traccar/api/resource/DeviceResource.java11
-rw-r--r--src/org/traccar/api/resource/GroupPermissionResource.java8
-rw-r--r--src/org/traccar/api/resource/GroupResource.java11
-rw-r--r--src/org/traccar/api/resource/NotificationResource.java59
-rw-r--r--src/org/traccar/api/resource/UserResource.java21
-rw-r--r--src/org/traccar/database/DataManager.java24
-rw-r--r--src/org/traccar/database/NotificationManager.java139
-rw-r--r--src/org/traccar/model/Notification.java40
-rw-r--r--src/org/traccar/model/User.java14
-rw-r--r--src/org/traccar/notification/NotificationFormatter.java158
-rw-r--r--src/org/traccar/notification/NotificationMail.java144
-rw-r--r--src/org/traccar/web/WebServer.java4
13 files changed, 616 insertions, 25 deletions
diff --git a/src/org/traccar/api/resource/DevicePermissionResource.java b/src/org/traccar/api/resource/DevicePermissionResource.java
index 2ef436f11..7de050074 100644
--- a/src/org/traccar/api/resource/DevicePermissionResource.java
+++ b/src/org/traccar/api/resource/DevicePermissionResource.java
@@ -38,7 +38,9 @@ public class DevicePermissionResource extends BaseResource {
Context.getPermissionsManager().checkAdmin(getUserId());
Context.getDataManager().linkDevice(entity.getUserId(), entity.getDeviceId());
Context.getPermissionsManager().refresh();
- Context.getGeofenceManager().refresh();
+ if (Context.getGeofenceManager() != null) {
+ Context.getGeofenceManager().refresh();
+ }
return Response.ok(entity).build();
}
@@ -47,7 +49,9 @@ public class DevicePermissionResource extends BaseResource {
Context.getPermissionsManager().checkAdmin(getUserId());
Context.getDataManager().unlinkDevice(entity.getUserId(), entity.getDeviceId());
Context.getPermissionsManager().refresh();
- Context.getGeofenceManager().refresh();
+ if (Context.getGeofenceManager() != null) {
+ Context.getGeofenceManager().refresh();
+ }
return Response.noContent().build();
}
diff --git a/src/org/traccar/api/resource/DeviceResource.java b/src/org/traccar/api/resource/DeviceResource.java
index d6032bb1e..d6c53dc04 100644
--- a/src/org/traccar/api/resource/DeviceResource.java
+++ b/src/org/traccar/api/resource/DeviceResource.java
@@ -60,7 +60,9 @@ public class DeviceResource extends BaseResource {
Context.getDataManager().addDevice(entity);
Context.getDataManager().linkDevice(getUserId(), entity.getId());
Context.getPermissionsManager().refresh();
- Context.getGeofenceManager().refresh();
+ if (Context.getGeofenceManager() != null) {
+ Context.getGeofenceManager().refresh();
+ }
return Response.ok(entity).build();
}
@@ -70,6 +72,9 @@ public class DeviceResource extends BaseResource {
Context.getPermissionsManager().checkReadonly(getUserId());
Context.getPermissionsManager().checkDevice(getUserId(), id);
Context.getDataManager().updateDevice(entity);
+ if (Context.getGeofenceManager() != null) {
+ Context.getGeofenceManager().refresh();
+ }
return Response.ok(entity).build();
}
@@ -80,7 +85,9 @@ public class DeviceResource extends BaseResource {
Context.getPermissionsManager().checkDevice(getUserId(), id);
Context.getDataManager().removeDevice(id);
Context.getPermissionsManager().refresh();
- Context.getGeofenceManager().refresh();
+ if (Context.getGeofenceManager() != null) {
+ Context.getGeofenceManager().refresh();
+ }
return Response.noContent().build();
}
diff --git a/src/org/traccar/api/resource/GroupPermissionResource.java b/src/org/traccar/api/resource/GroupPermissionResource.java
index 564a379d2..1fbf37a2b 100644
--- a/src/org/traccar/api/resource/GroupPermissionResource.java
+++ b/src/org/traccar/api/resource/GroupPermissionResource.java
@@ -38,7 +38,9 @@ public class GroupPermissionResource extends BaseResource {
Context.getPermissionsManager().checkAdmin(getUserId());
Context.getDataManager().linkGroup(entity.getUserId(), entity.getGroupId());
Context.getPermissionsManager().refresh();
- Context.getGeofenceManager().refresh();
+ if (Context.getGeofenceManager() != null) {
+ Context.getGeofenceManager().refresh();
+ }
return Response.ok(entity).build();
}
@@ -47,7 +49,9 @@ public class GroupPermissionResource extends BaseResource {
Context.getPermissionsManager().checkAdmin(getUserId());
Context.getDataManager().unlinkGroup(entity.getUserId(), entity.getGroupId());
Context.getPermissionsManager().refresh();
- Context.getGeofenceManager().refresh();
+ if (Context.getGeofenceManager() != null) {
+ Context.getGeofenceManager().refresh();
+ }
return Response.noContent().build();
}
diff --git a/src/org/traccar/api/resource/GroupResource.java b/src/org/traccar/api/resource/GroupResource.java
index dda9ab03b..957f39ebe 100644
--- a/src/org/traccar/api/resource/GroupResource.java
+++ b/src/org/traccar/api/resource/GroupResource.java
@@ -59,7 +59,9 @@ public class GroupResource extends BaseResource {
Context.getDataManager().addGroup(entity);
Context.getDataManager().linkGroup(getUserId(), entity.getId());
Context.getPermissionsManager().refresh();
- Context.getGeofenceManager().refresh();
+ if (Context.getGeofenceManager() != null) {
+ Context.getGeofenceManager().refresh();
+ }
return Response.ok(entity).build();
}
@@ -69,6 +71,9 @@ public class GroupResource extends BaseResource {
Context.getPermissionsManager().checkReadonly(getUserId());
Context.getPermissionsManager().checkGroup(getUserId(), id);
Context.getDataManager().updateGroup(entity);
+ if (Context.getGeofenceManager() != null) {
+ Context.getGeofenceManager().refresh();
+ }
return Response.ok(entity).build();
}
@@ -79,7 +84,9 @@ public class GroupResource extends BaseResource {
Context.getPermissionsManager().checkGroup(getUserId(), id);
Context.getDataManager().removeGroup(id);
Context.getPermissionsManager().refresh();
- Context.getGeofenceManager().refresh();
+ if (Context.getGeofenceManager() != null) {
+ Context.getGeofenceManager().refresh();
+ }
return Response.noContent().build();
}
diff --git a/src/org/traccar/api/resource/NotificationResource.java b/src/org/traccar/api/resource/NotificationResource.java
new file mode 100644
index 000000000..5bec7fd85
--- /dev/null
+++ b/src/org/traccar/api/resource/NotificationResource.java
@@ -0,0 +1,59 @@
+/*
+ * Copyright 2016 Anton Tananaev (anton.tananaev@gmail.com)
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.traccar.api.resource;
+
+import java.sql.SQLException;
+import java.util.Collection;
+
+import javax.ws.rs.Consumes;
+import javax.ws.rs.GET;
+import javax.ws.rs.POST;
+import javax.ws.rs.Path;
+import javax.ws.rs.Produces;
+import javax.ws.rs.QueryParam;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.Response;
+
+import org.traccar.Context;
+import org.traccar.api.BaseResource;
+import org.traccar.model.Notification;
+
+@Path("users/notifications")
+@Produces(MediaType.APPLICATION_JSON)
+@Consumes(MediaType.APPLICATION_JSON)
+public class NotificationResource extends BaseResource {
+
+ @GET
+ public Collection<Notification> get(@QueryParam("all") boolean all,
+ @QueryParam("userId") long userId) throws SQLException {
+ if (all) {
+ return Context.getNotificationManager().getAllNotifications();
+ }
+ if (userId == 0) {
+ userId = getUserId();
+ }
+ Context.getPermissionsManager().checkUser(getUserId(), userId);
+ return Context.getNotificationManager().getUserNotifications(userId);
+ }
+
+ @POST
+ public Response update(Notification entity) throws SQLException {
+ Context.getPermissionsManager().checkReadonly(getUserId());
+ Context.getPermissionsManager().checkUser(getUserId(), entity.getUserId());
+ Context.getNotificationManager().updateNotification(entity);
+ return Response.ok(entity).build();
+ }
+}
diff --git a/src/org/traccar/api/resource/UserResource.java b/src/org/traccar/api/resource/UserResource.java
index 7cc4ed09a..cfe338b56 100644
--- a/src/org/traccar/api/resource/UserResource.java
+++ b/src/org/traccar/api/resource/UserResource.java
@@ -52,7 +52,12 @@ public class UserResource extends BaseResource {
}
Context.getDataManager().addUser(entity);
Context.getPermissionsManager().refresh();
- Context.getGeofenceManager().refresh();
+ if (Context.getGeofenceManager() != null) {
+ Context.getGeofenceManager().refresh();
+ }
+ if (Context.getNotificationManager() != null) {
+ Context.getNotificationManager().refresh();
+ }
return Response.ok(entity).build();
}
@@ -66,7 +71,12 @@ public class UserResource extends BaseResource {
}
Context.getDataManager().updateUser(entity);
Context.getPermissionsManager().refresh();
- Context.getGeofenceManager().refresh();
+ if (Context.getGeofenceManager() != null) {
+ Context.getGeofenceManager().refresh();
+ }
+ if (Context.getNotificationManager() != null) {
+ Context.getNotificationManager().refresh();
+ }
return Response.ok(entity).build();
}
@@ -76,7 +86,12 @@ public class UserResource extends BaseResource {
Context.getPermissionsManager().checkUser(getUserId(), id);
Context.getDataManager().removeUser(id);
Context.getPermissionsManager().refresh();
- Context.getGeofenceManager().refresh();
+ if (Context.getGeofenceManager() != null) {
+ Context.getGeofenceManager().refresh();
+ }
+ if (Context.getNotificationManager() != null) {
+ Context.getNotificationManager().refresh();
+ }
return Response.noContent().build();
}
diff --git a/src/org/traccar/database/DataManager.java b/src/org/traccar/database/DataManager.java
index 860bf1b84..031703023 100644
--- a/src/org/traccar/database/DataManager.java
+++ b/src/org/traccar/database/DataManager.java
@@ -52,6 +52,7 @@ import org.traccar.model.Geofence;
import org.traccar.model.Group;
import org.traccar.model.GroupGeofence;
import org.traccar.model.GroupPermission;
+import org.traccar.model.Notification;
import org.traccar.model.Position;
import org.traccar.model.Server;
import org.traccar.model.User;
@@ -644,4 +645,27 @@ public class DataManager implements IdentityManager {
.setLong("geofenceId", geofenceId)
.executeUpdate();
}
+
+ public Collection<Notification> getNotifications() throws SQLException {
+ return QueryBuilder.create(dataSource, getQuery("database.selectNotifications"))
+ .executeQuery(Notification.class);
+ }
+
+ public void addNotification(Notification notification) throws SQLException {
+ notification.setId(QueryBuilder.create(dataSource, getQuery("database.insertNotification"), true)
+ .setObject(notification)
+ .executeUpdate());
+ }
+
+ public void updateNotification(Notification notification) throws SQLException {
+ QueryBuilder.create(dataSource, getQuery("database.updateNotification"))
+ .setObject(notification)
+ .executeUpdate();
+ }
+
+ public void removeNotification(Notification notification) throws SQLException {
+ QueryBuilder.create(dataSource, getQuery("database.deleteNotification"))
+ .setLong("id", notification.getId())
+ .executeUpdate();
+ }
}
diff --git a/src/org/traccar/database/NotificationManager.java b/src/org/traccar/database/NotificationManager.java
index ab6b2230a..ae91d81cc 100644
--- a/src/org/traccar/database/NotificationManager.java
+++ b/src/org/traccar/database/NotificationManager.java
@@ -15,21 +15,35 @@
*/
package org.traccar.database;
+import java.lang.reflect.Field;
+import java.lang.reflect.Modifier;
import java.sql.SQLException;
import java.util.Collection;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
import java.util.Set;
+import java.util.concurrent.locks.ReadWriteLock;
+import java.util.concurrent.locks.ReentrantReadWriteLock;
import org.traccar.Context;
import org.traccar.helper.Log;
import org.traccar.model.Event;
+import org.traccar.model.Notification;
import org.traccar.model.Position;
+import org.traccar.notification.NotificationMail;
public class NotificationManager {
private final DataManager dataManager;
+ private final Map<Long, Set<Notification>> userNotifications = new HashMap<>();
+
+ private final ReadWriteLock notificationsLock = new ReentrantReadWriteLock();
+
public NotificationManager(DataManager dataManager) {
this.dataManager = dataManager;
+ refresh();
}
public void updateEvent(Event event, Position position) {
@@ -43,7 +57,15 @@ public class NotificationManager {
for (Long userId : users) {
if (event.getGeofenceId() == 0 || Context.getGeofenceManager() != null
&& Context.getGeofenceManager().checkGeofence(userId, event.getGeofenceId())) {
- Context.getConnectionManager().updateEvent(userId, event, position);
+ Notification notification = getUserNotificationByType(userId, event.getType());
+ if (notification != null) {
+ if (notification.getAttributes().containsKey("web")) {
+ Context.getConnectionManager().updateEvent(userId, event, position);
+ }
+ if (notification.getAttributes().containsKey("mail")) {
+ NotificationMail.sendMailAsync(userId, event, position);
+ }
+ }
}
}
}
@@ -54,4 +76,119 @@ public class NotificationManager {
updateEvent(event, position);
}
}
+
+ private Set<Notification> getUserNotificationsUnsafe(long userId) {
+ if (!userNotifications.containsKey(userId)) {
+ userNotifications.put(userId, new HashSet<Notification>());
+ }
+ return userNotifications.get(userId);
+ }
+
+ public Set<Notification> getUserNotifications(long userId) {
+ notificationsLock.readLock().lock();
+ try {
+ return getUserNotificationsUnsafe(userId);
+ } finally {
+ notificationsLock.readLock().unlock();
+ }
+ }
+
+ public final void refresh() {
+ if (dataManager != null) {
+ try {
+ notificationsLock.writeLock().lock();
+ try {
+ userNotifications.clear();
+ for (Notification notification : dataManager.getNotifications()) {
+ getUserNotificationsUnsafe(notification.getUserId()).add(notification);
+ }
+ } finally {
+ notificationsLock.writeLock().unlock();
+ }
+ } catch (SQLException error) {
+ Log.warning(error);
+ }
+ }
+ }
+
+ public Notification getUserNotificationByType(long userId, String type) {
+ notificationsLock.readLock().lock();
+ try {
+ for (Notification notification : getUserNotificationsUnsafe(userId)) {
+ if (notification.getType().equals(type)) {
+ return notification;
+ }
+ }
+ } finally {
+ notificationsLock.readLock().unlock();
+ }
+ return null;
+ }
+
+ public void updateNotification(Notification notification) {
+ Notification cachedNotification = getUserNotificationByType(notification.getUserId(), notification.getType());
+ if (cachedNotification != null) {
+ if (!cachedNotification.getAttributes().equals(notification.getAttributes())) {
+ if (notification.getAttributes().isEmpty()) {
+ try {
+ dataManager.removeNotification(cachedNotification);
+ } catch (SQLException error) {
+ Log.warning(error);
+ }
+ notificationsLock.writeLock().lock();
+ try {
+ getUserNotificationsUnsafe(notification.getUserId()).remove(cachedNotification);
+ } finally {
+ notificationsLock.writeLock().unlock();
+ }
+ } else {
+ notificationsLock.writeLock().lock();
+ try {
+ cachedNotification.setAttributes(notification.getAttributes());
+ } finally {
+ notificationsLock.writeLock().unlock();
+ }
+ try {
+ dataManager.updateNotification(cachedNotification);
+ } catch (SQLException error) {
+ Log.warning(error);
+ }
+ }
+ } else {
+ notification.setId(cachedNotification.getId());
+ }
+ } else if (!notification.getAttributes().isEmpty()) {
+ try {
+ dataManager.addNotification(notification);
+ } catch (SQLException error) {
+ Log.warning(error);
+ }
+ notificationsLock.writeLock().lock();
+ try {
+ getUserNotificationsUnsafe(notification.getUserId()).add(notification);
+ } finally {
+ notificationsLock.writeLock().unlock();
+ }
+ }
+ }
+
+ public Set<Notification> getAllNotifications() {
+
+ Set<Notification> notifications = new HashSet<>();
+ long id = 0;
+ Field[] fields = Event.class.getDeclaredFields();
+ for (Field field : fields) {
+ if (Modifier.isStatic(field.getModifiers()) && field.getName().startsWith("TYPE_")) {
+ try {
+ Notification notification = new Notification();
+ notification.setType(field.get(null).toString());
+ notification.setId(id++);
+ notifications.add(notification);
+ } catch (IllegalArgumentException | IllegalAccessException error) {
+ Log.warning(error);
+ }
+ }
+ }
+ return notifications;
+ }
}
diff --git a/src/org/traccar/model/Notification.java b/src/org/traccar/model/Notification.java
new file mode 100644
index 000000000..0664d6a00
--- /dev/null
+++ b/src/org/traccar/model/Notification.java
@@ -0,0 +1,40 @@
+/*
+ * Copyright 2016 Anton Tananaev (anton.tananaev@gmail.com)
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.traccar.model;
+
+public class Notification extends Extensible {
+
+ private long userId;
+
+ public long getUserId() {
+ return userId;
+ }
+
+ public void setUserId(long userId) {
+ this.userId = userId;
+ }
+
+ private String type;
+
+ public String getType() {
+ return type;
+ }
+
+ public void setType(String type) {
+ this.type = type;
+ }
+
+}
diff --git a/src/org/traccar/model/User.java b/src/org/traccar/model/User.java
index 287de121c..aa73cfcff 100644
--- a/src/org/traccar/model/User.java
+++ b/src/org/traccar/model/User.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2013 - 2015 Anton Tananaev (anton.tananaev@gmail.com)
+ * Copyright 2013 - 2016 Anton Tananaev (anton.tananaev@gmail.com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -18,17 +18,7 @@ package org.traccar.model;
import com.fasterxml.jackson.annotation.JsonIgnore;
import org.traccar.helper.Hashing;
-public class User {
-
- private long id;
-
- public long getId() {
- return id;
- }
-
- public void setId(long id) {
- this.id = id;
- }
+public class User extends Extensible {
private String name;
diff --git a/src/org/traccar/notification/NotificationFormatter.java b/src/org/traccar/notification/NotificationFormatter.java
new file mode 100644
index 000000000..37bd7848c
--- /dev/null
+++ b/src/org/traccar/notification/NotificationFormatter.java
@@ -0,0 +1,158 @@
+/*
+ * Copyright 2016 Anton Tananaev (anton.tananaev@gmail.com)
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.traccar.notification;
+
+import java.util.Formatter;
+import java.util.Locale;
+
+import org.traccar.Context;
+import org.traccar.model.Device;
+import org.traccar.model.Event;
+import org.traccar.model.Position;
+
+public final class NotificationFormatter {
+
+ private NotificationFormatter() {
+ }
+
+ public static final String TITLE_TEMPLATE_TYPE_COMMAND_RESULT = "%1$s: command result received";
+ public static final String MESSAGE_TEMPLATE_TYPE_COMMAND_RESULT = "Device: %1$s%n"
+ + "Result: %3$s%n"
+ + "Time: %2$tc%n";
+
+ public static final String TITLE_TEMPLATE_TYPE_DEVICE_ONLINE = "%1$s: online";
+ public static final String MESSAGE_TEMPLATE_TYPE_DEVICE_ONLINE = "Device: %1$s%n"
+ + "Online%n"
+ + "Time: %2$tc%n";
+ public static final String TITLE_TEMPLATE_TYPE_DEVICE_OFFLINE = "%1$s: offline";
+ public static final String MESSAGE_TEMPLATE_TYPE_DEVICE_OFFLINE = "Device: %1$s%n"
+ + "Offline%n"
+ + "Time: %2$tc%n";
+
+ public static final String TITLE_TEMPLATE_TYPE_DEVICE_MOVING = "%1$s: moving";
+ public static final String MESSAGE_TEMPLATE_TYPE_DEVICE_MOVING = "Device: %1$s%n"
+ + "Moving%n"
+ + "Point: http://www.openstreetmap.org/?mlat=%3$f&mlon=%4$f#map=16/%3$f/%4$f%n"
+ + "Time: %2$tc%n";
+ public static final String TITLE_TEMPLATE_TYPE_DEVICE_STOPPED = "%1$s: stopped";
+ public static final String MESSAGE_TEMPLATE_TYPE_DEVICE_STOPPED = "Device: %1$s%n"
+ + "Stopped%n"
+ + "Point: http://www.openstreetmap.org/?mlat=%3$f&mlon=%4$f#map=16/%3$f/%4$f%n"
+ + "Time: %2$tc%n";
+
+ public static final String TITLE_TEMPLATE_TYPE_DEVICE_OVERSPEED = "%1$s: exeeds the speed";
+ public static final String MESSAGE_TEMPLATE_TYPE_DEVICE_OVERSPEED = "Device: %1$s%n"
+ + "Exeeds the speed: %5$f%n"
+ + "Point: http://www.openstreetmap.org/?mlat=%3$f&mlon=%4$f#map=16/%3$f/%4$f%n"
+ + "Time: %2$tc%n";
+
+ public static final String TITLE_TEMPLATE_TYPE_GEOFENCE_ENTER = "%1$s: has entered geofence";
+ public static final String MESSAGE_TEMPLATE_TYPE_GEOFENCE_ENTER = "Device: %1$s%n"
+ + "Has entered geofence: %5$s%n"
+ + "Point: http://www.openstreetmap.org/?mlat=%3$f&mlon=%4$f#map=16/%3$f/%4$f%n"
+ + "Time: %2$tc%n";
+ public static final String TITLE_TEMPLATE_TYPE_GEOFENCE_EXIT = "%1$s: has exited geofence";
+ public static final String MESSAGE_TEMPLATE_TYPE_GEOFENCE_EXIT = "Device: %1$s%n"
+ + "Has exited geofence: %5$s%n"
+ + "Point: http://www.openstreetmap.org/?mlat=%3$f&mlon=%4$f#map=16/%3$f/%4$f%n"
+ + "Time: %2$tc%n";
+
+ public static String formatTitle(long userId, Event event, Position position) {
+ Device device = Context.getIdentityManager().getDeviceById(event.getDeviceId());
+ StringBuilder stringBuilder = new StringBuilder();
+ Formatter formatter = new Formatter(stringBuilder, Locale.getDefault());
+
+ switch (event.getType()) {
+ case Event.TYPE_COMMAND_RESULT:
+ formatter.format(TITLE_TEMPLATE_TYPE_COMMAND_RESULT, device.getName());
+ break;
+ case Event.TYPE_DEVICE_ONLINE:
+ formatter.format(TITLE_TEMPLATE_TYPE_DEVICE_ONLINE, device.getName());
+ break;
+ case Event.TYPE_DEVICE_OFFLINE:
+ formatter.format(TITLE_TEMPLATE_TYPE_DEVICE_OFFLINE, device.getName());
+ break;
+ case Event.TYPE_DEVICE_MOVING:
+ formatter.format(TITLE_TEMPLATE_TYPE_DEVICE_MOVING, device.getName());
+ break;
+ case Event.TYPE_DEVICE_STOPPED:
+ formatter.format(TITLE_TEMPLATE_TYPE_DEVICE_STOPPED, device.getName());
+ break;
+ case Event.TYPE_DEVICE_OVERSPEED:
+ formatter.format(TITLE_TEMPLATE_TYPE_DEVICE_OVERSPEED, device.getName());
+ break;
+ case Event.TYPE_GEOFENCE_ENTER:
+ formatter.format(TITLE_TEMPLATE_TYPE_GEOFENCE_ENTER, device.getName());
+ break;
+ case Event.TYPE_GEOFENCE_EXIT:
+ formatter.format(TITLE_TEMPLATE_TYPE_GEOFENCE_EXIT, device.getName());
+ break;
+ default:
+ formatter.format("Unknown type");
+ break;
+ }
+ String result = formatter.toString();
+ formatter.close();
+ return result;
+ }
+
+ public static String formatMessage(long userId, Event event, Position position) {
+ Device device = Context.getIdentityManager().getDeviceById(event.getDeviceId());
+ StringBuilder stringBuilder = new StringBuilder();
+ Formatter formatter = new Formatter(stringBuilder, Locale.getDefault());
+
+ switch (event.getType()) {
+ case Event.TYPE_COMMAND_RESULT:
+ formatter.format(MESSAGE_TEMPLATE_TYPE_COMMAND_RESULT, device.getName(), event.getServerTime(),
+ position.getAttributes().get("result"));
+ break;
+ case Event.TYPE_DEVICE_ONLINE:
+ formatter.format(MESSAGE_TEMPLATE_TYPE_DEVICE_ONLINE, device.getName(), event.getServerTime());
+ break;
+ case Event.TYPE_DEVICE_OFFLINE:
+ formatter.format(MESSAGE_TEMPLATE_TYPE_DEVICE_OFFLINE, device.getName(), event.getServerTime());
+ break;
+ case Event.TYPE_DEVICE_MOVING:
+ formatter.format(MESSAGE_TEMPLATE_TYPE_DEVICE_MOVING, device.getName(), position.getFixTime(),
+ position.getLatitude(), position.getLongitude());
+ break;
+ case Event.TYPE_DEVICE_STOPPED:
+ formatter.format(MESSAGE_TEMPLATE_TYPE_DEVICE_STOPPED, device.getName(), position.getFixTime(),
+ position.getLatitude(), position.getLongitude());
+ break;
+ case Event.TYPE_DEVICE_OVERSPEED:
+ formatter.format(MESSAGE_TEMPLATE_TYPE_DEVICE_OVERSPEED, device.getName(), position.getFixTime(),
+ position.getLatitude(), position.getLongitude(), position.getSpeed());
+ break;
+ case Event.TYPE_GEOFENCE_ENTER:
+ formatter.format(MESSAGE_TEMPLATE_TYPE_GEOFENCE_ENTER, device.getName(), position.getFixTime(),
+ position.getLatitude(), position.getLongitude(),
+ Context.getGeofenceManager().getGeofence(event.getGeofenceId()).getName());
+ break;
+ case Event.TYPE_GEOFENCE_EXIT:
+ formatter.format(MESSAGE_TEMPLATE_TYPE_GEOFENCE_EXIT, device.getName(), position.getFixTime(),
+ position.getLatitude(), position.getLongitude(),
+ Context.getGeofenceManager().getGeofence(event.getGeofenceId()).getName());
+ break;
+ default:
+ formatter.format("Unknown type");
+ break;
+ }
+ String result = formatter.toString();
+ formatter.close();
+ return result;
+ }
+}
diff --git a/src/org/traccar/notification/NotificationMail.java b/src/org/traccar/notification/NotificationMail.java
new file mode 100644
index 000000000..443018221
--- /dev/null
+++ b/src/org/traccar/notification/NotificationMail.java
@@ -0,0 +1,144 @@
+/*
+ * Copyright 2016 Anton Tananaev (anton.tananaev@gmail.com)
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.traccar.notification;
+
+import java.sql.SQLException;
+import java.util.Properties;
+
+import javax.mail.Message;
+import javax.mail.MessagingException;
+import javax.mail.Session;
+import javax.mail.Transport;
+import javax.mail.internet.InternetAddress;
+import javax.mail.internet.MimeMessage;
+
+import org.traccar.Config;
+import org.traccar.Context;
+import org.traccar.database.DataManager;
+import org.traccar.helper.Log;
+import org.traccar.model.Event;
+import org.traccar.model.Extensible;
+import org.traccar.model.Position;
+import org.traccar.model.User;
+
+public final class NotificationMail {
+
+ private NotificationMail() {
+ }
+
+ private static Properties getConfigProperies() {
+ Config config = Context.getConfig();
+ Properties result = new Properties();
+ String host = config.getString("mail.smtp.host", null);
+ if (host != null) {
+ result.put("mail.smtp.host", host);
+ result.put("mail.smtp.port", config.getString("mail.smtp.port", "25"));
+
+ if (config.getBoolean("mail.smtp.starttls.enable")) {
+ result.put("mail.smtp.starttls.enable",
+ config.getBoolean("mail.smtp.starttls.enable"));
+ } else if (config.getBoolean("mail.smtp.ssl.enable")) {
+ result.put("mail.smtp.socketFactory.port",
+ result.getProperty("mail.smtp.port"));
+ result.put("mail.smtp.socketFactory.class",
+ "javax.net.ssl.SSLSocketFactory");
+ }
+
+ result.put("mail.smtp.auth", config.getBoolean("mail.smtp.auth"));
+ result.put("mail.smtp.user", config.getString("mail.smtp.username", null));
+ result.put("mail.smtp.password", config.getString("mail.smtp.password", null));
+ result.put("mail.smtp.from", config.getString("mail.smtp.from", null));
+ }
+ return result;
+ }
+
+ private static Properties getAttributesProperties(Extensible object) {
+ Properties result = new Properties();
+
+ if (object.getAttributes().containsKey("mail.smtp.host")) {
+ result.put("mail.smtp.host", object.getAttributes().get("mail.smtp.host"));
+ String port = (String) object.getAttributes().get("mail.smtp.port");
+ result.put("mail.smtp.port", (port != null) ? port : "25");
+ if (object.getAttributes().containsKey("mail.smtp.starttls.enable")) {
+ boolean tls = Boolean.parseBoolean((String) object.getAttributes().get("mail.smtp.starttls.enable"));
+ result.put("mail.smtp.starttls.enable", tls);
+ } else if (object.getAttributes().containsKey("mail.smtp.ssl.enable")) {
+ boolean ssl = Boolean.parseBoolean((String) object.getAttributes().get("mail.smtp.ssl.enable"));
+ if (ssl) {
+ result.put("mail.smtp.socketFactory.port",
+ result.getProperty("mail.smtp.port"));
+ result.put("mail.smtp.socketFactory.class",
+ "javax.net.ssl.SSLSocketFactory");
+ }
+ }
+ boolean auth = Boolean.parseBoolean((String) object.getAttributes().get("mail.smtp.auth"));
+ result.put("mail.smtp.auth", auth);
+
+ result.put("mail.smtp.username", object.getAttributes().get("mail.smtp.username"));
+ result.put("mail.smtp.password", object.getAttributes().get("mail.smtp.password"));
+ result.put("mail.smtp.from", object.getAttributes().get("mail.smtp.from"));
+ }
+ return result;
+ }
+
+ public static void sendMailSync(long userId, Event event, Position position) {
+ DataManager dataManager = Context.getDataManager();
+
+ Properties mailServerProperties;
+ Session mailSession;
+ MimeMessage mailMessage;
+
+ try {
+ User user = dataManager.getUser(userId);
+
+ mailServerProperties = getConfigProperies();
+ if (!mailServerProperties.containsKey("mail.smtp.host")) {
+ mailServerProperties = getAttributesProperties(user);
+ if (!mailServerProperties.containsKey("mail.smtp.host")) {
+ return;
+ }
+ }
+ mailSession = Session.getDefaultInstance(mailServerProperties, null);
+
+ mailMessage = new MimeMessage(mailSession);
+
+ mailMessage.addRecipient(Message.RecipientType.TO, new InternetAddress(
+ Context.getDataManager().getUser(userId).getEmail()));
+ mailMessage.setSubject(NotificationFormatter.formatTitle(userId, event, position));
+ mailMessage.setText(NotificationFormatter.formatMessage(userId, event, position));
+
+ Transport transport = mailSession.getTransport("smtp");
+ transport.connect(mailServerProperties.getProperty("mail.smtp.host"),
+ mailServerProperties.getProperty("mail.smtp.username"),
+ mailServerProperties.getProperty("mail.smtp.password"));
+ transport.sendMessage(mailMessage, mailMessage.getAllRecipients());
+ transport.close();
+
+ } catch (MessagingException | SQLException error) {
+ Log.warning(error);
+ }
+ }
+
+ public static void sendMailAsync(final long userId, final Event event, final Position position) {
+ Runnable runnableSend = new Runnable() {
+ public void run() {
+ sendMailSync(userId, event, position);
+ }
+ };
+
+ new Thread(runnableSend).start();
+ }
+}
diff --git a/src/org/traccar/web/WebServer.java b/src/org/traccar/web/WebServer.java
index c06ee5d35..5527e80f3 100644
--- a/src/org/traccar/web/WebServer.java
+++ b/src/org/traccar/web/WebServer.java
@@ -41,6 +41,7 @@ import org.traccar.api.resource.SessionResource;
import org.traccar.api.resource.DevicePermissionResource;
import org.traccar.api.resource.UserResource;
import org.traccar.api.resource.GroupResource;
+import org.traccar.api.resource.NotificationResource;
import org.traccar.api.resource.DeviceResource;
import org.traccar.api.resource.PositionResource;
import org.traccar.api.resource.CommandTypeResource;
@@ -155,7 +156,8 @@ public class WebServer {
GroupPermissionResource.class, DevicePermissionResource.class, UserResource.class,
GroupResource.class, DeviceResource.class, PositionResource.class,
CommandTypeResource.class, EventResource.class, GeofenceResource.class,
- DeviceGeofenceResource.class, GeofencePermissionResource.class, GroupGeofenceResource.class);
+ DeviceGeofenceResource.class, GeofencePermissionResource.class, GroupGeofenceResource.class,
+ NotificationResource.class);
servletHandler.addServlet(new ServletHolder(new ServletContainer(resourceConfig)), "/*");
handlers.addHandler(servletHandler);