better lights on/off management

This commit is contained in:
Egor 2017-12-02 16:26:18 -08:00
parent f7d8cff293
commit 826ab32229
3 changed files with 37 additions and 30 deletions

View file

@ -13,7 +13,7 @@ export default Component.extend({
selectedTab: 1, selectedTab: 1,
pauseLightUpdates: false, pauseLightUpdates: false,
displayFailure: true, displayNextFailure: true,
notify: inject.service(), notify: inject.service(),
@ -79,7 +79,7 @@ export default Component.extend({
if (!this.get('trial')) { if (!this.get('trial')) {
this.updateLightData(); this.updateLightData();
setInterval(this.updateLightData.bind(this), 2000); setInterval(this.updateLightData.bind(this), 3000);
} }
if (!isNone(storage.get('huegasm.selectedTab'))) { if (!isNone(storage.get('huegasm.selectedTab'))) {
@ -91,14 +91,14 @@ export default Component.extend({
let fail = () => { let fail = () => {
if (isNone(this.get('lightsData'))) { if (isNone(this.get('lightsData'))) {
this.send('clearBridge'); this.send('clearBridge');
} else if (this.get('displayFailure')) { } else if (this.get('displayNextFailure')) {
this.get('notify').warning({ html: '<div class="alert alert-warning" role="alert">Error retrieving data from your lights. Yikes.</div>' }); this.get('notify').warning({ html: '<div class="alert alert-warning" role="alert">Error retrieving data from your lights. Yikes.</div>' });
this.set('displayFailure', false); this.set('displayNextFailure', false);
later( later(
this, this,
function() { () => {
this.set('displayFailure', true); this.set('displayNextFailure', true);
}, },
30000 30000
); );

View file

@ -106,49 +106,56 @@ export default Component.extend({
} }
}), }),
// determines whether the lights are on/off for the lights switch // sync the system lights on/off state with the Huegasm UI
lightsOnChange: on( systemLightsOnChange: on(
'init', 'init',
observer('lightsData.@each.state.on', 'activeLights.[]', function() { observer('lightsData.@each.state.on', 'activeLights.[]', function() {
if (!this.get('strobeOn')) { if (!this.get('strobeOn')) {
let lightsData = this.get('lightsData'), let { lightsData, activeLights } = this.getProperties('lightsData', 'activeLights');
lightsOn = this.get('activeLights').some(function(light) {
return lightsData[light].state.on === true;
});
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() { onLightsOnChange: observer('lightsOn', function() {
let lightsData = this.get('lightsData'), let activeLightsLength = this.get('activeLights').length;
activeLights = this.get('activeLights'),
lightsOn = this.get('lightsOn');
let lightsOnSystem = activeLights.some(function(light) { this.set('lightsOnDisabled', true);
return lightsData[light].state.on === 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 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) { if (lightsOn !== lightsOnSystem) {
activeLights.forEach(light => { activeLights.forEach(lightId => {
$.ajax(this.get('apiURL') + '/lights/' + light + '/state', { $.ajax(`${apiURL}/lights/${lightId}/state`, {
data: JSON.stringify({ on: lightsOn }), data: JSON.stringify({ on: lightsOn }),
contentType: 'application/json', contentType: 'application/json',
type: 'PUT' type: 'PUT'
}); });
}); });
} }
}), },
changeLightsBrightness() { changeLightsBrightness() {
let lightsData = this.get('lightsData'), let { lightsData, lightsBrightness, activeLights } = this.getProperties('lightsData', 'lightsBrightness', 'activeLights'),
lightsBrightnessSystem = false, lightsBrightnessSystem = false;
lightsBrightness = this.get('lightsBrightness'),
activeLights = this.get('activeLights');
activeLights.forEach(function(light) { activeLights.forEach(light => {
lightsBrightnessSystem += lightsData[light].state.bri; lightsBrightnessSystem += lightsData[light].state.bri;
}); });
@ -167,9 +174,9 @@ export default Component.extend({
}, },
onBrightnessChanged: observer('lightsBrightness', function() { 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 // sync the current light settings to the newly added light

View file

@ -2,7 +2,7 @@
{{#paper-item}} {{#paper-item}}
{{paper-icon "power-settings-new" class=dimmerOnClass}} {{paper-icon "power-settings-new" class=dimmerOnClass}}
<p data-toggle="tooltip" data-placement="top auto" class="bootstrap-tooltip lights-control-tooltip" data-title="Turn the selected lights on/off">Power</p> <p data-toggle="tooltip" data-placement="top auto" class="bootstrap-tooltip lights-control-tooltip" data-title="Turn the selected lights on/off">Power</p>
{{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}}
{{#paper-item}} {{#paper-item}}