Package org.openremote.agent.protocol
Class AbstractProtocol<T extends org.openremote.model.asset.agent.Agent<T,?,U>,U extends org.openremote.model.asset.agent.AgentLink<?>>
java.lang.Object
org.openremote.agent.protocol.AbstractProtocol<T,U>
- All Implemented Interfaces:
org.openremote.model.asset.agent.Protocol<T>
- Direct Known Subclasses:
AbstractHTTPServerProtocol,AbstractIOClientProtocol,AbstractTCPServerProtocol,AbstractVelbusProtocol,BluetoothMeshProtocol,ControllerProtocol,HTTPProtocol,KNXProtocol,SimulatorProtocol,SNMPProtocol,StorageSimulatorProtocol,TradfriProtocol,ZWaveProtocol
public abstract class AbstractProtocol<T extends org.openremote.model.asset.agent.Agent<T,?,U>,U extends org.openremote.model.asset.agent.AgentLink<?>>
extends Object
implements org.openremote.model.asset.agent.Protocol<T>
Thread-safe base implementation for protocols.
Subclasses should use the GlobalLock.withLock(java.lang.String, java.lang.Runnable) and GlobalLock.withLockReturning(java.lang.String, java.util.function.Supplier<R>) methods
to guard critical sections when modifying shared state:
withLock(getProtocolName(), () -> { // Critical section });
return withLockReturning(() -> { // Critical section return ...; });
All abstract methods are always called within lock scope. An implementation can rely on this lock
and safely modify internal, protocol-specific shared state. However, if a protocol implementation schedules
an asynchronous task, this task must obtain the lock to call any protocol operations.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected Tprotected ProtocolAssetServiceprotected ProtocolDatapointServiceprotected final Set<org.openremote.model.attribute.AttributeRef>protected ScheduledExecutorServiceprotected final Map<org.openremote.model.attribute.AttributeRef,org.openremote.model.attribute.Attribute<?>> protected org.openremote.container.message.MessageBrokerContextprotected ProtocolPredictedDatapointServiceprotected org.apache.camel.ProducerTemplateprotected org.openremote.container.timer.TimerServiceFields inherited from interface org.openremote.model.asset.agent.Protocol
ACTUATOR_TOPIC, ACTUATOR_TOPIC_TARGET_PROTOCOL, DYNAMIC_TIME_PLACEHOLDER_REGEXP, DYNAMIC_VALUE_PLACEHOLDER, DYNAMIC_VALUE_PLACEHOLDER_REGEXP, SENSOR_QUEUE, SENSOR_QUEUE_SOURCE_PROTOCOL -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected abstract voiddoLinkAttribute(String assetId, org.openremote.model.attribute.Attribute<?> attribute, U agentLink) Link anAttributeto its linkedAgent.protected abstract voiddoLinkedAttributeWrite(org.openremote.model.attribute.Attribute<?> attribute, U agentLink, org.openremote.model.attribute.AttributeEvent event, Object processedValue) An Attribute event (write) has been requested for an attribute linked to this protocol.protected abstract voiddoStart(org.openremote.model.Container container) Start this protocol instanceprotected abstract voiddoStop(org.openremote.model.Container container) Stop this protocol instanceprotected abstract voiddoUnlinkAttribute(String assetId, org.openremote.model.attribute.Attribute<?> attribute, U agentLink) Unlink anAttributefrom its linkedAgent.getAgent()Get theAgentassociated with this protocol instance.Map<org.openremote.model.attribute.AttributeRef,org.openremote.model.attribute.Attribute<?>> Get list ofAttributes currently linked to this protocol instance grouped byAssetIDfinal voidlinkAttribute(String assetId, org.openremote.model.attribute.Attribute<?> attribute) Links anAttributeto its' agent; the agent would have been connected before this call.protected final voidprocessLinkedAttributeWrite(org.openremote.model.attribute.AttributeEvent event) protected final voidsendAttributeEvent(org.openremote.model.attribute.AttributeEvent event) Send an arbitraryAttributeEventthrough the processing chain.protected final voidsendAttributeEvent(org.openremote.model.attribute.AttributeState state) Send an arbitraryAttributeStatethrough the processing chain using the current system time as the timestamp.protected voidsetConnectionStatus(org.openremote.model.asset.agent.ConnectionStatus connectionStatus) voidstart(org.openremote.model.Container container) Called before any calls toProtocol.linkAttribute(java.lang.String, org.openremote.model.attribute.Attribute<?>)to allow the protocol to perform required tasks withContainerServices (e.g.final voidstop(org.openremote.model.Container container) Called once all linked attributes have been unlinked viaProtocol.unlinkAttribute(java.lang.String, org.openremote.model.attribute.Attribute<?>)to allow the protocol to perform required tasks withContainerServices e.g.toString()final voidunlinkAttribute(String assetId, org.openremote.model.attribute.Attribute<?> attribute) Un-links anAttributefrom its' agent; the agent will still be connected during this call.protected final voidupdateAgentAttribute(org.openremote.model.attribute.AttributeState state) Update the value of one of thisProtocols linkedAgent'sAttributes.protected final voidupdateLinkedAttribute(org.openremote.model.attribute.AttributeState state) Update the value of a linked attribute, with the current system time as event time seeupdateLinkedAttribute(AttributeState, long)for more details.protected final voidupdateLinkedAttribute(org.openremote.model.attribute.AttributeState state, long timestamp) Update the value of a linked attribute.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface org.openremote.model.asset.agent.Protocol
getProtocolInstanceUri, getProtocolName, prefixLogMessage
-
Field Details
-
linkedAttributes
protected final Map<org.openremote.model.attribute.AttributeRef,org.openremote.model.attribute.Attribute<?>> linkedAttributes -
dynamicAttributes
-
messageBrokerContext
protected org.openremote.container.message.MessageBrokerContext messageBrokerContext -
producerTemplate
protected org.apache.camel.ProducerTemplate producerTemplate -
timerService
protected org.openremote.container.timer.TimerService timerService -
executorService
-
assetService
-
predictedDatapointService
-
datapointService
-
agent
-
-
Constructor Details
-
AbstractProtocol
-
-
Method Details
-
start
Description copied from interface:org.openremote.model.asset.agent.ProtocolCalled before any calls toProtocol.linkAttribute(java.lang.String, org.openremote.model.attribute.Attribute<?>)to allow the protocol to perform required tasks withContainerServices (e.g. register Camel routes). The protocol instance should validate the settings defined in the associatedAgent; but whether or not the protocol establishes a connection/ fully initialises at this time is up to the protocol implementation, it could be desirable to wait untilProtocol.linkAttribute(java.lang.String, org.openremote.model.attribute.Attribute<?>)is called for the first time, or it could be a connectionless protocol. If there is a configuration issue etc. then an appropriate exception should be thrown and suitable logs made. -
stop
public final void stop(org.openremote.model.Container container) Description copied from interface:org.openremote.model.asset.agent.ProtocolCalled once all linked attributes have been unlinked viaProtocol.unlinkAttribute(java.lang.String, org.openremote.model.attribute.Attribute<?>)to allow the protocol to perform required tasks withContainerServices e.g. remove Camel routes, destroy/cleanup resources etc. -
setConnectionStatus
protected void setConnectionStatus(org.openremote.model.asset.agent.ConnectionStatus connectionStatus) -
linkAttribute
public final void linkAttribute(String assetId, org.openremote.model.attribute.Attribute<?> attribute) throws Exception Description copied from interface:org.openremote.model.asset.agent.ProtocolLinks anAttributeto its' agent; the agent would have been connected before this call. This is called when the agent is connected or when the attribute has been modified and re-linked.If the attribute is not valid for this protocol then it is up to the protocol to log the issue and return false.
Attributes are linked to an agent via an
MetaItemType.AGENT_LINKmeta item. -
unlinkAttribute
public final void unlinkAttribute(String assetId, org.openremote.model.attribute.Attribute<?> attribute) throws Exception Description copied from interface:org.openremote.model.asset.agent.ProtocolUn-links anAttributefrom its' agent; the agent will still be connected during this call. This is called whenever the attribute is modified or removed or when the agent is modified or removed. -
getAgent
Description copied from interface:org.openremote.model.asset.agent.ProtocolGet theAgentassociated with this protocol instance. -
getLinkedAttributes
public Map<org.openremote.model.attribute.AttributeRef,org.openremote.model.attribute.Attribute<?>> getLinkedAttributes()Description copied from interface:org.openremote.model.asset.agent.ProtocolGet list ofAttributes currently linked to this protocol instance grouped byAssetID -
processLinkedAttributeWrite
protected final void processLinkedAttributeWrite(org.openremote.model.attribute.AttributeEvent event) -
sendAttributeEvent
protected final void sendAttributeEvent(org.openremote.model.attribute.AttributeState state) Send an arbitraryAttributeStatethrough the processing chain using the current system time as the timestamp. UseupdateLinkedAttribute(org.openremote.model.attribute.AttributeState, long)to publish new sensor values, which performs additional verification and uses a different messaging queue. -
sendAttributeEvent
protected final void sendAttributeEvent(org.openremote.model.attribute.AttributeEvent event) Send an arbitraryAttributeEventthrough the processing chain. UseupdateLinkedAttribute(org.openremote.model.attribute.AttributeState, long)to publish new sensor values, which performs additional verification and uses a different messaging queue. -
updateLinkedAttribute
protected final void updateLinkedAttribute(org.openremote.model.attribute.AttributeState state, long timestamp) Update the value of a linked attribute. Call this to publish new sensor values. This will callProtocolUtil.doInboundValueProcessing(java.lang.String, org.openremote.model.attribute.Attribute<?>, org.openremote.model.asset.agent.AgentLink<?>, java.lang.Object)before sending on the sensor queue. -
updateAgentAttribute
protected final void updateAgentAttribute(org.openremote.model.attribute.AttributeState state) Update the value of one of thisProtocols linkedAgent'sAttributes. -
updateLinkedAttribute
protected final void updateLinkedAttribute(org.openremote.model.attribute.AttributeState state) Update the value of a linked attribute, with the current system time as event time seeupdateLinkedAttribute(AttributeState, long)for more details. -
doStart
Start this protocol instance- Throws:
Exception
-
doStop
Stop this protocol instance- Throws:
Exception
-
toString
-
doLinkAttribute
protected abstract void doLinkAttribute(String assetId, org.openremote.model.attribute.Attribute<?> attribute, U agentLink) throws RuntimeException Link anAttributeto its linkedAgent.- Throws:
RuntimeException
-
doUnlinkAttribute
protected abstract void doUnlinkAttribute(String assetId, org.openremote.model.attribute.Attribute<?> attribute, U agentLink) Unlink anAttributefrom its linkedAgent. -
doLinkedAttributeWrite
protected abstract void doLinkedAttributeWrite(org.openremote.model.attribute.Attribute<?> attribute, U agentLink, org.openremote.model.attribute.AttributeEvent event, Object processedValue) An Attribute event (write) has been requested for an attribute linked to this protocol. The processedValue is the resulting value after applying standard outbound value processing (seeProtocolUtil.doOutboundValueProcessing(java.lang.String, org.openremote.model.attribute.Attribute<?>, org.openremote.model.asset.agent.AgentLink<?>, java.lang.Object, boolean)). Protocol implementations should generally use the processedValue but may also choose to use the original value for some purpose if required.
-