100% open-source IoT Platform - Integrate your devices, add intelligence, build custom apps
Java asset-managementbuildingdashboarddataplatformdevice-managementedge-computingenergyenergy-managementenergy-monitorfleet-managementfleet-telematicsiotiot-applicationiot-platformmodbusmqttmqtt-brokerota-updaterules-enginesmartcity
Working on AlertNotification resource
Mica Hallin3b41224efa45
diff --git a/manager/server/src/main/java/org/openremote/manager/server/notification/FCMMessage.java b/manager/server/src/main/java/org/openremote/manager/server/notification/FCMMessage.javanew file mode 100644index 000000000..face5f8af--- /dev/null+++ b/manager/server/src/main/java/org/openremote/manager/server/notification/FCMMessage.java@@ -0,0 +1,19 @@+package org.openremote.manager.server.notification;+++public class FCMMessage {++ private String to;++ public FCMMessage(String to) {+ this.to = to;+ }++ public String getTo() {+ return to;+ }++ public void setTo(String to) {+ this.to = to;+ }+}diff --git a/manager/server/src/main/java/org/openremote/manager/server/notification/NotificationResourceImpl.java b/manager/server/src/main/java/org/openremote/manager/server/notification/NotificationResourceImpl.javaindex 17e225fb4..5edf8209a 100644--- a/manager/server/src/main/java/org/openremote/manager/server/notification/NotificationResourceImpl.java+++ b/manager/server/src/main/java/org/openremote/manager/server/notification/NotificationResourceImpl.java@@ -28,15 +28,23 @@ public class NotificationResourceImpl extends WebResource implements Notificatio}@Override- public void storeAlertNotification( AlertNotification alertNotification) {+ public void storeAlertNotification(AlertNotification alertNotification) {if (alertNotification == null) {throw new WebApplicationException("Missing alertNotification", BAD_REQUEST);}- notificationService.storeAlertNotification(getUserId(), alertNotification);+ notificationService.storeAndNotifyAlertNotification(getUserId(), alertNotification);}@Overridepublic List<AlertNotification> getAlertNotification() {return notificationService.getPendingAlertForUserId(getUserId());}++ @Override+ public void removeAlertNotification(Long id) {+ if (id == null) {+ throw new WebApplicationException("Missing alert id", BAD_REQUEST);+ }+ notificationService.removeAlertNotification(id);+ }}diff --git a/manager/server/src/main/java/org/openremote/manager/server/notification/NotificationService.java b/manager/server/src/main/java/org/openremote/manager/server/notification/NotificationService.javaindex 17e8ad7f7..24245bd1d 100644--- a/manager/server/src/main/java/org/openremote/manager/server/notification/NotificationService.java+++ b/manager/server/src/main/java/org/openremote/manager/server/notification/NotificationService.java@@ -19,6 +19,9 @@*/package org.openremote.manager.server.notification;+import org.jboss.resteasy.client.jaxrs.ResteasyClient;+import org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder;+import org.jboss.resteasy.client.jaxrs.ResteasyWebTarget;import org.openremote.container.Container;import org.openremote.container.ContainerService;import org.openremote.container.persistence.PersistenceService;@@ -28,6 +31,9 @@ import org.openremote.manager.shared.notification.DeliveryStatus;import javax.persistence.EntityManager;import javax.persistence.Query;+import javax.ws.rs.client.Entity;+import javax.ws.rs.client.Invocation;+import javax.ws.rs.core.Response;import java.util.List;import java.util.logging.Logger;@@ -37,13 +43,25 @@ public class NotificationService implements ContainerService {protected PersistenceService persistenceService;+ protected ResteasyWebTarget target;++ private String fcmKey;+@Overridepublic void init(Container container) throws Exception {++ fcmKey = container.getConfig().get("FCM_KEY");+ if (fcmKey == null) {+ LOG.severe("FCM_KEY not defined, notification service cannot send fcm notification");+ }this.persistenceService = container.getService(PersistenceService.class);container.getService(WebService.class).getApiSingletons().add(new NotificationResourceImpl(this));++ ResteasyClient client = new ResteasyClientBuilder().build();+ target = client.target("https://fcm.googleapis.com/fcm/send");}@Override@@ -78,14 +96,21 @@ public class NotificationService implements ContainerService {});}- public void storeAlertNotification(String userId, AlertNotification alertNotification) {++ public void storeAndNotifyAlertNotification(String userId, AlertNotification alertNotification) {alertNotification.setUserId(userId);alertNotification.setDeliveryStatus(DeliveryStatus.PENDING);persistenceService.doTransaction((EntityManager entityManager) -> {- AlertNotification notification = entityManager.merge(alertNotification);- List<DeviceNotificationToken> allTokenForUser = findAllTokenForUser(userId);- for (DeviceNotificationToken notificationToken : allTokenForUser) {- //TODO: Send FCM notification+ entityManager.merge(alertNotification);+ if (fcmKey != null) {+ List<DeviceNotificationToken> allTokenForUser = findAllTokenForUser(userId);+ for (DeviceNotificationToken notificationToken : allTokenForUser) {+ Invocation.Builder builder = target.request().header("Authorization", "key=");+ Response response = builder.post(Entity.entity(new FCMMessage(notificationToken.getToken()), "application/json"));+ if (response.getStatus() != 200) {+ LOG.severe("Error send FCM notification status=["+response.getStatus()+"], statusInformation=[" +response.getStatusInfo()+"]");+ }+ }}});@@ -100,4 +125,13 @@ public class NotificationService implements ContainerService {return query.getResultList();});}++ public void removeAlertNotification(Long id) {+ persistenceService.doTransaction(entityManager -> {+ Query query = entityManager.createQuery("UPDATE AlertNotification SET deliveryStatus=:status WHERE id =:id");+ query.setParameter("id", id);+ query.setParameter("status",DeliveryStatus.DELIVERED);+ query.executeUpdate();+ });+ }}\ No newline at end of filediff --git a/manager/shared/src/main/java/org/openremote/manager/shared/notification/AlertNotification.java b/manager/shared/src/main/java/org/openremote/manager/shared/notification/AlertNotification.javaindex 1e773c0a0..c5536cbd9 100644--- a/manager/shared/src/main/java/org/openremote/manager/shared/notification/AlertNotification.java+++ b/manager/shared/src/main/java/org/openremote/manager/shared/notification/AlertNotification.java@@ -24,7 +24,7 @@ public class AlertNotification {@Column(name = "MESSAGE", nullable = false)private String message;- @OneToMany(cascade = CascadeType.ALL)+ @OneToMany(cascade = CascadeType.ALL,fetch = FetchType.EAGER)@JoinColumn(name="ALERT_ID")private Set<AlertAction> actions;diff --git a/manager/shared/src/main/java/org/openremote/manager/shared/notification/NotificationResource.java b/manager/shared/src/main/java/org/openremote/manager/shared/notification/NotificationResource.javaindex 6491f273a..0642ad9dd 100644--- a/manager/shared/src/main/java/org/openremote/manager/shared/notification/NotificationResource.java+++ b/manager/shared/src/main/java/org/openremote/manager/shared/notification/NotificationResource.java@@ -54,4 +54,10 @@ public interface NotificationResource {@Produces(APPLICATION_JSON)@RolesAllowed({"write:user"})List<AlertNotification> getAlertNotification();++ @DELETE+ @Path("alert/{alertId}")+ @SuccessStatusCode(204)+ @RolesAllowed({"write:user"})+ void removeAlertNotification(@PathParam("alertId") Long id);}diff --git a/manager/test/src/test/groovy/org/openremote/test/user/NotificationServiceTest.groovy b/manager/test/src/test/groovy/org/openremote/test/user/NotificationServiceTest.groovyindex 50f0bb82a..ad85ad741 100644--- a/manager/test/src/test/groovy/org/openremote/test/user/NotificationServiceTest.groovy+++ b/manager/test/src/test/groovy/org/openremote/test/user/NotificationServiceTest.groovy@@ -65,11 +65,16 @@ class NotificationServiceTest extends Specification implements ManagerContainerTwhen: "the Alert Notification is stored"notificationResource.storeAlertNotification(notificationAlert)+ def alerts = notificationResource.getAlertNotification()then: "it should be one alert notification pending"- notificationResource.getAlertNotification().size() == 1+ alerts.size() == 1- //TODO: delete alertResource //then should be 0 pending+ when: "the alert Notification is acknowledge"+ notificationResource.removeAlertNotification(alerts.get(0).id)++ then: "it should be no allert"+ notificationResource.getAlertNotification().size() == 0cleanup: "the server should be stopped"stopContainer(container)