UNPKG

node-red-contrib-etekcity-smartplug

Version:

etekcity smartplug

57 lines (53 loc) 2 kB
module.exports = function (RED) { 'use strict'; var Client = require('etekcity-smartplug'); var client = new Client(); function EtekCity(n) { RED.nodes.createNode(this, n); var account = RED.nodes.getNode(n.account); var deviceID = n.device || 'none'; var node = this; node.on('input', function (msg) { var action = msg.payload.toLowerCase(); if(action === 'on' || action === 'off') { if(deviceID !== 'none') { client.login(account.username, account.password).then( () => { if(action === 'off') { return client.turnOff(deviceID); } else { return client.turnOn(deviceID); } }); node.status({ fill: 'green', shape: 'dot', text: 'status: '+action }); node.send(msg); }else{ node.status({ fill: 'red', shape: 'ring', text: 'no device selected' }); node.send({payload:'error: no device selected'}) } }else{ node.status({ fill: 'red', shape: 'ring', text: 'invalid payload' }); node.send({payload:'error: invalid payload'}) } }); RED.httpAdmin.get("/etekcityDevices", RED.auth.needsPermission('etekcity.read'), function(req,res) { client.login(account.username, account.password).then( () => { return client.getDevices(); }).then( devices => { res.json(devices); }); }); } RED.nodes.registerType('etekcity',EtekCity); };