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 unknown value type handling (#1169)

Rich Turner3a636911bbd8
diff --git a/manager/src/web/shared/locales/en/or.json b/manager/src/web/shared/locales/en/or.json
index b8f5b28df..9c78be9ee 100644
--- a/manager/src/web/shared/locales/en/or.json
+++ b/manager/src/web/shared/locales/en/or.json
@@ -453,7 +453,9 @@
},
"meta": {},
"attribute": {},
- "value": {}
+ "value": {
+ "unknown": "Unknown"
+ }
},
"metaOverrides": {
"label": {}
diff --git a/manager/src/web/shared/locales/nl/or.json b/manager/src/web/shared/locales/nl/or.json
index 2dccafff3..04bb0228e 100644
--- a/manager/src/web/shared/locales/nl/or.json
+++ b/manager/src/web/shared/locales/nl/or.json
@@ -453,7 +453,9 @@
},
"meta": {},
"attribute": {},
- "value": {}
+ "value": {
+ "unknown": "Onbekend"
+ }
},
"metaOverrides": {
"label": {}
diff --git a/model/src/main/java/org/openremote/model/value/ValueType.java b/model/src/main/java/org/openremote/model/value/ValueType.java
index bfd34781e..31ed08cb8 100644
--- a/model/src/main/java/org/openremote/model/value/ValueType.java
+++ b/model/src/main/java/org/openremote/model/value/ValueType.java
@@ -48,6 +48,7 @@ import java.util.List;
public final class ValueType {
/* SOME CUSTOM TYPES TO AVOID GENERIC TYPE SO THESE CAN BE CONSUMED IN VALUE DESCRIPTORS */
+ public static class ObjectMap extends HashMap<String, Object> {}
public static class StringMap extends HashMap<String, String> {}
public static class IntegerMap extends HashMap<String, Integer> {}
public static class DoubleMap extends HashMap<String, Double> {}
@@ -76,6 +77,10 @@ public final class ValueType {
public static final ValueDescriptor<StringMap> TEXT_MAP = new ValueDescriptor<>("textMap", StringMap.class);
+ public static final ValueDescriptor<ObjectMap> JSON_OBJECT = new ValueDescriptor<>("JSONObject", ObjectMap.class);
+
+ public static final ValueDescriptor<Object> JSON = new ValueDescriptor<>("JSON", Object.class);
+
public static final ValueDescriptor<MultivaluedStringMap> MULTIVALUED_TEXT_MAP = new ValueDescriptor<>("multivaluedTextMap", MultivaluedStringMap.class);
public static final ValueDescriptor<Integer> POSITIVE_INTEGER = new ValueDescriptor<>("positiveInteger", Integer.class,
diff --git a/test/src/test/groovy/org/openremote/test/model/AssetModelTest.groovy b/test/src/test/groovy/org/openremote/test/model/AssetModelTest.groovy
index f0cb4a86b..f211b832d 100644
--- a/test/src/test/groovy/org/openremote/test/model/AssetModelTest.groovy
+++ b/test/src/test/groovy/org/openremote/test/model/AssetModelTest.groovy
@@ -168,7 +168,7 @@ class AssetModelTest extends Specification implements ManagerContainerTrait {
new Attribute<>(ModelTestAsset.NOT_BLANK_STRING_ATTRIBUTE_DESCRIPTOR, "abcde"),
new Attribute<>(ModelTestAsset.OBJECT_ATTRIBUTE_DESCRIPTOR, new ModelTestAsset.TestObject(110, true)),
// Custom attributes
- new Attribute<>("custom1"),
+ new Attribute<>("custom1", null, 456), // Any value can go in here
new Attribute<>("custom2", ValueType.GEO_JSON_POINT, new GeoJSONPoint(1.234, 5.678)).addMeta(
new MetaItem<>(MetaItemType.CONSTRAINTS, ValueConstraint.constraints(new ValueConstraint.NotNull()))
)
@@ -200,7 +200,7 @@ class AssetModelTest extends Specification implements ManagerContainerTrait {
modelTestAsset.getAttribute(ModelTestAsset.NOT_EMPTY_ARRAY_ATTRIBUTE_DESCRIPTOR).flatMap {it.value}.orElse(null).length == 1
modelTestAsset.getAttribute(ModelTestAsset.NOT_EMPTY_ARRAY_ATTRIBUTE_DESCRIPTOR).flatMap {it.value}.orElse(null)[0] == 1
modelTestAsset.getAttribute(ModelTestAsset.NOT_BLANK_STRING_ATTRIBUTE_DESCRIPTOR).flatMap {it.value}.orElse(null) == "abcde"
- modelTestAsset.getAttribute("custom1").flatMap {it.value}.orElse(null) == null
+ modelTestAsset.getAttribute("custom1").flatMap {it.value}.orElse(null) == 456
modelTestAsset.getAttribute("custom2").flatMap {it.value}.orElse(null).x == 1.234d
modelTestAsset.getAttribute("custom2").flatMap {it.value}.orElse(null).y == 5.678d
modelTestAsset.getAttribute(ModelTestAsset.OBJECT_ATTRIBUTE_DESCRIPTOR).flatMap {it.value} != null
@@ -284,6 +284,9 @@ class AssetModelTest extends Specification implements ManagerContainerTrait {
and: "we simulate a value descriptor being removed or changed (by changing the type name of an attribute)"
((ObjectNode)((ObjectNode)assetObjectNode.get("attributes")).get(ModelTestAsset.PAST_OR_PRESENT_DATE_ATTRIBUTE_DESCRIPTOR.name)).put("type", "missingType")
+ and: "we simulate a value descriptor being removed on a custom attribute (by changing the type name of the attribute)"
+ ((ObjectNode)((ObjectNode)assetObjectNode.get("attributes")).get("custom1")).put("type", "missingType2")
+
and: "we deserialise the object back into an Asset"
modelTestAsset = ValueUtil.parse(assetObjectNode.toString(), Asset).orElse(null) as ModelTestAsset
@@ -298,6 +301,13 @@ class AssetModelTest extends Specification implements ManagerContainerTrait {
Array.getLength(modelTestAsset.getAttribute(ModelTestAsset.NOT_EMPTY_STRING_ATTRIBUTE_DESCRIPTOR).flatMap {it.getMeta().get("missingMeta")}.flatMap {it.value}.orElse(null)) == 1
Array.get(modelTestAsset.getAttribute(ModelTestAsset.NOT_EMPTY_STRING_ATTRIBUTE_DESCRIPTOR).flatMap {it.getMeta().get("missingMeta")}.flatMap {it.value}.orElse(null), 0) instanceof Map
+ and: "the well known attribute with the missing value type should have the correct value type"
+ modelTestAsset.getAttribute(ModelTestAsset.PAST_OR_PRESENT_DATE_ATTRIBUTE_DESCRIPTOR.name).map {it.type}.orElse(null) == ModelTestAsset.PAST_OR_PRESENT_DATE_ATTRIBUTE_DESCRIPTOR.type
+
+ and: "the custom attribute with the missing value type should have no value type but the value should still be there"
+ modelTestAsset.getAttribute("custom1").map {it.type}.isEmpty()
+ modelTestAsset.getAttribute("custom1").flatMap {it.value}.orElse(null) == 456
+
when: "the attribute with the missing attribute descriptor now violates its' previous constraint"
modelTestAsset.getAttribute("missingAttr").ifPresent {it.value = timerService.getCurrentTimeMillis() + 100000}
diff --git a/ui/component/core/src/util.ts b/ui/component/core/src/util.ts
index 2ce002eb4..bb6b30318 100644
--- a/ui/component/core/src/util.ts
+++ b/ui/component/core/src/util.ts
@@ -530,7 +530,10 @@ export function getAssetTypeLabel(type: string | AssetDescriptor | undefined): s
return i18next.t("label.asset." + type.name, {defaultValue: camelCaseToSentenceCase(type.name!)});
}
-export function getValueDescriptorLabel(descriptor: ValueDescriptor | string): string {
+export function getValueDescriptorLabel(descriptor: ValueDescriptor | undefined | string): string {
+ if (!descriptor) {
+ return i18next.t("label.value.unknown", {defaultValue: "Unknown"});
+ }
const name = (typeof(descriptor) === "string" ? descriptor : descriptor.name);
return i18next.t("label.value." + name, {defaultValue: camelCaseToSentenceCase(name || "")});
}
diff --git a/ui/component/model/src/util.ts b/ui/component/model/src/util.ts
index 92037e75e..540c929a4 100644
--- a/ui/component/model/src/util.ts
+++ b/ui/component/model/src/util.ts
@@ -121,7 +121,7 @@ export class AssetModelUtil {
if (!valueDescriptor && valueHolder) {
// Try and determine the value descriptor based on the value type
- valueDescriptor = this.resolveValueDescriptorFromValue(valueHolder.value);
+ valueDescriptor = AssetModelUtil.getValueDescriptor(WellknownValueTypes.JSON);
}
return valueDescriptor;
@@ -168,11 +168,6 @@ export class AssetModelUtil {
}
}
- public static resolveValueDescriptorFromValue(value: any): ValueDescriptor | undefined {
- const valueType = AssetModelUtil.resolveValueTypeFromValue(value);
- return AssetModelUtil.getValueDescriptor(valueType);
- }
-
public static getAttributeAndValueDescriptors(assetType: string | undefined, attributeNameOrDescriptor: string | AttributeDescriptor | undefined, attribute?: Attribute<any>): [AttributeDescriptor | undefined, ValueDescriptor | undefined] {
let attributeDescriptor: AttributeDescriptor | undefined;
let valueDescriptor: ValueDescriptor | undefined;
diff --git a/ui/component/or-mwc-components/src/or-mwc-input.ts b/ui/component/or-mwc-components/src/or-mwc-input.ts
index 6127f10d0..652aa1d6a 100644
--- a/ui/component/or-mwc-components/src/or-mwc-input.ts
+++ b/ui/component/or-mwc-components/src/or-mwc-input.ts
@@ -108,6 +108,7 @@ export enum InputType {
DATETIME = "datetime-local",
EMAIL = "email",
JSON = "json",
+ JSON_OBJECT = "json-object",
MONTH = "month",
NUMBER = "number",
BIG_INT = "big-int",
@@ -164,6 +165,7 @@ function inputTypeSupportsButton(inputType: InputType): boolean {
|| inputType === InputType.DATETIME
|| inputType === InputType.EMAIL
|| inputType === InputType.JSON
+ || inputType === InputType.JSON_OBJECT
|| inputType === InputType.MONTH
|| inputType === InputType.TEXTAREA
|| inputType === InputType.TIME
@@ -277,6 +279,9 @@ export const getValueHolderInputTemplateProvider: ValueInputProviderGenerator =
case WellknownValueTypes.TIMEANDPERIODDURATIONISO8601:
inputType = InputType.DURATION;
break;
+ case WellknownValueTypes.JSONOBJECT:
+ inputType = InputType.JSON_OBJECT;
+ break;
}
if (valueDescriptor.arrayDimensions && valueDescriptor.arrayDimensions > 0) {
@@ -1288,7 +1293,8 @@ export class OrMwcInput extends LitElement {
case InputType.URL:
case InputType.TEXT:
case InputType.TEXTAREA:
- case InputType.JSON: {
+ case InputType.JSON:
+ case InputType.JSON_OBJECT: {
// The following HTML input types require the values as specially formatted strings
let valMinMax: [any, any, any] = [this.value === undefined || this.value === null ? undefined : this.value, this.min, this.max];
@@ -1361,10 +1367,10 @@ export class OrMwcInput extends LitElement {
"mdc-text-field--invalid": !this.valid,
"mdc-text-field--filled": !outlined,
"mdc-text-field--outlined": outlined,
- "mdc-text-field--textarea": type === InputType.TEXTAREA || type === InputType.JSON,
+ "mdc-text-field--textarea": type === InputType.TEXTAREA || type === InputType.JSON || type === InputType.JSON_OBJECT,
"mdc-text-field--disabled": this.disabled,
"mdc-text-field--fullwidth": this.fullWidth && !outlined,
- "dense-comfortable": this.comfortable && !(type === InputType.TEXTAREA || type === InputType.JSON),
+ "dense-comfortable": this.comfortable && !(type === InputType.TEXTAREA || type === InputType.JSON || type === InputType.JSON_OBJECT),
"dense-compact": !this.comfortable && this.compact,
"mdc-text-field--label-floating": hasValue,
"mdc-text-field--no-label": !this.label,
@@ -1373,7 +1379,7 @@ export class OrMwcInput extends LitElement {
"or-mwc-input--rounded": this.rounded
};
- inputElem = type === InputType.TEXTAREA || type === InputType.JSON
+ inputElem = type === InputType.TEXTAREA || type === InputType.JSON || type === InputType.JSON_OBJECT
? html`
<textarea id="elem" class="mdc-text-field__input ${this.resizeVertical ? "resize-vertical" : ""}" ?required="${this.required}"
?readonly="${this.readonly}" ?disabled="${this.disabled}" minlength="${ifDefined(this.minLength)}"
@@ -1676,9 +1682,9 @@ export class OrMwcInput extends LitElement {
valid = nativeValidity.valid;
}
- if (valid && this.type === InputType.JSON) {
+ if (valid && (this.type === InputType.JSON || this.type === InputType.JSON_OBJECT)) {
// JSON needs special validation - if no text value but this.value then parsing failed
- if (this.value !== undefined && (this._mdcComponent as MDCTextField).value === "") {
+ if (this.value !== undefined && this.value !== null && (this._mdcComponent as MDCTextField).value === "") {
valid = false;
}
}
@@ -1718,6 +1724,7 @@ export class OrMwcInput extends LitElement {
newValue = newValue === "on";
break;
case InputType.JSON:
+ case InputType.JSON_OBJECT:
case InputType.NUMBER:
case InputType.RANGE:
if (newValue === "") {
@@ -1725,9 +1732,13 @@ export class OrMwcInput extends LitElement {
} else {
try {
newValue = JSON.parse(newValue);
+ if (this.type === InputType.JSON_OBJECT && (typeof newValue !== 'object' || Array.isArray(newValue))) {
+ newValue = this.value;
+ errorMsg = i18next.t("validation.invalidJSON");
+ }
} catch (e) {
newValue = this.value;
- errorMsg = this.type === InputType.JSON ? i18next.t("validation.invalidJSON") : i18next.t("validation.invalidNumber");
+ errorMsg = this.type === InputType.JSON || this.type == InputType.JSON_OBJECT ? i18next.t("validation.invalidJSON") : i18next.t("validation.invalidNumber");
}
}
break;