Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Prepare for this section by manually creating three child picos of your owner root pico.

  1. Install this ruleset into your owner root pico: https://raw.githubusercontent.com/Picolab/pico_lessons/master/subscriptions/mischief.owner.krl
  2. Install this ruleset into your Mischief pico: https://raw.githubusercontent.com/Picolab/pico_lessons/master/subscriptions/mischief.krl, and 
  3. Finally, install this ruleset into each of your thing picos: https://raw.githubusercontent.com/Picolab/pico_lessons/master/subscriptions/mischief.thing.krl

...

It is possible to initiate a subscription with a rule running in a pico which already has in its possession the ECI's of the two picos which need to be related by the subscription. Once you have set up the three child picos, Mischief, Thing1, and Thing2, and installed their respective rulesets, study the code and prepare for the introductions.

The owner root pico already has all three of the other ECI's in its array of children (maintained by the io.picolabs.wrangler aka Wrangler ruleset), but it doesn't know which is which. So we added this rule to allow it to distinguish between the Mischief pico (which identifies itself by ECI) and the Thing1 and Thing2 picos.

...

The ruleset for the Mischief pico sets up a button "mischief/identity" in that pico's Testing tab which will send this event to the owner pico. Navigate to the Testing tab of the Mischief pico and click the button. Doing so will send the event mischief:who to the owner pico, and that event will trigger the rule shown above. You can check the result of this calculation by looking at the entity variables held by the mischief.owner ruleset in the Rulesets tab of the Owner Pico.

Once the owner root pico has all of the ECI's, the code to introduce the Mischief pico to each of the thing picos is straight-forward.

Code Block
  rule mischief_subscriptions {
    select when mischief subscriptions
    foreach ent:things setting(thing,index)
      // introduce mischief pico to thing[index] pico
      event:send(
        { "eci": ent:mischief, "eid": "subscription",
          "domain": "wrangler", "type": "subscription",
          "attrs": { "name": "thing" + (index.as("Number")+1),
                     "Rx_role": "thing",
                     "Tx_role": "controller",
                     "channel_type": "subscription",
                     "wellKnown_Tx": thing } } )
  }

The mischief.owner ruleset also sets up a button in the owner root pico's Testing tab, "mischief/subscriptions" to send the event that causes this rule to evaluate. Make sure to install io.picolabs.subscription on all of your picos, otherwise none of the rulesets we provided will select on these events. Navigate to the Testing tab for the Owner Root Pico to click on this button, which will cause the mischief_subscriptions rule to evaluate and make the subscription requests to all of the thing picos.

...