Restore platform support

This commit is contained in:
Nick Farina
2015-08-04 15:48:52 -07:00
parent 2ad9643bee
commit f71730cff6
+41 -17
View File
@@ -18,7 +18,7 @@ if (!fs.existsSync(configPath)) {
} }
// Initialize persistent storage // Initialize persistent storage
//storage.initSync(); storage.initSync();
// Start by creating our Bridge which will host all loaded Accessories // Start by creating our Bridge which will host all loaded Accessories
var bridge = new Bridge('HomeBridge', uuid.generate("HomeBridge")); var bridge = new Bridge('HomeBridge', uuid.generate("HomeBridge"));
@@ -27,18 +27,19 @@ var bridge = new Bridge('HomeBridge', uuid.generate("HomeBridge"));
var config = JSON.parse(fs.readFileSync(configPath)); var config = JSON.parse(fs.readFileSync(configPath));
// keep track of async calls we're waiting for callbacks on before we can start up // keep track of async calls we're waiting for callbacks on before we can start up
// this is hacky but this is all going away once we build proper plugin support
var asyncCalls = 0; var asyncCalls = 0;
var asyncWait = false;
function startup() { function startup() {
// if (config.platforms) loadPlatforms(); asyncWait = true;
if (config.platforms) loadPlatforms();
if (config.accessories) loadAccessories(); if (config.accessories) loadAccessories();
asyncWait = false;
bridge.publish({ // publish now unless we're waiting on anyone
username: "CC:22:3D:E3:CE:27", if (asyncCalls == 0)
port: 51826, publish();
pincode: "031-45-154",
category: Accessory.Categories.OTHER
});
} }
function loadAccessories() { function loadAccessories() {
@@ -81,6 +82,7 @@ function loadAccessories() {
function loadPlatforms() { function loadPlatforms() {
console.log("Loading " + config.platforms.length + " platforms..."); console.log("Loading " + config.platforms.length + " platforms...");
for (var i=0; i<config.platforms.length; i++) { for (var i=0; i<config.platforms.length; i++) {
var platformConfig = config.platforms[i]; var platformConfig = config.platforms[i];
@@ -96,24 +98,46 @@ function loadPlatforms() {
log("Initializing " + platformName + " platform..."); log("Initializing " + platformName + " platform...");
var platform = new platformConstructor(log, platformConfig); var platformInstance = new platformConstructor(log, platformConfig);
// query for devices // query for devices
platform.accessories(function(foundAccessories){ asyncCalls++;
platformInstance.accessories(function(foundAccessories){
asyncCalls--;
// loop through accessories adding them to the list and registering them // loop through accessories adding them to the list and registering them
for (var i = 0; i < foundAccessories.length; i++) { for (var i = 0; i < foundAccessories.length; i++) {
accessory = foundAccessories[i] var accessoryInstance = foundAccessories[i];
accessories.push(accessory);
log("Initializing device with name " + accessory.name + "...") log("Initializing device with name " + accessoryInstance.name + "...")
// Extract the raw "services" for this accessory which is a big array of objects describing the various // Extract the raw "services" for this accessory which is a big array of objects describing the various
// hooks in and out of HomeKit for the HAP-NodeJS server. // hooks in and out of HomeKit for the HAP-NodeJS server.
var services = accessory.getServices(); var services = accessoryInstance.getServices();
// Create the HAP server for this accessory
createHAPServer(accessory.name, services, accessory.transportCategory); // Create the actual HAP-NodeJS "Accessory" instance
var accessory = accessoryLoader.parseAccessoryJSON({
displayName: name,
services: services
});
// add it to the bridge
bridge.addBridgedAccessory(accessory);
} }
accessories.push.apply(accessories, foundAccessories);
// were we the last callback?
if (asyncCalls === 0 && !asyncWait)
publish();
}) })
} }
} }
function publish() {
bridge.publish({
username: "CC:22:3D:E3:CE:27",
port: 51826,
pincode: "031-45-154",
category: Accessory.Categories.OTHER
});
}
startup(); startup();