From d6ce365f254f8c4819034362170e8ee5f16adb11 Mon Sep 17 00:00:00 2001 From: Martin Donnelly Date: Fri, 10 Nov 2017 21:33:14 +0000 Subject: [PATCH] using a map in the lights --- lib/aida.js | 6 +++--- lib/light-controller.js | 19 +++++++++++-------- lib/recipes.js | 2 +- 3 files changed, 15 insertions(+), 12 deletions(-) diff --git a/lib/aida.js b/lib/aida.js index dfa49db..197525c 100644 --- a/lib/aida.js +++ b/lib/aida.js @@ -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); } }); diff --git a/lib/light-controller.js b/lib/light-controller.js index 035246b..658b7bf 100644 --- a/lib/light-controller.js +++ b/lib/light-controller.js @@ -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); } }); diff --git a/lib/recipes.js b/lib/recipes.js index 6a78233..1f8bc14 100644 --- a/lib/recipes.js +++ b/lib/recipes.js @@ -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) {