commander_agent.rb 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. module Agents
  2. class CommanderAgent < Agent
  3. include AgentControllerConcern
  4. cannot_create_events!
  5. description <<~MD
  6. The Commander Agent is triggered by schedule or an incoming event, and commands other agents ("targets") to run, disable, configure, or enable themselves.
  7. # Action types
  8. Set `action` to one of the action types below:
  9. * `run`: Target Agents are run when this agent is triggered.
  10. * `disable`: Target Agents are disabled (if not) when this agent is triggered.
  11. * `enable`: Target Agents are enabled (if not) when this agent is triggered.
  12. * If the option `drop_pending_events` is set to `true`, pending events will be cleared before the agent is enabled.
  13. * `configure`: Target Agents have their options updated with the contents of `configure_options`.
  14. Here's a tip: you can use [Liquid](https://github.com/huginn/huginn/wiki/Formatting-Events-using-Liquid) templating to dynamically determine the action type. For example:
  15. - To create a CommanderAgent that receives an event from a WeatherAgent every morning to kick an agent flow that is only useful in a nice weather, try this: `{% if conditions contains 'Sunny' or conditions contains 'Cloudy' %}` `run{% endif %}`
  16. - Likewise, if you have a scheduled agent flow specially crafted for rainy days, try this: `{% if conditions contains 'Rain' %}enable{% else %}disabled{% endif %}`
  17. - If you want to update a WeatherAgent based on a UserLocationAgent, you could use `'action': 'configure'` and set 'configure_options' to `{ 'location': '{{_location_.latlng}}' }`.
  18. - In templating, you can use the variable `target` to refer to each target agent, which has the following attributes: #{Agent::Drop.instance_methods(false).map { |m| "`#{m}`" }.to_sentence}.
  19. # Targets
  20. Select Agents that you want to control from this CommanderAgent.
  21. MD
  22. def working?
  23. true
  24. end
  25. def check
  26. control!
  27. end
  28. def receive(incoming_events)
  29. incoming_events.each do |event|
  30. interpolate_with(event) do
  31. control!
  32. end
  33. end
  34. end
  35. end
  36. end