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

Rich Turner7cc74353270d
diff --git a/agent/src/main/java/org/openremote/agent3/protocol/AbstractProtocol.java b/agent/src/main/java/org/openremote/agent3/protocol/AbstractProtocol.java
index ee0c509f0..13e1831c7 100644
--- a/agent/src/main/java/org/openremote/agent3/protocol/AbstractProtocol.java
+++ b/agent/src/main/java/org/openremote/agent3/protocol/AbstractProtocol.java
@@ -30,13 +30,11 @@ import org.openremote.model.AbstractValueHolder;
import org.openremote.model.asset.AssetAttribute;
import org.openremote.model.asset.AssetMeta;
import org.openremote.model.asset.agent.AgentLink;
-import org.openremote.model.attribute.AttributeEvent;
-import org.openremote.model.attribute.AttributeRef;
-import org.openremote.model.attribute.AttributeState;
-import org.openremote.model.attribute.MetaItem;
+import org.openremote.model.attribute.*;
import org.openremote.model.util.Triplet;
import org.openremote.model.value.Values;
+import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
@@ -104,7 +102,7 @@ public abstract class AbstractProtocol implements Protocol {
if (enabled == protocolConfiguration.isEnabled()) {
LOG.finer("Protocol configuration enabled status is already: " + enabled);
} else {
- updateLinkedProtocolConfiguration(protocolConfiguration, new MetaItem(AssetMeta.ENABLED, Values.create(enabled)));
+ updateLinkedProtocolConfiguration(protocolConfiguration, AssetMeta.ENABLED, new MetaItem(AssetMeta.ENABLED, Values.create(enabled)));
}
} else {
processLinkedAttributeWrite(event, protocolConfiguration);
@@ -235,7 +233,11 @@ public abstract class AbstractProtocol implements Protocol {
*/
protected void sendAttributeEvent(AttributeEvent attributeEvent) {
LOG.info("Sending attribute event from protocol '" + getProtocolName() + "': " + attributeEvent);
- assetService.sendAttributeEvent(attributeEvent);
+ // TODO: Decide whether protocols should be allowed to send arbitrary attribute events to read only attributes
+ // Use same mechanism as clients - cannot write to readonly attributes
+ //assetProcessingService.sendAttributeEvent();
+ // Use special route for protocols to allow write on readonly attributes
+ producerTemplate.sendBodyAndHeader(SENSOR_QUEUE, attributeEvent, "isSensorUpdate", false);
}
/**
@@ -258,10 +260,14 @@ public abstract class AbstractProtocol implements Protocol {
* Update a linked protocol configuration; allows protocols to reconfigure their
* own protocol configurations to persist changing data e.g. authorization tokens.
*/
- protected void updateLinkedProtocolConfiguration(AssetAttribute protocolConfiguration, MetaItem newMetaItem) {
+ protected void updateLinkedProtocolConfiguration(AssetAttribute protocolConfiguration, HasMetaName metaName, MetaItem... newMetaItem) {
+ updateLinkedProtocolConfiguration(protocolConfiguration, metaName.getUrn(), newMetaItem);
+ }
+
+ protected void updateLinkedProtocolConfiguration(AssetAttribute protocolConfiguration, String metaName, MetaItem... newMetaItem) {
// Clone the protocol configuration rather than modify this one
AssetAttribute modifiedProtocolConfiguration = protocolConfiguration.deepCopy();
- MetaItem.replaceMetaByName(modifiedProtocolConfiguration.getMeta(), AssetMeta.ENABLED, newMetaItem);
+ MetaItem.replaceMetaByName(modifiedProtocolConfiguration.getMeta(), metaName, Arrays.asList(newMetaItem));
assetService.updateProtocolConfiguration(modifiedProtocolConfiguration);
}
diff --git a/agent/src/main/java/org/openremote/agent3/protocol/ProtocolAssetService.java b/agent/src/main/java/org/openremote/agent3/protocol/ProtocolAssetService.java
index e889a885a..3f2a2bd40 100644
--- a/agent/src/main/java/org/openremote/agent3/protocol/ProtocolAssetService.java
+++ b/agent/src/main/java/org/openremote/agent3/protocol/ProtocolAssetService.java
@@ -21,20 +21,15 @@ package org.openremote.agent3.protocol;
import org.openremote.container.ContainerService;
import org.openremote.model.asset.AssetAttribute;
-import org.openremote.model.attribute.AttributeEvent;
/**
- * Interface for protocols to perform limited asset
- * related operations.
+ * Interface for protocols to perform limited asset related operations.
* <p>
* <ul>
- * <li>Send attribute events into the processing chain</li>
- * <li>Update their own protocol configurations</li>
+ * <li>Update their own protocol configurations</li>
* </ul>
*/
public interface ProtocolAssetService extends ContainerService {
- void sendAttributeEvent(AttributeEvent attributeEvent);
-
// TODO: Find a way of limiting protocols to only modifying their own protocol configurations
void updateProtocolConfiguration(AssetAttribute protocolConfiguration);
}
diff --git a/manager/server/src/main/java/org/openremote/manager/server/agent/AgentService.java b/manager/server/src/main/java/org/openremote/manager/server/agent/AgentService.java
index 6ec363676..11945c638 100644
--- a/manager/server/src/main/java/org/openremote/manager/server/agent/AgentService.java
+++ b/manager/server/src/main/java/org/openremote/manager/server/agent/AgentService.java
@@ -21,6 +21,7 @@ package org.openremote.manager.server.agent;
import org.apache.camel.builder.RouteBuilder;
import org.openremote.agent3.protocol.Protocol;
+import org.openremote.agent3.protocol.ProtocolAssetService;
import org.openremote.container.Container;
import org.openremote.container.ContainerService;
import org.openremote.container.message.MessageBrokerService;
@@ -60,7 +61,7 @@ import static org.openremote.model.util.TextUtil.isValidURN;
/**
* Finds all {@link AssetType#AGENT} and {@link AssetType#THING} assets and starts the protocols for them.
*/
-public class AgentService extends RouteBuilder implements ContainerService, Consumer<AssetState> {
+public class AgentService extends RouteBuilder implements ContainerService, Consumer<AssetState>, ProtocolAssetService {
private static final Logger LOG = Logger.getLogger(AgentService.class.getName());
protected Container container;
@@ -139,6 +140,40 @@ public class AgentService extends RouteBuilder implements ContainerService, Cons
});
}
+ /**
+ * This should only be called by protocol implementations to request an update to
+ * one of their own protocol configuration attributes. The {@link ProtocolAssetService}
+ * interface was implemented here because the sendAttributeEvent already exists here
+ * and there are already protocol related things here anyway.
+ */
+ // TODO: Need a way of restricting protocols to their own protocolConfigurations
+ @Override
+ public void updateProtocolConfiguration(AssetAttribute protocolConfiguration) {
+ if (protocolConfiguration == null || !protocolConfiguration.getReference().isPresent()) {
+ LOG.warning("Cannot update protocol configuration as it is not valid: " + protocolConfiguration);
+ return;
+ }
+
+ AttributeRef protocolRef = protocolConfiguration.getReference().get();
+ ServerAsset agent = assetStorageService.find(protocolRef.getEntityId(), true);
+ if (agent == null || agent.getWellKnownType() != AssetType.AGENT || !agent.hasAttribute(protocolRef.getAttributeName())) {
+ LOG.warning("Protocol configuration doesn't belong to a valid agent");
+ return;
+ }
+
+ // Check protocol configuration has changed
+ @SuppressWarnings("ConstantConditions")
+ AssetAttribute oldProtocolConfiguration = agent.getAttribute(protocolRef.getAttributeName()).get();
+ if (oldProtocolConfiguration.getObjectValue().equals(protocolConfiguration.getObjectValue())) {
+ LOG.fine("Protocol configuration hasn't changed so nothing to do here");
+ return;
+ }
+
+ agent.replaceAttribute(protocolConfiguration);
+ LOG.fine("Updating agent protocol configuration: " + protocolRef);
+ assetStorageService.merge(agent);
+ }
+
/**
* Looks for new, modified and obsolete protocol configurations and links / unlinks any associated
* attributes
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 50c9ca757..2bf0ef603 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
@@ -20,7 +20,6 @@
package org.openremote.manager.server.asset;
import org.apache.camel.builder.RouteBuilder;
-import org.openremote.agent3.protocol.ProtocolAssetService;
import org.openremote.container.Container;
import org.openremote.container.ContainerService;
import org.openremote.container.message.MessageBrokerService;
@@ -36,7 +35,6 @@ import org.openremote.manager.shared.security.ClientRole;
import org.openremote.model.asset.*;
import org.openremote.model.attribute.AttributeEvent;
import org.openremote.model.attribute.AttributeExecuteStatus;
-import org.openremote.model.attribute.AttributeRef;
import org.openremote.model.value.Values;
import java.util.ArrayList;
@@ -118,7 +116,7 @@ import static org.openremote.model.asset.agent.AgentLink.getAgentLink;
* and if it does then the {@link AttributeEvent} is stored in a time series DB. Then allows the message to continue
* if the commit was successful. TODO Should the datapoint service only store northbound updates?
*/
-public class AssetProcessingService extends RouteBuilder implements ContainerService, ProtocolAssetService {
+public class AssetProcessingService extends RouteBuilder implements ContainerService {
private static final Logger LOG = Logger.getLogger(AssetProcessingService.class.getName());
@@ -211,7 +209,13 @@ public class AssetProcessingService extends RouteBuilder implements ContainerSer
.process(exchange -> {
AttributeEvent event = exchange.getIn().getBody(AttributeEvent.class);
LOG.fine("Received on sensor queue: " + event);
- processSensorUpdate(event);
+ // Can either come from onUpdateSensor or sendAttributeUpdate
+ boolean isSensorUpdate = (boolean) exchange.getIn().getHeader("isSensorUpdate", true);
+ if (isSensorUpdate) {
+ processSensorUpdate(event);
+ } else {
+ updateAttributeValue(event, true);
+ }
});
// Process attribute events from clients
@@ -276,40 +280,6 @@ public class AssetProcessingService extends RouteBuilder implements ContainerSer
messageBrokerService.getProducerTemplate().sendBody(ASSET_QUEUE, attributeEvent);
}
- /**
- * This should only be called by protocol implementations to request an update to
- * one of their own protocol configuration attributes. The {@link ProtocolAssetService}
- * interface was implemented here because the sendAttributeEvent already exists here
- * and there are already protocol related things here anyway.
- */
- // TODO: Need a way of restricting protocols to their own protocolConfigurations
- @Override
- public void updateProtocolConfiguration(AssetAttribute protocolConfiguration) {
- if (protocolConfiguration == null || !protocolConfiguration.getReference().isPresent()) {
- LOG.warning("Cannot update protocol configuration as it is not valid: " + protocolConfiguration);
- return;
- }
-
- AttributeRef protocolRef = protocolConfiguration.getReference().get();
- ServerAsset agent = assetStorageService.find(protocolRef.getEntityId(), true);
- if (agent == null || agent.getWellKnownType() != AssetType.AGENT || !agent.hasAttribute(protocolRef.getAttributeName())) {
- LOG.warning("Protocol configuration doesn't belong to a valid agent");
- return;
- }
-
- // Check protocol configuration has changed
- @SuppressWarnings("ConstantConditions")
- AssetAttribute oldProtocolConfiguration = agent.getAttribute(protocolRef.getAttributeName()).get();
- if (oldProtocolConfiguration.getObjectValue().equals(protocolConfiguration.getObjectValue())) {
- LOG.fine("Protocol configuration hasn't changed so nothing to do here");
- return;
- }
-
- agent.replaceAttribute(protocolConfiguration);
- LOG.fine("Updating agent protocol configuration: " + protocolRef);
- assetStorageService.merge(agent);
- }
-
/**
* This is the entry point for any attribute value change event in the entire system.
* <p>
@@ -354,7 +324,8 @@ public class AssetProcessingService extends RouteBuilder implements ContainerSer
// Prevent editing of read only attributes
// TODO This also means a rule RHS can't write a read-only attribute with Assets#dispatch!
- if (attribute.get().isReadOnly()) {
+ // TODO should protocols be allowed to write to read-only attributes
+ if (!fromProtocol && attribute.get().isReadOnly()) {
LOG.warning("Ignoring " + attributeEvent + ", attribute is read-only in: " + asset);
return;
}