mirror of
https://github.com/mtan93/homebridge.git
synced 2026-04-19 06:13:10 +01:00
Merge pull request #125 from nfarina/possible-domoticz-async-fix
Fix for Domoticz platform async callbacks
This commit is contained in:
@@ -69,8 +69,14 @@ DomoticzPlatform.prototype = {
|
||||
this.log("Fetching Domoticz lights and switches...");
|
||||
var that = this;
|
||||
var foundAccessories = [];
|
||||
|
||||
// mechanism to ensure callback is only executed once all requests complete
|
||||
var asyncCalls = 0;
|
||||
function callbackLater() { if (--asyncCalls == 0) callback(foundAccessories); }
|
||||
|
||||
if (this.roomid == 0) {
|
||||
//Get Lights
|
||||
asyncCalls++;
|
||||
request.get({
|
||||
url: this.urlForQuery("type=devices&filter=light&used=true&order=Name"),
|
||||
json: true
|
||||
@@ -83,7 +89,7 @@ DomoticzPlatform.prototype = {
|
||||
foundAccessories.push(accessory);
|
||||
})
|
||||
}
|
||||
callback(foundAccessories);
|
||||
callbackLater();
|
||||
} else {
|
||||
that.log("There was a problem connecting to Domoticz. (" + err + ")");
|
||||
}
|
||||
@@ -91,6 +97,7 @@ DomoticzPlatform.prototype = {
|
||||
}
|
||||
else {
|
||||
//Get all devices specified in the room
|
||||
asyncCalls++;
|
||||
request.get({
|
||||
url: this.urlForQuery("type=devices&plan=" + this.roomid),
|
||||
json: true
|
||||
@@ -106,14 +113,14 @@ DomoticzPlatform.prototype = {
|
||||
}
|
||||
})
|
||||
}
|
||||
callback(foundAccessories);
|
||||
callbackLater();
|
||||
} else {
|
||||
that.log("There was a problem connecting to Domoticz.");
|
||||
}
|
||||
});
|
||||
}
|
||||
//Get Scenes
|
||||
foundAccessories = [];
|
||||
asyncCalls++;
|
||||
request.get({
|
||||
url: this.urlForQuery("type=scenes"),
|
||||
json: true
|
||||
@@ -126,7 +133,7 @@ DomoticzPlatform.prototype = {
|
||||
foundAccessories.push(accessory);
|
||||
})
|
||||
}
|
||||
callback(foundAccessories);
|
||||
callbackLater();
|
||||
} else {
|
||||
that.log("There was a problem connecting to Domoticz.");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user