Skip to content

armgit / core

Public Imported from GitHub
0

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

Protocol updates: Have IO protocols fallback to \n or \r\n delimiters if no delimiters or max length attributes specified

Rich Turner004af62c30c1
diff --git a/agent/src/main/java/org/openremote/agent/protocol/AbstractProtocol.java b/agent/src/main/java/org/openremote/agent/protocol/AbstractProtocol.java
index 01639cb75..36e8ebe7b 100644
--- a/agent/src/main/java/org/openremote/agent/protocol/AbstractProtocol.java
+++ b/agent/src/main/java/org/openremote/agent/protocol/AbstractProtocol.java
@@ -217,10 +217,12 @@ public abstract class AbstractProtocol<T extends Agent<T, ?, U>, U extends Agent
LOG.warning("Attribute not linked to protocol '" + this + "':" + event);
} else {
+ AgentLink<?> agentLink = agent.getAgentLink(attribute);
+
Pair<Boolean, Object> ignoreAndConverted = ProtocolUtil.doOutboundValueProcessing(
event.getAssetId(),
attribute,
- agent.getAgentLink(attribute),
+ agentLink,
event.getValue().orElse(null),
dynamicAttributes.contains(event.getAttributeRef()));
@@ -231,7 +233,7 @@ public abstract class AbstractProtocol<T extends Agent<T, ?, U>, U extends Agent
doLinkedAttributeWrite(attribute, agent.getAgentLink(attribute), event, ignoreAndConverted.value);
- if (agent.isUpdateOnWrite().orElse(false)) {
+ if (agent.isUpdateOnWrite().orElse(false) || agentLink.getUpdateOnWrite().orElse(false)) {
updateLinkedAttribute(new AttributeState(event.getAttributeRef(), ignoreAndConverted.value));
}
}
diff --git a/agent/src/main/java/org/openremote/agent/protocol/io/AbstractIOClientProtocol.java b/agent/src/main/java/org/openremote/agent/protocol/io/AbstractIOClientProtocol.java
index 8e83b44ab..0fa10a6d8 100644
--- a/agent/src/main/java/org/openremote/agent/protocol/io/AbstractIOClientProtocol.java
+++ b/agent/src/main/java/org/openremote/agent/protocol/io/AbstractIOClientProtocol.java
@@ -23,6 +23,7 @@ import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandler;
import io.netty.handler.codec.DelimiterBasedFrameDecoder;
+import io.netty.handler.codec.Delimiters;
import io.netty.handler.codec.FixedLengthFrameDecoder;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
@@ -102,13 +103,18 @@ public abstract class AbstractIOClientProtocol<T extends AbstractIOClientProtoco
);
} else {
encodersDecoders.add(new StringEncoder(charset));
- if (delimiters.length > 0) {
- ByteBuf[] byteDelimiters = Arrays.stream(delimiters)
- .map(delim -> Unpooled.wrappedBuffer(delim.getBytes(charset)))
- .toArray(ByteBuf[]::new);
- encodersDecoders.add(new DelimiterBasedFrameDecoder(maxLength, stripDelimiter, byteDelimiters));
- } else {
+ if (agent.getMessageMaxLength().isPresent()) {
encodersDecoders.add(new FixedLengthFrameDecoder(maxLength));
+ } else {
+ ByteBuf[] byteDelimiters;
+ if (delimiters.length > 0) {
+ byteDelimiters = Arrays.stream(delimiters)
+ .map(delim -> Unpooled.wrappedBuffer(delim.getBytes(charset)))
+ .toArray(ByteBuf[]::new);
+ } else {
+ byteDelimiters = Delimiters.lineDelimiter();
+ }
+ encodersDecoders.add(new DelimiterBasedFrameDecoder(maxLength, stripDelimiter, byteDelimiters));
}
encodersDecoders.add(new StringDecoder(charset));
encodersDecoders.add(new AbstractNettyIOClient.MessageToMessageDecoder<>(String.class, client));
diff --git a/model/src/main/java/org/openremote/model/asset/agent/AgentLink.java b/model/src/main/java/org/openremote/model/asset/agent/AgentLink.java
index 5a308a2b8..0f925eba4 100644
--- a/model/src/main/java/org/openremote/model/asset/agent/AgentLink.java
+++ b/model/src/main/java/org/openremote/model/asset/agent/AgentLink.java
@@ -67,6 +67,8 @@ public abstract class AgentLink<T extends AgentLink<?>> implements Serializable
protected ValuePredicate messageMatchPredicate;
@JsonPropertyDescription("ValueFilters to apply to incoming messages prior to comparison with the messageMatchPredicate")
protected ValueFilter[] messageMatchFilters;
+ @JsonPropertyDescription("Don't expect a response from the protocol just update the attribute immediately on write")
+ protected Boolean updateOnWrite;
@JsonSerialize
protected String getType() {
@@ -109,6 +111,10 @@ public abstract class AgentLink<T extends AgentLink<?>> implements Serializable
return Optional.ofNullable(messageMatchFilters);
}
+ public Optional<Boolean> getUpdateOnWrite() {
+ return Optional.ofNullable(updateOnWrite);
+ }
+
@SuppressWarnings("unchecked")
public T setValueFilters(ValueFilter[] valueFilters) {
this.valueFilters = valueFilters;
@@ -145,6 +151,12 @@ public abstract class AgentLink<T extends AgentLink<?>> implements Serializable
return (T)this;
}
+ @SuppressWarnings("unchecked")
+ public T setUpdateOnWrite(Boolean updateOnWrite) {
+ this.updateOnWrite = updateOnWrite;
+ return (T)this;
+ }
+
public static <T> T getOrThrowAgentLinkProperty(Optional<T> value, String name) {
return value.orElseThrow(() -> {
String msg = "Required agent link property is undefined: " + name;
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 860abb99f..844283d8b 100644
--- a/model/src/main/java/org/openremote/model/query/AssetQuery.java
+++ b/model/src/main/java/org/openremote/model/query/AssetQuery.java
@@ -343,7 +343,8 @@ public class AssetQuery implements Serializable {
}
@SuppressWarnings("unchecked")
- public AssetQuery types(AssetDescriptor<? extends Asset<?>>... types) {
+ @SafeVarargs
+ public final AssetQuery types(AssetDescriptor<? extends Asset<?>>... types) {
if (types == null || types.length == 0) {
this.types = null;
return this;
diff --git a/test/src/test/groovy/org/openremote/test/protocol/udp/UdpClientProtocolTest.groovy b/test/src/test/groovy/org/openremote/test/protocol/udp/UdpClientProtocolTest.groovy
index 6b9e49bad..4775b6eb5 100644
--- a/test/src/test/groovy/org/openremote/test/protocol/udp/UdpClientProtocolTest.groovy
+++ b/test/src/test/groovy/org/openremote/test/protocol/udp/UdpClientProtocolTest.groovy
@@ -40,6 +40,9 @@ import org.openremote.model.asset.impl.ThingAsset
import org.openremote.model.attribute.*
import org.openremote.model.query.AssetQuery
import org.openremote.model.query.filter.StringPredicate
+import org.openremote.model.query.filter.ValueAnyPredicate
+import org.openremote.model.value.SubStringValueFilter
+import org.openremote.model.value.ValueFilter
import org.openremote.test.ManagerContainerTrait
import spock.lang.Specification
import spock.util.concurrent.PollingConditions
@@ -52,7 +55,7 @@ class UdpClientProtocolTest extends Specification implements ManagerContainerTra
def "Check UDP client protocol and linked attribute deployment"() {
given: "expected conditions"
- def conditions = new PollingConditions(timeout: 20, delay: 0.2)
+ def conditions = new PollingConditions(timeout: 5, delay: 0.2)
and: "the container starts"
def container = startContainer(defaultConfig(), defaultServices())
@@ -81,7 +84,7 @@ class UdpClientProtocolTest extends Specification implements ManagerContainerTra
receivedMessages.add(message)
if (echoSkipCount == 0) {
- echoServer.sendMessage(message, sender)
+ echoServer.sendMessage(message + ";", sender)
lastSend = message
} else {
echoSkipCount--
@@ -144,6 +147,22 @@ class UdpClientProtocolTest extends Specification implements ManagerContainerTra
.setMessageMatchPredicate(
new StringPredicate(AssetQuery.Match.BEGIN, true, "Hello"))
)
+ ),
+ new Attribute<>("updateOnWrite", TEXT)
+ .addMeta(
+ new MetaItem<>(AGENT_LINK, new DefaultAgentLink(agent.id)
+ .setUpdateOnWrite(true)
+ .setValueFilters([
+ new SubStringValueFilter(0, -1)
+ ] as ValueFilter[])
+ )
+ ),
+ new Attribute<>("anyValue", TEXT)
+ .addMeta(
+ new MetaItem<>(AGENT_LINK, new DefaultAgentLink(agent.id)
+ .setMessageMatchPredicate(
+ new ValueAnyPredicate())
+ )
)
)
@@ -152,8 +171,8 @@ class UdpClientProtocolTest extends Specification implements ManagerContainerTra
then: "the attributes should be linked"
conditions.eventually {
- assert agentService.getProtocolInstance(agent.id).linkedAttributes.size() == 5
- assert ((UDPProtocol)agentService.getProtocolInstance(agent.id)).protocolMessageConsumers.size() == 2
+ assert agentService.getProtocolInstance(agent.id).linkedAttributes.size() == 7
+ assert ((UDPProtocol)agentService.getProtocolInstance(agent.id)).protocolMessageConsumers.size() == 3
}
when: "a linked attribute value is updated"
@@ -167,6 +186,39 @@ class UdpClientProtocolTest extends Specification implements ManagerContainerTra
assert receivedMessages.indexOf("Hello there") >= 0
}
+ and: "the server should have responded and the responseWorld attribute should be updated"
+ conditions.eventually {
+ asset = assetStorageService.find(asset.id)
+ assert asset.getAttribute("responseWorld").flatMap{it.getValue()}.orElse(null) == "Hello there"
+ }
+
+ when: "the update on write attribute is written to which wouldn't generate a response from the server"
+ echoSkipCount = 1
+ attributeEvent = new AttributeEvent(asset.id,
+ "updateOnWrite",
+ "No response;")
+ assetProcessingService.sendAttributeEvent(attributeEvent)
+
+ then: "the server should have received the request but not responded"
+ conditions.eventually {
+ assert receivedMessages.indexOf("No response") >= 0
+ }
+
+ and: "the attribute value should have been updated"
+ conditions.eventually {
+ asset = assetStorageService.find(asset.id)
+ assert asset.getAttribute("updateOnWrite").flatMap{it.getValue()}.orElse(null) == "No response"
+ }
+
+ when: "the server sends a message to the client"
+ echoServer.sendMessage("anyValueTest;", new InetSocketAddress("127.0.0.1", clientPort))
+
+ then: "the any value message match attribute should be updated"
+ conditions.eventually {
+ asset = assetStorageService.find(asset.id)
+ assert asset.getAttribute("anyValue").flatMap{it.getValue()}.orElse(null) == "anyValueTest"
+ }
+
when: "the agent is disabled"
agent.setDisabled(true)
agent = assetStorageService.merge(agent)
@@ -191,8 +243,8 @@ class UdpClientProtocolTest extends Specification implements ManagerContainerTra
then: "the attributes should be re-linked"
conditions.eventually {
assert agentService.getProtocolInstance(agent.id) != null
- assert agentService.getProtocolInstance(agent.id).linkedAttributes.size() == 5
- assert ((UDPProtocol)agentService.getProtocolInstance(agent.id)).protocolMessageConsumers.size() == 2
+ assert agentService.getProtocolInstance(agent.id).linkedAttributes.size() == 7
+ assert ((UDPProtocol)agentService.getProtocolInstance(agent.id)).protocolMessageConsumers.size() == 3
}
when: "the echo server is changed to a byte based server"
@@ -239,8 +291,8 @@ class UdpClientProtocolTest extends Specification implements ManagerContainerTra
assert agentService.agentMap.get(agent.id) != null
assert ((UDPAgent)agentService.agentMap.get(agent.id)).getMessageConvertHex().orElse(false)
assert agentService.getProtocolInstance(agent.id) != null
- assert agentService.getProtocolInstance(agent.id).linkedAttributes.size() == 5
- assert ((UDPProtocol)agentService.getProtocolInstance(agent.id)).protocolMessageConsumers.size() == 2
+ assert agentService.getProtocolInstance(agent.id).linkedAttributes.size() == 7
+ assert ((UDPProtocol)agentService.getProtocolInstance(agent.id)).protocolMessageConsumers.size() == 3
}
and: "the protocol should become CONNECTED"
@@ -260,8 +312,8 @@ class UdpClientProtocolTest extends Specification implements ManagerContainerTra
then: "the attributes should be relinked"
conditions.eventually {
assert agentService.getProtocolInstance(agent.id) != null
- assert agentService.getProtocolInstance(agent.id).linkedAttributes.size() == 5
- assert ((UDPProtocol)agentService.getProtocolInstance(agent.id)).protocolMessageConsumers.size() == 2
+ assert agentService.getProtocolInstance(agent.id).linkedAttributes.size() == 7
+ assert ((UDPProtocol)agentService.getProtocolInstance(agent.id)).protocolMessageConsumers.size() == 3
assert agentService.getProtocolInstance(agent.id).linkedAttributes.get(new AttributeRef(asset.getId(), "echoWorld")).getMetaItem(AGENT_LINK).flatMap{it.value}.flatMap{it.writeValue}.orElse(null) == "123456"
}