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