mirror of
https://github.com/mtan93/homebridge.git
synced 2026-04-06 14:23:11 +01:00
Merge pull request #16 from nfarina/smart-config-loading
Don't crash on missing config items
This commit is contained in:
85
app.js
85
app.js
@@ -20,45 +20,16 @@ storage.initSync();
|
|||||||
// Load up the configuration file
|
// Load up the configuration file
|
||||||
var config = JSON.parse(fs.readFileSync(configPath));
|
var config = JSON.parse(fs.readFileSync(configPath));
|
||||||
|
|
||||||
|
// Just to prevent them getting garbage collected
|
||||||
|
var accessories = [];
|
||||||
|
|
||||||
|
function startup() {
|
||||||
|
if (config.platforms) loadPlatforms();
|
||||||
|
if (config.accessories) loadAccessories();
|
||||||
|
}
|
||||||
|
|
||||||
function loadAccessories() {
|
function loadAccessories() {
|
||||||
|
|
||||||
var accessories = [];
|
|
||||||
|
|
||||||
console.log("Loading " + config.platforms.length + " platforms...");
|
|
||||||
for (var i=0; i<config.platforms.length; i++) {
|
|
||||||
|
|
||||||
var platformConfig = config.platforms[i];
|
|
||||||
|
|
||||||
// Load up the class for this accessory
|
|
||||||
var platformName = platformConfig["platform"]; // like "Wink"
|
|
||||||
var platformModule = require('./platforms/' + platformName + ".js"); // like "./platforms/Wink.js"
|
|
||||||
var platformConstructor = platformModule.platform; // like "WinkPlatform", a JavaScript constructor
|
|
||||||
|
|
||||||
// Create a custom logging function that prepends the platform display name for debugging
|
|
||||||
var name = platformConfig["name"];
|
|
||||||
var log = function(name) { return function(s) { console.log("[" + name + "] " + s); }; }(name);
|
|
||||||
|
|
||||||
log("Initializing " + platformName + " platform...");
|
|
||||||
|
|
||||||
var platform = new platformConstructor(log, platformConfig);
|
|
||||||
|
|
||||||
// query for devices
|
|
||||||
platform.accessories(function(foundAccessories){
|
|
||||||
// loop through accessories adding them to the list and registering them
|
|
||||||
for (var i = 0; i < foundAccessories.length; i++) {
|
|
||||||
accessory = foundAccessories[i]
|
|
||||||
accessories.push(accessory);
|
|
||||||
log("Initializing device with name " + accessory.name + "...")
|
|
||||||
// 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.
|
|
||||||
var services = accessory.getServices();
|
|
||||||
// Create the HAP server for this accessory
|
|
||||||
createHAPServer(accessory.name, services);
|
|
||||||
}
|
|
||||||
accessories.push.apply(accessories, foundAccessories);
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// Instantiate all accessories in the config
|
// Instantiate all accessories in the config
|
||||||
console.log("Loading " + config.accessories.length + " accessories...");
|
console.log("Loading " + config.accessories.length + " accessories...");
|
||||||
for (var i=0; i<config.accessories.length; i++) {
|
for (var i=0; i<config.accessories.length; i++) {
|
||||||
@@ -87,6 +58,44 @@ function loadAccessories() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function loadPlatforms() {
|
||||||
|
|
||||||
|
console.log("Loading " + config.platforms.length + " platforms...");
|
||||||
|
for (var i=0; i<config.platforms.length; i++) {
|
||||||
|
|
||||||
|
var platformConfig = config.platforms[i];
|
||||||
|
|
||||||
|
// Load up the class for this accessory
|
||||||
|
var platformName = platformConfig["platform"]; // like "Wink"
|
||||||
|
var platformModule = require('./platforms/' + platformName + ".js"); // like "./platforms/Wink.js"
|
||||||
|
var platformConstructor = platformModule.platform; // like "WinkPlatform", a JavaScript constructor
|
||||||
|
|
||||||
|
// Create a custom logging function that prepends the platform display name for debugging
|
||||||
|
var name = platformConfig["name"];
|
||||||
|
var log = function(name) { return function(s) { console.log("[" + name + "] " + s); }; }(name);
|
||||||
|
|
||||||
|
log("Initializing " + platformName + " platform...");
|
||||||
|
|
||||||
|
var platform = new platformConstructor(log, platformConfig);
|
||||||
|
|
||||||
|
// query for devices
|
||||||
|
platform.accessories(function(foundAccessories){
|
||||||
|
// loop through accessories adding them to the list and registering them
|
||||||
|
for (var i = 0; i < foundAccessories.length; i++) {
|
||||||
|
accessory = foundAccessories[i]
|
||||||
|
accessories.push(accessory);
|
||||||
|
log("Initializing device with name " + accessory.name + "...")
|
||||||
|
// 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.
|
||||||
|
var services = accessory.getServices();
|
||||||
|
// Create the HAP server for this accessory
|
||||||
|
createHAPServer(accessory.name, services);
|
||||||
|
}
|
||||||
|
accessories.push.apply(accessories, foundAccessories);
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
// Creates the actual HAP servers which listen on different sockets
|
// Creates the actual HAP servers which listen on different sockets
|
||||||
//
|
//
|
||||||
@@ -175,4 +184,4 @@ function createUsername(str) {
|
|||||||
hash[10] + hash[11];
|
hash[10] + hash[11];
|
||||||
}
|
}
|
||||||
|
|
||||||
loadAccessories();
|
startup();
|
||||||
|
|||||||
Reference in New Issue
Block a user