Skip to content

armgit / core

Public Imported from GitHub
0

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

Model related updates

Rich Turner085dc0b99e9b
diff --git a/model/build.gradle b/model/build.gradle
index 8869a573f..9af2d278d 100644
--- a/model/build.gradle
+++ b/model/build.gradle
@@ -7,6 +7,7 @@ dependencies {
compile "com.fasterxml.jackson.core:jackson-databind:$jacksonVersion"
compile("com.google.gwt:gwt-user:$gwtVersion") {
exclude group: "javax.servlet"
+ exclude group: "org.sonatype.sisu.inject"
}
compile "org.hibernate.javax.persistence:hibernate-jpa-2.1-api:$javaxPersistenceVersion"
compile "org.hibernate:hibernate-core:$hibernateVersion"
@@ -22,10 +23,6 @@ dependencies {
compile "org.keycloak:keycloak-core:$keycloakVersion:sources"
compile "org.jeasy:easy-rules-core:$easyRulesVersion"
-
- compile("com.google.inject:guice:4.2.2") {
- force = true
- }
}
jar {
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 4ddc664b6..6ca1d97f9 100644
--- a/model/src/main/java/org/openremote/model/value/Values.java
+++ b/model/src/main/java/org/openremote/model/value/Values.java
@@ -15,6 +15,10 @@
*/
package org.openremote.model.value;
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.google.gwt.core.shared.GwtIncompatible;
+import jsinterop.annotations.JsIgnore;
import jsinterop.annotations.JsMethod;
import jsinterop.base.Any;
import org.openremote.model.asset.AssetAttribute;
@@ -285,9 +289,38 @@ public class Values {
break;
}
break;
+ case ARRAY:
+
+ // Only works when the array has a single value
+ ArrayValue arrayValue = (ArrayValue)value;
+ if (arrayValue.length() == 1) {
+
+ Value firstValue = arrayValue.get(0).orElse(null);
+
+ if (firstValue != null) {
+ return convert(firstValue, toType);
+ }
+ }
}
}
return Optional.ofNullable(outputValue);
}
+
+ @SuppressWarnings("unchecked")
+ @JsIgnore
+ @GwtIncompatible
+ public static <T extends Value> Optional<T> convert(Object object, ObjectMapper objectMapper) {
+ if (object == null || objectMapper == null) {
+ return Optional.empty();
+ }
+
+ try {
+ Value v = parse(objectMapper.writeValueAsString(object)).orElse(null);
+ return Optional.ofNullable((T)v);
+ } 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 734a657d2..40fa71da6 100644
--- a/test/src/test/groovy/org/openremote/test/assets/AssetAttributeLinkingTest.groovy
+++ b/test/src/test/groovy/org/openremote/test/assets/AssetAttributeLinkingTest.groovy
@@ -3,7 +3,6 @@ package org.openremote.test.assets
import org.openremote.container.Container
import org.openremote.manager.asset.AssetProcessingService
import org.openremote.manager.asset.AssetStorageService
-
import org.openremote.model.Constants
import org.openremote.model.asset.Asset
import org.openremote.model.asset.AssetAttribute
@@ -61,18 +60,18 @@ class AssetAttributeLinkingTest extends Specification implements ManagerContaine
converterOnOff.put("PRESSED", "@TOGGLE")
converterOnOff.put("RELEASED", "@IGNORE")
converterOnOff.put("LONG_PRESSED", "@IGNORE")
- def attributeLinkOnOff = Values.parse(Container.JSON.writeValueAsString(new AttributeLink(new AttributeRef(asset2.id, "lightOnOff"), converterOnOff, null))).orElse(null)
+ def attributeLinkOnOff = Values.convert(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.parse(Container.JSON.writeValueAsString(new AttributeLink(new AttributeRef(asset2.id, "counter"), converterCounter, null))).orElse(null)
+ def attributeLinkCounter = Values.convert(new AttributeLink(new AttributeRef(asset2.id, "counter"), converterCounter, null), Container.JSON).orElse(null)
- def attributeLinkProp = Values.parse(Container.JSON.writeValueAsString(new AttributeLink(
+ def attributeLinkProp = Values.convert(new AttributeLink(
new AttributeRef(asset2.id, "item2Prop1"), null, [
new JsonPathFilter("\$[1].prop1")
- ] as ValueFilter[]))).orElse(null)
+ ] as ValueFilter[]), Container.JSON).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 6db7c8b61..382c25f27 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
@@ -19,23 +19,11 @@
*/
package org.openremote.test.protocol.websocket
-import io.netty.buffer.Unpooled
-import io.netty.channel.ChannelHandlerContext
-import io.netty.channel.socket.DatagramChannel
-import io.netty.handler.codec.FixedLengthFrameDecoder
-import io.netty.handler.codec.MessageToMessageEncoder
-import io.netty.handler.codec.bytes.ByteArrayDecoder
-import org.jboss.resteasy.spi.ResteasyUriInfo
-import org.jboss.resteasy.util.BasicAuthHelper
+
import org.openremote.agent.protocol.Protocol
import org.openremote.agent.protocol.ProtocolExecutorService
-import org.openremote.agent.protocol.http.OAuthGrant
import org.openremote.agent.protocol.http.OAuthPasswordGrant
-import org.openremote.agent.protocol.http.OAuthRefreshTokenGrant
-import org.openremote.agent.protocol.http.OAuthServerResponse
import org.openremote.agent.protocol.simulator.SimulatorProtocol
-import org.openremote.agent.protocol.udp.AbstractUdpServer
-import org.openremote.agent.protocol.udp.UdpClientProtocol
import org.openremote.agent.protocol.websocket.WebsocketClientProtocol
import org.openremote.agent.protocol.websocket.WebsocketHttpSubscription
import org.openremote.agent.protocol.websocket.WebsocketSubscription
@@ -46,11 +34,7 @@ import org.openremote.manager.asset.AssetStorageService
import org.openremote.manager.setup.SetupService
import org.openremote.manager.setup.builtin.ManagerDemoSetup
import org.openremote.model.Constants
-import org.openremote.model.asset.Asset
-import org.openremote.model.asset.AssetAttribute
-import org.openremote.model.asset.AssetEvent
-import org.openremote.model.asset.AssetType
-import org.openremote.model.asset.ReadAssetAttributesEvent
+import org.openremote.model.asset.*
import org.openremote.model.asset.agent.ConnectionStatus
import org.openremote.model.attribute.*
import org.openremote.model.event.shared.EventSubscription
@@ -58,11 +42,7 @@ import org.openremote.model.event.shared.SharedEvent
import org.openremote.model.query.AssetQuery
import org.openremote.model.query.BaseAssetQuery
import org.openremote.model.query.filter.StringPredicate
-import org.openremote.model.value.JsonPathFilter
-import org.openremote.model.value.ObjectValue
-import org.openremote.model.value.SubStringValueFilter
-import org.openremote.model.value.ValueFilter
-import org.openremote.model.value.Values
+import org.openremote.model.value.*
import org.openremote.test.ManagerContainerTrait
import spock.lang.Shared
import spock.lang.Specification
@@ -71,11 +51,9 @@ import spock.util.concurrent.PollingConditions
import javax.ws.rs.HttpMethod
import javax.ws.rs.client.ClientRequestContext
import javax.ws.rs.client.ClientRequestFilter
-import javax.ws.rs.core.Form
import javax.ws.rs.core.HttpHeaders
import javax.ws.rs.core.MediaType
import javax.ws.rs.core.Response
-import javax.ws.rs.core.UriInfo
import static org.openremote.container.util.MapAccess.getString
import static org.openremote.manager.setup.AbstractKeycloakSetup.SETUP_ADMIN_PASSWORD
@@ -224,58 +202,59 @@ class WebsocketClientProtocolTest extends Specification implements ManagerContai
"targetTemperature",
Values.create(0.12345))).replace("0.12345", Protocol.DYNAMIC_VALUE_PLACEHOLDER))),
new MetaItem(WebsocketClientProtocol.META_ATTRIBUTE_MESSAGE_MATCH_FILTERS,
- Values.parse(Container.JSON.writeValueAsString(
+ Values.convert(
[
new SubStringValueFilter(SharedEvent.MESSAGE_PREFIX.length()),
new JsonPathFilter("\$..attributeState.attributeRef")
] as ValueFilter[]
- )).orElse(null)),
+ , Container.JSON).orElse(null)),
new MetaItem(WebsocketClientProtocol.META_ATTRIBUTE_MESSAGE_MATCH_PREDICATE,
new StringPredicate(BaseAssetQuery.Match.CONTAINS, true, "targetTemperature").toModelValue()),
new MetaItem(MetaItemType.VALUE_FILTERS,
- Values.parse(Container.JSON.writeValueAsString(
+ Values.convert(
[
new SubStringValueFilter(SharedEvent.MESSAGE_PREFIX.length()),
new JsonPathFilter("\$..events[?(@.attributeState.attributeRef.attributeName == \"targetTemperature\")].attributeState.value"),
new JsonPathFilter("\$.min()")
] as ValueFilter[]
- )).orElse(null)),
+ , Container.JSON).orElse(null)),
new MetaItem(WebsocketClientProtocol.META_SUBSCRIPTIONS,
- Values.parse(
- Container.JSON.writeValueAsString(Arrays.asList(
+ Values.convert(
+ [
new WebsocketSubscription().body(SharedEvent.MESSAGE_PREFIX + Container.JSON.writeValueAsString(
new ReadAssetAttributesEvent(managerDemoSetup.apartment1LivingroomId, "targetTemperature")
- )))
- )).orElse(null))
+ ))
+ ], Container.JSON).orElse(null))
),
new AssetAttribute("readCo2Level", AttributeValueType.NUMBER)
.addMeta(
new MetaItem(MetaItemType.AGENT_LINK, new AttributeRef(agent.id, "protocolConfig").toArrayValue()),
new MetaItem(MetaItemType.READ_ONLY),
new MetaItem(WebsocketClientProtocol.META_ATTRIBUTE_MESSAGE_MATCH_FILTERS,
- Values.parse(Container.JSON.writeValueAsString(
+ Values.convert(
[
new SubStringValueFilter(SharedEvent.MESSAGE_PREFIX.length()),
new JsonPathFilter("\$..attributeState.attributeRef")
] as ValueFilter[]
- )).orElse(null)),
+ , Container.JSON).orElse(null)),
new MetaItem(WebsocketClientProtocol.META_ATTRIBUTE_MESSAGE_MATCH_PREDICATE,
new StringPredicate(BaseAssetQuery.Match.CONTAINS, "co2Level").toModelValue()),
new MetaItem(MetaItemType.VALUE_FILTERS,
- Values.parse(Container.JSON.writeValueAsString(
+ Values.convert(
[
new SubStringValueFilter(SharedEvent.MESSAGE_PREFIX.length()),
new JsonPathFilter("\$..events[?(@.attributeState.attributeRef.attributeName == \"co2Level\")].attributeState.value"),
new JsonPathFilter("\$.min()")
] as ValueFilter[]
- )).orElse(null)),
+ , Container.JSON).orElse(null)),
new MetaItem(WebsocketClientProtocol.META_SUBSCRIPTIONS,
- Values.parse(
- Container.JSON.writeValueAsString(Arrays.asList(
+ Values.convert(
+ [
new WebsocketSubscription().body(SharedEvent.MESSAGE_PREFIX + Container.JSON.writeValueAsString(
new ReadAssetAttributesEvent(managerDemoSetup.apartment1LivingroomId, "co2Level")
- )))
- )).orElse(null))
+ ))
+ ]
+ , Container.JSON).orElse(null))
)
)