Aida-Server/lib/hs100-controller.js

43 lines
970 B
JavaScript
Raw Normal View History

2018-01-23 20:58:05 +00:00
const Hs100Api = require('tplink-smarthome-api');
2017-11-09 00:05:30 +00:00
const client = new Hs100Api.Client();
const em = require('events').EventEmitter;
const util = require('util');
const logger = require('log4js').getLogger('hs100');
logger.level = 'debug';
const Hs100Controller = function() {
const _this = this;
client.startDiscovery().on('device-new', (device) => {
2017-11-11 22:37:59 +00:00
device.getSysInfo().
catch((err) => {
logger.error(err);
}).then((deviceInfo) => {
logger.debug(deviceInfo);
_this.emit('found', deviceInfo);
});
2017-11-09 00:05:30 +00:00
device.setPowerState(true);
device.startPolling(10000);
device.on('power-on', (d) => {
logger.debug('Power on', d);
});
device.on('power-off', (d) => {
logger.debug('Power off', d);
});
});
//
this.on('newListener', listener => {
logger.info(`Event Listener: ${ listener}`);
});
return this;
};
util.inherits(Hs100Controller, em);
module.exports = Hs100Controller;