My Zwave Node is going through a massive cleanse (v10), and in doing so optimising A LOT.
My node subscribes to 20+ comm API messages from the runtime counterparts, to facilitate the UI
I needed a way to shrink this and make it more manageable
- Subscribing
- Unsubscribing (Yes!!! I clean things up
)
I present a little snippet for you all to benefit from - for me it works really well!
Enjoy
const setSubscription = (subscribe) => {
const Hooks = [
{ address: `zwave-js/ui/${networkId}/status`, method: commsStatus },
{ address: `zwave-js/ui/${networkId}/s2/grant`, method: commsGrant },
{ address: `zwave-js/ui/${networkId}/s2/dsk`, method: commsDSK },
{ Another },
{ Another },
{ Another * 20 more }
];
switch (subscribe) {
case true:
Hooks.forEach((H) => {
RED.comms.subscribe(H.address, H.method);
});
break;
case false:
Hooks.forEach((H) => {
RED.comms.unsubscribe(H.address, H.method);
});
break;
}
}
/* Example */
setSubscription(true | false)
/* Example responder */
const commsDSK = (topic, data) =>{
// do stuff
}