Skip to content

armgit / core

Public Imported from GitHub
0

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

Values helper method rename

Rich Turner0cff7e96b049
diff --git a/agent/src/main/java/org/openremote/agent/protocol/Protocol.java b/agent/src/main/java/org/openremote/agent/protocol/Protocol.java
index a9323e6a7..8d320dce8 100644
--- a/agent/src/main/java/org/openremote/agent/protocol/Protocol.java
+++ b/agent/src/main/java/org/openremote/agent/protocol/Protocol.java
@@ -108,7 +108,7 @@ import static org.openremote.model.value.Values.NULL_LITERAL;
* way before being written to the linked attribute (see {@link #META_ATTRIBUTE_VALUE_CONVERTER})</li>
* <li>Automatic basic value conversion should be performed when the {@link ValueType} of the value produced by the
* protocol and any configured value conversion does not match the linked attributes underlying {@link ValueType}; this
- * basic conversion should use the {@link Values#convert} method</li>
+ * basic conversion should use the {@link Values#convertToValue} method</li>
* </ol>
* <h1>Outbound value conversion (Linked Attribute -> Protocol)</h1>
* Standard value conversion should be performed in the following order, this is encapsulated in
@@ -651,7 +651,7 @@ public interface Protocol extends ContainerService {
if (value != null && attributeValueType.isPresent()) {
if (attributeValueType.get() != value.getType()) {
LOG.fine("Trying to convert value: " + value.getType() + " -> " + attributeValueType.get());
- Optional<Value> convertedValue = Values.convert(value, attributeValueType.get());
+ Optional<Value> convertedValue = Values.convertToValue(value, attributeValueType.get());
if (!convertedValue.isPresent()) {
LOG.warning("Failed to convert value: " + value.getType() + " -> " + attributeValueType.get());
diff --git a/manager/src/main/java/org/openremote/manager/agent/AgentService.java b/manager/src/main/java/org/openremote/manager/agent/AgentService.java
index 91dbe5837..8a684b18c 100644
--- a/manager/src/main/java/org/openremote/manager/agent/AgentService.java
+++ b/manager/src/main/java/org/openremote/manager/agent/AgentService.java
@@ -908,7 +908,7 @@ public class AgentService extends RouteBuilder implements ContainerService, Asse
LOG.fine("Value filter type unknown: " + filter.getValueType().getName());
value = null;
} else {
- Optional<Value> val = Values.convert(value, filterValueType);
+ Optional<Value> val = Values.convertToValue(value, filterValueType);
if (!val.isPresent()) {
LOG.fine("Value filter type '" + filter.getValueType().getName()
+ "' is not compatible with actual value type '" + value.getType().getModelType().getName()
diff --git a/manager/src/main/java/org/openremote/manager/asset/AssetAttributeLinkingService.java b/manager/src/main/java/org/openremote/manager/asset/AssetAttributeLinkingService.java
index af00fa5fd..50f2fb7d2 100644
--- a/manager/src/main/java/org/openremote/manager/asset/AssetAttributeLinkingService.java
+++ b/manager/src/main/java/org/openremote/manager/asset/AssetAttributeLinkingService.java
@@ -174,7 +174,7 @@ public class AssetAttributeLinkingService implements ContainerService, AssetUpda
if (value != null && attributeValueType.isPresent()) {
if (attributeValueType.get() != value.getType()) {
LOG.fine("Trying to convert value: " + value.getType() + " -> " + attributeValueType.get());
- Optional<Value> convertedValue = Values.convert(value, attributeValueType.get());
+ Optional<Value> convertedValue = Values.convertToValue(value, attributeValueType.get());
if (!convertedValue.isPresent()) {
LOG.warning("Failed to convert value: " + value.getType() + " -> " + attributeValueType.get());
LOG.warning("Cannot send linked attribute update");
diff --git a/model/src/main/java/org/openremote/model/query/AssetQuery.java b/model/src/main/java/org/openremote/model/query/AssetQuery.java
index 4b552cb72..40db5d34e 100644
--- a/model/src/main/java/org/openremote/model/query/AssetQuery.java
+++ b/model/src/main/java/org/openremote/model/query/AssetQuery.java
@@ -19,6 +19,7 @@
*/
package org.openremote.model.query;
+import org.openremote.model.asset.AssetDescriptor;
import org.openremote.model.asset.AssetType;
import org.openremote.model.attribute.MetaItemDescriptor;
import org.openremote.model.query.filter.*;
@@ -344,7 +345,7 @@ public class AssetQuery {
return this;
}
- public AssetQuery types(AssetType... types) {
+ public AssetQuery types(AssetDescriptor... types) {
if (types == null || types.length == 0) {
this.types = null;
return this;
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 f7b865b0a..20b8606dd 100644
--- a/model/src/main/java/org/openremote/model/value/Values.java
+++ b/model/src/main/java/org/openremote/model/value/Values.java
@@ -23,6 +23,7 @@ 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;
@@ -111,7 +112,7 @@ public class Values {
*/
public static Optional<Boolean> getBooleanCoerced(Value value) {
- return convert(value, BooleanValue.class)
+ return convertToValue(value, BooleanValue.class)
.map(BooleanValue::getBoolean);
}
@@ -120,7 +121,7 @@ public class Values {
*/
public static Optional<Integer> getIntegerCoerced(Value value) {
- return convert(value, NumberValue.class)
+ return convertToValue(value, NumberValue.class)
.map(NumberValue::getNumber)
.map(Double::intValue);
}
@@ -130,7 +131,7 @@ public class Values {
*/
public static Optional<Long> getLongCoerced(Value value) {
- return convert(value, NumberValue.class)
+ return convertToValue(value, NumberValue.class)
.map(NumberValue::getNumber)
.map(Double::longValue);
}
@@ -232,12 +233,12 @@ public class Values {
return Optional.of((T)value.get());
}
- public static <T extends Value> Optional<T> convert(Value value, Class<T> toType) {
- return convert(value, ValueType.fromModelType(toType));
+ public static <T extends Value> Optional<T> convertToValue(Value value, Class<T> toType) {
+ return convertToValue(value, ValueType.fromModelType(toType));
}
@SuppressWarnings("unchecked")
- public static <T extends Value> Optional<T> convert(Value value, ValueType toType) {
+ public static <T extends Value> Optional<T> convertToValue(Value value, ValueType toType) {
if (value == null) {
return Optional.empty();
}
@@ -310,7 +311,7 @@ public class Values {
Value firstValue = arrayValue.get(0).orElse(null);
if (firstValue != null) {
- return convert(firstValue, toType);
+ return convertToValue(firstValue, toType);
}
}
}
@@ -322,7 +323,7 @@ public class Values {
@SuppressWarnings("unchecked")
@JsIgnore
@GwtIncompatible
- public static <T extends Value> Optional<T> convert(Object object, ObjectMapper objectMapper) {
+ public static <T extends Value> Optional<T> convertToValue(Object object, ObjectMapper objectMapper) {
if (object == null || objectMapper == null) {
return Optional.empty();
}
@@ -335,4 +336,18 @@ public class Values {
return Optional.empty();
}
+
+ public static <T> Optional<T> convertFromValue(Value value, Class<T> clazz, ObjectMapper objectMapper) {
+ if (value == null || objectMapper == null) {
+ return Optional.empty();
+ }
+
+ try {
+ String str = value.toJson();
+ return Optional.of(objectMapper.readValue(str, clazz));
+ } catch (Exception ignored) {
+ }
+
+ return Optional.empty();
+ }
}
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 e7f458d54..fbe217def 100644
--- a/test/src/test/groovy/org/openremote/test/assets/AssetAttributeLinkingTest.groovy
+++ b/test/src/test/groovy/org/openremote/test/assets/AssetAttributeLinkingTest.groovy
@@ -58,15 +58,15 @@ class AssetAttributeLinkingTest extends Specification implements ManagerContaine
converterOnOff.put("PRESSED", "@TOGGLE")
converterOnOff.put("RELEASED", "@IGNORE")
converterOnOff.put("LONG_PRESSED", "@IGNORE")
- def attributeLinkOnOff = Values.convert(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).orElse(null)
def converterCounter = Values.createObject()
converterCounter.put("PRESSED", "@INCREMENT")
converterCounter.put("RELEASED", "@DECREMENT")
converterCounter.put("LONG_PRESSED", "@IGNORE")
- def attributeLinkCounter = Values.convert(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).orElse(null)
- def attributeLinkProp = Values.convert(new AttributeLink(
+ def attributeLinkProp = Values.convertToValue(new AttributeLink(
new AttributeRef(asset2.id, "item2Prop1"), null, [
new JsonPathFilter("\$[1].prop1", true, false)
] as ValueFilter[]), Container.JSON).orElse(null)
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 656f08f72..a09c495d8 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
@@ -199,7 +199,7 @@ class WebsocketClientProtocolTest extends Specification implements ManagerContai
"targetTemperature",
Values.create(0.12345))).replace("0.12345", Protocol.DYNAMIC_VALUE_PLACEHOLDER) + "\'")),
new MetaItem(WebsocketClientProtocol.META_ATTRIBUTE_MATCH_FILTERS,
- Values.convert(
+ Values.convertToValue(
[
new SubStringValueFilter(SharedEvent.MESSAGE_PREFIX.length()),
new JsonPathFilter("\$..attributeState.attributeRef.attributeName", false, false)
@@ -208,14 +208,14 @@ class WebsocketClientProtocolTest extends Specification implements ManagerContai
new MetaItem(WebsocketClientProtocol.META_ATTRIBUTE_MATCH_PREDICATE,
new StringPredicate(AssetQuery.Match.CONTAINS, true, "targetTemperature").toModelValue()),
new MetaItem(Protocol.META_ATTRIBUTE_VALUE_FILTERS,
- Values.convert(
+ Values.convertToValue(
[
new SubStringValueFilter(SharedEvent.MESSAGE_PREFIX.length()),
new JsonPathFilter("\$..events[?(@.attributeState.attributeRef.attributeName == \"targetTemperature\")].attributeState.value", true, false)
] as ValueFilter[]
, Container.JSON).orElse(null)),
new MetaItem(WebsocketClientProtocol.META_SUBSCRIPTIONS,
- Values.convert(
+ Values.convertToValue(
[
new WebsocketSubscription().body(SharedEvent.MESSAGE_PREFIX + Container.JSON.writeValueAsString(
new ReadAssetAttributesEvent(managerDemoSetup.apartment1LivingroomId, "targetTemperature")
@@ -227,7 +227,7 @@ class WebsocketClientProtocolTest extends Specification implements ManagerContai
new MetaItem(MetaItemType.AGENT_LINK, new AttributeRef(agent.id, "protocolConfig").toArrayValue()),
new MetaItem(MetaItemType.READ_ONLY),
new MetaItem(WebsocketClientProtocol.META_ATTRIBUTE_MATCH_FILTERS,
- Values.convert(
+ Values.convertToValue(
[
new SubStringValueFilter(SharedEvent.MESSAGE_PREFIX.length()),
new JsonPathFilter("\$..attributeState.attributeRef.attributeName", false, false)
@@ -236,14 +236,14 @@ class WebsocketClientProtocolTest extends Specification implements ManagerContai
new MetaItem(WebsocketClientProtocol.META_ATTRIBUTE_MATCH_PREDICATE,
new StringPredicate(AssetQuery.Match.CONTAINS, "co2Level").toModelValue()),
new MetaItem(Protocol.META_ATTRIBUTE_VALUE_FILTERS,
- Values.convert(
+ Values.convertToValue(
[
new SubStringValueFilter(SharedEvent.MESSAGE_PREFIX.length()),
new JsonPathFilter("\$..events[?(@.attributeState.attributeRef.attributeName == \"co2Level\")].attributeState.value", true, false),
] as ValueFilter[]
, Container.JSON).orElse(null)),
new MetaItem(WebsocketClientProtocol.META_SUBSCRIPTIONS,
- Values.convert(
+ Values.convertToValue(
[
new WebsocketSubscription().body(SharedEvent.MESSAGE_PREFIX + Container.JSON.writeValueAsString(
new ReadAssetAttributesEvent(managerDemoSetup.apartment1LivingroomId, "co2Level")