Class AgentService

java.lang.Object
org.apache.camel.builder.BuilderSupport
org.apache.camel.builder.RouteBuilder
org.openremote.manager.agent.AgentService
All Implemented Interfaces:
org.apache.camel.builder.ModelRoutesBuilder, org.apache.camel.CamelContextAware, org.apache.camel.Ordered, org.apache.camel.RoutesBuilder, org.apache.camel.spi.HasCamelContext, org.apache.camel.spi.ResourceAware, org.openremote.model.ContainerService

public class AgentService extends org.apache.camel.builder.RouteBuilder implements org.openremote.model.ContainerService
This service's role is to communicate asset attribute writes to actuators, through protocol instances. It also handles redeploying Protocol instances when an Attribute of the associated Agent is modified either via Asset CRUD or just via an AttributeEvent.

Only an AttributeEvent for an Attribute containing a MetaItemType.AGENT_LINK MetaItem will be intercepted here and passed to the associated Protocol instance for processing; the event will not be committed to the DB and it is up to the Protocol to generate a new AttributeEvent to signal that the action has been successfully handled.

Any AttributeEvent that originates from an Agent Protocol will not be consumed by the source Protocol when it passes back through this service; this is to prevent infinite loops.

  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    protected class 
     
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected final Map<String,Future<Void>>
     
    protected final Object
     
    protected Map<String,org.openremote.model.asset.agent.Agent<?,?,?>>
     
     
     
    protected final Map<String,Set<Consumer<org.openremote.model.PersistenceEvent<org.openremote.model.asset.Asset<?>>>>>
     
     
    protected org.openremote.model.Container
     
    protected ExecutorService
     
    protected GatewayService
     
    protected boolean
     
    static final int
     
    protected final Map<String,org.openremote.model.asset.agent.Protocol<?>>
     

    Fields inherited from class org.apache.camel.builder.RouteBuilder

    log

    Fields inherited from interface org.openremote.model.ContainerService

    DEFAULT_PRIORITY, HIGH_PRIORITY, LOW_PRIORITY, MED_PRIORITY

    Fields inherited from interface org.apache.camel.Ordered

    HIGHEST, LOWEST
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    protected org.openremote.model.asset.agent.Agent<?,?,?>
    addAgent(org.openremote.model.asset.agent.Agent<?,?,?> agent)
     
    void
     
    protected void
    deployAgent(org.openremote.model.asset.agent.Agent<?,?,?> agent)
    Deploy the Agent by creating a protocol instance, starting it and linking all attributes
    protected void
    doAgentInit(org.openremote.model.asset.agent.Agent<?,?,?> agent)
     
    doProtocolAssetDiscovery(org.openremote.model.asset.agent.Agent<?,?,?> agent, Consumer<org.openremote.model.asset.AssetTreeNode[]> onDiscovered)
     
    doProtocolAssetImport(org.openremote.model.asset.agent.Agent<?,?,?> agent, byte[] fileData, Consumer<org.openremote.model.asset.AssetTreeNode[]> onDiscovered)
     
    doProtocolInstanceDiscovery(String parentId, Class<? extends org.openremote.model.protocol.ProtocolInstanceDiscovery> instanceDiscoveryProviderClass, Consumer<org.openremote.model.asset.agent.Agent<?,?,?>[]> onDiscovered)
     
    org.openremote.model.asset.agent.Agent<?,?,?>
    getAgent(String agentId)
     
    protected Map<String,org.openremote.model.asset.agent.Agent<?,?,?>>
     
    protected Map<org.openremote.model.asset.agent.Agent<?,?,?>,List<org.openremote.model.attribute.Attribute<?>>>
    getGroupedAgentLinkAttributes(Stream<org.openremote.model.attribute.Attribute<?>> attributes, Predicate<org.openremote.model.attribute.Attribute<?>> filter)
    Gets all agent link attributes and their linked agent and groups them by agent
    int
     
    org.openremote.model.asset.agent.Protocol<?>
     
    org.openremote.model.asset.agent.Protocol<?>
    getProtocolInstance(org.openremote.model.asset.agent.Agent<?,?,?> agent)
     
    void
    init(org.openremote.model.Container container)
     
    boolean
     
    protected void
    linkAttributes(org.openremote.model.asset.agent.Agent<?,?,?> agent, String assetId, Collection<org.openremote.model.attribute.Attribute<?>> attributes)
     
    protected void
    notifyAgentAncestor(org.openremote.model.asset.Asset<?> asset, org.openremote.model.PersistenceEvent<org.openremote.model.asset.Asset<?>> persistenceEvent)
     
    protected void
    notifyChildAssetChange(String agentId, org.openremote.model.PersistenceEvent<org.openremote.model.asset.Asset<?>> assetPersistenceEvent)
     
    protected void
     
    protected void
    onAgentAttributeEvent(org.openremote.model.attribute.AttributeEvent event)
    Called when an AttributeEvent for an Agent is broadcast on the client event bus (i.e.
    protected boolean
    onAttributeEventIntercepted(jakarta.persistence.EntityManager em, org.openremote.model.attribute.AttributeEvent event)
    Intercepts any AttributeEvent for an Attribute that has an MetaItemType.AGENT_LINK MetaItem and passes it to the Agent's Protocol.processLinkedAttributeWrite(org.openremote.model.attribute.AttributeEvent) method for handling.
    protected void
    processAgentChange(org.openremote.model.PersistenceEvent<org.openremote.model.asset.agent.Agent<?,?,?>> persistenceEvent)
    Called when an Agent is modified in the DB
    protected void
    processAssetChange(org.openremote.model.PersistenceEvent<org.openremote.model.asset.Asset<?>> persistenceEvent)
    Looks for new, modified and obsolete AGENT_LINK attributes and links / unlinks them with the protocol
    protected boolean
     
    protected void
    sendAttributeEvent(org.openremote.model.attribute.AttributeEvent event)
     
    void
    start(org.openremote.model.Container container)
     
    protected void
    startAgent(org.openremote.model.asset.agent.Agent<?,?,?> agent)
     
    void
    stop(org.openremote.model.Container container)
     
     
    protected void
     
    protected void
    unlinkAttributes(String agentId, String assetId, List<org.openremote.model.attribute.Attribute<?>> attributes)
     

    Methods inherited from class org.apache.camel.builder.RouteBuilder

    addLifecycleInterceptor, addRoutes, addRoutesToCamelContext, addTemplatedRoutesToCamelContext, bindToRegistry, bindToRegistry, checkInitialized, configuration, configureRest, configureRests, configureRoute, configureRouteConfiguration, configureRoutes, configureRouteTemplate, configureTemplatedRoute, customize, customize, dataFormat, errorHandler, errorHandler, expression, from, from, from, fromF, fromV, fromV, getBeans, getOrder, getResource, getRestCollection, getRestConfiguration, getRests, getRouteCollection, getRoutes, getRouteTemplateCollection, getTemplatedRouteCollection, initializeCamelContext, intercept, interceptFrom, interceptFrom, interceptSendToEndpoint, loadRoutesBuilder, loadRoutesBuilder, onCompletion, onException, onException, populateBeans, populateOrUpdateRoutes, populateRests, populateRoutes, populateRouteTemplates, populateTemplatedRoutes, populateTransformers, populateValidators, prepareModel, property, propertyInject, removeLifecycleInterceptor, rest, rest, restConfiguration, routeTemplate, setErrorHandlerFactory, setResource, setRestCollection, setRouteTemplateCollection, setTemplatedRouteCollection, templatedRoute, tokenizer, transformer, updateRoutesToCamelContext, validator

    Methods inherited from class org.apache.camel.builder.BuilderSupport

    body, bodyAs, constant, constant, constant, createErrorHandlerBuilder, csimple, csimple, datasonnet, datasonnet, datasonnet, deadLetterChannel, deadLetterChannel, defaultErrorHandler, endpoint, endpoint, exceptionMessage, exchangeProperty, expression, getCamelContext, getContext, getErrorHandlerFactory, hasErrorHandlerFactory, header, java, java, joor, joor, jq, jq, jsonpath, jsonpath, jtaTransactionErrorHandler, jtaTransactionErrorHandler, jtaTransactionErrorHandler, method, method, method, method, noErrorHandler, regexReplaceAll, regexReplaceAll, setCamelContext, simple, simple, simpleF, simpleF, springTransactionErrorHandler, springTransactionErrorHandler, springTransactionErrorHandler, systemProperty, systemProperty, variable, xpath, xpath, xpath, xpath

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Field Details

    • PRIORITY

      public static final int PRIORITY
      See Also:
    • assetProcessingService

      protected AssetProcessingService assetProcessingService
    • assetStorageService

      protected AssetStorageService assetStorageService
    • clientEventService

      protected ClientEventService clientEventService
    • gatewayService

      protected GatewayService gatewayService
    • executorService

      protected ExecutorService executorService
    • agentMap

      protected Map<String,org.openremote.model.asset.agent.Agent<?,?,?>> agentMap
    • agentDiscoveryImportFutureMap

      protected final Map<String,Future<Void>> agentDiscoveryImportFutureMap
    • protocolInstanceMap

      protected final Map<String,org.openremote.model.asset.agent.Protocol<?>> protocolInstanceMap
    • childAssetSubscriptions

      protected final Map<String,Set<Consumer<org.openremote.model.PersistenceEvent<org.openremote.model.asset.Asset<?>>>>> childAssetSubscriptions
    • initDone

      protected boolean initDone
    • container

      protected org.openremote.model.Container container
    • agentLock

      protected final Object agentLock
  • Constructor Details

    • AgentService

      public AgentService()
  • Method Details

    • getPriority

      public int getPriority()
      Specified by:
      getPriority in interface org.openremote.model.ContainerService
    • init

      public void init(org.openremote.model.Container container) throws Exception
      Specified by:
      init in interface org.openremote.model.ContainerService
      Throws:
      Exception
    • start

      public void start(org.openremote.model.Container container) throws Exception
      Specified by:
      start in interface org.openremote.model.ContainerService
      Throws:
      Exception
    • stop

      public void stop(org.openremote.model.Container container) throws Exception
      Specified by:
      stop in interface org.openremote.model.ContainerService
      Throws:
      Exception
    • configure

      public void configure() throws Exception
      Specified by:
      configure in class org.apache.camel.builder.RouteBuilder
      Throws:
      Exception
    • processAgentChange

      protected void processAgentChange(org.openremote.model.PersistenceEvent<org.openremote.model.asset.agent.Agent<?,?,?>> persistenceEvent)
      Called when an Agent is modified in the DB
    • deployAgent

      protected void deployAgent(org.openremote.model.asset.agent.Agent<?,?,?> agent)
      Deploy the Agent by creating a protocol instance, starting it and linking all attributes
    • processAssetChange

      protected void processAssetChange(org.openremote.model.PersistenceEvent<org.openremote.model.asset.Asset<?>> persistenceEvent)
      Looks for new, modified and obsolete AGENT_LINK attributes and links / unlinks them with the protocol
    • notifyAgentAncestor

      protected void notifyAgentAncestor(org.openremote.model.asset.Asset<?> asset, org.openremote.model.PersistenceEvent<org.openremote.model.asset.Asset<?>> persistenceEvent)
    • sendAttributeEvent

      protected void sendAttributeEvent(org.openremote.model.attribute.AttributeEvent event)
    • doAgentInit

      protected void doAgentInit(org.openremote.model.asset.agent.Agent<?,?,?> agent)
    • startAgent

      protected void startAgent(org.openremote.model.asset.agent.Agent<?,?,?> agent)
    • undeployAgent

      protected void undeployAgent(String agentId)
    • linkAttributes

      protected void linkAttributes(org.openremote.model.asset.agent.Agent<?,?,?> agent, String assetId, Collection<org.openremote.model.attribute.Attribute<?>> attributes)
    • unlinkAttributes

      protected void unlinkAttributes(String agentId, String assetId, List<org.openremote.model.attribute.Attribute<?>> attributes)
    • onAttributeEventIntercepted

      protected boolean onAttributeEventIntercepted(jakarta.persistence.EntityManager em, org.openremote.model.attribute.AttributeEvent event) throws AssetProcessingException
      Intercepts any AttributeEvent for an Attribute that has an MetaItemType.AGENT_LINK MetaItem and passes it to the Agent's Protocol.processLinkedAttributeWrite(org.openremote.model.attribute.AttributeEvent) method for handling.

      If the AttributeEvent originated from the Agent that the Attribute is linked to then it is not intercepted.

      Throws:
      AssetProcessingException
    • onAgentAttributeEvent

      protected void onAgentAttributeEvent(org.openremote.model.attribute.AttributeEvent event)
      Called when an AttributeEvent for an Agent is broadcast on the client event bus (i.e. the attribute has been updated in the DB).

      We use this to try and react to agent changes in a generic way by re-initialising the agent to simplify each agent implementation.

    • getGroupedAgentLinkAttributes

      protected Map<org.openremote.model.asset.agent.Agent<?,?,?>,List<org.openremote.model.attribute.Attribute<?>>> getGroupedAgentLinkAttributes(Stream<org.openremote.model.attribute.Attribute<?>> attributes, Predicate<org.openremote.model.attribute.Attribute<?>> filter)
      Gets all agent link attributes and their linked agent and groups them by agent
    • toString

      public String toString()
      Overrides:
      toString in class org.apache.camel.builder.RouteBuilder
    • addAgent

      protected org.openremote.model.asset.agent.Agent<?,?,?> addAgent(org.openremote.model.asset.agent.Agent<?,?,?> agent)
    • removeAgent

      protected boolean removeAgent(String agentId)
    • getAgent

      public org.openremote.model.asset.agent.Agent<?,?,?> getAgent(String agentId)
    • getAgents

      protected Map<String,org.openremote.model.asset.agent.Agent<?,?,?>> getAgents()
    • getProtocolInstance

      public org.openremote.model.asset.agent.Protocol<?> getProtocolInstance(org.openremote.model.asset.agent.Agent<?,?,?> agent)
    • getProtocolInstance

      public org.openremote.model.asset.agent.Protocol<?> getProtocolInstance(String agentId)
    • notifyChildAssetChange

      protected void notifyChildAssetChange(String agentId, org.openremote.model.PersistenceEvent<org.openremote.model.asset.Asset<?>> assetPersistenceEvent)
    • isProtocolAssetDiscoveryOrImportRunning

      public boolean isProtocolAssetDiscoveryOrImportRunning(String agentId)
    • doProtocolInstanceDiscovery

      public Future<Void> doProtocolInstanceDiscovery(String parentId, Class<? extends org.openremote.model.protocol.ProtocolInstanceDiscovery> instanceDiscoveryProviderClass, Consumer<org.openremote.model.asset.agent.Agent<?,?,?>[]> onDiscovered)
    • doProtocolAssetDiscovery

      public Future<Void> doProtocolAssetDiscovery(org.openremote.model.asset.agent.Agent<?,?,?> agent, Consumer<org.openremote.model.asset.AssetTreeNode[]> onDiscovered) throws RuntimeException
      Throws:
      RuntimeException
    • doProtocolAssetImport

      public Future<Void> doProtocolAssetImport(org.openremote.model.asset.agent.Agent<?,?,?> agent, byte[] fileData, Consumer<org.openremote.model.asset.AssetTreeNode[]> onDiscovered) throws RuntimeException
      Throws:
      RuntimeException
    • okToContinueWithImportOrDiscovery

      protected void okToContinueWithImportOrDiscovery(String agentId)