← back to the blog


Efficient Selectors

Simple example:

A single relay A executes:

dev$.byTag('light').setOff();

This will turn all devices tagged as a ‘light’ to off. The most efficient way to do this is use a broadcast for certain devices, in this case the ZigBee devices.
broadcast-unicast
Steps:

  1. Relay executes code, triggering two asynchronous network events
  2. The relay sends unicast network traffic to the single 6loWPAN light… and at the same time sends a broadcast ZigBee message to the ZigBee lights.

Selectors Across Physical Locations

When you have a selector which spans multiple locations, DeviceJS distributes commands to multiple locations simultaneously:

Back to our example code

dev$.byTag('light').setOff();

If this script was executed in a DeviceJS configuration such as below from relay A:

top-down-execution

then relay A would determine that all lights were on downline relays.

The steps would be:

  1. Relay A evaluates the script, and sees that all the devices are off relays B and C. It sends a message stating it’s reached this portion of the script, and B and C need to execute it from this point.
  2. Relays B and C would turn off the lights.

Events

Selectors may be used to trigger events. Here a motion sensor is added into the scenario. Relay B talks to this motion sensor. When looking at this example, remember that DeviceJS scripts run on a system of relays, not a single relay. So the same script is residing at every relay, and is actively running. (In this case waiting for ‘motion’). [How it works]

propogate-execution

function func1(){
    dev$.byTag('light').setOn();}
dev$.trigger('motion', func1);

Steps:

  1. Relay B gets a message from the motion sensor. This event is mapped to the functionfunc1func1() executes. Locally it immediately turns on the locally connected light. It also sends a message to the upline relay A that the motion event has happened.
  2. The upline relay A receives the message that ‘motion’ has occurred and passes the message to other relays – the only other relay is C. It also executes func1 which does nothing locally.
  3. Relay C recieves the message of ‘motion’ triggering the execution of func1. It turns its local lights on.

Please Sign In to leave a comment.