diff --git a/accessories/WeMo.js b/accessories/WeMo.js index 0313878..273e4b3 100644 --- a/accessories/WeMo.js +++ b/accessories/WeMo.js @@ -68,7 +68,7 @@ WeMoAccessory.prototype = { if (!err) { var binaryState = parseInt(result) that.log("power state for " + that.wemoName + " is: " + binaryState) - callback(binaryState) + callback(binaryState > 0 ? 1 : 0); } else { that.log(err) diff --git a/app.js b/app.js index 7ed29d1..b24ad18 100644 --- a/app.js +++ b/app.js @@ -1,7 +1,11 @@ var fs = require('fs'); var path = require('path'); var storage = require('node-persist'); -var crypto = require('crypto'); +var hap = require('HAP-NodeJS'); +var uuid = require('HAP-NodeJS').uuid; +var Bridge = require('HAP-NodeJS').Bridge; +var Accessory = require('HAP-NodeJS').Accessory; +var accessoryLoader = require('HAP-NodeJS').AccessoryLoader; console.log("Starting HomeBridge server..."); @@ -14,24 +18,36 @@ if (!fs.existsSync(configPath)) { process.exit(1); } -// Initialize persistent storage -storage.initSync(); +// Initialize HAP-NodeJS +hap.init(); + +// Start by creating our Bridge which will host all loaded Accessories +var bridge = new Bridge('HomeBridge', uuid.generate("HomeBridge")); // Load up the configuration file var config = JSON.parse(fs.readFileSync(configPath)); -// Just to prevent them getting garbage collected -var accessories = []; +// 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