var fs = require('fs'); var path = require('path'); var storage = require('node-persist'); var hap = require("hap-nodejs"); var uuid = require("hap-nodejs").uuid; var Bridge = require("hap-nodejs").Bridge; var Accessory = require("hap-nodejs").Accessory; var Service = require("hap-nodejs").Service; var Characteristic = require("hap-nodejs").Characteristic; var accessoryLoader = require("hap-nodejs").AccessoryLoader; var once = require("hap-nodejs/lib/util/once").once; console.log("Starting HomeBridge server..."); console.log("_____________________________________________________________________"); console.log("IMPORTANT: Homebridge is in the middle of some big changes."); console.log(" Read more about it here:"); console.log(" https://github.com/nfarina/homebridge/wiki/Migration-Guide"); console.log("_____________________________________________________________________"); console.log(""); // Look for the configuration file var configPath = path.join(__dirname, "config.json"); // Complain and exit if it doesn't exist yet if (!fs.existsSync(configPath)) { console.log("Couldn't find a config.json file in the same directory as app.js. Look at config-sample.json for examples of how to format your config.js and add your home accessories."); process.exit(1); } // Initialize HAP-NodeJS hap.init(); // Load up the configuration file var config; try { config = JSON.parse(fs.readFileSync(configPath)); } catch (err) { console.log("There was a problem reading your config.json file."); console.log("Please try pasting your config.json file here to validate it: http://jsonlint.com"); console.log(""); throw err; } // pull out our custom Bridge settings from config.json, if any var bridgeConfig = config.bridge || {}; // Start by creating our Bridge which will host all loaded Accessories var bridge = new Bridge(bridgeConfig.name || 'Homebridge', uuid.generate("HomeBridge")); // 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 asyncWait = false; function startup() { asyncWait = true; if (config.platforms) loadPlatforms(); if (config.accessories) loadAccessories(); asyncWait = false; // publish now unless we're waiting on anyone if (asyncCalls == 0) publish(); } function loadAccessories() { // Instantiate all accessories in the config console.log("Loading " + config.accessories.length + " accessories..."); for (var i=0; i