Skip to content

Commit

Permalink
Issue #15
Browse files Browse the repository at this point in the history
Heartbeat now (again) does separate requests for sensors, lights,
groups, etc.
  • Loading branch information
ebaauw committed Feb 22, 2017
1 parent ba815ad commit ff1581a
Showing 1 changed file with 74 additions and 39 deletions.
113 changes: 74 additions & 39 deletions lib/HueBridge.js
Original file line number Diff line number Diff line change
Expand Up @@ -278,8 +278,13 @@ HueBridge.prototype.createGroup0 = function() {

HueBridge.prototype.heartbeat = function(beat) {
if (beat % this.state.heartrate === 0) {
this.heartbeatResources()
this.heartbeatConfig()
.then(this.heartbeatSensors.bind(this))
.then(this.heartbeatLights.bind(this))
.then(this.heartbeatGroups.bind(this))
.then(this.heartbeatGroup0.bind(this))
.then(this.heartbeatSchedules.bind(this))
.then(this.heartbeatRules.bind(this))
.catch(function (err) {
if (err.message) {
this.log.error(err.message);
Expand All @@ -288,50 +293,52 @@ HueBridge.prototype.heartbeat = function(beat) {
}
};

HueBridge.prototype.heartbeatResources = function() {
return this.request('get', '/', null).then(function(obj) {
this.state.lastupdated = obj.config.UTC === 'none' ?
'n/a' : String(new Date(obj.config.UTC)).substring(0, 25);
HueBridge.prototype.heartbeatConfig = function() {
return this.request('get', '/config').then(function(config) {
this.state.lastupdated = config.UTC === 'none' ?
'n/a' : String(new Date(config.UTC)).substring(0, 25);
this.service
.updateCharacteristic(Characteristic.LastUpdated, this.state.lastupdated);
if (this.platform.config.sensors) {
for (const id in obj.sensors) {
const a = this.sensors[id];
if (a) {
a.heartbeat(obj.sensors[id]);
}
}
}
if (this.platform.config.lights) {
for (const id in obj.lights) {
const a = this.lights[id];
if (a) {
a.heartbeat(obj.lights[id]);
}
}
}
if (this.platform.config.groups) {
for (const id in obj.groups) {
const a = this.groups[id];
if (a) {
a.heartbeat(obj.groups[id]);
}
}.bind(this));
};

HueBridge.prototype.heartbeatSensors = function() {
if (!this.platform.config.sensors) {
return Promise.resolve();
}
return this.request('get', '/sensors', null).then(function(sensors) {
for (const id in sensors) {
const a = this.sensors[id];
if (a) {
a.heartbeat(sensors[id]);
}
}
if (this.platform.config.schedules) {
for (const id in obj.schedules) {
const a = this.schedules[id];
if (a) {
a.heartbeat(obj.schedules[id]);
}
}.bind(this));
};

HueBridge.prototype.heartbeatLights = function() {
if (!this.platform.config.lights) {
return Promise.resolve();
}
return this.request('get', '/lights', null).then(function(lights) {
for (const id in lights) {
const a = this.lights[id];
if (a) {
a.heartbeat(lights[id]);
}
}
if (this.platform.config.rules) {
for (const id in obj.rules) {
const a = this.rules[id];
if (a) {
a.heartbeat(obj.rules[id]);
}
}.bind(this));
};

HueBridge.prototype.heartbeatGroups = function() {
if (!this.platform.config.groups) {
return Promise.resolve();
}
return this.request('get', '/groups', null).then(function(groups) {
for (const id in groups) {
const a = this.groups[id];
if (a) {
a.heartbeat(groups[id]);
}
}
}.bind(this));
Expand All @@ -349,6 +356,34 @@ HueBridge.prototype.heartbeatGroup0 = function() {
}.bind(this));
};

HueBridge.prototype.heartbeatSchedules = function() {
if (!this.platform.config.schedules) {
return Promise.resolve();
}
return this.request('get', '/schedules', null).then(function(schedules) {
for (const id in schedules) {
const a = this.schedules[id];
if (a) {
a.heartbeat(schedules[id]);
}
}
}.bind(this));
};

HueBridge.prototype.heartbeatRules = function() {
if (!this.platform.config.rules) {
return Promise.resolve();
}
return this.request('get', '/rules', null).then(function(rules) {
for (const id in rules) {
const a = this.rules[id];
if (a) {
a.heartbeat(rules[id]);
}
}
}.bind(this));
};

// ===== Homekit Events ========================================================

HueBridge.prototype.setHeartrate = function(rate, callback) {
Expand Down

0 comments on commit ff1581a

Please sign in to comment.