Skip to content

armgit / core

Public Imported from GitHub
0

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

Improve converter methods in Values

Rich Turnerfcdf7918f01f
diff --git a/model/src/main/java/org/openremote/model/value/Values.java b/model/src/main/java/org/openremote/model/value/Values.java
index da3127554..7eb6d2cf3 100644
--- a/model/src/main/java/org/openremote/model/value/Values.java
+++ b/model/src/main/java/org/openremote/model/value/Values.java
@@ -15,7 +15,8 @@
*/
package org.openremote.model.value;
-import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.ObjectReader;
+import com.fasterxml.jackson.databind.ObjectWriter;
import com.google.gwt.core.shared.GwtIncompatible;
import jsinterop.annotations.JsIgnore;
import jsinterop.annotations.JsMethod;
@@ -23,7 +24,6 @@ import jsinterop.base.Any;
import org.openremote.model.attribute.Attribute;
import org.openremote.model.attribute.MetaItem;
import org.openremote.model.attribute.MetaItemDescriptor;
-import org.openremote.model.rules.flow.Option;
import org.openremote.model.value.impl.ValueFactoryImpl;
import java.util.List;
@@ -323,13 +323,14 @@ public class Values {
@SuppressWarnings("unchecked")
@JsIgnore
@GwtIncompatible
- public static <T extends Value> Optional<T> convertToValue(Object object, ObjectMapper objectMapper) {
- if (object == null || objectMapper == null) {
+ public static <T extends Value> Optional<T> convertToValue(Object object, ObjectWriter writer) {
+ if (object == null || writer == null) {
return Optional.empty();
}
try {
- Value v = parse(objectMapper.writeValueAsString(object)).orElse(null);
+ Value v;
+ v = parse(writer.writeValueAsString(object)).orElse(null);
return Optional.ofNullable((T)v);
} catch (Exception ignored) {
}
@@ -339,14 +340,14 @@ public class Values {
@JsIgnore
@GwtIncompatible
- public static <T> Optional<T> convertFromValue(Value value, Class<T> clazz, ObjectMapper objectMapper) {
- if (value == null || objectMapper == null) {
+ public static <T> Optional<T> convertFromValue(Value value, Class<T> clazz, ObjectReader reader) {
+ if (value == null || reader == null) {
return Optional.empty();
}
try {
String str = value.toJson();
- return Optional.of(objectMapper.readValue(str, clazz));
+ return Optional.of(reader.forType(clazz).readValue(str));
} catch (Exception ignored) {
}
diff --git a/test/src/test/groovy/org/openremote/test/assets/AssetAttributeLinkingTest.groovy b/test/src/test/groovy/org/openremote/test/assets/AssetAttributeLinkingTest.groovy
index fbe217def..82715a8b4 100644
--- a/test/src/test/groovy/org/openremote/test/assets/AssetAttributeLinkingTest.groovy
+++ b/test/src/test/groovy/org/openremote/test/assets/AssetAttributeLinkingTest.groovy
@@ -58,18 +58,18 @@ class AssetAttributeLinkingTest extends Specification implements ManagerContaine
converterOnOff.put("PRESSED", "@TOGGLE")
converterOnOff.put("RELEASED", "@IGNORE")
converterOnOff.put("LONG_PRESSED", "@IGNORE")
- def attributeLinkOnOff = Values.convertToValue(new AttributeLink(new AttributeRef(asset2.id, "lightOnOff"), converterOnOff, null), Container.JSON).orElse(null)
+ def attributeLinkOnOff = Values.convertToValue(new AttributeLink(new AttributeRef(asset2.id, "lightOnOff"), converterOnOff, null), Container.JSON.writer()).orElse(null)
def converterCounter = Values.createObject()
converterCounter.put("PRESSED", "@INCREMENT")
converterCounter.put("RELEASED", "@DECREMENT")
converterCounter.put("LONG_PRESSED", "@IGNORE")
- def attributeLinkCounter = Values.convertToValue(new AttributeLink(new AttributeRef(asset2.id, "counter"), converterCounter, null), Container.JSON).orElse(null)
+ def attributeLinkCounter = Values.convertToValue(new AttributeLink(new AttributeRef(asset2.id, "counter"), converterCounter, null), Container.JSON.writer()).orElse(null)
def attributeLinkProp = Values.convertToValue(new AttributeLink(
new AttributeRef(asset2.id, "item2Prop1"), null, [
new JsonPathFilter("\$[1].prop1", true, false)
- ] as ValueFilter[]), Container.JSON).orElse(null)
+ ] as ValueFilter[]), Container.JSON.writer()).orElse(null)
asset1.getAttribute("button").get().addMeta(new MetaItem(MetaItemType.ATTRIBUTE_LINK, attributeLinkOnOff))
asset1.getAttribute("button").get().addMeta(new MetaItem(MetaItemType.ATTRIBUTE_LINK, attributeLinkCounter))
diff --git a/test/src/test/groovy/org/openremote/test/protocol/websocket/WebsocketClientProtocolTest.groovy b/test/src/test/groovy/org/openremote/test/protocol/websocket/WebsocketClientProtocolTest.groovy
index a09c495d8..5dbd8627e 100644
--- a/test/src/test/groovy/org/openremote/test/protocol/websocket/WebsocketClientProtocolTest.groovy
+++ b/test/src/test/groovy/org/openremote/test/protocol/websocket/WebsocketClientProtocolTest.groovy
@@ -204,7 +204,7 @@ class WebsocketClientProtocolTest extends Specification implements ManagerContai
new SubStringValueFilter(SharedEvent.MESSAGE_PREFIX.length()),
new JsonPathFilter("\$..attributeState.attributeRef.attributeName", false, false)
] as ValueFilter[]
- , Container.JSON).orElse(null)),
+ , Container.JSON.writer()).orElse(null)),
new MetaItem(WebsocketClientProtocol.META_ATTRIBUTE_MATCH_PREDICATE,
new StringPredicate(AssetQuery.Match.CONTAINS, true, "targetTemperature").toModelValue()),
new MetaItem(Protocol.META_ATTRIBUTE_VALUE_FILTERS,
@@ -213,14 +213,14 @@ class WebsocketClientProtocolTest extends Specification implements ManagerContai
new SubStringValueFilter(SharedEvent.MESSAGE_PREFIX.length()),
new JsonPathFilter("\$..events[?(@.attributeState.attributeRef.attributeName == \"targetTemperature\")].attributeState.value", true, false)
] as ValueFilter[]
- , Container.JSON).orElse(null)),
+ , Container.JSON.writer()).orElse(null)),
new MetaItem(WebsocketClientProtocol.META_SUBSCRIPTIONS,
Values.convertToValue(
[
new WebsocketSubscription().body(SharedEvent.MESSAGE_PREFIX + Container.JSON.writeValueAsString(
new ReadAssetAttributesEvent(managerDemoSetup.apartment1LivingroomId, "targetTemperature")
))
- ], Container.JSON).orElse(null))
+ ], Container.JSON.writer()).orElse(null))
),
new AssetAttribute("readCo2Level", AttributeValueType.NUMBER)
.addMeta(
@@ -232,7 +232,7 @@ class WebsocketClientProtocolTest extends Specification implements ManagerContai
new SubStringValueFilter(SharedEvent.MESSAGE_PREFIX.length()),
new JsonPathFilter("\$..attributeState.attributeRef.attributeName", false, false)
] as ValueFilter[]
- , Container.JSON).orElse(null)),
+ , Container.JSON.writer()).orElse(null)),
new MetaItem(WebsocketClientProtocol.META_ATTRIBUTE_MATCH_PREDICATE,
new StringPredicate(AssetQuery.Match.CONTAINS, "co2Level").toModelValue()),
new MetaItem(Protocol.META_ATTRIBUTE_VALUE_FILTERS,
@@ -241,7 +241,7 @@ class WebsocketClientProtocolTest extends Specification implements ManagerContai
new SubStringValueFilter(SharedEvent.MESSAGE_PREFIX.length()),
new JsonPathFilter("\$..events[?(@.attributeState.attributeRef.attributeName == \"co2Level\")].attributeState.value", true, false),
] as ValueFilter[]
- , Container.JSON).orElse(null)),
+ , Container.JSON.writer()).orElse(null)),
new MetaItem(WebsocketClientProtocol.META_SUBSCRIPTIONS,
Values.convertToValue(
[
@@ -249,7 +249,7 @@ class WebsocketClientProtocolTest extends Specification implements ManagerContai
new ReadAssetAttributesEvent(managerDemoSetup.apartment1LivingroomId, "co2Level")
))
]
- , Container.JSON).orElse(null))
+ , Container.JSON.writer()).orElse(null))
)
)