diff --git a/package.json b/package.json index 436f72a..9ec5cb0 100644 --- a/package.json +++ b/package.json @@ -11,7 +11,7 @@ "product": [ "Tapo Smart Plug" ], - "shortname": "tplink-kasasmartplug", + "shortname": "tplink-taposmartplug", "description": "Companion module for TP-Link Tapo Smart Plugs", "main": "src/index.js", "scripts": { diff --git a/src/feedbacks.js b/src/feedbacks.js index 2718a89..bb84e2a 100644 --- a/src/feedbacks.js +++ b/src/feedbacks.js @@ -11,7 +11,7 @@ module.exports = { const backgroundColorGreen = self.rgb(0, 255, 0) // Green const backgroundColorOrange = self.rgb(255, 102, 0) // Orange - /*feedbacks.powerState = { + feedbacks.powerState = { type: 'boolean', label: 'Power State', description: 'Indicate if Plug is On or Off', @@ -35,7 +35,7 @@ module.exports = { let opt = feedback.options; if (self.PLUGINFO) { - let plug_state = self.PLUGINFO.relay_state; + let plug_state = self.PLUGINFO.device_on; if (plug_state == opt.option) { return true; @@ -44,7 +44,7 @@ module.exports = { return false } - }*/ + } return feedbacks } diff --git a/src/index.js b/src/index.js index 4dd522b..de2b572 100644 --- a/src/index.js +++ b/src/index.js @@ -1,14 +1,38 @@ var instance_skel = require('../../../instance_skel') var actions = require('./actions.js') -var presets = require('./presets.js') var feedbacks = require('./feedbacks.js') var variables = require('./variables.js') +var presets = require('./presets.js') var debug; const tapoapi = require('tp-link-tapo-connect'); +instance.prototype.INTERVAL = null; //used for polling device instance.prototype.PLUGINFO = { + device_id: '', + fw_ver: '', + hw_ver: '', + model: '', + mac: '', + + hw_id: '', + hw_id: '', + oem_id: '', + + on_time: '', + overheated: false, + nickname: '', + location: '', + + latitude: '', + longitude: '', + + ssid: '', + signal_level: '', + rssi: '', + + device_on: false }; instance.prototype.DEVICE_TOKEN = null; @@ -33,14 +57,7 @@ instance.GetUpgradeScripts = function () { instance.prototype.destroy = function () { let self = this; - try { - if (self.DEVICE) { - self.DEVICE.closeConnection(); - } - } - catch(error) { - self.DEVICE = null; - } + self.stopInterval(); debug('destroy', self.id) }; @@ -55,6 +72,7 @@ instance.prototype.init = function () { self.status(self.STATUS_WARNING, 'connecting'); self.getInformation(); + self.setupInterval(); self.init_actions(); self.init_feedbacks(); @@ -73,6 +91,7 @@ instance.prototype.updateConfig = function (config) { self.status(self.STATUS_WARNING, 'connecting'); self.getInformation(); + self.setupInterval(); self.init_actions(); self.init_feedbacks(); @@ -83,17 +102,51 @@ instance.prototype.updateConfig = function (config) { self.checkFeedbacks(); }; +instance.prototype.setupInterval = function() { + let self = this; + + if (self.INTERVAL !== null) { + clearInterval(self.INTERVAL); + self.INTERVAL = null; + } + + self.config.interval = parseInt(self.config.interval); + + if (self.config.interval > 0) { + self.log('info', 'Starting Update Interval.'); + self.INTERVAL = setInterval(self.getInformation.bind(self), self.config.interval); + } +}; + +instance.prototype.stopInterval = function () { + let self = this; + + self.log('info', 'Stopping Update Interval.'); + + if (self.INTERVAL) { + clearInterval(self.INTERVAL); + self.INTERVAL = null; + } +}; + instance.prototype.handleError = function(err) { let self = this; + self.log('error', 'Stopping Update interval due to error.'); + self.stopInterval(); + let error = err.toString(); Object.keys(err).forEach(function(key) { if (key === 'code') { - if (err[key] === 'ECONNREFUSED') { - error = 'Unable to communicate with Device. Connection refused. Is this the right IP address? Is it still online?'; - self.log('error', error); - self.status(self.STATUS_ERROR); + switch(err[key]) { + case 'ECONNREFUSED': + case 'EHOSTUNREACH': + case 'ETIMEDOUT': + error = 'Unable to communicate with Device. Connection refused. Is this the right IP address? Is it still online?'; + self.log('error', error); + self.status(self.STATUS_ERROR); + break; } } }); @@ -108,10 +161,11 @@ instance.prototype.getInformation = async function () { if (!self.DEVICE_TOKEN) { self.DEVICE_TOKEN = await tapoapi.loginDeviceByIp(self.config.email, self.config.password, self.config.host); } - - const getDeviceInfoResponse = await tapoapi.getDeviceInfo(self.DEVICE_TOKEN); - console.log(getDeviceInfoResponse); - //update data based on response + + if (self.DEVICE_TOKEN) { + let data = await tapoapi.getDeviceInfo(self.DEVICE_TOKEN); + self.updateData(data); + } } catch(error) { self.handleError(error); @@ -120,8 +174,42 @@ instance.prototype.getInformation = async function () { }; -instance.prototype.updateData = function () { +instance.prototype.updateData = function (data) { let self = this; + + self.status(self.STATUS_OK); + + try { + self.PLUGINFO.device_id = data.device_id; + self.PLUGINFO.fw_ver = data.fw_ver; + self.PLUGINFO.hw_ver = data.hw_ver; + self.PLUGINFO.model = data.model; + self.PLUGINFO.mac = data.mac; + + self.PLUGINFO.hw_id = data.hw_id; + self.PLUGINFO.fw_id = data.fw_id; + self.PLUGINFO.oem_id = data.oem_id; + + self.PLUGINFO.on_time = data.on_time; + self.PLUGINFO.overheated = data.overheated; + self.PLUGINFO.nickname = data.nickname; + self.PLUGINFO.location = data.location; + + self.PLUGINFO.latitude = data.latitude; + self.PLUGINFO.longitude = data.longitude; + + self.PLUGINFO.ssid = data.ssid; + self.PLUGINFO.signal_level = data.signal_level; + self.PLUGINFO.rssi = data.rssi; + + self.PLUGINFO.device_on = data.device_on; + + self.checkFeedbacks(); + self.checkVariables(); + } + catch(error) { + //error setting data + } }; // Return config fields for web config @@ -227,6 +315,7 @@ instance.prototype.power = async function(powerState) { if (self.DEVICE_TOKEN) { try { + let plugName = self.PLUGINFO.nickname; self.log('info', `Setting ${plugName} Power State to: ${(powerState ? 'On' : 'Off')}`); if (powerState == 1) { diff --git a/src/variables.js b/src/variables.js index f068ade..49ad8ef 100644 --- a/src/variables.js +++ b/src/variables.js @@ -6,6 +6,28 @@ module.exports = { let self = this; let variables = []; + variables.push({ name: 'device_id', label: 'Device ID' }); + variables.push({ name: 'fw_ver', label: 'FW Version' }); + variables.push({ name: 'hw_ver', label: 'HW Version' }); + variables.push({ name: 'model', label: 'Model' }); + variables.push({ name: 'mac', label: 'MAC Address' }); + + variables.push({ name: 'hw_id', label: 'HW ID' }); + variables.push({ name: 'fw_id', label: 'FW ID' }); + variables.push({ name: 'oem_id', label: 'OEM ID' }); + + variables.push({ name: 'on_time', label: 'On Time' }); + variables.push({ name: 'overheated', label: 'Overheated' }); + variables.push({ name: 'nickname', label: 'Nickname' }); + variables.push({ name: 'location', label: 'Location' }); + + variables.push({ name: 'latitude', label: 'Latitude' }); + variables.push({ name: 'longitude', label: 'Longitude' }); + + variables.push({ name: 'ssid', label: 'SSID' }); + variables.push({ name: 'signal_level', label: 'Signal Level' }); + variables.push({ name: 'rssi', label: 'RSSI' }); + variables.push({ name: 'power_state', label: 'Power State' }); return variables @@ -18,7 +40,29 @@ module.exports = { let self = this; try { - self.setVariable('power_state', (self.PLUGINFO.relay_state == 1 ? 'On' : 'Off')); + self.setVariable('device_id', self.PLUGINFO.device_id); + self.setVariable('fw_ver', self.PLUGINFO.fw_ver); + self.setVariable('hw_ver', self.PLUGINFO.hw_ver); + self.setVariable('model', self.PLUGINFO.model); + self.setVariable('mac', self.PLUGINFO.mac); + + self.setVariable('hw_id', self.PLUGINFO.hw_id); + self.setVariable('fw_id', self.PLUGINFO.fw_id); + self.setVariable('oem_id', self.PLUGINFO.oem_id); + + self.setVariable('on_time', self.PLUGINFO.on_time); + self.setVariable('overheated', (self.PLUGINFO.overheated == true ? 'On' : 'Off')); + self.setVariable('nickname', self.PLUGINFO.nickname); + self.setVariable('location', self.PLUGINFO.location); + + self.setVariable('latitude', self.PLUGINFO.latitude); + self.setVariable('longitude', self.PLUGINFO.longitude); + + self.setVariable('ssid', self.PLUGINFO.ssid); + self.setVariable('signal_level', self.PLUGINFO.signal_level); + self.setVariable('rssi', self.PLUGINFO.rssi); + + self.setVariable('power_state', (self.PLUGINFO.device_on == true ? 'On' : 'Off')); } catch(error) { self.log('error', 'Error from Plug: ' + String(error));