aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnton Tananaev <anton.tananaev@gmail.com>2016-06-28 14:09:44 +1200
committerGitHub <noreply@github.com>2016-06-28 14:09:44 +1200
commit7327681f0bd2338f9c8ee3ea1b4a19dcc5f0de48 (patch)
tree0ea99ab937fbae96b35cc5dc62edbc74594af9e4
parent67f46c80d3b5e34440a2644f52b81dddfbaba5fa (diff)
parent61b2486353c742afe6214f0a0c5c2e9956ea3b97 (diff)
downloadtrackermap-server-7327681f0bd2338f9c8ee3ea1b4a19dcc5f0de48.tar.gz
trackermap-server-7327681f0bd2338f9c8ee3ea1b4a19dcc5f0de48.tar.bz2
trackermap-server-7327681f0bd2338f9c8ee3ea1b4a19dcc5f0de48.zip
Merge pull request #2052 from Abyss777/master
Notifications via email
-rw-r--r--checkstyle.xml1
-rw-r--r--debug.xml36
-rw-r--r--pom.xml6
-rw-r--r--schema/changelog-3.6.xml23
-rw-r--r--setup/unix/traccar.xml21
-rw-r--r--setup/windows/traccar.xml21
-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
-rw-r--r--web/app/Application.js7
-rw-r--r--web/app/controller/Root.js2
-rw-r--r--web/app/model/Notification.js33
-rw-r--r--web/app/model/User.js2
-rw-r--r--web/app/store/AllNotifications.js30
-rw-r--r--web/app/store/Notifications.js25
-rw-r--r--web/app/view/Notifications.js68
-rw-r--r--web/app/view/NotificationsController.js79
-rw-r--r--web/app/view/SettingsMenu.js3
-rw-r--r--web/app/view/SettingsMenuController.js13
-rw-r--r--web/app/view/Users.js5
-rw-r--r--web/app/view/UsersController.js14
-rw-r--r--web/l10n/en.json8
32 files changed, 1007 insertions, 31 deletions
diff --git a/checkstyle.xml b/checkstyle.xml
index 14cfd1d13..9d0314b06 100644
--- a/checkstyle.xml
+++ b/checkstyle.xml
@@ -98,7 +98,6 @@
<!-- Checks for common coding problems -->
<!-- See http://checkstyle.sf.net/config_coding.html -->
- <module name="AvoidInlineConditionals"/>
<module name="EmptyStatement"/>
<module name="EqualsHashCode"/>
<module name="IllegalInstantiation"/>
diff --git a/debug.xml b/debug.xml
index bf8f8e2b5..039f5a7a9 100644
--- a/debug.xml
+++ b/debug.xml
@@ -52,6 +52,21 @@
<entry key='event.geofenceHandler'>true</entry>
+
+ <!--<entry key='mail.smtp.host'>smtp.example.com</entry>
+ for STARTTLS
+ <entry key='mail.smtp.port'>587</entry>
+ <entry key='mail.smtp.starttls.enable'>true</entry>
+ for SSL
+ <entry key='mail.smtp.port'>465</entry>
+ <entry key='mail.smtp.ssl.enable'>true</entry>
+
+ <entry key='mail.smtp.from'>traccar@example.com</entry>
+
+ <entry key='mail.smtp.auth'>true</entry>
+ <entry key='mail.smtp.username'>traccar@example.com</entry>
+ <entry key='mail.smtp.password'>password</entry>-->
+
<!-- DATABASE CONFIG -->
<!--<entry key='database.driverFile'>hsqldb.jar</entry>-->
@@ -280,6 +295,27 @@
DELETE FROM device_geofence WHERE deviceId = :deviceId AND geofenceId = :geofenceId;
</entry>
+ <entry key='database.selectNotifications'>
+ SELECT * FROM notifications;
+ </entry>
+
+ <entry key='database.insertNotification'>
+ INSERT INTO notifications (userId, type, attributes)
+ VALUES (:userId, :type, :attributes);
+ </entry>
+
+ <entry key='database.updateNotification'>
+ UPDATE notifications SET
+ userId = :userId,
+ type = :type,
+ attributes = :attributes
+ WHERE id = :id;
+ </entry>
+
+ <entry key='database.deleteNotification'>
+ DELETE FROM notifications WHERE id = :id;
+ </entry>
+
<!-- PROTOCOL CONFIG -->
<entry key='gps103.port'>5001</entry>
diff --git a/pom.xml b/pom.xml
index eb1ac20de..0df84a4aa 100644
--- a/pom.xml
+++ b/pom.xml
@@ -112,6 +112,12 @@
<artifactId>liquibase-core</artifactId>
<version>3.4.2</version>
</dependency>
+ <dependency>
+ <groupId>javax.mail</groupId>
+ <artifactId>mail</artifactId>
+ <version>1.4.7</version>
+ </dependency>
+
</dependencies>
<build>
diff --git a/schema/changelog-3.6.xml b/schema/changelog-3.6.xml
index 13d4cae60..a17b68e47 100644
--- a/schema/changelog-3.6.xml
+++ b/schema/changelog-3.6.xml
@@ -90,5 +90,28 @@
<addForeignKeyConstraint baseTableName="device_geofence" baseColumnNames="deviceid" constraintName="fk_user_device_geofence_deviceid" referencedTableName="devices" referencedColumnNames="id" onDelete="CASCADE" />
<addForeignKeyConstraint baseTableName="device_geofence" baseColumnNames="geofenceid" constraintName="fk_user_device_geofence_geofenceid" referencedTableName="geofences" referencedColumnNames="id" onDelete="CASCADE" />
+ <createTable tableName="notifications">
+ <column name="id" type="INT" autoIncrement="true">
+ <constraints primaryKey="true" />
+ </column>
+ <column name="userid" type="INT">
+ <constraints nullable="false" />
+ </column>
+ <column name="type" type="VARCHAR(128)">
+ <constraints nullable="false" />
+ </column>
+ <column name="attributes" type="VARCHAR(4096)">
+ <constraints nullable="false" />
+ </column>
+ </createTable>
+
+ <addForeignKeyConstraint baseTableName="notifications" baseColumnNames="userid" constraintName="fk_notifications_userid" referencedTableName="users" referencedColumnNames="id" onDelete="CASCADE" />
+
+ <addColumn tableName="users">
+ <column name="attributes" type="VARCHAR(4096)" />
+ </addColumn>
+
+ <addNotNullConstraint tableName="users" columnName="attributes" columnDataType="VARCHAR(4096)" defaultNullValue="{}" />
+
</changeSet>
</databaseChangeLog>
diff --git a/setup/unix/traccar.xml b/setup/unix/traccar.xml
index b75376434..c5759f87f 100644
--- a/setup/unix/traccar.xml
+++ b/setup/unix/traccar.xml
@@ -252,6 +252,27 @@
DELETE FROM device_geofence WHERE deviceId = :deviceId AND geofenceId = :geofenceId;
</entry>
+ <entry key='database.selectNotifications'>
+ SELECT * FROM notifications;
+ </entry>
+
+ <entry key='database.insertNotification'>
+ INSERT INTO notifications (userId, type, attributes)
+ VALUES (:userId, :type, :attributes);
+ </entry>
+
+ <entry key='database.updateNotification'>
+ UPDATE notifications SET
+ userId = :userId,
+ type = :type,
+ attributes = :attributes
+ WHERE id = :id;
+ </entry>
+
+ <entry key='database.deleteNotification'>
+ DELETE FROM notifications WHERE id = :id;
+ </entry>
+
<!-- PROTOCOL CONFIG -->
<entry key='gps103.port'>5001</entry>
diff --git a/setup/windows/traccar.xml b/setup/windows/traccar.xml
index 995b52c0d..1d573f282 100644
--- a/setup/windows/traccar.xml
+++ b/setup/windows/traccar.xml
@@ -252,6 +252,27 @@
DELETE FROM device_geofence WHERE deviceId = :deviceId AND geofenceId = :geofenceId;
</entry>
+ <entry key='database.selectNotifications'>
+ SELECT * FROM notifications;
+ </entry>
+
+ <entry key='database.insertNotification'>
+ INSERT INTO notifications (userId, type, attributes)
+ VALUES (:userId, :type, :attributes);
+ </entry>
+
+ <entry key='database.updateNotification'>
+ UPDATE notifications SET
+ userId = :userId,
+ type = :type,
+ attributes = :attributes
+ WHERE id = :id;
+ </entry>
+
+ <entry key='database.deleteNotification'>
+ DELETE FROM notifications WHERE id = :id;
+ </entry>
+
<!-- PROTOCOL CONFIG -->
<entry key='gps103.port'>5001</entry>
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);
diff --git a/web/app/Application.js b/web/app/Application.js
index dbbc7a594..5911579b8 100644
--- a/web/app/Application.js
+++ b/web/app/Application.js
@@ -32,7 +32,8 @@ Ext.define('Traccar.Application', {
'Attribute',
'Command',
'Event',
- 'Geofence'
+ 'Geofence',
+ 'Notification'
],
stores: [
@@ -52,7 +53,9 @@ Ext.define('Traccar.Application', {
'Languages',
'Events',
'Geofences',
- 'AllGeofences'
+ 'AllGeofences',
+ 'Notifications',
+ 'AllNotifications'
],
controllers: [
diff --git a/web/app/controller/Root.js b/web/app/controller/Root.js
index da3e3a2a9..ba23b90bf 100644
--- a/web/app/controller/Root.js
+++ b/web/app/controller/Root.js
@@ -144,7 +144,7 @@ Ext.define('Traccar.controller.Root', {
break;
}
}
- text = Strings.eventCommandResult + text;
+ text = Strings.eventCommandResult + ": " + text;
} else {
typeKey = 'event' + array[i].type.charAt(0).toUpperCase() + array[i].type.slice(1);
text = Strings[typeKey];
diff --git a/web/app/model/Notification.js b/web/app/model/Notification.js
new file mode 100644
index 000000000..9b4d61eb4
--- /dev/null
+++ b/web/app/model/Notification.js
@@ -0,0 +1,33 @@
+/*
+ * 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.
+ */
+
+Ext.define('Traccar.model.Notification', {
+ extend: 'Ext.data.Model',
+ identifier: 'negative',
+
+ fields: [{
+ name: 'id',
+ type: 'int'
+ }, {
+ name: 'type',
+ type: 'string'
+ }, {
+ name: 'userId',
+ type: 'int'
+ }, {
+ name: 'attributes'
+ }]
+});
diff --git a/web/app/model/User.js b/web/app/model/User.js
index 35b849187..352acc651 100644
--- a/web/app/model/User.js
+++ b/web/app/model/User.js
@@ -54,6 +54,8 @@ Ext.define('Traccar.model.User', {
}, {
name: 'twelveHourFormat',
type: 'boolean'
+ }, {
+ name: 'attributes'
}],
proxy: {
diff --git a/web/app/store/AllNotifications.js b/web/app/store/AllNotifications.js
new file mode 100644
index 000000000..fed02b260
--- /dev/null
+++ b/web/app/store/AllNotifications.js
@@ -0,0 +1,30 @@
+/*
+ * Copyright 2015 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.
+ */
+
+Ext.define('Traccar.store.AllNotifications', {
+ extend: 'Ext.data.Store',
+ model: 'Traccar.model.Notification',
+
+ proxy: {
+ type: 'rest',
+ url: '/api/users/notifications',
+ extraParams: {
+ all: true
+ }
+ },
+ sortOnLoad: true,
+ sorters: { property: 'type', direction : 'ASC' }
+});
diff --git a/web/app/store/Notifications.js b/web/app/store/Notifications.js
new file mode 100644
index 000000000..5dc8cb614
--- /dev/null
+++ b/web/app/store/Notifications.js
@@ -0,0 +1,25 @@
+/*
+ * Copyright 2015 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.
+ */
+
+Ext.define('Traccar.store.Notifications', {
+ extend: 'Ext.data.Store',
+ model: 'Traccar.model.Notification',
+
+ proxy: {
+ type: 'rest',
+ url: '/api/users/notifications'
+ }
+});
diff --git a/web/app/view/Notifications.js b/web/app/view/Notifications.js
new file mode 100644
index 000000000..211e5cd15
--- /dev/null
+++ b/web/app/view/Notifications.js
@@ -0,0 +1,68 @@
+/*
+ * 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.
+ */
+
+Ext.define('Traccar.view.Notifications', {
+ extend: 'Ext.grid.Panel',
+ xtype: 'notificationsView',
+
+ requires: [
+ 'Traccar.view.NotificationsController'
+ ],
+
+ controller: 'notificationsController',
+ store: 'AllNotifications',
+
+ selModel: {
+ selType: 'cellmodel',
+ },
+
+ columns: [{
+ text: Strings.notificationType,
+ dataIndex: 'type',
+ flex: 1,
+ renderer: function (value) {
+ var typeKey = 'event' + value.charAt(0).toUpperCase() + value.slice(1);
+ return Strings[typeKey];
+ }
+ }, {
+ text: Strings.notificationWeb,
+ dataIndex: 'attributes.web',
+ xtype: 'checkcolumn',
+ flex: 1,
+ listeners: {
+ beforeCheckChange: 'onBeforeCheckChange',
+ checkChange: 'onCheckChange'
+ },
+ renderer: function (value, metaData, record) {
+ var fields = this.dataIndex.split('\.',2);
+ return (new Ext.ux.CheckColumn()).renderer(record.get(fields[0])[fields[1]], metaData);
+ }
+ }, {
+ text: Strings.notificationMail,
+ dataIndex: 'attributes.mail',
+ xtype: 'checkcolumn',
+ flex: 1,
+ listeners: {
+ beforeCheckChange: 'onBeforeCheckChange',
+ checkChange: 'onCheckChange'
+ },
+ renderer: function (value, metaData, record) {
+ var fields = this.dataIndex.split('\.',2);
+ return (new Ext.ux.CheckColumn()).renderer(record.get(fields[0])[fields[1]], metaData);
+ }
+ }],
+
+});
diff --git a/web/app/view/NotificationsController.js b/web/app/view/NotificationsController.js
new file mode 100644
index 000000000..a7c4290ca
--- /dev/null
+++ b/web/app/view/NotificationsController.js
@@ -0,0 +1,79 @@
+/*
+ * 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.
+ */
+
+Ext.define('Traccar.view.NotificationsController', {
+ extend: 'Ext.app.ViewController',
+ alias: 'controller.notificationsController',
+
+ init: function () {
+ this.userId = this.getView().user.getData().id;
+ this.getView().getStore().load({
+ scope: this,
+ callback: function (records, operation, success) {
+ var notificationsStore = Ext.create('Traccar.store.Notifications');
+ notificationsStore.load({
+ params: {
+ userId: this.userId
+ },
+ scope: this,
+ callback: function (records, operation, success) {
+ var i, index, attributes, storeRecord;
+ if (success) {
+ for (i = 0; i < records.length; i++) {
+ index = this.getView().getStore().find('type', records[i].getData().type);
+ attributes = records[i].getData().attributes;
+ storeRecord = this.getView().getStore().getAt(index);
+ storeRecord.set('attributes', attributes);
+ storeRecord.commit();
+ }
+ }
+ }
+ });
+ }
+ });
+ },
+
+ onBeforeCheckChange: function (column, rowIndex, checked, eOpts) {
+ var fields = column.dataIndex.split('\.',2);
+ var record = this.getView().getStore().getAt(rowIndex);
+ var data = record.get(fields[0]);
+ if (!data[fields[1]]) {
+ data[fields[1]] = "true";
+ } else {
+ delete data[fields[1]];
+ }
+ record.set(fields[0], data);
+ record.commit();
+ },
+
+ onCheckChange: function (column, rowIndex, checked, eOpts) {
+ var record = this.getView().getStore().getAt(rowIndex);
+ Ext.Ajax.request({
+ scope: this,
+ url: '/api/users/notifications',
+ jsonData: {
+ userId: this.userId,
+ type: record.getData().type,
+ attributes: record.getData().attributes
+ },
+ callback: function (options, success, response) {
+ if (!success) {
+ Traccar.app.showError(response);
+ }
+ }
+ });
+ }
+});
diff --git a/web/app/view/SettingsMenu.js b/web/app/view/SettingsMenu.js
index 704884928..70041bd63 100644
--- a/web/app/view/SettingsMenu.js
+++ b/web/app/view/SettingsMenu.js
@@ -48,6 +48,9 @@ Ext.define('Traccar.view.SettingsMenu', {
handler: 'onUsersClick',
reference: 'settingsUsersButton'
}, {
+ text: Strings.sharedNotifications,
+ handler: 'onNotificationsClick',
+ }, {
text: Strings.loginLogout,
handler: 'onLogoutClick'
}]
diff --git a/web/app/view/SettingsMenuController.js b/web/app/view/SettingsMenuController.js
index 48ae60aa5..45b159ccb 100644
--- a/web/app/view/SettingsMenuController.js
+++ b/web/app/view/SettingsMenuController.js
@@ -25,6 +25,7 @@ Ext.define('Traccar.view.SettingsMenuController', {
'Traccar.view.Users',
'Traccar.view.Groups',
'Traccar.view.Geofences',
+ 'Traccar.view.Notifications',
'Traccar.view.BaseWindow'
],
@@ -77,6 +78,18 @@ Ext.define('Traccar.view.SettingsMenuController', {
}).show();
},
+ onNotificationsClick: function () {
+ var user = Traccar.app.getUser();
+ Ext.create('Traccar.view.BaseWindow', {
+ title: Strings.sharedNotifications,
+ modal: false,
+ items: {
+ xtype: 'notificationsView',
+ user: user
+ }
+ }).show();
+ },
+
onLogoutClick: function () {
Ext.create('Traccar.view.LoginController').logout();
}
diff --git a/web/app/view/Users.js b/web/app/view/Users.js
index b6301b38b..4abfff1ef 100644
--- a/web/app/view/Users.js
+++ b/web/app/view/Users.js
@@ -45,6 +45,11 @@ Ext.define('Traccar.view.Users', {
disabled: true,
handler: 'onGeofencesClick',
reference: 'userGeofencesButton'
+ }, {
+ text: Strings.sharedNotifications,
+ disabled: true,
+ handler: 'onNotificationsClick',
+ reference: 'userNotificationsButton'
}]
},
diff --git a/web/app/view/UsersController.js b/web/app/view/UsersController.js
index acba66b4d..e745ee04c 100644
--- a/web/app/view/UsersController.js
+++ b/web/app/view/UsersController.js
@@ -23,6 +23,7 @@ Ext.define('Traccar.view.UsersController', {
'Traccar.view.UserDevices',
'Traccar.view.UserGroups',
'Traccar.view.UserGeofences',
+ 'Traccar.view.Notifications',
'Traccar.view.BaseWindow'
],
@@ -114,6 +115,18 @@ Ext.define('Traccar.view.UsersController', {
}).show();
},
+ onNotificationsClick: function () {
+ var user = this.getView().getSelectionModel().getSelection()[0];
+ Ext.create('Traccar.view.BaseWindow', {
+ title: Strings.sharedNotifications,
+ modal: false,
+ items: {
+ xtype: 'notificationsView',
+ user: user
+ }
+ }).show();
+ },
+
onSelectionChange: function (selected) {
var disabled = selected.length > 0;
this.lookupReference('toolbarEditButton').setDisabled(disabled);
@@ -121,5 +134,6 @@ Ext.define('Traccar.view.UsersController', {
this.lookupReference('userDevicesButton').setDisabled(disabled);
this.lookupReference('userGroupsButton').setDisabled(disabled);
this.lookupReference('userGeofencesButton').setDisabled(disabled);
+ this.lookupReference('userNotificationsButton').setDisabled(disabled);
}
});
diff --git a/web/l10n/en.json b/web/l10n/en.json
index 8ac09caf2..351ad462d 100644
--- a/web/l10n/en.json
+++ b/web/l10n/en.json
@@ -19,6 +19,7 @@
"sharedSearch": "Search",
"sharedGeofence": "Geofence",
"sharedGeofences": "Geofences",
+ "sharedNotifications": "Notifications",
"errorTitle": "Error",
"errorUnknown": "Unknown error",
"errorConnection": "Connection error",
@@ -106,7 +107,10 @@
"eventDeviceMoving": "Device is moving",
"eventDeviceStopped": "Device is stopped",
"eventDeviceOverspeed": "Device exceeds the speed",
- "eventCommandResult": "Command result: ",
+ "eventCommandResult": "Command result",
"eventGeofenceEnter": "Device has entered geofence",
- "eventGeofenceExit": "Device has exited geofence"
+ "eventGeofenceExit": "Device has exited geofence",
+ "notificationType": "Type of Notification",
+ "notificationWeb": "Send via Web",
+ "notificationMail": "Send via Mail"
} \ No newline at end of file