Aida-Server/lib/hive-controller.js
2017-11-09 00:05:30 +00:00

62 lines
1.3 KiB
JavaScript

const Hive = require('bg-hive-api');
const ClimateControl = require('bg-hive-api/climateControl');
const em = require('events').EventEmitter;
const util = require('util');
const logger = require('log4js').getLogger('Hive');
logger.level = 'debug';
const config = {
'login': 'martind2000@gmail.com',
'password': '1V3D4m526i'
};
const hive = new Hive(config.login, config.password);
const HiveController = function() {
const _this = this;
hive.on('login', function(context) {
logger.debug('Connected');
// hive.Logout();
const climate = new ClimateControl(context);
// Handle the on complete event.
climate.on('complete', function(response) {
// write the response state object to the console.
// console.log(response);
_this.emit('update', response);
// log out
hive.Logout();
});
climate.GetState();
});
// On logout call this event handler
hive.on('logout', () => {
logger.info('Connection Closed');
});
// On invalid username or password
hive.on('not_authorised', () => {
logger.error('Connection Refused');
});
// Log in
this.on('newListener', listener => {
logger.info(`Event Listener: ${ listener}`);
});
return this;
};
HiveController.prototype.update = function() {
hive.Login();
};
util.inherits(HiveController, em);
module.exports = HiveController;