forked from felixls/node-red-contrib-tplink-smarthome
-
Notifications
You must be signed in to change notification settings - Fork 0
/
smart-plug.js
11 lines (11 loc) · 5.54 KB
/
smart-plug.js
1
2
3
4
5
6
7
8
9
10
11
module.exports=function(h){var k=require("tplink-smarthome-api").Client;h.nodes.registerType("smart-plug",function(f){h.nodes.createNode(this,f);this.config={name:f.name,device:f.device,interval:f.interval,eventInterval:f.eventInterval};var e=this.config.device,d=require("moment"),l=require("numeral"),g=this.context(),a=this;a.deviceInstance=null;a.deviceConnected=!1;if(null===e||""===e)return a.status({fill:"red",shape:"ring",text:"Not configured"}),!1;a.status({fill:"grey",shape:"dot",text:"Initializing\u2026"});
a.connectClient=function(){(new k).getDevice({host:e}).then(function(b){a.deviceConnected=!0;a.deviceInstance=b;a.status({fill:"yellow",shape:"dot",text:"Connected"});b.on("power-on",function(){a.sendPowerUpdateEvent(!0)});b.on("power-off",function(){a.sendPowerUpdateEvent(!1)});b.on("in-use",function(){a.sendInUseEvent(!0)});b.on("not-in-use",function(){a.sendInUseEvent(!1)});b.on("device-online",function(){a.sendDeviceOnlineEvent(!0)});b.on("device-offline",function(){a.sendDeviceOnlineEvent(!1)});
a.startPolling()})["catch"](function(){return a.handleConnectionError()})};a.disconnectClient=function(){a.deviceConnected=!1};a.isClientConnected=function(){return!0===a.deviceConnected};a.startIsAlivePolling=function(){a.pingPolling=setInterval(function(){if(a.isClientConnected())a.deviceInstance.getInfo()["catch"](function(){return a.handleConnectionError()});else return a.connectClient()},parseInt(a.config.interval))};a.stopIsAlivePolling=function(){clearInterval(a.pingPolling);a.pingPolling=
null};a.startPolling=function(){a.eventPolling=setInterval(function(){if(null===a.deviceInstance)a.stopPolling();else if(a.isClientConnected())a.checkAction("getInfoEvents")&&a.sendDeviceSysInfo(),a.checkAction("getMeterEvents")&&a.sendDeviceMeterInfo();else return a.status({fill:"red",shape:"ring",text:"Not reachable"}),a.stopPolling(),!1},parseInt(a.config.eventInterval))};a.stopPolling=function(){clearInterval(a.eventPolling);a.eventPolling=null};a.on("input",function(b){if(!a.isClientConnected())return a.handleConnectionError("not reachable");
var c=["getMeterEvents","getInfoEvents","getPowerUpdateEvents","getInUseEvents","getOnlineEvents"];if(1==b.payload||0==b.payload)a.deviceInstance.setPowerState(b.payload).then(function(){a.sendDeviceSysInfo()})["catch"](function(b){return a.handleConnectionError(b)});else if("getInfo"===b.payload)a.sendDeviceSysInfo();else if("getCloudInfo"===b.payload)a.sendDeviceCloudInfo();else if("getQuickInfo"===b.payload)a.sendDeviceQuickInfo();else if("switch"===b.payload)a.deviceInstance.togglePowerState();
else if("getMeterInfo"===b.payload)a.sendDeviceMeterInfo();else if("clearEvents"===b.payload)g.set("action",b.payload);else if("eraseStats"===b.payload)a.sendEraseStatsResult();else{var d=[];b.payload.split("|").forEach(function(a){-1!==c.indexOf(a)&&d.push(a)});0<d.length?g.set("action",d.join("|")):g.set("action","")}});a.checkAction=function(a){return void 0!==g.get("action")&&null!==g.get("action")&&g.get("action").includes(a)};a.sendDeviceSysInfo=function(){a.deviceInstance.getSysInfo().then(function(b){1===
b.relay_state?(g.set("state","on"),a.status({fill:"green",shape:"dot",text:"Turned ON"})):(g.set("state","off"),a.status({fill:"red",shape:"dot",text:"Turned OFF"}));var c={};c.payload=b;c.payload.timestamp=d().format();a.send(c)})["catch"](function(b){return a.handleConnectionError(b)})};a.sendDeviceCloudInfo=function(){a.deviceInstance.cloud.getInfo().then(function(b){var c={};c.payload=b;c.payload.timestamp=d().format();a.send(c)})["catch"](function(b){return a.handleConnectionError(b)})};a.sendDeviceQuickInfo=
function(){a.deviceInstance.getInfo().then(function(b){var c={};c.payload=b;c.payload.timestamp=d().format();a.send(c)})["catch"](function(b){return a.handleConnectionError(b)})};a.sendDeviceMeterInfo=function(){a.deviceInstance.emeter.getRealtime().then(function(b){var c="on"===g.get("state")?"turned on":"turned off",e=l(b.current).format("0.[000]"),f=l(b.voltage).format("0.[0]"),h=l(b.power).format("0.[00]");a.status({fill:"gray",shape:"dot",text:c+" ["+h+"W: "+f+"V@"+e+"A]"});c={};c.payload=b;
c.payload.timestamp=d().format();a.send(c)})["catch"](function(b){return a.handleConnectionError(b)})};a.sendPowerUpdateEvent=function(b){if(a.checkAction("getPowerUpdateEvents")){var c={payload:{}};c.payload.powerOn=b;c.payload.timestamp=d().format();a.send(c)}};a.sendInUseEvent=function(b){if(a.checkAction("getInUseEvents")){var c={payload:{}};c.payload.inUse=b;c.payload.timestamp=d().format();a.send(c)}};a.sendDeviceOnlineEvent=function(b){if(a.checkAction("getOnlineEvents")){var c={payload:{}};
c.payload.online=b;c.payload.timestamp=d().format();a.send(c)}};a.sendEraseStatsResult=function(){a.deviceInstance.emeter.eraseStats({}).then(function(b){var c={};c.payload=b;a.send(c)})["catch"](function(b){return a.handleConnectionError(b)})};a.handleConnectionError=function(b){b&&a.error(b);a.status({fill:"red",shape:"ring",text:"Not reachable"});a.disconnectClient();return!1};a.on("close",function(){a.deviceConnected=!1;a.stopPolling();a.stopIsAlivePolling()});a.connectClient();a.startIsAlivePolling()});
h.httpAdmin.get("/smarthome/plugs",function(f,e){try{var d=new k,h=[];d.on("device-new",function(d){h.push(d.host)});d.startDiscovery({deviceTypes:["plug"]});setTimeout(function(){d.stopDiscovery();e.end(JSON.stringify(h))},1E4)}catch(g){e.sendStatus(500).send(g.message)}});h.httpAdmin.get("/smarthome/plug",function(f,e){if(!f.query.ip)return e.status(500).send("Missing Device IP\u2026");(new k).getDevice({host:f.query.ip}).then(function(d){e.end(d.model)})["catch"](function(d){e.sendStatus(500).send(d.message)})})};