node-red-contrib-blynk-ws
Version:
Node Red integration with Blynk App and Server through websockets
65 lines (59 loc) • 1.82 kB
JavaScript
module.exports = (RED) => {
function BlynkInWriteNode(n) {
RED.nodes.createNode(this, n);
this.client = n.client;
const node = this;
this.blynkClient = RED.nodes.getNode(this.client);
this.nodeType = 'write';
this.pin = n.pin;
this.pin_all = n.pin_all;
if (this.pin_all) this.connected_label = RED._('blynk-ws-in-write.status.connected-all');
else this.connected_label = RED._('blynk-ws-in-write.status.connected-fixed') + this.pin;
if (this.blynkClient) {
this.blynkClient.registerInputNode(this);
this.blynkClient.on('opened', (n) => { // eslint-disable-line no-shadow
node.status({
fill: 'yellow',
shape: 'dot',
text: RED._('blynk-ws-in-write.status.connecting') + n,
});
});
this.blynkClient.on('connected', () => {
node.status({
fill: 'green',
shape: 'dot',
text: node.connected_label,
});
});
this.blynkClient.on('error', () => {
node.status({
fill: 'red',
shape: 'ring',
text: 'blynk-ws-in-write.status.error',
});
});
this.blynkClient.on('closed', () => {
node.status({
fill: 'red',
shape: 'ring',
text: 'blynk-ws-in-write.status.disconnected',
});
});
this.blynkClient.on('disabled', () => {
node.status({
fill: 'red',
shape: 'dot',
text: 'blynk-ws-in-write.status.disabled',
});
});
} else {
this.error(RED._('blynk-ws-in-write.errors.missing-conf'));
}
this.on('close', () => {
if (node.blynkClient) {
node.blynkClient.removeInputNode(node);
}
});
}
RED.nodes.registerType('blynk-ws-in-write', BlynkInWriteNode);
};