commander_agent.rb 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. * `configure`: Target Agents have their options updated with the contents of `configure_options`.
  13. 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:
  14. - 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 %}`
  15. - Likewise, if you have a scheduled agent flow specially crafted for rainy days, try this: `{% if conditions contains 'Rain' %}enable{% else %}disabled{% endif %}`
  16. - If you want to update a WeatherAgent based on a UserLocationAgent, you could use `'action': 'configure'` and set 'configure_options' to `{ 'location': '{{_location_.latlng}}' }`.
  17. - In templating, you can use the variable `target` to refer to each target agent, which has the following attributes: #{AgentDrop.instance_methods(false).map { |m| "`#{m}`" }.to_sentence}.
  18. # Targets
  19. Select Agents that you want to control from this CommanderAgent.
  20. MD
  21. def working?
  22. true
  23. end
  24. def check
  25. control!
  26. end
  27. def receive(incoming_events)
  28. incoming_events.each do |event|
  29. interpolate_with(event) do
  30. control!
  31. end
  32. end
  33. end
  34. end
  35. end