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, AbstractMailProtocol, AbstractTCPServerProtocol, AbstractVelbusProtocol, BluetoothMeshProtocol, 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>
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected T
     
    protected org.openremote.model.protocol.ProtocolAssetService
     
     
    protected final Set<org.openremote.model.attribute.AttributeRef>
     
    protected ExecutorService
     
    protected final Map<org.openremote.model.attribute.AttributeRef,org.openremote.model.attribute.Attribute<?>>
     
    protected org.apache.camel.impl.DefaultCamelContext
     
     
    protected final Object
     
    protected org.apache.camel.ProducerTemplate
     
     
    protected org.openremote.container.timer.TimerService
     
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    protected abstract void
    doLinkAttribute(String assetId, org.openremote.model.attribute.Attribute<?> attribute, U agentLink)
    Link an Attribute to its linked Agent.
    protected abstract void
    doLinkedAttributeWrite(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 void
    doStart(org.openremote.model.Container container)
    Start this protocol instance
    protected abstract void
    doStop(org.openremote.model.Container container)
    Stop this protocol instance
    protected abstract void
    doUnlinkAttribute(String assetId, org.openremote.model.attribute.Attribute<?> attribute, U agentLink)
    Unlink an Attribute from its linked Agent.
    Get the Agent associated with this protocol instance.
    Map<org.openremote.model.attribute.AttributeRef,org.openremote.model.attribute.Attribute<?>>
    Get list of Attributes currently linked to this protocol instance grouped by Asset ID
    final void
    linkAttribute(String assetId, org.openremote.model.attribute.Attribute<?> attribute)
    Links an Attribute to its' agent; the agent would have been connected before this call.
    boolean
    onAgentAttributeChanged(org.openremote.model.attribute.AttributeEvent event)
    Called when one of the associated Agent's Attributes are updated; the implementation can decide if it wants to request the protocol instance to be re-created thus simplifying handling of config changes.
    void
    processLinkedAttributeWrite(org.openremote.model.attribute.AttributeEvent event)
    An Attribute event (write) has been requested for an attribute linked to this protocol; implementations should handle multithreaded requests or use some sort of synchronisation for thread safety.
    protected final void
    sendAttributeEvent(org.openremote.model.attribute.AttributeEvent event)
    Send an arbitrary AttributeEvent through the processing chain.
    protected final void
    sendAttributeEvent(org.openremote.model.attribute.AttributeState state)
    Send an arbitrary AttributeState through the processing chain using the current system time as the timestamp.
    void
    setAssetService(org.openremote.model.protocol.ProtocolAssetService assetService)
    Called by instantiator during instantiation to allow the Protocol to interact with Assets and Attributes.
    protected void
    setConnectionStatus(org.openremote.model.asset.agent.ConnectionStatus connectionStatus)
     
    void
    start(org.openremote.model.Container container)
    Called before any calls to Protocol.linkAttribute(java.lang.String, org.openremote.model.attribute.Attribute<?>) to allow the protocol to perform required tasks with ContainerServices (e.g.
    final void
    stop(org.openremote.model.Container container)
    Called once all linked attributes have been unlinked via Protocol.unlinkAttribute(java.lang.String, org.openremote.model.attribute.Attribute<?>) to allow the protocol to perform required tasks with ContainerServices e.g.
     
    final void
    unlinkAttribute(String assetId, org.openremote.model.attribute.Attribute<?> attribute)
    Un-links an Attribute from its' agent; the agent will still be connected during this call.
    final void
    updateLinkedAttribute(org.openremote.model.attribute.AttributeRef attributeRef, Object value)
    Update the value of a linked Attribute linked to this Protocol; call this to publish new sensor values.
    final void
    updateLinkedAttribute(org.openremote.model.attribute.AttributeRef attributeRef, Object value, long timestamp)
    Update the value of a linked Attribute linked to this Protocol; call this to publish new sensor values.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait

    Methods 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

      protected final Set<org.openremote.model.attribute.AttributeRef> dynamicAttributes
    • messageBrokerContext

      protected org.apache.camel.impl.DefaultCamelContext messageBrokerContext
    • producerTemplate

      protected org.apache.camel.ProducerTemplate producerTemplate
    • timerService

      protected org.openremote.container.timer.TimerService timerService
    • executorService

      protected ExecutorService executorService
    • scheduledExecutorService

      protected ScheduledExecutorService scheduledExecutorService
    • assetService

      protected org.openremote.model.protocol.ProtocolAssetService assetService
    • predictedDatapointService

      protected ProtocolPredictedDatapointService predictedDatapointService
    • datapointService

      protected ProtocolDatapointService datapointService
    • agent

      protected T extends org.openremote.model.asset.agent.Agent<T,?,U> agent
    • processorLock

      protected final Object processorLock
  • Constructor Details

    • AbstractProtocol

      public AbstractProtocol(T agent)
  • Method Details

    • setAssetService

      public void setAssetService(org.openremote.model.protocol.ProtocolAssetService assetService)
      Description copied from interface: org.openremote.model.asset.agent.Protocol
      Called by instantiator during instantiation to allow the Protocol to interact with Assets and Attributes.
      Specified by:
      setAssetService in interface org.openremote.model.asset.agent.Protocol<T extends org.openremote.model.asset.agent.Agent<T,?,U>>
    • start

      public void start(org.openremote.model.Container container) throws Exception
      Description copied from interface: org.openremote.model.asset.agent.Protocol
      Called before any calls to Protocol.linkAttribute(java.lang.String, org.openremote.model.attribute.Attribute<?>) to allow the protocol to perform required tasks with ContainerServices (e.g. register Camel routes). The protocol instance should validate the settings defined in the associated Agent; 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 until Protocol.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.
      Specified by:
      start in interface org.openremote.model.asset.agent.Protocol<T extends org.openremote.model.asset.agent.Agent<T,?,U>>
      Throws:
      Exception
    • stop

      public final void stop(org.openremote.model.Container container)
      Description copied from interface: org.openremote.model.asset.agent.Protocol
      Called once all linked attributes have been unlinked via Protocol.unlinkAttribute(java.lang.String, org.openremote.model.attribute.Attribute<?>) to allow the protocol to perform required tasks with ContainerServices e.g. remove Camel routes, destroy/cleanup resources etc.
      Specified by:
      stop in interface org.openremote.model.asset.agent.Protocol<T extends org.openremote.model.asset.agent.Agent<T,?,U>>
    • 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.Protocol
      Links an Attribute to 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_LINK meta item.

      Specified by:
      linkAttribute in interface org.openremote.model.asset.agent.Protocol<T extends org.openremote.model.asset.agent.Agent<T,?,U>>
      Throws:
      Exception
    • unlinkAttribute

      public final void unlinkAttribute(String assetId, org.openremote.model.attribute.Attribute<?> attribute) throws Exception
      Description copied from interface: org.openremote.model.asset.agent.Protocol
      Un-links an Attribute from 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.
      Specified by:
      unlinkAttribute in interface org.openremote.model.asset.agent.Protocol<T extends org.openremote.model.asset.agent.Agent<T,?,U>>
      Throws:
      Exception
    • getAgent

      public T getAgent()
      Description copied from interface: org.openremote.model.asset.agent.Protocol
      Get the Agent associated with this protocol instance.
      Specified by:
      getAgent in interface org.openremote.model.asset.agent.Protocol<T extends org.openremote.model.asset.agent.Agent<T,?,U>>
    • getLinkedAttributes

      public Map<org.openremote.model.attribute.AttributeRef,org.openremote.model.attribute.Attribute<?>> getLinkedAttributes()
      Description copied from interface: org.openremote.model.asset.agent.Protocol
      Get list of Attributes currently linked to this protocol instance grouped by Asset ID
      Specified by:
      getLinkedAttributes in interface org.openremote.model.asset.agent.Protocol<T extends org.openremote.model.asset.agent.Agent<T,?,U>>
    • processLinkedAttributeWrite

      public void processLinkedAttributeWrite(org.openremote.model.attribute.AttributeEvent event)
      Description copied from interface: org.openremote.model.asset.agent.Protocol
      An Attribute event (write) has been requested for an attribute linked to this protocol; implementations should handle multithreaded requests or use some sort of synchronisation for thread safety. The AttributeEvent will be enriched with data about the relating asset and attribute.
      Specified by:
      processLinkedAttributeWrite in interface org.openremote.model.asset.agent.Protocol<T extends org.openremote.model.asset.agent.Agent<T,?,U>>
    • sendAttributeEvent

      protected final void sendAttributeEvent(org.openremote.model.attribute.AttributeState state)
      Send an arbitrary AttributeState through the processing chain using the current system time as the timestamp. Use updateLinkedAttribute(org.openremote.model.attribute.AttributeRef, java.lang.Object, 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 arbitrary AttributeEvent through the processing chain. Use updateLinkedAttribute(org.openremote.model.attribute.AttributeRef, java.lang.Object, long) to publish new sensor values, which performs additional verification and uses a different messaging queue.
    • updateLinkedAttribute

      public final void updateLinkedAttribute(org.openremote.model.attribute.AttributeRef attributeRef, Object value, long timestamp)
      Description copied from interface: org.openremote.model.asset.agent.Protocol
      Update the value of a linked Attribute linked to this Protocol; call this to publish new sensor values. Implementors are responsible for ensuring the requested Attribute is actually linked to this Protocol.
      Specified by:
      updateLinkedAttribute in interface org.openremote.model.asset.agent.Protocol<T extends org.openremote.model.asset.agent.Agent<T,?,U>>
    • updateLinkedAttribute

      public final void updateLinkedAttribute(org.openremote.model.attribute.AttributeRef attributeRef, Object value)
      Description copied from interface: org.openremote.model.asset.agent.Protocol
      Update the value of a linked Attribute linked to this Protocol; call this to publish new sensor values. Implementors are responsible for ensuring the requested Attribute is actually linked to this Protocol.
      Specified by:
      updateLinkedAttribute in interface org.openremote.model.asset.agent.Protocol<T extends org.openremote.model.asset.agent.Agent<T,?,U>>
    • onAgentAttributeChanged

      public boolean onAgentAttributeChanged(org.openremote.model.attribute.AttributeEvent event)
      Description copied from interface: org.openremote.model.asset.agent.Protocol
      Called when one of the associated Agent's Attributes are updated; the implementation can decide if it wants to request the protocol instance to be re-created thus simplifying handling of config changes.
      Specified by:
      onAgentAttributeChanged in interface org.openremote.model.asset.agent.Protocol<T extends org.openremote.model.asset.agent.Agent<T,?,U>>
      Returns:
      true if the agent should be redeployed (this will stop current protocol instance and create a new one).
    • doStart

      protected abstract void doStart(org.openremote.model.Container container) throws Exception
      Start this protocol instance
      Throws:
      Exception
    • doStop

      protected abstract void doStop(org.openremote.model.Container container) throws Exception
      Stop this protocol instance
      Throws:
      Exception
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • doLinkAttribute

      protected abstract void doLinkAttribute(String assetId, org.openremote.model.attribute.Attribute<?> attribute, U agentLink) throws RuntimeException
      Link an Attribute to its linked Agent.
      Throws:
      RuntimeException
    • doUnlinkAttribute

      protected abstract void doUnlinkAttribute(String assetId, org.openremote.model.attribute.Attribute<?> attribute, U agentLink)
      Unlink an Attribute from its linked Agent.
    • doLinkedAttributeWrite

      protected abstract void doLinkedAttributeWrite(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 (see ProtocolUtil.doOutboundValueProcessing(org.openremote.model.attribute.AttributeRef, org.openremote.model.asset.agent.AgentLink<?>, java.lang.Object, boolean, java.time.Instant)). Protocol implementations should generally use the processedValue but may also choose to use the original value for some purpose if required.