mirror of
https://github.com/mtan93/homebridge.git
synced 2026-04-13 06:13:11 +01:00
Merge pull request #125 from nfarina/possible-domoticz-async-fix
Fix for Domoticz platform async callbacks
This commit is contained in:
@@ -66,11 +66,17 @@ DomoticzPlatform.prototype = {
|
|||||||
},
|
},
|
||||||
|
|
||||||
accessories: function(callback) {
|
accessories: function(callback) {
|
||||||
this.log("Fetching Domoticz lights and switches...");
|
this.log("Fetching Domoticz lights and switches...");
|
||||||
var that = this;
|
var that = this;
|
||||||
var foundAccessories = [];
|
var foundAccessories = [];
|
||||||
if (this.roomid == 0) {
|
|
||||||
|
// 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
|
//Get Lights
|
||||||
|
asyncCalls++;
|
||||||
request.get({
|
request.get({
|
||||||
url: this.urlForQuery("type=devices&filter=light&used=true&order=Name"),
|
url: this.urlForQuery("type=devices&filter=light&used=true&order=Name"),
|
||||||
json: true
|
json: true
|
||||||
@@ -83,14 +89,15 @@ DomoticzPlatform.prototype = {
|
|||||||
foundAccessories.push(accessory);
|
foundAccessories.push(accessory);
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
callback(foundAccessories);
|
callbackLater();
|
||||||
} else {
|
} else {
|
||||||
that.log("There was a problem connecting to Domoticz. (" + err + ")");
|
that.log("There was a problem connecting to Domoticz. (" + err + ")");
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
//Get all devices specified in the room
|
//Get all devices specified in the room
|
||||||
|
asyncCalls++;
|
||||||
request.get({
|
request.get({
|
||||||
url: this.urlForQuery("type=devices&plan=" + this.roomid),
|
url: this.urlForQuery("type=devices&plan=" + this.roomid),
|
||||||
json: true
|
json: true
|
||||||
@@ -106,30 +113,30 @@ DomoticzPlatform.prototype = {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
callback(foundAccessories);
|
callbackLater();
|
||||||
} else {
|
} else {
|
||||||
that.log("There was a problem connecting to Domoticz.");
|
that.log("There was a problem connecting to Domoticz.");
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
//Get Scenes
|
//Get Scenes
|
||||||
foundAccessories = [];
|
asyncCalls++;
|
||||||
request.get({
|
request.get({
|
||||||
url: this.urlForQuery("type=scenes"),
|
url: this.urlForQuery("type=scenes"),
|
||||||
json: true
|
json: true
|
||||||
}, function(err, response, json) {
|
}, function(err, response, json) {
|
||||||
if (!err && response.statusCode == 200) {
|
if (!err && response.statusCode == 200) {
|
||||||
if (json['result'] != undefined) {
|
if (json['result'] != undefined) {
|
||||||
var sArray=sortByKey(json['result'],"Name");
|
var sArray=sortByKey(json['result'],"Name");
|
||||||
sArray.map(function(s) {
|
sArray.map(function(s) {
|
||||||
accessory = new DomoticzAccessory(that.log, that, true, s.idx, s.Name, false, 0, false);
|
accessory = new DomoticzAccessory(that.log, that, true, s.idx, s.Name, false, 0, false);
|
||||||
foundAccessories.push(accessory);
|
foundAccessories.push(accessory);
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
callback(foundAccessories);
|
callbackLater();
|
||||||
} else {
|
} else {
|
||||||
that.log("There was a problem connecting to Domoticz.");
|
that.log("There was a problem connecting to Domoticz.");
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user