Skip to content

armgit / core

Public Imported from GitHub
0

100% open-source IoT Platform - Integrate your devices, add intelligence, build custom apps

working on notification

Mica Hallin55ed337748a2
diff --git a/console/android/src/main/java/org/openremote/android/MainActivity.java b/console/android/src/main/java/org/openremote/android/MainActivity.java
index 0f7973ac7..0cdb9c042 100644
--- a/console/android/src/main/java/org/openremote/android/MainActivity.java
+++ b/console/android/src/main/java/org/openremote/android/MainActivity.java
@@ -61,6 +61,13 @@ public class MainActivity extends Activity {
ConnectivityManager.CONNECTIVITY_ACTION));
}
+ @Override
+ protected void onPause() {
+ super.onPause();
+
+ unregisterReceiver(connectivityChangeReceiver);
+ }
+
@Override
protected void onSaveInstanceState(Bundle outState) {
webView.saveState(outState);
diff --git a/console/android/src/main/java/org/openremote/android/service/TokenService.java b/console/android/src/main/java/org/openremote/android/service/TokenService.java
index d6aeb7618..67e9f67e0 100644
--- a/console/android/src/main/java/org/openremote/android/service/TokenService.java
+++ b/console/android/src/main/java/org/openremote/android/service/TokenService.java
@@ -31,7 +31,7 @@ public class TokenService {
public TokenService(Context context) {
Retrofit retrofit = new Retrofit.Builder()
- .baseUrl("http://192.168.0.143:8080")
+ .baseUrl(context.getString(R.string.OR_BASE_SERVER))
.addConverterFactory(JacksonConverterFactory.create())
.build();
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.java
index 5dc7f6b24..17e225fb4 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
@@ -2,10 +2,13 @@ package org.openremote.manager.server.notification;
import org.openremote.container.web.WebResource;
import org.openremote.manager.shared.http.RequestParams;
+import org.openremote.manager.shared.notification.AlertNotification;
import org.openremote.manager.shared.notification.NotificationResource;
import javax.ws.rs.WebApplicationException;
+import java.util.List;
+
import static javax.ws.rs.core.Response.Status.BAD_REQUEST;
public class NotificationResourceImpl extends WebResource implements NotificationResource {
@@ -23,4 +26,17 @@ public class NotificationResourceImpl extends WebResource implements Notificatio
}
notificationService.storeDeviceToken(deviceId, getUserId(), token);
}
+
+ @Override
+ public void storeAlertNotification( AlertNotification alertNotification) {
+ if (alertNotification == null) {
+ throw new WebApplicationException("Missing alertNotification", BAD_REQUEST);
+ }
+ notificationService.storeAlertNotification(getUserId(), alertNotification);
+ }
+
+ @Override
+ public List<AlertNotification> getAlertNotification() {
+ return notificationService.getPendingAlertForUserId(getUserId());
+ }
}
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.java
index 251019abd..17e8ad7f7 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
@@ -23,7 +23,12 @@ import org.openremote.container.Container;
import org.openremote.container.ContainerService;
import org.openremote.container.persistence.PersistenceService;
import org.openremote.container.web.WebService;
+import org.openremote.manager.shared.notification.AlertNotification;
+import org.openremote.manager.shared.notification.DeliveryStatus;
+import javax.persistence.EntityManager;
+import javax.persistence.Query;
+import java.util.List;
import java.util.logging.Logger;
public class NotificationService implements ContainerService {
@@ -37,7 +42,7 @@ public class NotificationService implements ContainerService {
this.persistenceService = container.getService(PersistenceService.class);
container.getService(WebService.class).getApiSingletons().add(
- new NotificationResourceImpl(this)
+ new NotificationResourceImpl(this)
);
}
@@ -64,4 +69,35 @@ public class NotificationService implements ContainerService {
return deviceToken != null ? deviceToken.getToken() : null;
});
}
+
+ public List<DeviceNotificationToken> findAllTokenForUser(String userId) {
+ return persistenceService.doReturningTransaction(entityManager -> {
+ Query query = entityManager.createQuery("SELECT dnt FROM DeviceNotificationToken dnt WHERE dnt.id.userId =:userId");
+ query.setParameter("userId", userId);
+ return query.getResultList();
+ });
+ }
+
+ public void storeAlertNotification(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
+ }
+ });
+
+
+ }
+
+ public List<AlertNotification> getPendingAlertForUserId(String userId) {
+ return persistenceService.doReturningTransaction(entityManager -> {
+ Query query = entityManager.createQuery("SELECT an FROM AlertNotification an WHERE an.userId =:userId and an.deliveryStatus =:deliveryStatus");
+ query.setParameter("userId", userId);
+ query.setParameter("deliveryStatus",DeliveryStatus.PENDING);
+ return query.getResultList();
+ });
+ }
}
\ No newline at end of file
diff --git a/manager/server/src/main/resources/META-INF/persistence.xml b/manager/server/src/main/resources/META-INF/persistence.xml
index b574c5b56..03ce221b6 100644
--- a/manager/server/src/main/resources/META-INF/persistence.xml
+++ b/manager/server/src/main/resources/META-INF/persistence.xml
@@ -11,6 +11,8 @@
<class>org.openremote.manager.server.asset.ServerAsset</class>
<class>org.openremote.manager.server.notification.DeviceNotificationToken</class>
+ <class>org.openremote.manager.shared.notification.AlertNotification</class>
+ <class>org.openremote.manager.shared.notification.AlertAction</class>
<class>org.openremote.manager.server.security.UserConfiguration</class>
<class>org.openremote.model.datapoint.AssetDatapoint</class>
<class>org.openremote.model.asset.UserAsset</class>
diff --git a/manager/shared/src/main/java/org/openremote/manager/shared/notification/AlertAction.java b/manager/shared/src/main/java/org/openremote/manager/shared/notification/AlertAction.java
new file mode 100644
index 000000000..76cb00d28
--- /dev/null
+++ b/manager/shared/src/main/java/org/openremote/manager/shared/notification/AlertAction.java
@@ -0,0 +1,29 @@
+package org.openremote.manager.shared.notification;
+
+import javax.persistence.*;
+import javax.validation.constraints.NotNull;
+
+import static org.openremote.model.Constants.PERSISTENCE_SEQUENCE_ID_GENERATOR;
+
+
+@Entity
+@Table(name = "ALERT_ACTION")
+public class AlertAction {
+
+ @Id
+ @Column(name = "ID")
+ @GeneratedValue(generator = PERSISTENCE_SEQUENCE_ID_GENERATOR)
+ Long id;
+
+ @NotNull
+ @Column(name = "TYPE", nullable = false)
+ String type;
+
+ @NotNull
+ @Column(name = "NAME", nullable = false)
+ String name;
+
+ /*
+ * action (button) name ◦ type & parameters
+ * */
+}
diff --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.java
new file mode 100644
index 000000000..1e773c0a0
--- /dev/null
+++ b/manager/shared/src/main/java/org/openremote/manager/shared/notification/AlertNotification.java
@@ -0,0 +1,103 @@
+package org.openremote.manager.shared.notification;
+
+import javax.persistence.*;
+import javax.validation.constraints.NotNull;
+
+import java.util.Set;
+
+import static org.openremote.model.Constants.PERSISTENCE_SEQUENCE_ID_GENERATOR;
+
+@Entity
+@Table(name = "ALERT_NOTIFICATION")
+public class AlertNotification {
+
+ @Id
+ @Column(name = "ID")
+ @GeneratedValue(generator = PERSISTENCE_SEQUENCE_ID_GENERATOR)
+ private Long id;
+
+ @NotNull
+ @Column(name = "TITLE", nullable = false)
+ private String title;
+
+ @NotNull
+ @Column(name = "MESSAGE", nullable = false)
+ private String message;
+
+ @OneToMany(cascade = CascadeType.ALL)
+ @JoinColumn(name="ALERT_ID")
+ private Set<AlertAction> actions;
+
+ @NotNull
+ @Column(name = "APP_URL", nullable = false)
+ private String appUrl;
+
+ @NotNull()
+ @Column(name = "USER_ID")
+ private String userId;
+
+ @NotNull()
+ @Column(name = "DELIVERY_STATUS")
+ @Enumerated(EnumType.STRING)
+ private DeliveryStatus deliveryStatus;
+
+ /*
+ Actuator
+ */
+
+ public Long getId() {
+ return id;
+ }
+
+ public void setId(Long id) {
+ this.id = id;
+ }
+
+ public String getTitle() {
+ return title;
+ }
+
+ public void setTitle(String title) {
+ this.title = title;
+ }
+
+ public String getMessage() {
+ return message;
+ }
+
+ public void setMessage(String message) {
+ this.message = message;
+ }
+
+ public Set<AlertAction> getActions() {
+ return actions;
+ }
+
+ public void setActions(Set<AlertAction> actions) {
+ this.actions = actions;
+ }
+
+ public String getAppUrl() {
+ return appUrl;
+ }
+
+ public void setAppUrl(String appUrl) {
+ this.appUrl = appUrl;
+ }
+
+ public String getUserId() {
+ return userId;
+ }
+
+ public void setUserId(String userId) {
+ this.userId = userId;
+ }
+
+ public DeliveryStatus getDeliveryStatus() {
+ return deliveryStatus;
+ }
+
+ public void setDeliveryStatus(DeliveryStatus deliveryStatus) {
+ this.deliveryStatus = deliveryStatus;
+ }
+}
diff --git a/manager/shared/src/main/java/org/openremote/manager/shared/notification/DeliveryStatus.java b/manager/shared/src/main/java/org/openremote/manager/shared/notification/DeliveryStatus.java
new file mode 100644
index 000000000..6ab1a084b
--- /dev/null
+++ b/manager/shared/src/main/java/org/openremote/manager/shared/notification/DeliveryStatus.java
@@ -0,0 +1,5 @@
+package org.openremote.manager.shared.notification;
+
+public enum DeliveryStatus {
+ PENDING, DELIVERED
+}
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.java
index 851214623..6491f273a 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
@@ -24,10 +24,11 @@ import org.openremote.manager.shared.http.RequestParams;
import org.openremote.manager.shared.http.SuccessStatusCode;
import javax.annotation.security.RolesAllowed;
-import javax.ws.rs.BeanParam;
-import javax.ws.rs.FormParam;
-import javax.ws.rs.PUT;
-import javax.ws.rs.Path;
+import javax.ws.rs.*;
+
+import java.util.List;
+
+import static javax.ws.rs.core.MediaType.APPLICATION_JSON;
@Path("notification")
@JsType(isNative = true)
@@ -40,4 +41,17 @@ public interface NotificationResource {
void storeDeviceToken(@BeanParam RequestParams requestParams,
@FormParam("device_id") String deviceId,
@FormParam("token") String token);
+
+ @POST
+ @Path("alert")
+ @SuccessStatusCode(204)
+ @Consumes(APPLICATION_JSON)
+ @RolesAllowed({"write:user"})
+ void storeAlertNotification(AlertNotification alertNotification);
+
+ @GET
+ @Path("alert")
+ @Produces(APPLICATION_JSON)
+ @RolesAllowed({"write:user"})
+ List<AlertNotification> getAlertNotification();
}
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.groovy
index 54b4a5008..50f0bb82a 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
@@ -3,6 +3,8 @@ package org.openremote.test.user
import org.openremote.manager.server.setup.SetupService
import org.openremote.manager.server.setup.builtin.KeycloakDemoSetup
import org.openremote.manager.server.notification.NotificationService
+import org.openremote.manager.shared.notification.AlertAction
+import org.openremote.manager.shared.notification.AlertNotification
import org.openremote.manager.shared.notification.NotificationResource
import org.openremote.test.ManagerContainerTrait
import spock.lang.Specification
@@ -30,6 +32,17 @@ class NotificationServiceTest extends Specification implements ManagerContainerT
"testuser1"
).token
+ def notificationAlert = new AlertNotification(
+ title: "The Title",
+ actions: [new AlertAction(
+ name: "TEST_ACTION",
+ type: "TEST"
+ )],
+ appUrl: '#test',
+ message: "Message",
+
+ )
+
and: "the notification resource"
def client = createClient(container).build()
def serverUri = serverUri(serverPort)
@@ -48,6 +61,15 @@ class NotificationServiceTest extends Specification implements ManagerContainerT
then: "the updated token should be in the database"
notificationService.findDeviceToken("device456", keycloakDemoSetup.testuser1Id) == "token789"
+ notificationService.findAllTokenForUser(keycloakDemoSetup.testuser1Id).size() == 2
+
+ when: "the Alert Notification is stored"
+ notificationResource.storeAlertNotification(notificationAlert)
+
+ then: "it should be one alert notification pending"
+ notificationResource.getAlertNotification().size() == 1
+
+ //TODO: delete alertResource //then should be 0 pending
cleanup: "the server should be stopped"
stopContainer(container)