Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: sample code using subscriptions

...

Writing rules that use subscriptions

Endnotes

This page is inspired by (and often copied verbatim from) the original fourth lesson, Lesson: Pico to Pico Subscriptions.

Suppose that we wanted the Mischief pico to respond to a mischief/hat_lifted event and send the same event to all of the thing picos that it controls. Assuming that you have arranged to use Subscriptions as a module in the meta block, as in

Code Block
  use module Subscriptions

a rule like this one would accomplish this:

Code Block
  rule mischief_hat_lifted {
    select when mischief hat_lifted
    foreach Subscriptions:getSubscriptions() setting (subscription)
      pre {
        subs_attrs = subscription{"attributes"}
      }
      if subs_attrs{"subscriber_role"} == "thing" then
        event:send(
          { "eci": subs_attrs{"outbound_eci"}, "eid": "hat-lifted",
            "domain": "mischief", "type": "hat_lifted" }
        )
  }


Endnotes

This page is inspired by (and often copied verbatim from) the original fourth lesson, Lesson: Pico to Pico Subscriptions.