Skip to content

armgit / core

Public Imported from GitHub
0

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

UI updates: Fix for console storage provider (now accepts any JSON stringify-able data)

Rich Turner48b193b01cdc
diff --git a/manager/src/main/java/org/openremote/manager/apps/ConsoleAppService.java b/manager/src/main/java/org/openremote/manager/apps/ConsoleAppService.java
index 73b05ee8c..1eb670a5e 100644
--- a/manager/src/main/java/org/openremote/manager/apps/ConsoleAppService.java
+++ b/manager/src/main/java/org/openremote/manager/apps/ConsoleAppService.java
@@ -74,13 +74,15 @@ public class ConsoleAppService implements ContainerService {
consoleAppDocRoot = Paths.get(getString(container.getConfig(), CONSOLE_APP_CONFIG_DOCROOT, CONSOLE_APP_CONFIG_DOCROOT_DEFAULT));
// Serve console app config files
- HttpHandler customBaseFileHandler = ManagerWebService.createFileHandler(container, consoleAppDocRoot, null);
-
- HttpHandler pathHandler = new PathHandler().addPrefixPath(CONSOLE_APP_CONFIG_PATH, customBaseFileHandler);
- managerWebService.getRequestHandlers().add(0, pathStartsWithHandler(
- "Console app config files",
- CONSOLE_APP_CONFIG_PATH,
- pathHandler));
+ if (Files.isDirectory(consoleAppDocRoot)) {
+ HttpHandler customBaseFileHandler = ManagerWebService.createFileHandler(container, consoleAppDocRoot, null);
+
+ HttpHandler pathHandler = new PathHandler().addPrefixPath(CONSOLE_APP_CONFIG_PATH, customBaseFileHandler);
+ managerWebService.getRequestHandlers().add(0, pathStartsWithHandler(
+ "Console app config files",
+ CONSOLE_APP_CONFIG_PATH,
+ pathHandler));
+ }
}
@Override
diff --git a/test/src/test/groovy/org/openremote/test/protocol/TcpClientTest.groovy b/test/src/test/groovy/org/openremote/test/protocol/TcpClientTest.groovy
index 53b2c063c..c8c223e99 100644
--- a/test/src/test/groovy/org/openremote/test/protocol/TcpClientTest.groovy
+++ b/test/src/test/groovy/org/openremote/test/protocol/TcpClientTest.groovy
@@ -39,7 +39,7 @@ class TcpClientTest extends Specification implements ManagerContainerTrait {
def "Check client"() {
given: "expected conditions"
- def conditions = new PollingConditions(timeout: 10, delay: 0.2)
+ def conditions = new PollingConditions(timeout: 20, delay: 0.2)
and: "the IO client reconnect time is set low for test purposes"
def initialMillis = AbstractNettyIOClient.RECONNECT_DELAY_INITIAL_MILLIS
diff --git a/test/src/test/groovy/org/openremote/test/protocol/udp/UdpClientTest.groovy b/test/src/test/groovy/org/openremote/test/protocol/udp/UdpClientTest.groovy
index 63c0fa5c5..143d643ea 100644
--- a/test/src/test/groovy/org/openremote/test/protocol/udp/UdpClientTest.groovy
+++ b/test/src/test/groovy/org/openremote/test/protocol/udp/UdpClientTest.groovy
@@ -42,7 +42,7 @@ class UdpClientTest extends Specification implements ManagerContainerTrait {
def "Check client"() {
given: "expected conditions"
- def conditions = new PollingConditions(timeout: 10, delay: 0.2)
+ def conditions = new PollingConditions(timeout: 20, delay: 0.2)
and: "the IO client reconnect time is set low for test purposes"
def initialMillis = AbstractNettyIOClient.RECONNECT_DELAY_INITIAL_MILLIS
diff --git a/test/src/test/groovy/org/openremote/test/protocol/websocket/WebsocketClientTest.groovy b/test/src/test/groovy/org/openremote/test/protocol/websocket/WebsocketClientTest.groovy
index 2e9c1f874..010b97689 100644
--- a/test/src/test/groovy/org/openremote/test/protocol/websocket/WebsocketClientTest.groovy
+++ b/test/src/test/groovy/org/openremote/test/protocol/websocket/WebsocketClientTest.groovy
@@ -74,7 +74,7 @@ class WebsocketClientTest extends Specification implements ManagerContainerTrait
def "Check client"() {
given: "expected conditions"
- def conditions = new PollingConditions(timeout: 10, delay: 0.2)
+ def conditions = new PollingConditions(timeout: 20, delay: 0.2)
and: "the IO client reconnect time is set low for test purposes"
def initialMillis = AbstractNettyIOClient.RECONNECT_DELAY_INITIAL_MILLIS
def maxMillis = AbstractNettyIOClient.RECONNECT_DELAY_MAX_MILLIS
diff --git a/ui/app/manager/src/pages/page-export.ts b/ui/app/manager/src/pages/page-export.ts
index a12173d1d..8a386eb08 100644
--- a/ui/app/manager/src/pages/page-export.ts
+++ b/ui/app/manager/src/pages/page-export.ts
@@ -391,15 +391,12 @@ export class PageExport extends Page<AppStateKeyed> {
protected async loadConfig() {
- const configStr = await manager.console.retrieveData("OrExportConfig") || "[]";
-
- const parsedConfig = JSON.parse(configStr);
- if (!parsedConfig.length || !Object.getOwnPropertyNames(parsedConfig[0]).includes("realm")) {
- manager.console.storeData("OrExportConfig", "[]");
+ let configs: OrExportConfig[] = await manager.console.retrieveData("OrExportConfig") || [];
+ if (!configs.length || !Object.getOwnPropertyNames(configs[0]).includes("realm")) {
+ manager.console.storeData("OrExportConfig", null);
}
- this.config = parsedConfig.find(e => e.realm === this.realm) || {realm:this.realm,selectedAttributes:[]};
-
+ this.config = configs.find(e => e.realm === this.realm) || {realm:this.realm,selectedAttributes:[]};
// prune removed assets that still exist in localstorage
const response = await manager.rest.api.AssetResource.queryAssets({
@@ -414,15 +411,13 @@ export class PageExport extends Page<AppStateKeyed> {
}
protected async saveConfig() {
- const configStr = await manager.console.retrieveData("OrExportConfig");
- let config = JSON.parse(configStr);
- try {
- config.find(c => c.realm === this.realm).selectedAttributes = this.config.selectedAttributes;
- } catch (e) {
- config = [...config, this.config];
+ let configs: OrExportConfig[] = await manager.console.retrieveData("OrExportConfig") || [];
+ let config = configs.find(c => c.realm === this.realm);
+ if (config) {
+ config.selectedAttributes = this.config.selectedAttributes;
}
- manager.console.storeData("OrExportConfig", JSON.stringify(config));
+ manager.console.storeData("OrExportConfig", [...configs, this.config]);
}
protected clearSelection = () => {
diff --git a/ui/component/core/src/console.ts b/ui/component/core/src/console.ts
index e2eda36bb..a6d81bbca 100644
--- a/ui/component/core/src/console.ts
+++ b/ui/component/core/src/console.ts
@@ -321,7 +321,7 @@ export class Console {
});
}
- public storeData(key: string, value: string | null) {
+ public storeData(key: string, value: any) {
this.sendProviderMessage({
provider: "storage",
action: "STORE",
@@ -331,7 +331,7 @@ export class Console {
}
- public async retrieveData(key: string): Promise<string | undefined> {
+ public async retrieveData<T>(key: string): Promise<T | undefined> {
let response = await this.sendProviderMessage({
provider: "storage",
action: "RETRIEVE",
@@ -339,7 +339,7 @@ export class Console {
}, true);
if (response && response.value) {
- return response.value;
+ return response.value as T;
}
}
@@ -426,12 +426,10 @@ export class Console {
throw new Error("Storage provider 'store' action requires a `key`");
}
- if (msg.value) {
- if (msg.value === null) {
- window.localStorage.removeItem(keyValue);
- } else {
- window.localStorage.setItem(keyValue, msg.value);
- }
+ if (msg.value === null) {
+ window.localStorage.removeItem(keyValue);
+ } else {
+ window.localStorage.setItem(keyValue, JSON.stringify(msg.value));
}
}
break;
@@ -442,11 +440,20 @@ export class Console {
throw new Error("Storage provider 'retrieve' action requires a `key`");
}
+ let val = window.localStorage.getItem(keyValue);
+ if (val !== null) {
+ try {
+ val = JSON.parse(val);
+ } catch (e) {
+ // Fallback to just returning the val as it might not be JSON
+ }
+ }
+
this._handleProviderResponse(JSON.stringify({
action: "RETRIEVE",
provider: "storage",
key: keyValue,
- value: window.localStorage.getItem(keyValue)
+ value: val
}));
}
break;
diff --git a/ui/component/core/src/index.ts b/ui/component/core/src/index.ts
index b36cbbd29..67a793b42 100644
--- a/ui/component/core/src/index.ts
+++ b/ui/component/core/src/index.ts
@@ -755,7 +755,7 @@ export class Manager implements EventProviderFactory {
});
// Look for language preference in local storage
- const language = !this.console ? undefined : await this.console.retrieveData("LANGUAGE");
+ const language: string | undefined = !this.console ? undefined : await this.console.retrieveData("LANGUAGE");
const initOptions: InitOptions = {
lng: language,
fallbackLng: "en",
diff --git a/ui/component/or-attribute-card/src/index.ts b/ui/component/or-attribute-card/src/index.ts
index 7081dcb4c..78bf2e439 100644
--- a/ui/component/or-attribute-card/src/index.ts
+++ b/ui/component/or-attribute-card/src/index.ts
@@ -535,27 +535,17 @@ export class OrAttributeCard extends LitElement {
this.realm = manager.getRealm();
}
- const configStr = await manager.console.retrieveData("OrChartConfig");
+ let allConfigs: OrChartConfig[] = await manager.console.retrieveData("OrChartConfig") || [];
+ if (!Array.isArray(allConfigs)) {
+ allConfigs = [allConfigs];
+ }
- if (!configStr || !this.panelName) {
+ if (allConfigs.length === 0 || !this.panelName) {
return;
}
const viewSelector = window.location.hash;
- let allConfigs: OrChartConfig[] = [];
- let config: OrChartConfig;
-
- try {
- allConfigs = JSON.parse(configStr);
- if (!Array.isArray(allConfigs)) {
- manager.console.storeData("OrChartConfig", JSON.stringify([allConfigs]));
- }
- config = allConfigs.find(e => e.realm === this.realm) as OrChartConfig;
- } catch (e) {
- console.error("Failed to load chart config", e);
- manager.console.storeData("OrChartConfig", null);
- return;
- }
+ let config: OrChartConfig = allConfigs.find(e => e.realm === this.realm) as OrChartConfig;
const view = config && config.views && config.views[viewSelector] ? config.views[viewSelector][this.panelName] : undefined;
@@ -567,7 +557,7 @@ export class OrAttributeCard extends LitElement {
// Old/invalid config format remove it
delete config.views[viewSelector][this.panelName];
const cleanData = [...allConfigs.filter(e => e.realm !== this.realm), config];
- manager.console.storeData("OrChartConfig", JSON.stringify(cleanData));
+ manager.console.storeData("OrChartConfig", cleanData);
return;
}
@@ -594,7 +584,7 @@ export class OrAttributeCard extends LitElement {
view.attributeRefs = view.attributeRefs.filter((attrRef) => !!assets.find((asset) => asset.id === attrRef.id && asset.attributes && asset.attributes.hasOwnProperty(attrRef.name!)));
allConfigs = [...allConfigs.filter(e => e.realm !== this.realm), config];
- manager.console.storeData("OrChartConfig", JSON.stringify(allConfigs));
+ manager.console.storeData("OrChartConfig", allConfigs);
this.assets = assets.filter((asset) => view.attributeRefs!.find((attrRef) => attrRef.id === asset.id));
} catch (e) {
console.error("Failed to get assets requested in settings", e);
@@ -622,18 +612,8 @@ export class OrAttributeCard extends LitElement {
}
const viewSelector = window.location.hash;
- const configStr = await manager.console.retrieveData("OrChartConfig");
- let allConfigs: OrChartConfig[] = [];
- let config: OrChartConfig | undefined;
-
- if (configStr) {
- try {
- allConfigs = JSON.parse(configStr);
- config = allConfigs.find(e => e.realm === this.realm);
- } catch (e) {
- console.error("Failed to load chart config", e);
- }
- }
+ let allConfigs: OrChartConfig[] = await manager.console.retrieveData("OrChartConfig") || [];
+ let config: OrChartConfig | undefined = allConfigs.find(e => e.realm === this.realm);
if (!config) {
config = {
@@ -663,7 +643,7 @@ export class OrAttributeCard extends LitElement {
}
allConfigs = [...allConfigs.filter(e => e.realm !== this.realm), config];
- manager.console.storeData("OrChartConfig", JSON.stringify(allConfigs));
+ manager.console.storeData("OrChartConfig", allConfigs);
}
protected async loadData() {
diff --git a/ui/component/or-chart/src/index.ts b/ui/component/or-chart/src/index.ts
index 91f5d5946..6580dfb20 100644
--- a/ui/component/or-chart/src/index.ts
+++ b/ui/component/or-chart/src/index.ts
@@ -686,29 +686,24 @@ export class OrChart extends translate(i18next)(LitElement) {
this.realm = manager.getRealm();
}
- const configStr = await manager.console.retrieveData("OrChartConfig");
-
- if (!configStr || !this.panelName) {
+ if (!this.panelName) {
return;
}
const viewSelector = window.location.hash;
- let allConfigs: OrChartConfig[] = [];
- let config: OrChartConfig;
+ const allConfigs: OrChartConfig[] = await manager.console.retrieveData("OrChartConfig") || [];
- try {
- allConfigs = JSON.parse(configStr);
- if (!Array.isArray(allConfigs)) {
- manager.console.storeData("OrChartConfig", JSON.stringify([allConfigs]));
- }
- config = allConfigs.find(e => e.realm === this.realm) as OrChartConfig;
- } catch (e) {
- console.error("Failed to load chart config", e);
- manager.console.storeData("OrChartConfig", null);
+ if (!Array.isArray(allConfigs)) {
+ manager.console.storeData("OrChartConfig", [allConfigs]);
+ }
+
+ let config: OrChartConfig | undefined = allConfigs.find(e => e.realm === this.realm);
+
+ if (!config) {
return;
}
- const view = config && config.views && config.views[viewSelector] ? config.views[viewSelector][this.panelName] : undefined;
+ const view = config.views && config.views[viewSelector] ? config.views[viewSelector][this.panelName] : undefined;
if (!view) {
return;
@@ -718,7 +713,7 @@ export class OrChart extends translate(i18next)(LitElement) {
// Old/invalid config format remove it
delete config.views[viewSelector][this.panelName];
const cleanData = [...allConfigs.filter(e => e.realm !== this.realm), config];
- manager.console.storeData("OrChartConfig", JSON.stringify(cleanData));
+ manager.console.storeData("OrChartConfig", cleanData);
return;
}
@@ -744,8 +739,7 @@ export class OrChart extends translate(i18next)(LitElement) {
const assets = response.data || [];
view.attributeRefs = view.attributeRefs.filter((attrRef) => !!assets.find((asset) => asset.id === attrRef.id && asset.attributes && asset.attributes.hasOwnProperty(attrRef.name!)));
- allConfigs = [...allConfigs.filter(e => e.realm !== this.realm), config];
- manager.console.storeData("OrChartConfig", JSON.stringify(allConfigs));
+ manager.console.storeData("OrChartConfig", [...allConfigs.filter(e => e.realm !== this.realm), config]);
this.assets = assets.filter((asset) => view.attributeRefs!.find((attrRef) => attrRef.id === asset.id));
} catch (e) {
console.error("Failed to get assets requested in settings", e);
@@ -773,19 +767,9 @@ export class OrChart extends translate(i18next)(LitElement) {
}
const viewSelector = window.location.hash;
- const configStr = await manager.console.retrieveData("OrChartConfig");
- let allConfigs: OrChartConfig[] = [];
- let config: OrChartConfig | undefined;
+ const allConfigs: OrChartConfig[] = await manager.console.retrieveData("OrChartConfig") || [];
+ let config: OrChartConfig | undefined = allConfigs.find(e => e.realm === this.realm);
- if (configStr) {
- try {
- allConfigs = JSON.parse(configStr);
- config = allConfigs.find(e => e.realm === this.realm);
- } catch (e) {
- console.error("Failed to load chart config", e);
- }
- }
-
if (!config) {
config = {
realm: this.realm,
@@ -812,8 +796,7 @@ export class OrChart extends translate(i18next)(LitElement) {
};
}
- allConfigs = [...allConfigs.filter(e => e.realm !== this.realm), config];
- manager.console.storeData("OrChartConfig", JSON.stringify(allConfigs));
+ manager.console.storeData("OrChartConfig", [...allConfigs.filter(e => e.realm !== this.realm), config]);
}
_openDialog() {