Use WIP version of HAP-NodeJS refactor

This commit is contained in:
Nick Farina
2015-08-03 08:33:41 -07:00
parent ab71dc352f
commit 6e61287f59
3 changed files with 32 additions and 102 deletions

View File

@@ -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)

130
app.js
View File

@@ -1,7 +1,10 @@
var fs = require('fs');
var path = require('path');
var storage = require('node-persist');
var crypto = require('crypto');
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...");
@@ -15,23 +18,34 @@ if (!fs.existsSync(configPath)) {
}
// Initialize persistent storage
storage.initSync();
//storage.initSync();
// 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
var asyncCalls = 0;
function startup() {
if (config.platforms) loadPlatforms();
// if (config.platforms) loadPlatforms();
if (config.accessories) loadAccessories();
bridge.publish({
username: "CC:22:3D:E3:CE:27",
port: 51826,
pincode: "031-45-154",
category: Accessory.Categories.OTHER
});
}
function loadAccessories() {
// Instantiate all accessories in the config
console.log("Loading " + config.accessories.length + " accessories...");
for (var i=0; i<config.accessories.length; i++) {
var accessoryConfig = config.accessories[i];
@@ -46,15 +60,21 @@ function loadAccessories() {
var log = function(name) { return function(s) { console.log("[" + name + "] " + s); }; }(name);
log("Initializing " + accessoryName + " accessory...");
var accessory = new accessoryConstructor(log, accessoryConfig);
accessories.push(accessory);
var accessoryInstance = new accessoryConstructor(log, accessoryConfig);
// 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();
var services = accessoryInstance.getServices();
// Create the actual HAP-NodeJS "Accessory" instance
var accessory = accessoryLoader.parseAccessoryJSON({
displayName: name,
services: services
});
// Create the HAP server for this accessory
createHAPServer(name, services, accessory.transportCategory);
// add it to the bridge
bridge.addBridgedAccessory(accessory);
}
}
@@ -96,94 +116,4 @@ function loadPlatforms() {
}
}
//
// Creates the actual HAP servers which listen on different sockets
//
// Pull in required HAP-NodeJS stuff
var accessory_Factor = new require("HAP-NodeJS/Accessory.js");
var accessoryController_Factor = new require("HAP-NodeJS/AccessoryController.js");
var service_Factor = new require("HAP-NodeJS/Service.js");
var characteristic_Factor = new require("HAP-NodeJS/Characteristic.js");
// Each accessory has its own little server. We'll need to allocate some ports for these servers
var nextPort = 51826;
var nextServer = 0;
var accessoryServers = [];
var accessoryControllers = [];
var usernames = {};
function createHAPServer(name, services, transportCategory) {
var accessoryController = new accessoryController_Factor.AccessoryController();
//loop through services
for (var j = 0; j < services.length; j++) {
var service = new service_Factor.Service(services[j].sType);
//loop through characteristics
for (var k = 0; k < services[j].characteristics.length; k++) {
var options = {
onRead: services[j].characteristics[k].onRead,
onRegister: services[j].characteristics[k].onRegister,
type: services[j].characteristics[k].cType,
perms: services[j].characteristics[k].perms,
format: services[j].characteristics[k].format,
initialValue: services[j].characteristics[k].initialValue,
supportEvents: services[j].characteristics[k].supportEvents,
supportBonjour: services[j].characteristics[k].supportBonjour,
manfDescription: services[j].characteristics[k].manfDescription,
designedMaxLength: services[j].characteristics[k].designedMaxLength,
designedMinValue: services[j].characteristics[k].designedMinValue,
designedMaxValue: services[j].characteristics[k].designedMaxValue,
designedMinStep: services[j].characteristics[k].designedMinStep,
unit: services[j].characteristics[k].unit
};
var characteristic = new characteristic_Factor.Characteristic(options, services[j].characteristics[k].onUpdate);
service.addCharacteristic(characteristic);
}
accessoryController.addService(service);
}
// create a unique "username" for this accessory based on the default display name
var username = createUsername(name);
if (usernames[username]) {
console.log("Cannot create another accessory with the same name '" + name + "'. The 'name' property must be unique for each accessory.");
return;
}
// remember that we used this name already
usernames[username] = name;
// increment ports for each accessory
nextPort = nextPort + (nextServer*2);
// hardcode the PIN to something random (same PIN as HAP-NodeJS sample accessories)
var pincode = "031-45-154";
var accessory = new accessory_Factor.Accessory(name, username, storage, parseInt(nextPort), pincode, accessoryController, transportCategory);
accessoryServers[nextServer] = accessory;
accessoryControllers[nextServer] = accessoryController;
accessory.publishAccessory();
nextServer++;
}
// Creates a unique "username" for HomeKit from a hash of the given string
function createUsername(str) {
// Hash str into something like "098F6BCD4621D373CADE4E832627B4F6"
var hash = crypto.createHash('md5').update(str).digest("hex").toUpperCase();
// Turn it into a MAC-address-looking "username" for HomeKit
return hash[0] + hash[1] + ":" +
hash[2] + hash[3] + ":" +
hash[4] + hash[5] + ":" +
hash[6] + hash[7] + ":" +
hash[8] + hash[9] + ":" +
hash[10] + hash[11];
}
startup();

View File

@@ -14,7 +14,7 @@
"ad2usb": "git+https://github.com/alistairg/node-ad2usb.git#local",
"carwingsjs": "0.0.x",
"elkington": "kevinohara80/elkington",
"hap-nodejs": "git+https://github.com/khaost/HAP-NodeJS#2a1bc8d99a2009317ab5da93faebea34c89f197c",
"hap-nodejs": "git+https://github.com/nfarina/HAP-NodeJS#aaa5770488d698d777e7d7706909c3afa888b031",
"harmonyhubjs-client": "^1.1.4",
"harmonyhubjs-discover": "git+https://github.com/swissmanu/harmonyhubjs-discover.git",
"mdns": "^2.2.4",