Skip to content

armgit / core

Public Imported from GitHub
0

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

AttributeLinkingService updates

Rich Turner0428e628c8ba
diff --git a/manager/server/src/main/java/org/openremote/manager/server/asset/AssetAttributeLinkingService.java b/manager/server/src/main/java/org/openremote/manager/server/asset/AssetAttributeLinkingService.java
index 0cddf0c1e..ebc0383ec 100644
--- a/manager/server/src/main/java/org/openremote/manager/server/asset/AssetAttributeLinkingService.java
+++ b/manager/server/src/main/java/org/openremote/manager/server/asset/AssetAttributeLinkingService.java
@@ -29,6 +29,7 @@ import org.openremote.model.attribute.MetaItem;
import org.openremote.model.util.Pair;
import org.openremote.model.value.*;
+import java.util.Locale;
import java.util.NoSuchElementException;
import java.util.Optional;
import java.util.function.Consumer;
@@ -108,6 +109,11 @@ public class AssetAttributeLinkingService implements ContainerService, Consumer<
@Override
public void accept(AssetState assetState) {
+ if (assetState.getSource() == AttributeEvent.Source.ATTRIBUTE_LINKING_SERVICE) {
+ LOG.fine("Attribute update came from this service so ignoring to avoid infinite loops: " + assetState);
+ return;
+ }
+
assetState
.getAttribute()
.getMetaStream()
@@ -117,7 +123,7 @@ public class AssetAttributeLinkingService implements ContainerService, Consumer<
protected void sendAttributeEvent(AttributeEvent attributeEvent) {
LOG.fine("Sending attribute event for linked attribute: " + attributeEvent);
- assetProcessingService.sendAttributeEvent(attributeEvent);
+ assetProcessingService.sendAttributeEvent(attributeEvent, AttributeEvent.Source.ATTRIBUTE_LINKING_SERVICE);
}
protected void processLinkedAttributeUpdate(MetaItem metaItem, AssetState assetState) {
@@ -148,7 +154,7 @@ public class AssetAttributeLinkingService implements ContainerService, Consumer<
return attributeLink.getConverter()
.map(
converter -> {
- String converterKey = originalValue == null ? "NULL": originalValue.toString();
+ String converterKey = originalValue == null ? "NULL": originalValue.toString().toUpperCase(Locale.ROOT);
Optional<Value> converterValue = converter.get(converterKey);
// Convert the value
return converterValue
diff --git a/manager/server/src/main/java/org/openremote/manager/server/asset/AssetProcessingService.java b/manager/server/src/main/java/org/openremote/manager/server/asset/AssetProcessingService.java
index aea0ecc65..51a632126 100644
--- a/manager/server/src/main/java/org/openremote/manager/server/asset/AssetProcessingService.java
+++ b/manager/server/src/main/java/org/openremote/manager/server/asset/AssetProcessingService.java
@@ -252,11 +252,14 @@ public class AssetProcessingService extends RouteBuilder implements ContainerSer
* Send internal attribute change events into the {@link #ASSET_QUEUE}.
*/
public void sendAttributeEvent(AttributeEvent attributeEvent) {
+ sendAttributeEvent(attributeEvent, INTERNAL);
+ }
+ public void sendAttributeEvent(AttributeEvent attributeEvent, AttributeEvent.Source source) {
// Set event source time if not already set
if (attributeEvent.getTimestamp() <= 0) {
attributeEvent.setTimestamp(timerService.getCurrentTimeMillis());
}
- messageBrokerService.getProducerTemplate().sendBodyAndHeader(ASSET_QUEUE, attributeEvent, HEADER_SOURCE, INTERNAL);
+ messageBrokerService.getProducerTemplate().sendBodyAndHeader(ASSET_QUEUE, attributeEvent, HEADER_SOURCE, source);
}
/**
@@ -267,6 +270,9 @@ public class AssetProcessingService extends RouteBuilder implements ContainerSer
protected void processAssetState(Exchange exchange) {
AssetState assetState = exchange.getIn().getHeader(HEADER_ASSET_STATE, AssetState.class);
LOG.fine(">>> Processing start: " + assetState);
+ // Need to record time here otherwise an infinite loop generated inside one of the processors means the timestamp
+ // is not updated so tests can't then detect the problem.
+ lastProcessedEventTimestamp = System.currentTimeMillis();
processorLoop:
for (Consumer<AssetState> processor : processors) {
try {
@@ -296,7 +302,6 @@ public class AssetProcessingService extends RouteBuilder implements ContainerSer
assetState.getId(), assetState.getAttributeName(), assetState.getValue(), timerService.getCurrentTimeMillis()
));
}
- lastProcessedEventTimestamp = System.currentTimeMillis();
LOG.fine("<<< Processing complete: " + assetState);
}
diff --git a/manager/test/src/test/groovy/org/openremote/test/assets/AssetAttributeLinkingTest.groovy b/manager/test/src/test/groovy/org/openremote/test/assets/AssetAttributeLinkingTest.groovy
index fec2e10ac..dca4d0b36 100644
--- a/manager/test/src/test/groovy/org/openremote/test/assets/AssetAttributeLinkingTest.groovy
+++ b/manager/test/src/test/groovy/org/openremote/test/assets/AssetAttributeLinkingTest.groovy
@@ -155,6 +155,38 @@ class AssetAttributeLinkingTest extends Specification implements ManagerContaine
assert asset2.getAttribute("counter").get().getValueAsNumber().get() == 0.0
}
+ when: "the linked attribute is linked back to the source attribute (to create circular reference)"
+ def converterLoop = Values.createObject()
+ converterLoop.put("TRUE", "PRESSED")
+ converterLoop.put("FALSE", "PRESSED")
+ def attributeLinkLoop = Values.createObject()
+ attributeLinkLoop.put("attributeRef", new AttributeRef(asset1.id, "button").toArrayValue())
+ attributeLinkLoop.put("converter", converterLoop)
+ asset2.getAttribute("lightOnOff").get().addMeta(new MetaItem(AssetMeta.ATTRIBUTE_LINK, attributeLinkLoop))
+ asset2 = assetStorageService.merge(asset2)
+
+ and: "the button is pressed for a short period"
+ buttonPressed = new AttributeEvent(
+ new AttributeState(new AttributeRef(asset1.id, "button"), Values.create("PRESSED"))
+ )
+ assetProcessingService.sendAttributeEvent(buttonPressed)
+ Thread.sleep(10)
+ buttonReleased = new AttributeEvent(
+ new AttributeState(new AttributeRef(asset1.id, "button"), Values.create("RELEASED"))
+ )
+ assetProcessingService.sendAttributeEvent(buttonReleased)
+
+ then: "the linked attribute value should be toggled on"
+ conditions.eventually {
+ asset2 = assetStorageService.find(asset2.id, true)
+ assert asset2.getAttribute("lightOnOff").get().getValueAsBoolean().get()
+ }
+
+ and: "no more events should be processed"
+ conditions.eventually {
+ assert noEventProcessedIn(assetProcessingService, 500)
+ }
+
cleanup: "the server should be stopped"
stopContainer(container)
}