This repository has been archived on 2026-04-30. You can view files and clone it, but cannot push or open issues or pull requests.
huegasm/app/components/bridge-controls.js

48 lines
1.3 KiB
JavaScript

import Em from 'ember';
export default Em.Component.extend({
bridgeIp: null,
bridgeUsername: null,
lightsData: null,
numLights: function(){
var lightsData = this.get('lightsData'), numLights = 0;
for (let key in this.get('lightsData')) {
if(lightsData.hasOwnProperty(key)){
numLights++;
}
}
return numLights;
}.property('lightsData'),
lightsApiURL: function(){
return 'http://' + this.get('bridgeIp') + '/api/' + this.get('bridgeUsername') + '/lights';
}.property('bridgeIp', 'bridgeUsername'),
didInsertElement: function() {
this.set('lightsDataIntervalHandle', setInterval(this.updateLightData.bind(this), 1000));
},
updateLightData: function(){
var self = this;
Em.$.get(this.get('lightsApiURL'), function (result, status) {
if (status === 'success' && JSON.stringify(self.get('lightsData')) !== JSON.stringify(result) ) {
self.set('lightsData', result);
} else if(status !== 'success' ) {
// something went terribly wrong ( user got unauthenticated? ) and we'll need to start all over
clearInterval(self.get('lightsDataIntervalHandle'));
this.setProperties({
bridgeIp: null,
bridgeUsername: null
});
console.error(status + ': ' + result);
}
});
}
});