using a map in the lights

This commit is contained in:
Martin Donnelly 2017-11-10 21:33:14 +00:00
parent fbb7d8aaa9
commit d6ce365f25
3 changed files with 15 additions and 12 deletions

View File

@ -13,7 +13,7 @@ logger.level = 'debug';
const devices = {
'sensors': {},
'lights': {},
'lights': new Map(),
'wemo': {},
'heating': {}
};
@ -39,9 +39,9 @@ Aida.prototype.init = function() {
this.mqtt = new MqttController();
this.lights.on('found', (d) => {
if (devices.lights[d.id] === undefined) {
if (devices.lights.get(d.id) === undefined) {
logger.debug('Adding to light list');
devices.lights[d.id] = d;
devices.lights.set(d.id, d);
}
});

View File

@ -7,10 +7,8 @@ logger.level = 'debug';
const LightController = function() {
const _this = this;
const devices = [];
const devices = new Map();
client.on('light-new', light => {
_this.emit('found', light);
devices.push(light);
logger.debug('New light found.');
logger.debug(`ID: ${ light.id}`);
logger.debug(`IP: ${ light.address }:${ light.port}`);
@ -19,10 +17,16 @@ const LightController = function() {
if (err)
logger.debug(err);
// if (devices.get(light.id) === undefined)
devices.set(light.id, Object.assign({}, light));
logger.debug(`Label: ${ info.label}`);
logger.debug('Power:', (info.power === 1) ? 'on' : 'off');
logger.debug('Color:', info.color);
logger.debug('info', info);
_this.emit('found', light);
const label = light.label || '';
_this.emit(label.replace(' ', ''), { 'status':light.status, 'id':light.id });
// _this.emit(info.label.replace(' ',''), )
});
@ -76,11 +80,10 @@ const LightController = function() {
});
this.on('setColour', (data) => {
for(const light of devices) {
if (light.id === data.id) {
logger.info('Setting color of ', light.label);
light.color(data.color.hue, data.color.saturation, data.color.brightness, data.color.kelvin);
}
const wLight = devices.get(data.id);
if (wLight) {
logger.info('Setting color of ', wLight.label);
wLight.color(data.color.hue, data.color.saturation, data.color.brightness, data.color.kelvin);
}
});

View File

@ -1,6 +1,6 @@
const em = require('events').EventEmitter;
const util = require('util');
const logger = require('log4js').getLogger('BedroomRecipe');
const logger = require('log4js').getLogger('recipes');
logger.level = 'debug';
const BedroomRecipe = function(devices) {