From 826ab32229d39d49a02e0eb845bda4621944debb Mon Sep 17 00:00:00 2001 From: Egor Date: Sat, 2 Dec 2017 16:26:18 -0800 Subject: [PATCH] better lights on/off management --- .../pods/components/hue-controls/component.js | 12 ++--- .../pods/components/lights-tab/component.js | 53 +++++++++++-------- .../pods/components/lights-tab/template.hbs | 2 +- 3 files changed, 37 insertions(+), 30 deletions(-) diff --git a/web/app/pods/components/hue-controls/component.js b/web/app/pods/components/hue-controls/component.js index 573a644..d880d6b 100644 --- a/web/app/pods/components/hue-controls/component.js +++ b/web/app/pods/components/hue-controls/component.js @@ -13,7 +13,7 @@ export default Component.extend({ selectedTab: 1, pauseLightUpdates: false, - displayFailure: true, + displayNextFailure: true, notify: inject.service(), @@ -79,7 +79,7 @@ export default Component.extend({ if (!this.get('trial')) { this.updateLightData(); - setInterval(this.updateLightData.bind(this), 2000); + setInterval(this.updateLightData.bind(this), 3000); } if (!isNone(storage.get('huegasm.selectedTab'))) { @@ -91,14 +91,14 @@ export default Component.extend({ let fail = () => { if (isNone(this.get('lightsData'))) { this.send('clearBridge'); - } else if (this.get('displayFailure')) { + } else if (this.get('displayNextFailure')) { this.get('notify').warning({ html: '' }); - this.set('displayFailure', false); + this.set('displayNextFailure', false); later( this, - function() { - this.set('displayFailure', true); + () => { + this.set('displayNextFailure', true); }, 30000 ); diff --git a/web/app/pods/components/lights-tab/component.js b/web/app/pods/components/lights-tab/component.js index 8906aa4..eb059d1 100644 --- a/web/app/pods/components/lights-tab/component.js +++ b/web/app/pods/components/lights-tab/component.js @@ -106,49 +106,56 @@ export default Component.extend({ } }), - // determines whether the lights are on/off for the lights switch - lightsOnChange: on( + // sync the system lights on/off state with the Huegasm UI + systemLightsOnChange: on( 'init', observer('lightsData.@each.state.on', 'activeLights.[]', function() { if (!this.get('strobeOn')) { - let lightsData = this.get('lightsData'), - lightsOn = this.get('activeLights').some(function(light) { - return lightsData[light].state.on === true; - }); + let { lightsData, activeLights } = this.getProperties('lightsData', 'activeLights'); - this.set('lightsOn', lightsOn); + this.set('lightsOn', activeLights.some(light => lightsData[light].state.on === true)); } }) ), + // sync the system lights on/off state from the Huegasm UI onLightsOnChange: observer('lightsOn', function() { - let lightsData = this.get('lightsData'), - activeLights = this.get('activeLights'), - lightsOn = this.get('lightsOn'); + let activeLightsLength = this.get('activeLights').length; - let lightsOnSystem = activeLights.some(function(light) { - return lightsData[light].state.on === true; - }); + this.set('lightsOnDisabled', true); + + throttle(this, this.changeLightsOnOff, activeLightsLength * 69, false); + }), + + changeLightsOnOff() { + let { lightsData, activeLights, lightsOn, apiURL } = this.getProperties('lightsData', 'activeLights', 'lightsOn', 'apiURL'), + lightsOnSystem = activeLights.some(light => lightsData[light].state.on === true); + + later( + this, + () => { + this.set('lightsOnDisabled', false); + }, + 400 + ); // if the internal lights state is different than the one from lightsData (user manually toggled the switch), send the request to change the bulbs state if (lightsOn !== lightsOnSystem) { - activeLights.forEach(light => { - $.ajax(this.get('apiURL') + '/lights/' + light + '/state', { + activeLights.forEach(lightId => { + $.ajax(`${apiURL}/lights/${lightId}/state`, { data: JSON.stringify({ on: lightsOn }), contentType: 'application/json', type: 'PUT' }); }); } - }), + }, changeLightsBrightness() { - let lightsData = this.get('lightsData'), - lightsBrightnessSystem = false, - lightsBrightness = this.get('lightsBrightness'), - activeLights = this.get('activeLights'); + let { lightsData, lightsBrightness, activeLights } = this.getProperties('lightsData', 'lightsBrightness', 'activeLights'), + lightsBrightnessSystem = false; - activeLights.forEach(function(light) { + activeLights.forEach(light => { lightsBrightnessSystem += lightsData[light].state.bri; }); @@ -167,9 +174,9 @@ export default Component.extend({ }, onBrightnessChanged: observer('lightsBrightness', function() { - let activeLights = this.get('activeLights').length; + let activeLightsLength = this.get('activeLights').length; - throttle(this, this.changeLightsBrightness, activeLights * 69, false); + throttle(this, this.changeLightsBrightness, activeLightsLength * 69, false); }), // sync the current light settings to the newly added light diff --git a/web/app/pods/components/lights-tab/template.hbs b/web/app/pods/components/lights-tab/template.hbs index e773534..ccdd79f 100644 --- a/web/app/pods/components/lights-tab/template.hbs +++ b/web/app/pods/components/lights-tab/template.hbs @@ -2,7 +2,7 @@ {{#paper-item}} {{paper-icon "power-settings-new" class=dimmerOnClass}}

Power

- {{paper-switch value=lightsOn onChange=(action (mut lightsOn)) disabled=(or trial playing) skipProxy=trial label=lightsOnTxt}} + {{paper-switch value=lightsOn onChange=(action (mut lightsOn)) disabled=(or trial playing lightsOnDisabled) skipProxy=trial label=lightsOnTxt}} {{/paper-item}} {{#paper-item}}