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 mobile console notification

Mica Hallin8f419a844195
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 0cdb9c042..dbb0d8bdb 100644
--- a/console/android/src/main/java/org/openremote/android/MainActivity.java
+++ b/console/android/src/main/java/org/openremote/android/MainActivity.java
@@ -16,6 +16,7 @@ import android.webkit.JavascriptInterface;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
+import android.webkit.WebResourceResponse;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
@@ -39,8 +40,7 @@ public class MainActivity extends Activity {
webView = (WebView) findViewById(R.id.webview);
if (savedInstanceState != null) {
webView.restoreState(savedInstanceState);
- }
- else {
+ } else {
loadIndex();
}
errorView = findViewById(R.id.errorView);
@@ -75,7 +75,7 @@ public class MainActivity extends Activity {
private void loadIndex() {
- webView.addJavascriptInterface(new WebAppInterface(this),"MobileInterface");
+ webView.addJavascriptInterface(new WebAppInterface(this), "MobileInterface");
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setAppCacheEnabled(true);
@@ -88,7 +88,15 @@ public class MainActivity extends Activity {
}
});
webView.setLongClickable(false);
+
webView.setWebViewClient(new WebViewClient() {
+ @Override
+ public void onReceivedHttpError(WebView view, WebResourceRequest request, WebResourceResponse errorResponse) {
+ Log.e("WEBVIEW", "request url :" + request.getUrl().toString());
+ Log.e("WEBVIEW", "error :" + errorResponse.getStatusCode());
+ handleError(request);
+ }
+
@Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
@@ -97,20 +105,26 @@ public class MainActivity extends Activity {
Log.e("WEBVIEW", "error :" + error.getErrorCode());
Log.e("WEBVIEW", "error :" + error.getDescription());
}
- if (request.getUrl().toString().equals(getString(R.string.OR_BASE_SERVER)+getString(R.string.OR_CONSOLE_URL))) {
- webView.loadUrl("about:blank");
- errorView.setVisibility(View.VISIBLE);
- }
+ handleError(request);
}
});
webView.setWebChromeClient(new WebChromeClient());
- webView.loadUrl(getString(R.string.OR_BASE_SERVER)+getString(R.string.OR_CONSOLE_URL));
+ webView.loadUrl(getString(R.string.OR_BASE_SERVER) + getString(R.string.OR_CONSOLE_URL));
}
+ private void handleError(WebResourceRequest request) {
+ if (request.getUrl().toString().startsWith(getString(R.string.OR_BASE_SERVER) + getString(R.string.OR_CONSOLE_URL)) ||
+ request.getUrl().toString().startsWith(getString(R.string.OR_BASE_SERVER) + getString(R.string.AUTH_URL))
+ ) {
+ webView.loadUrl("about:blank");
+ errorView.setVisibility(View.VISIBLE);
+ }
+ }
+
public void reloadPage(View view) {
errorView.setVisibility(View.GONE);
- webView.loadUrl(getString(R.string.OR_BASE_SERVER)+getString(R.string.OR_CONSOLE_URL));
+ webView.loadUrl(getString(R.string.OR_BASE_SERVER) + getString(R.string.OR_CONSOLE_URL));
}
@@ -126,22 +140,21 @@ public class MainActivity extends Activity {
@JavascriptInterface
public String getMobileToken() {
return tokenService.getJsonToken();
- }
+ }
@JavascriptInterface
- public void setMobileToken(String token, String refreshToken, String idToken) {
- tokenService.saveToken(token,refreshToken,idToken);
+ public void setMobileToken(String token, String refreshToken, String idToken) {
+ tokenService.saveToken(token, refreshToken, idToken);
}
}
-
private void onConnectivityChanged(boolean connectivity) {
- if (connectivity) {
- noConnectivityView.setVisibility(View.GONE);
- } else {
- noConnectivityView.setVisibility(View.VISIBLE);
- }
+ if (connectivity) {
+ noConnectivityView.setVisibility(View.GONE);
+ } else {
+ noConnectivityView.setVisibility(View.VISIBLE);
+ }
}
private class ConnectivityChangeReceiver extends BroadcastReceiver {
diff --git a/console/android/src/main/res/values/strings.xml b/console/android/src/main/res/values/strings.xml
index 275702026..cc69a7284 100644
--- a/console/android/src/main/res/values/strings.xml
+++ b/console/android/src/main/res/values/strings.xml
@@ -11,4 +11,5 @@
<string name="reload">reload</string>
<string name="noConnectivity">No connectivity Detected</string>
<string name="SHARED_PREF_OPENED_ALERT">OPENED_ALERT</string>
+ <string name="AUTH_URL">/auth</string>
</resources>
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 5edf8209a..4e230ce12 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,7 +2,7 @@ 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.model.notification.AlertNotification;
import org.openremote.manager.shared.notification.NotificationResource;
import javax.ws.rs.WebApplicationException;
@@ -32,7 +32,7 @@ public class NotificationResourceImpl extends WebResource implements Notificatio
if (alertNotification == null) {
throw new WebApplicationException("Missing alertNotification", BAD_REQUEST);
}
- notificationService.storeAndNotifyAlertNotification(getUserId(), alertNotification);
+ notificationService.storeAndNotify(getUserId(), alertNotification);
}
@Override
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 af25800fa..c41cfa88d 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
@@ -26,8 +26,8 @@ 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 org.openremote.model.notification.AlertNotification;
+import org.openremote.model.notification.DeliveryStatus;
import javax.persistence.EntityManager;
import javax.persistence.Query;
@@ -97,7 +97,7 @@ public class NotificationService implements ContainerService {
}
- public void storeAndNotifyAlertNotification(String userId, AlertNotification alertNotification) {
+ public void storeAndNotify(String userId, AlertNotification alertNotification) {
alertNotification.setUserId(userId);
alertNotification.setDeliveryStatus(DeliveryStatus.PENDING);
persistenceService.doTransaction((EntityManager entityManager) -> {
@@ -136,4 +136,11 @@ public class NotificationService implements ContainerService {
query.executeUpdate();
});
}
+
+ public List<String> findAllUsersWithToken() {
+ return persistenceService.doReturningTransaction(entityManager -> {
+ Query query = entityManager.createQuery("SELECT dnt.id.userId FROM DeviceNotificationToken dnt");
+ return query.getResultList();
+ });
+ }
}
\ No newline at end of file
diff --git a/manager/server/src/main/java/org/openremote/manager/server/rules/RulesDeployment.java b/manager/server/src/main/java/org/openremote/manager/server/rules/RulesDeployment.java
index 28060842d..d74504681 100644
--- a/manager/server/src/main/java/org/openremote/manager/server/rules/RulesDeployment.java
+++ b/manager/server/src/main/java/org/openremote/manager/server/rules/RulesDeployment.java
@@ -40,6 +40,7 @@ import org.openremote.container.util.Util;
import org.openremote.manager.server.asset.AssetProcessingService;
import org.openremote.manager.server.asset.AssetStorageService;
import org.openremote.manager.server.asset.ServerAsset;
+import org.openremote.manager.server.notification.NotificationService;
import org.openremote.manager.shared.rules.AssetRuleset;
import org.openremote.manager.shared.rules.GlobalRuleset;
import org.openremote.manager.shared.rules.TenantRuleset;
@@ -48,7 +49,9 @@ import org.openremote.model.Consumer;
import org.openremote.model.asset.AssetQuery;
import org.openremote.model.asset.AssetUpdate;
import org.openremote.manager.shared.rules.Ruleset;
+import org.openremote.model.notification.AlertNotification;
import org.openremote.model.rules.Assets;
+import org.openremote.model.rules.Users;
import java.util.*;
import java.util.concurrent.Executors;
@@ -62,6 +65,7 @@ public class RulesDeployment<T extends Ruleset> {
public static final Logger LOG = Logger.getLogger(RulesDeployment.class.getName());
+ protected final NotificationService notificationService;
final protected AssetStorageService assetStorageService;
final protected AssetProcessingService assetProcessingService;
final protected Class<T> rulesetType;
@@ -69,7 +73,6 @@ public class RulesDeployment<T extends Ruleset> {
// This is here so Clock Type can be set to pseudo from tests
protected static ClockTypeOption DefaultClockType;
-
private static final int AUTO_START_DELAY_SECONDS = 2;
private static Long counter = 1L;
static final protected Util UTIL = new Util();
@@ -88,9 +91,10 @@ public class RulesDeployment<T extends Ruleset> {
protected static final ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
protected ScheduledFuture startTimer;
- public RulesDeployment(AssetStorageService assetStorageService, AssetProcessingService assetProcessingService,
+ public RulesDeployment(AssetStorageService assetStorageService, NotificationService notificationService, AssetProcessingService assetProcessingService,
Class<T> rulesetType, String id) {
this.assetStorageService = assetStorageService;
+ this.notificationService = notificationService; // shouldBeUser service or Identity Service ?
this.assetProcessingService = assetProcessingService;
this.rulesetType = rulesetType;
this.id = id;
@@ -348,6 +352,7 @@ public class RulesDeployment<T extends Ruleset> {
running = true;
setGlobal("assets", createAssetsFacade());
+ setGlobal("users", createUsersFacade());
setGlobal("LOG", LOG);
// TODO Still need this UTIL?
@@ -554,6 +559,48 @@ public class RulesDeployment<T extends Ruleset> {
};
}
+
+ protected Users createUsersFacade() {
+ return new Users() {
+
+ @Override
+ public RestrictedQuery query() {
+ RestrictedQuery query = new RestrictedQuery() {
+
+ @Override
+ public List<String> getResults() {
+ return notificationService.findAllUsersWithToken();
+ }
+
+ @Override
+ public void applyResults(Consumer<List<String>> usersIdListConsumer) {
+ usersIdListConsumer.accept(getResults());
+ }
+ };
+
+ if (TenantRuleset.class.isAssignableFrom(rulesetType)) {
+ //TODO: restrict users of tenant
+ }
+ if (AssetRuleset.class.isAssignableFrom(rulesetType)) {
+ ServerAsset restrictedAsset = assetStorageService.find(id, true);
+ if (restrictedAsset == null) {
+ throw new IllegalStateException("Asset is no longer available for this deployment: " + id);
+ }
+ //TODO: restrict users of assets
+ }
+ return query;
+
+ }
+
+ @Override
+ public void storeAndNotify(String userId, AlertNotification alert) {
+ notificationService.storeAndNotify(userId,alert);
+ }
+
+
+ };
+ }
+
protected synchronized String getRulesetDebug() {
return Arrays.toString(rulesets.values().stream().map(rd ->
rd.getClass().getSimpleName()
diff --git a/manager/server/src/main/java/org/openremote/manager/server/rules/RulesService.java b/manager/server/src/main/java/org/openremote/manager/server/rules/RulesService.java
index 8705fe583..948d2b2a0 100644
--- a/manager/server/src/main/java/org/openremote/manager/server/rules/RulesService.java
+++ b/manager/server/src/main/java/org/openremote/manager/server/rules/RulesService.java
@@ -31,6 +31,7 @@ import org.openremote.container.util.Util;
import org.openremote.manager.server.asset.AssetProcessingService;
import org.openremote.manager.server.asset.AssetStorageService;
import org.openremote.manager.server.asset.ServerAsset;
+import org.openremote.manager.server.notification.NotificationService;
import org.openremote.manager.server.security.ManagerIdentityService;
import org.openremote.manager.shared.rules.AssetRuleset;
import org.openremote.manager.shared.rules.GlobalRuleset;
@@ -68,6 +69,7 @@ public class RulesService extends RouteBuilder implements ContainerService, Cons
protected RulesetStorageService rulesetStorageService;
protected ManagerIdentityService identityService;
protected AssetStorageService assetStorageService;
+ protected NotificationService notificationService;
protected AssetProcessingService assetProcessingService;
protected RulesDeployment<GlobalRuleset> globalDeployment;
protected final Map<String, RulesDeployment<TenantRuleset>> tenantDeployments = new HashMap<>();
@@ -83,6 +85,7 @@ public class RulesService extends RouteBuilder implements ContainerService, Cons
persistenceService = container.getService(PersistenceService.class);
rulesetStorageService = container.getService(RulesetStorageService.class);
identityService = container.getService(ManagerIdentityService.class);
+ notificationService = container.getService(NotificationService.class);
assetStorageService = container.getService(AssetStorageService.class);
assetProcessingService = container.getService(AssetProcessingService.class);
container.getService(MessageBrokerSetupService.class).getContext().addRoutes(this);
@@ -377,7 +380,7 @@ public class RulesService extends RouteBuilder implements ContainerService, Cons
// Global rules have access to everything in the system
if (globalDeployment == null) {
- globalDeployment = new RulesDeployment<>(assetStorageService, assetProcessingService, GlobalRuleset.class, "GLOBAL");
+ globalDeployment = new RulesDeployment<>(assetStorageService, notificationService, assetProcessingService, GlobalRuleset.class, "GLOBAL");
}
globalDeployment.insertRuleset(ruleset);
@@ -403,7 +406,7 @@ public class RulesService extends RouteBuilder implements ContainerService, Cons
RulesDeployment<TenantRuleset> deployment = tenantDeployments
.computeIfAbsent(ruleset.getRealmId(), (realmId) -> {
created[0] = true;
- return new RulesDeployment<>(assetStorageService, assetProcessingService, TenantRuleset.class, realmId);
+ return new RulesDeployment<>(assetStorageService,notificationService, assetProcessingService, TenantRuleset.class, realmId);
});
deployment.insertRuleset(ruleset);
@@ -459,7 +462,7 @@ public class RulesService extends RouteBuilder implements ContainerService, Cons
RulesDeployment<AssetRuleset> deployment = assetDeployments
.computeIfAbsent(ruleset.getAssetId(), (assetId) -> {
created[0] = true;
- return new RulesDeployment<>(assetStorageService, assetProcessingService, AssetRuleset.class, assetId);
+ return new RulesDeployment<>(assetStorageService,notificationService, assetProcessingService, AssetRuleset.class, assetId);
});
deployment.insertRuleset(ruleset);
diff --git a/manager/server/src/main/resources/META-INF/persistence.xml b/manager/server/src/main/resources/META-INF/persistence.xml
index 41520d5f3..78b04d011 100644
--- a/manager/server/src/main/resources/META-INF/persistence.xml
+++ b/manager/server/src/main/resources/META-INF/persistence.xml
@@ -11,8 +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.model.notification.AlertNotification</class>
+ <class>org.openremote.model.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/NotificationResource.java b/manager/shared/src/main/java/org/openremote/manager/shared/notification/NotificationResource.java
index c2e854748..13a828c75 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
@@ -22,6 +22,7 @@ package org.openremote.manager.shared.notification;
import jsinterop.annotations.JsType;
import org.openremote.manager.shared.http.RequestParams;
import org.openremote.manager.shared.http.SuccessStatusCode;
+import org.openremote.model.notification.AlertNotification;
import javax.annotation.security.RolesAllowed;
import javax.ws.rs.*;
diff --git a/manager/shared/src/main/java/org/openremote/manager/shared/notification/ActionType.java b/model/src/main/java/org/openremote/model/notification/ActionType.java
similarity index 54%
rename from manager/shared/src/main/java/org/openremote/manager/shared/notification/ActionType.java
rename to model/src/main/java/org/openremote/model/notification/ActionType.java
index b1fa3340b..bfe89766a 100644
--- a/manager/shared/src/main/java/org/openremote/manager/shared/notification/ActionType.java
+++ b/model/src/main/java/org/openremote/model/notification/ActionType.java
@@ -1,4 +1,4 @@
-package org.openremote.manager.shared.notification;
+package org.openremote.model.notification;
public enum ActionType {
diff --git a/manager/shared/src/main/java/org/openremote/manager/shared/notification/AlertAction.java b/model/src/main/java/org/openremote/model/notification/AlertAction.java
similarity index 93%
rename from manager/shared/src/main/java/org/openremote/manager/shared/notification/AlertAction.java
rename to model/src/main/java/org/openremote/model/notification/AlertAction.java
index 813c23609..ea7e6572f 100644
--- a/manager/shared/src/main/java/org/openremote/manager/shared/notification/AlertAction.java
+++ b/model/src/main/java/org/openremote/model/notification/AlertAction.java
@@ -1,8 +1,7 @@
-package org.openremote.manager.shared.notification;
+package org.openremote.model.notification;
import elemental.json.Json;
import elemental.json.JsonObject;
-import elemental.json.JsonValue;
public class AlertAction {
diff --git a/manager/shared/src/main/java/org/openremote/manager/shared/notification/AlertNotification.java b/model/src/main/java/org/openremote/model/notification/AlertNotification.java
similarity index 92%
rename from manager/shared/src/main/java/org/openremote/manager/shared/notification/AlertNotification.java
rename to model/src/main/java/org/openremote/model/notification/AlertNotification.java
index 713652473..129ac3b28 100644
--- a/manager/shared/src/main/java/org/openremote/manager/shared/notification/AlertNotification.java
+++ b/model/src/main/java/org/openremote/model/notification/AlertNotification.java
@@ -1,19 +1,15 @@
-package org.openremote.manager.shared.notification;
+package org.openremote.model.notification;
import elemental.json.Json;
import elemental.json.JsonArray;
-import elemental.json.JsonObject;
-import elemental.json.impl.JreJsonArray;
import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;
-import java.util.Set;
import static org.openremote.model.Constants.PERSISTENCE_JSON_ARRAY_TYPE;
-import static org.openremote.model.Constants.PERSISTENCE_JSON_OBJECT_TYPE;
import static org.openremote.model.Constants.PERSISTENCE_SEQUENCE_ID_GENERATOR;
@Entity
diff --git a/manager/shared/src/main/java/org/openremote/manager/shared/notification/DeliveryStatus.java b/model/src/main/java/org/openremote/model/notification/DeliveryStatus.java
similarity index 51%
rename from manager/shared/src/main/java/org/openremote/manager/shared/notification/DeliveryStatus.java
rename to model/src/main/java/org/openremote/model/notification/DeliveryStatus.java
index 6ab1a084b..35bf32583 100644
--- a/manager/shared/src/main/java/org/openremote/manager/shared/notification/DeliveryStatus.java
+++ b/model/src/main/java/org/openremote/model/notification/DeliveryStatus.java
@@ -1,4 +1,4 @@
-package org.openremote.manager.shared.notification;
+package org.openremote.model.notification;
public enum DeliveryStatus {
PENDING, DELIVERED
diff --git a/model/src/main/java/org/openremote/model/rules/Users.java b/model/src/main/java/org/openremote/model/rules/Users.java
new file mode 100644
index 000000000..55424ec98
--- /dev/null
+++ b/model/src/main/java/org/openremote/model/rules/Users.java
@@ -0,0 +1,46 @@
+/*
+ * Copyright 2017, OpenRemote Inc.
+ *
+ * See the CONTRIBUTORS.txt file in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+package org.openremote.model.rules;
+
+import org.openremote.model.AttributeEvent;
+import org.openremote.model.Consumer;
+import org.openremote.model.asset.AssetQuery;
+import org.openremote.model.notification.AlertNotification;
+import org.openremote.model.user.UserQuery;
+
+import java.util.List;
+
+/**
+ * Facade for writing rules RHS actions, supporting asset queries within the scope
+ * of the rule engine.
+ */
+public abstract class Users {
+
+ public abstract class RestrictedQuery extends UserQuery<RestrictedQuery> {
+ abstract public List<String> getResults();
+
+ abstract public void applyResults(Consumer<List<String>> assetIdListConsumer);
+ }
+
+ abstract public RestrictedQuery query();
+
+ abstract public void storeAndNotify(String userId, AlertNotification alert);
+
+}
diff --git a/model/src/main/java/org/openremote/model/user/UserQuery.java b/model/src/main/java/org/openremote/model/user/UserQuery.java
new file mode 100644
index 000000000..858c79b38
--- /dev/null
+++ b/model/src/main/java/org/openremote/model/user/UserQuery.java
@@ -0,0 +1,5 @@
+package org.openremote.model.user;
+
+
+public class UserQuery<CHILD extends UserQuery<CHILD>> {
+}