Skip to content

armgit / core

Public Imported from GitHub
0

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

Agent: Add write value filter functionality to agent links (#2579)

Wouter Voorberg4c3c11d8a083
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 c10ead6e1..02f284b20 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
@@ -54,6 +54,8 @@ public abstract class AgentLink<T extends AgentLink<?>> implements Serializable
" you want to connect this to a Boolean attribute")
@JsonSchemaSupplier(supplier = SchemaNodeMapper.SCHEMA_SUPPLIER_NAME_PATTERN_PROPERTIES_ANY_KEY_ANY_TYPE)
protected Map<String, Object> valueConverter;
+ @JsonSchemaDescription("Similar to valueFilter but will be applied to outgoing values allowing for inverse filtering.")
+ protected ValueFilter[] writeValueFilters;
@JsonSchemaDescription("Similar to valueConverter but will be applied to outgoing values allowing for the opposite conversion")
@JsonSchemaSupplier(supplier = SchemaNodeMapper.SCHEMA_SUPPLIER_NAME_PATTERN_PROPERTIES_ANY_KEY_ANY_TYPE)
protected Map<String, Object> writeValueConverter;
@@ -116,6 +118,16 @@ public abstract class AgentLink<T extends AgentLink<?>> implements Serializable
return (T) this;
}
+ public Optional<ValueFilter[]> getWriteValueFilters() {
+ return Optional.ofNullable(writeValueFilters);
+ }
+
+ @SuppressWarnings("unchecked")
+ public T setWriteValueFilters(ValueFilter[] writeValueFilters) {
+ this.writeValueFilters = writeValueFilters;
+ return (T) this;
+ }
+
public Optional<Map<String, Object>> getWriteValueConverter() {
return Optional.ofNullable(writeValueConverter);
}
diff --git a/model/src/main/java/org/openremote/model/protocol/ProtocolUtil.java b/model/src/main/java/org/openremote/model/protocol/ProtocolUtil.java
index 9b0959452..29c541fe1 100644
--- a/model/src/main/java/org/openremote/model/protocol/ProtocolUtil.java
+++ b/model/src/main/java/org/openremote/model/protocol/ProtocolUtil.java
@@ -66,6 +66,13 @@ public final class ProtocolUtil {
Pair<Boolean, Object> ignoreAndConvertedValue;
+ // value filters
+ Object valueBeforeFilters = value;
+ value = agentLink.getWriteValueFilters().<Object>map(filters -> {
+ LOG.finest("Applying attribute write value filters to attribute: " + attributeRef);
+ return applyValueFilters(valueBeforeFilters, filters);
+ }).orElse(valueBeforeFilters);
+
// value conversion
Object finalValue = value;
ignoreAndConvertedValue = agentLink.getWriteValueConverter().map(converter -> {
diff --git a/setup/src/integration/java/org/openremote/setup/integration/protocol/MockProtocol.java b/setup/src/integration/java/org/openremote/setup/integration/protocol/MockProtocol.java
index 4dc356221..2b03b84b7 100644
--- a/setup/src/integration/java/org/openremote/setup/integration/protocol/MockProtocol.java
+++ b/setup/src/integration/java/org/openremote/setup/integration/protocol/MockProtocol.java
@@ -45,6 +45,7 @@ public class MockProtocol extends AbstractProtocol<MockAgent, MockAgentLink> imp
public static final String PROTOCOl_NAME = "Mock protocol";
public List<AttributeEvent> protocolWriteAttributeEvents = new ArrayList<>();
+ public List<Object> protocolWriteProcessedValues = new ArrayList<>();
public List<String> protocolMethodCalls = new ArrayList<>();
public boolean updateSensor = true;
protected Container container;
@@ -89,6 +90,7 @@ public class MockProtocol extends AbstractProtocol<MockAgent, MockAgentLink> imp
protected void doLinkedAttributeWrite(MockAgentLink agentLink, AttributeEvent event, Object processedValue) {
protocolMethodCalls.add("WRITE_ATTRIBUTE:" + event.getId() + ":" + event.getName());
protocolWriteAttributeEvents.add(event);
+ protocolWriteProcessedValues.add(processedValue);
if (updateSensor) {
updateReceived(event.getRef(), event.getValue(), event.getTimestamp());
}
diff --git a/test/src/test/groovy/org/openremote/test/protocol/BasicProtocolTest.groovy b/test/src/test/groovy/org/openremote/test/protocol/BasicProtocolTest.groovy
index 1b7e2f67c..cd565607e 100644
--- a/test/src/test/groovy/org/openremote/test/protocol/BasicProtocolTest.groovy
+++ b/test/src/test/groovy/org/openremote/test/protocol/BasicProtocolTest.groovy
@@ -53,7 +53,7 @@ class BasicProtocolTest extends Specification implements ManagerContainerTrait {
given: "expected conditions"
def conditions = new PollingConditions(timeout: 10, initialDelay: 0.3, delay: 0.2)
Map<String, Integer> protocolExpectedLinkedAttributeCount = [:]
- protocolExpectedLinkedAttributeCount["mockAgent1"] = 6
+ protocolExpectedLinkedAttributeCount["mockAgent1"] = 8
protocolExpectedLinkedAttributeCount["mockAgent2"] = 2
protocolExpectedLinkedAttributeCount["mockAgent3"] = 2
protocolExpectedLinkedAttributeCount['mockConfig4'] = 2
@@ -219,7 +219,43 @@ class BasicProtocolTest extends Specification implements ManagerContainerTrait {
] as ValueFilter[]
)
)
- )
+ ),
+ new Attribute<>("readWriteMath", NUMBER)
+ .addOrReplaceMeta(
+ new MetaItem<>(
+ AGENT_LINK,
+ new MockAgentLink(mockAgent1.id)
+ .setRequiredValue("true")
+ .setValueFilters(
+ [
+ new MathExpressionValueFilter("x*10")
+ ] as ValueFilter[]
+ )
+ .setWriteValueFilters(
+ [
+ new MathExpressionValueFilter("x/10")
+ ] as ValueFilter[]
+ )
+ )
+ ),
+ new Attribute<>("readWriteRegex", NUMBER)
+ .addOrReplaceMeta(
+ new MetaItem<>(
+ AGENT_LINK,
+ new MockAgentLink(mockAgent1.id)
+ .setRequiredValue("true")
+ .setValueFilters(
+ [
+ new RegexValueFilter(Pattern.compile("temp=(\\d+)")).setMatchGroup(1)
+ ] as ValueFilter[]
+ )
+ .setWriteValueFilters(
+ [
+ new RegexValueFilter(Pattern.compile("^(\\d+)\$")).setMatchGroup(1)
+ ] as ValueFilter[]
+ )
+ )
+ )
)
mockThing = assetStorageService.merge(mockThing)
@@ -227,7 +263,7 @@ class BasicProtocolTest extends Specification implements ManagerContainerTrait {
then: "the mock thing to be fully deployed in the correct order"
conditions.eventually {
assert agentService.getProtocolInstance(mockAgent1.id) != null
- assert ((MockProtocol)agentService.getProtocolInstance(mockAgent1.id)).protocolMethodCalls.size() == 8
+ assert ((MockProtocol)agentService.getProtocolInstance(mockAgent1.id)).protocolMethodCalls.size() == 10
assert ((MockProtocol)agentService.getProtocolInstance(mockAgent1.id)).protocolMethodCalls.get(0) == "START"
assert ((MockProtocol)agentService.getProtocolInstance(mockAgent1.id)).protocolMethodCalls.get(1).startsWith("LINK_ATTRIBUTE")
assert ((MockProtocol)agentService.getProtocolInstance(mockAgent1.id)).protocolMethodCalls.get(2).startsWith("LINK_ATTRIBUTE")
@@ -236,6 +272,8 @@ class BasicProtocolTest extends Specification implements ManagerContainerTrait {
assert ((MockProtocol)agentService.getProtocolInstance(mockAgent1.id)).protocolMethodCalls.get(5).startsWith("LINK_ATTRIBUTE")
assert ((MockProtocol)agentService.getProtocolInstance(mockAgent1.id)).protocolMethodCalls.get(6).startsWith("LINK_ATTRIBUTE")
assert ((MockProtocol)agentService.getProtocolInstance(mockAgent1.id)).protocolMethodCalls.get(7).startsWith("LINK_ATTRIBUTE")
+ assert ((MockProtocol)agentService.getProtocolInstance(mockAgent1.id)).protocolMethodCalls.get(8).startsWith("LINK_ATTRIBUTE")
+ assert ((MockProtocol)agentService.getProtocolInstance(mockAgent1.id)).protocolMethodCalls.get(9).startsWith("LINK_ATTRIBUTE")
assert agentService.getProtocolInstance(mockAgent1.id).linkedAttributes.size() == protocolExpectedLinkedAttributeCount["mockAgent1"]
}
@@ -412,5 +450,48 @@ class BasicProtocolTest extends Specification implements ManagerContainerTrait {
mockThing = assetStorageService.find(mockThing.id, true)
assert !mockThing.getAttribute("filterRegexSubstring").get().getValue().isPresent()
}
+
+ when: "a sensor value is received on the read/write math attribute (inbound: x*10)"
+ ((MockProtocol)agentService.getProtocolInstance(mockAgent1.id)).updateReceived(new AttributeRef(mockThing.id, "readWriteMath"), 25, null)
+
+ then: "the inbound value filter should multiply by 10"
+ conditions.eventually {
+ mockThing = assetStorageService.find(mockThing.id, true) as ThingAsset
+ assert mockThing.getAttribute("readWriteMath").get().getValue(Double.class).orElse(0d) == 250d
+ }
+
+ when: "the same attribute is written to by the user (outbound: x/10)"
+ ((MockProtocol)agentService.getProtocolInstance(mockAgent1.id)).protocolWriteAttributeEvents.clear()
+ ((MockProtocol)agentService.getProtocolInstance(mockAgent1.id)).protocolWriteProcessedValues.clear()
+ ((MockProtocol)agentService.getProtocolInstance(mockAgent1.id)).updateSensor = false
+ assetProcessingService.sendAttributeEvent(new AttributeEvent(mockThing.id, "readWriteMath", 250d))
+
+ then: "the outbound write value filter should divide by 10 before reaching the protocol"
+ conditions.eventually {
+ assert ((MockProtocol)agentService.getProtocolInstance(mockAgent1.id)).protocolWriteAttributeEvents.size() == 1
+ assert ((MockProtocol)agentService.getProtocolInstance(mockAgent1.id)).protocolWriteAttributeEvents.get(0).name == "readWriteMath"
+ assert ((MockProtocol)agentService.getProtocolInstance(mockAgent1.id)).protocolWriteProcessedValues.get(0) == 25.0d
+ }
+
+ when: "a sensor value is received on the read/write regex attribute (inbound: extract number from temp=N)"
+ ((MockProtocol)agentService.getProtocolInstance(mockAgent1.id)).updateSensor = true
+ ((MockProtocol)agentService.getProtocolInstance(mockAgent1.id)).updateReceived(new AttributeRef(mockThing.id, "readWriteRegex"), "temp=42", null)
+
+ then: "the inbound value filter should extract the number"
+ conditions.eventually {
+ mockThing = assetStorageService.find(mockThing.id, true) as ThingAsset
+ assert mockThing.getAttribute("readWriteRegex").get().getValue(Double.class).orElse(0d) == 42d
+ }
+
+ when: "the same attribute is written with a non-numeric value (outbound regex won't match)"
+ ((MockProtocol)agentService.getProtocolInstance(mockAgent1.id)).protocolWriteAttributeEvents.clear()
+ ((MockProtocol)agentService.getProtocolInstance(mockAgent1.id)).protocolWriteProcessedValues.clear()
+ ((MockProtocol)agentService.getProtocolInstance(mockAgent1.id)).updateSensor = false
+ assetProcessingService.sendAttributeEvent(new AttributeEvent(mockThing.id, "readWriteRegex", "not a number"))
+
+ then: "the write should be suppressed because the outbound filter produced null"
+ new PollingConditions(timeout: 3, initialDelay: 1).eventually {
+ assert ((MockProtocol)agentService.getProtocolInstance(mockAgent1.id)).protocolWriteAttributeEvents.size() == 0
+ }
}
}
diff --git a/ui/app/shared/locales/ar/or.json b/ui/app/shared/locales/ar/or.json
index 617cc5520..fdd21717f 100644
--- a/ui/app/shared/locales/ar/or.json
+++ b/ui/app/shared/locales/ar/or.json
@@ -538,6 +538,9 @@
},
"updateOnWrite": {
"description": ""
+ },
+ "writeValueFilters": {
+ "description": ""
}
}
}
diff --git a/ui/app/shared/locales/cn/or.json b/ui/app/shared/locales/cn/or.json
index 21b0c9471..29d455401 100644
--- a/ui/app/shared/locales/cn/or.json
+++ b/ui/app/shared/locales/cn/or.json
@@ -587,6 +587,9 @@
},
"updateOnWrite": {
"description": ""
+ },
+ "writeValueFilters": {
+ "description": ""
}
}
}
diff --git a/ui/app/shared/locales/de/or.json b/ui/app/shared/locales/de/or.json
index 2f83d934e..ae851154e 100644
--- a/ui/app/shared/locales/de/or.json
+++ b/ui/app/shared/locales/de/or.json
@@ -633,6 +633,9 @@
},
"updateOnWrite": {
"description": ""
+ },
+ "writeValueFilters": {
+ "description": ""
}
}
}
diff --git a/ui/app/shared/locales/en/or.json b/ui/app/shared/locales/en/or.json
index 0f770a54e..b6d883046 100644
--- a/ui/app/shared/locales/en/or.json
+++ b/ui/app/shared/locales/en/or.json
@@ -1202,6 +1202,9 @@
},
"updateOnWrite": {
"description": "Don't expect a response from the protocol just update the attribute immediately on write"
+ },
+ "writeValueFilters": {
+ "description": "Similar to valueFilter but will be applied to outgoing values allowing for inverse filtering."
}
}
}
diff --git a/ui/app/shared/locales/es/or.json b/ui/app/shared/locales/es/or.json
index ee9553163..741d67f81 100644
--- a/ui/app/shared/locales/es/or.json
+++ b/ui/app/shared/locales/es/or.json
@@ -590,6 +590,9 @@
},
"updateOnWrite": {
"description": ""
+ },
+ "writeValueFilters": {
+ "description": ""
}
}
}
diff --git a/ui/app/shared/locales/fr/or.json b/ui/app/shared/locales/fr/or.json
index 9cc1e660d..d9b04f845 100644
--- a/ui/app/shared/locales/fr/or.json
+++ b/ui/app/shared/locales/fr/or.json
@@ -593,6 +593,9 @@
},
"updateOnWrite": {
"description": ""
+ },
+ "writeValueFilters": {
+ "description": ""
}
}
}
diff --git a/ui/app/shared/locales/it/or.json b/ui/app/shared/locales/it/or.json
index ba7791286..1103def5e 100644
--- a/ui/app/shared/locales/it/or.json
+++ b/ui/app/shared/locales/it/or.json
@@ -587,6 +587,9 @@
},
"updateOnWrite": {
"description": ""
+ },
+ "writeValueFilters": {
+ "description": ""
}
}
}
diff --git a/ui/app/shared/locales/nl/or.json b/ui/app/shared/locales/nl/or.json
index 4689ee81f..a1ea8a395 100644
--- a/ui/app/shared/locales/nl/or.json
+++ b/ui/app/shared/locales/nl/or.json
@@ -1197,6 +1197,9 @@
},
"updateOnWrite": {
"description": "Verwacht geen reactie van het protocol; het werkt de attribuut onmiddellijk bij tijdens schrijven."
+ },
+ "writeValueFilters": {
+ "description": ""
}
}
}
diff --git a/ui/app/shared/locales/pt/or.json b/ui/app/shared/locales/pt/or.json
index 6f4e1b582..3380cfbfa 100644
--- a/ui/app/shared/locales/pt/or.json
+++ b/ui/app/shared/locales/pt/or.json
@@ -588,6 +588,9 @@
},
"updateOnWrite": {
"description": ""
+ },
+ "writeValueFilters": {
+ "description": ""
}
}
}
diff --git a/ui/app/shared/locales/ro/or.json b/ui/app/shared/locales/ro/or.json
index 6a49852e9..4329bb1b6 100644
--- a/ui/app/shared/locales/ro/or.json
+++ b/ui/app/shared/locales/ro/or.json
@@ -826,6 +826,9 @@
},
"updateOnWrite": {
"description": ""
+ },
+ "writeValueFilters": {
+ "description": ""
}
}
}
diff --git a/ui/app/shared/locales/uk/or.json b/ui/app/shared/locales/uk/or.json
index 030ed1174..fd39f6b8e 100644
--- a/ui/app/shared/locales/uk/or.json
+++ b/ui/app/shared/locales/uk/or.json
@@ -868,6 +868,9 @@
},
"updateOnWrite": {
"description": ""
+ },
+ "writeValueFilters": {
+ "description": ""
}
}
}