forked from felixls/node-red-contrib-tplink-smarthome
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsmart-bulb.js
12 lines (12 loc) · 6.18 KB
/
smart-bulb.js
1
2
3
4
5
6
7
8
9
10
11
12
module.exports=function(h){var k=require("tplink-smarthome-api").Client;h.nodes.registerType("smart-bulb",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;b.startPolling(parseInt(a.config.interval));a.status({fill:"yellow",shape:"dot",text:"Connected"});b.on("lightstate-on",function(){a.sendPowerUpdateEvent(!0)});b.on("lightstate-off",function(){a.sendPowerUpdateEvent(!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","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(b.payload.includes("brightness")){var d=parseInt(b.payload.split(":")[1]);a.deviceInstance.getSysInfo().then(function(b){if(1===b.is_dimmable){if(1<=d&&100>=d)return a.deviceInstance.lighting.setLightState({brightness:d});throw"Brightness Should be between 1 and 100.";}throw"Changing Brightness is not supported !.";
}).then(function(){return a.sendDeviceSysInfo()})["catch"](function(b){return a.sendError(b)})}else if(b.payload.includes("temperature")){var e=parseInt(b.payload.split(":")[1]);a.deviceInstance.getSysInfo().then(function(b){if(1===b.is_variable_color_temp){if(2700<=e&&6500>=e)return a.deviceInstance.lighting.setLightState({color_temp:e});throw"Temperature Should be between 2700 and 6500.";}throw"Changing Temperature is not supported !.";}).then(function(){return a.sendDeviceSysInfo()})["catch"](function(b){return a.sendError(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 f=[];b.payload.split("|").forEach(function(a){-1!==c.indexOf(a)&&f.push(a)});0<f.length?g.set("action",
f.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.light_state.on_off?(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.sendPowerUpdateEvent=function(b){if(a.checkAction("getPowerUpdateEvents")){var c={payload:{}};
c.payload.powerOn=b;c.payload.timestamp=d().format();a.send(c)}};a.sendDeviceMeterInfo=function(){a.deviceInstance.emeter.getRealtime().then(function(b){var c="on"===g.get("state")?"Turned ON":"Turned OFF",e=l(b.power_mw).format("0.[00]")/1E3;a.status({fill:"gray",shape:"dot",text:c+" ["+e+"W]"});c={};c.payload=b;c.payload.power_w=e;c.payload.timestamp=d().format();a.send(c)})["catch"](function(b){return a.handleConnectionError(b)})};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.sendError=function(b){b&&a.error(b);return!1};a.on("close",function(){a.deviceConnected=!1;a.stopPolling();
a.stopIsAlivePolling()});a.connectClient();a.startIsAlivePolling()});h.httpAdmin.get("/smarthome/bulbs",function(f,e){try{var d=new k,h=[];d.on("device-new",function(d){h.push(d.host)});d.startDiscovery({deviceTypes:["bulb"]});setTimeout(function(){d.stopDiscovery();e.end(JSON.stringify(h))},1E4)}catch(g){e.sendStatus(500).send(g.message)}});h.httpAdmin.get("/smarthome/bulb",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)})})};