monteslu / node-red-contrib-gpio

A set of node-red nodes for connecting to johnny-five IO Plugins
MIT License
41 stars 31 forks source link

improve j5 node sandbox #14

Closed boneskull closed 8 years ago

boneskull commented 8 years ago

Howdy,

I found it would be convenient if I could easily store my board or other device objects within the flow variable available to all "function" node sandboxes. e.g.:

// this is a j5 node
const left = new five.Relay({pin: 11, type: 'NC'});

left.on();

const relays = flow.get('relays') || {};
relays.left = left;
flow.set('relays', relays);

node.send({
    payload: {
        relay: 'left'
    }
});

Later, a "function" node:

// this is a plain old function node
const relay = flow.get('relays').left;
const data = msg.payload.data = {};
const isOn = relay.isOn;
data[`${msg.payload.relay}_on`] = isOn;
if (isOn) {
    node.status({fill: 'green', shape: 'dot', text: 'on'});
} else {
    node.status({fill: 'red', shape: 'ring', text: 'off'});
}

return msg;

Caveats:

monteslu commented 8 years ago

This looks great. Thanks @boneskull I'll give it a spin

monteslu commented 8 years ago

Works great. Thanks!

monteslu commented 8 years ago

updated deps for latest j5, and published version 0.5.0