From 01d2c21aac1045d789a6fc3e7d23e02dfc2eed28 Mon Sep 17 00:00:00 2001 From: Nick Farina Date: Sat, 29 Aug 2015 22:28:52 -0700 Subject: [PATCH 01/18] New iControl accessory, supports Xfinity Home. - No more mysterious "dsig" param --- accessories/XfinityHome.js | 284 ------------------------------------- accessories/iControl.js | 130 +++++++++++++++++ config-sample.json | 10 +- package.json | 5 +- 4 files changed, 138 insertions(+), 291 deletions(-) delete mode 100644 accessories/XfinityHome.js create mode 100644 accessories/iControl.js diff --git a/accessories/XfinityHome.js b/accessories/XfinityHome.js deleted file mode 100644 index cafdbd1..0000000 --- a/accessories/XfinityHome.js +++ /dev/null @@ -1,284 +0,0 @@ -var types = require("HAP-NodeJS/accessories/types.js"); -var request = require("request"); -var xmldoc = require("xmldoc"); - -function XfinityHomeAccessory(log, config) { - this.log = log; - this.name = config["name"]; - this.email = config["email"]; - this.password = config["password"]; - this.dsig = config["dsig"]; - this.pinCode = config["pin"]; -} - -XfinityHomeAccessory.prototype = { - - armWithType: function(armed, type) { - this.log("Arming with type " + type + " = " + armed + "..."); - this.targetArmed = armed; - this.targetArmType = type; - this.getLoginToken(); - }, - - getLoginToken: function() { - this.log("Retrieving login token..."); - - var that = this; - - request.post({ - url: "https://login.comcast.net/api/login", - form: { - appkey:"iControl", - dsig: this.dsig, - u: this.email, - p: this.password - } - }, function(err, response, body) { - - if (!err && response.statusCode == 200) { - - var doc = new xmldoc.XmlDocument(body); - that.loginToken = doc.valueWithPath("LoginToken"); - that.refreshLoginCookie(); - } - else { - that.log("Error '"+err+"' getting login token: " + body); - } - }); - }, - - refreshLoginCookie: function() { - this.log("Refreshing login cookie..."); - - var that = this; - - request.post({ - url: "https://www.xfinityhomesecurity.com/rest/icontrol/login", - form: { - token: this.loginToken - } - }, function(err, response, body) { - - if (!err && response.statusCode == 200) { - - // extract our "site" from the login response - var json = JSON.parse(body); - that.siteHref = json["login"]["site"]["href"]; - - // manual cookie handling - that.loginCookie = response.headers["set-cookie"]; - - that.getInstances(); - } - else { - that.log("Error '"+err+"' refreshing login cookie: " + body); - } - }); - }, - - getInstances: function() { - this.log("Getting instances for site " + this.siteHref + "..."); - - this.panelHref = null; - var that = this; - - request.get({ - url: "https://www.xfinityhomesecurity.com/"+that.siteHref+"/network/instances", - headers: { Cookie: this.loginCookie }, - json: true - }, function(err, response, json) { - - if (!err && response.statusCode == 200) { - - // extract our "instance" from the response. look for the first "panel" - var instances = json["instances"]["instance"]; - for (var i=0; i= 200 && response.statusCode < 300) { - that.log("Arm response: " + response); - } - else { - that.log("Error '"+err+"' performing arm request: " + body); - } - }); - }, - - getServices: function() { - var that = this; - return [{ - sType: types.ACCESSORY_INFORMATION_STYPE, - characteristics: [{ - cType: types.NAME_CTYPE, - onUpdate: null, - perms: ["pr"], - format: "string", - initialValue: this.name, - supportEvents: false, - supportBonjour: false, - manfDescription: "Name of the accessory", - designedMaxLength: 255 - },{ - cType: types.MANUFACTURER_CTYPE, - onUpdate: null, - perms: ["pr"], - format: "string", - initialValue: "Comcast", - supportEvents: false, - supportBonjour: false, - manfDescription: "Manufacturer", - designedMaxLength: 255 - },{ - cType: types.MODEL_CTYPE, - onUpdate: null, - perms: ["pr"], - format: "string", - initialValue: "Rev-1", - supportEvents: false, - supportBonjour: false, - manfDescription: "Model", - designedMaxLength: 255 - },{ - cType: types.SERIAL_NUMBER_CTYPE, - onUpdate: null, - perms: ["pr"], - format: "string", - initialValue: "A1S2NASF88EW", - supportEvents: false, - supportBonjour: false, - manfDescription: "SN", - designedMaxLength: 255 - },{ - cType: types.IDENTIFY_CTYPE, - onUpdate: null, - perms: ["pw"], - format: "bool", - initialValue: false, - supportEvents: false, - supportBonjour: false, - manfDescription: "Identify Accessory", - designedMaxLength: 1 - }] - },{ - sType: types.SWITCH_STYPE, - characteristics: [{ - cType: types.NAME_CTYPE, - onUpdate: null, - perms: ["pr"], - format: "string", - initialValue: "Away Mode", - supportEvents: false, - supportBonjour: false, - manfDescription: "Away Mode service", - designedMaxLength: 255 - },{ - cType: types.POWER_STATE_CTYPE, - onUpdate: function(value) { that.armWithType(value, "away"); }, - perms: ["pw","pr","ev"], - format: "bool", - initialValue: false, - supportEvents: false, - supportBonjour: false, - manfDescription: "Turn on the Away alarm", - designedMaxLength: 1 - }] - },{ - sType: types.SWITCH_STYPE, - characteristics: [{ - cType: types.NAME_CTYPE, - onUpdate: null, - perms: ["pr"], - format: "string", - initialValue: "Night Mode", - supportEvents: false, - supportBonjour: false, - manfDescription: "Night Mode service", - designedMaxLength: 255 - },{ - cType: types.POWER_STATE_CTYPE, - onUpdate: function(value) { that.armWithType(value, "night"); }, - perms: ["pw","pr","ev"], - format: "bool", - initialValue: false, - supportEvents: false, - supportBonjour: false, - manfDescription: "Turn on the Night alarm", - designedMaxLength: 1 - }] - },{ - sType: types.SWITCH_STYPE, - characteristics: [{ - cType: types.NAME_CTYPE, - onUpdate: null, - perms: ["pr"], - format: "string", - initialValue: "Stay Mode", - supportEvents: false, - supportBonjour: false, - manfDescription: "Stay Mode service", - designedMaxLength: 255 - },{ - cType: types.POWER_STATE_CTYPE, - onUpdate: function(value) { that.armWithType(value, "stay"); }, - perms: ["pw","pr","ev"], - format: "bool", - initialValue: false, - supportEvents: false, - supportBonjour: false, - manfDescription: "Turn on the Stay alarm", - designedMaxLength: 1 - }] - }]; - } -}; - -// Enable cookie handling and append our expected headers -request = request.defaults({ - headers: { - "X-appkey": "comcastTokenKey", - "X-ClientInfo": "5.2.51", - "X-format": "json" - } -}); - -module.exports.accessory = XfinityHomeAccessory; diff --git a/accessories/iControl.js b/accessories/iControl.js new file mode 100644 index 0000000..d948867 --- /dev/null +++ b/accessories/iControl.js @@ -0,0 +1,130 @@ +var iControl = require('node-icontrol').iControl; +var Service = require('HAP-NodeJS').Service; +var Characteristic = require('HAP-NodeJS').Characteristic; + +module.exports = { + accessory: iControlAccessory +} + +/** + * Provides a Security System accessory for an iControl-based security system like Xfinity Home. + */ + +function iControlAccessory(log, config) { + this.log = log; + + this.iControl = new iControl({ + system: iControl.Systems[config.system], + email: config.email, + password: config.password, + pinCode: config.pin + }); + + this.iControl.on('change', this._handleChange.bind(this)); + this.iControl.on('error', this._handleError.bind(this)); + + this.log("Logging into iControl..."); + this.iControl.login(); + + this._securitySystem = new Service.SecuritySystem("Security System"); + + this._securitySystem + .getCharacteristic(Characteristic.SecuritySystemTargetState) + .on('get', this._getTargetState.bind(this)) + .on('set', this._setTargetState.bind(this)); + + this._securitySystem + .getCharacteristic(Characteristic.SecuritySystemCurrentState) + .on('get', this._getCurrentState.bind(this)); +} + +iControlAccessory.prototype._getTargetState = function(callback) { + this.iControl.getArmState(function(err, armState) { + if (err) return callback(err); + + var currentState = this._getHomeKitStateFromArmState(armState); + callback(null, currentState); + + }.bind(this)); +} + +iControlAccessory.prototype._getCurrentState = function(callback) { + this.iControl.getArmState(function(err, armState) { + if (err) return callback(err); + + var currentState = this._getHomeKitStateFromArmState(armState); + callback(null, currentState); + + }.bind(this)); +} + +iControlAccessory.prototype._setTargetState = function(targetState, callback, context) { + if (context == "internal") return callback(null); // we set this state ourself, no need to react to it + + var armState = this._getArmStateFromHomeKitState(targetState); + this.log("Setting target state to %s", armState); + + this.iControl.setArmState(armState, function(err) { + if (err) return callback(err); + + this.log("Successfully set target state to %s", armState); + + // also update current state + this._securitySystem + .getCharacteristic(Characteristic.SecuritySystemCurrentState) + .setValue(targetState); + + callback(null); // success! + + }.bind(this)); +} + +iControlAccessory.prototype._handleChange = function(armState) { + this.log("Arm state changed to %s", armState); + + var homeKitState = this._getHomeKitStateFromArmState(armState); + + this._securitySystem + .getCharacteristic(Characteristic.SecuritySystemCurrentState) + .setValue(homeKitState); + + this._securitySystem + .getCharacteristic(Characteristic.SecuritySystemTargetState) + .setValue(homeKitState, null, "internal"); // these characteristics happen to share underlying values +} + +iControlAccessory.prototype._handleError = function(err) { + this.log(err.message); +} + +iControlAccessory.prototype.getServices = function() { + return [this._securitySystem]; +} + +iControlAccessory.prototype._getHomeKitStateFromArmState = function(armState) { + switch (armState) { + case "disarmed": return Characteristic.SecuritySystemCurrentState.DISARMED; + case "away": return Characteristic.SecuritySystemCurrentState.AWAY_ARM; + case "night": return Characteristic.SecuritySystemCurrentState.NIGHT_ARM; + case "stay": return Characteristic.SecuritySystemCurrentState.STAY_ARM; + } +} + +iControlAccessory.prototype._getArmStateFromHomeKitState = function(homeKitState) { + switch (homeKitState) { + case Characteristic.SecuritySystemCurrentState.DISARMED: return "disarmed"; + case Characteristic.SecuritySystemCurrentState.AWAY_ARM: return "away"; + case Characteristic.SecuritySystemCurrentState.NIGHT_ARM: return "night"; + case Characteristic.SecuritySystemCurrentState.STAY_ARM: return "stay"; + } +} + + +/** + * TESTING + */ + +if (require.main === module) { + var config = JSON.parse(require('fs').readFileSync("config.json")).accessories[0]; + var accessory = new iControlAccessory(console.log, config); +} diff --git a/config-sample.json b/config-sample.json index c9ba829..4245bd5 100644 --- a/config-sample.json +++ b/config-sample.json @@ -104,15 +104,15 @@ "password" : "your-carwings-password" }, { - "accessory": "XfinityHome", + "accessory": "iControl", "name": "Xfinity Home", - "description": "This shim supports the 'Xfinity Home' security system. Unfortunately I don't know how to generate the 'dsig' property, so you'll need to figure yours out by running the Xfinity Home app on your iOS device while connected to a proxy server like Charles. If you didn't understand any of that, sorry! I welcome any suggestions for how to figure out dsig automatically.", - "email": "your-comcast-email@example.com", + "description": "This shim supports iControl-based security systems like Xfinity Home.", + "system": "XFINITY_HOME", + "email": "your-comcast-email", "password": "your-comcast-password", - "dsig": "your-digital-signature", "pin": "your-security-system-pin-code" }, - { + { "accessory": "HomeMatic", "name": "Light", "description": "Control HomeMatic devices (The XMP-API addon for the CCU is required)", diff --git a/package.json b/package.json index ab5b117..7eb761f 100644 --- a/package.json +++ b/package.json @@ -20,17 +20,18 @@ "harmonyhubjs-discover": "git+https://github.com/swissmanu/harmonyhubjs-discover.git", "mdns": "^2.2.4", "node-hue-api": "^1.0.5", + "node-icontrol": "^0.1.0", "node-milight-promise": "0.0.x", "node-persist": "0.0.x", "request": "2.49.x", "sonos": "0.8.x", "telldus-live": "0.2.x", + "teslams": "1.0.1", "unofficial-nest-api": "git+https://github.com/hachidorii/unofficial_nodejs_nest.git#d8d48edc952b049ff6320ef99afa7b2f04cdee98", "wemo": "0.2.x", "wink-js": "0.0.5", "xml2js": "0.4.x", "xmldoc": "0.1.x", - "yamaha-nodejs": "0.4.x", - "teslams": "1.0.1" + "yamaha-nodejs": "0.4.x" } } From 86e17a392256ef68122e20b60e6784b5a9efcdba Mon Sep 17 00:00:00 2001 From: Nick Farina Date: Wed, 2 Sep 2015 07:20:36 -0700 Subject: [PATCH 02/18] Bump node-icontrol with fixes --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 7eb761f..da0b266 100644 --- a/package.json +++ b/package.json @@ -20,7 +20,7 @@ "harmonyhubjs-discover": "git+https://github.com/swissmanu/harmonyhubjs-discover.git", "mdns": "^2.2.4", "node-hue-api": "^1.0.5", - "node-icontrol": "^0.1.0", + "node-icontrol": "^0.1.2", "node-milight-promise": "0.0.x", "node-persist": "0.0.x", "request": "2.49.x", From a3cbf5a380a7ce00404f7e3265fa9e046c5adcb8 Mon Sep 17 00:00:00 2001 From: Nick Farina Date: Wed, 2 Sep 2015 18:51:23 -0700 Subject: [PATCH 03/18] Another node-icontrol bump --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index da0b266..e7fd564 100644 --- a/package.json +++ b/package.json @@ -20,7 +20,7 @@ "harmonyhubjs-discover": "git+https://github.com/swissmanu/harmonyhubjs-discover.git", "mdns": "^2.2.4", "node-hue-api": "^1.0.5", - "node-icontrol": "^0.1.2", + "node-icontrol": "^0.1.3", "node-milight-promise": "0.0.x", "node-persist": "0.0.x", "request": "2.49.x", From bad0ba0c3b87b4fb0570516b182aad051a566f9c Mon Sep 17 00:00:00 2001 From: Snowdd1 Date: Fri, 4 Sep 2015 15:45:19 +0200 Subject: [PATCH 04/18] initial test version --- accessories/knxlamp.js | 180 +++++++++++++++++++++++++++++++++++ platforms/KNX.js | 206 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 386 insertions(+) create mode 100644 accessories/knxlamp.js create mode 100644 platforms/KNX.js diff --git a/accessories/knxlamp.js b/accessories/knxlamp.js new file mode 100644 index 0000000..337904c --- /dev/null +++ b/accessories/knxlamp.js @@ -0,0 +1,180 @@ +/* + * This is a demo KNX lamp accessory shim. + * It can switch a light on and off, and optionally set a brightness if configured to do so + * + */ +var Service = require("HAP-NodeJS").Service; +var Characteristic = require("HAP-NodeJS").Characteristic; +var knxd = require("eibd"); + + + +function KNXlampAccessory(log, config) { + this.log = log; + + + // knx information on object + this.group_address = config.group_address; + this.listen_addresses = config.listen_addresses; // supposed to be undefined, an array of strings, or single string + this.can_dim = config.can_dim; //supposed to be true or false + this.brightness_group_address = config.brightness_group_address; + this.brightness_listen_addresses = config.brightness_listen_addresses; + this.knxd_ip = config.knxd_ip ; // eg 127.0.0.1 if running on localhost + this.knxd_port = config.knxd_port || 6720; // eg 6720 default knxd port + +} + + +module.exports = { + accessory: KNXlampAccessory + }; + + +KNXlampAccessory.prototype = { + + + knxwrite: function(callback, groupAddress, dpt, value) { + // this.log("DEBUG in knxwrite"); + var knxdConnection = new knxd.Connection(); + // this.log("DEBUG in knxwrite: created empty connection, trying to connect socket to "+this.knxd_ip+":"+this.knxd_port); + knxdConnection.socketRemote({ host: this.knxd_ip, port: this.knxd_port }, function() { + var dest = knxd.str2addr(groupAddress); + // this.log("DEBUG got dest="+dest); + knxdConnection.openTGroup(dest, 1, function(err) { + if (err) { + this.log("[ERROR] knxwrite:openTGroup: " + err); + callback(err); + } else { + // this.log("DEBUG opened TGroup "); + var msg = knxd.createMessage('write', dpt, parseFloat(value)); + knxdConnection.sendAPDU(msg, function(err) { + if (err) { + this.log("[ERROR] knxwrite:sendAPDU: " + err); + callback(err); + } else { + // this.log("knx data sent"); + callback(); + } + }.bind(this)); + } + }.bind(this)); + }.bind(this)); + }, + + // issues a read request on the knx bus + // DOES NOT WAIT for an answer. Please register the address with a callback using registerGA() function + knxread: function(groupAddress){ + // this.log("DEBUG in knxread"); + var knxdConnection = new knxd.Connection(); + // this.log("DEBUG in knxread: created empty connection, trying to connect socket to "+this.knxd_ip+":"+this.knxd_port); + knxdConnection.socketRemote({ host: this.knxd_ip, port: this.knxd_port }, function() { + var dest = knxd.str2addr(groupAddress); + // this.log("DEBUG got dest="+dest); + knxdConnection.openTGroup(dest, 1, function(err) { + if (err) { + this.log("[ERROR] knxread:openTGroup: " + err); + } else { + // this.log("DEBUG knxread: opened TGroup "); + var msg = knxd.createMessage('read', 'DPT1', 0); + knxdConnection.sendAPDU(msg, function(err) { + if (err) { + this.log("[ERROR] knxread:sendAPDU: " + err); + } else { + this.log("knx request sent"); + } + }.bind(this)); + } + }.bind(this)); + }.bind(this)); + }, + + knxregister: function(addresses, characteristic) { + console.log("knx registering " + addresses); + knxd.registerGA(addresses, function(value){ + // parameters do not match + this.log("Getting value from bus:"+value); + characteristic.setValue(value, undefined, 'fromKNXBus'); + }.bind(this)); + }, + + setPowerState: function(value, callback, context) { + if (context === 'fromKNXBus') { + this.log("event ping pong, exit!"); + if (callback) { + callback(); + } + } else { + console.log("Setting power to %s", value); + var numericValue = 0; + if (value) { + numericValue = 1; // need 0 or 1, not true or something + } + this.knxwrite(callback, this.group_address,'DPT1',numericValue); + } + + }, + + + setBrightness: function(value, callback, context) { + if (context === 'fromKNXBus') { + this.log("event ping pong, exit!"); + if (callback) { + callback(); + } + } else { + this.log("Setting brightness to %s", value); + var numericValue = 0; + if (value) { + numericValue = 255*value/100; // convert 1..100 to 1..255 for KNX bus + } + this.knxwrite(callback, this.brightness_group_address,'DPT5',numericValue); + } + }, + + + + identify: function(callback) { + this.log("Identify requested!"); + callback(); // success + }, + + getServices: function() { + + // you can OPTIONALLY create an information service if you wish to override + // the default values for things like serial number, model, etc. + var informationService = new Service.AccessoryInformation(); + + informationService + .setCharacteristic(Characteristic.Manufacturer, "Opensource Community") + .setCharacteristic(Characteristic.Model, "KNX Light Switch with or without dimmer") + .setCharacteristic(Characteristic.SerialNumber, "Version 1"); + + var lightbulbService = new Service.Lightbulb(); + + var onCharacteristic = lightbulbService + .getCharacteristic(Characteristic.On) + .on('set', this.setPowerState.bind(this)); + onCharacteristic.supportsEventNotification=true; + // register with value update service + this.addresses = [this.group_address]; + this.log("DEBUG1 this.addresses = "+this.addresses); + this.log("DEBUG2 this.listen_addresses = "+this.listen_addresses); + this.addresses = this.addresses.concat(this.listen_addresses || []); // do not join anything if empty (do not add undefined) + this.log("DEBUG3 this.addresses = "+this.addresses); + this.knxregister(this.addresses, onCharacteristic); + this.knxread(this.group_address); // issue a read request on the bus, maybe the device answers to that! + + if (this.can_dim) { + var brightnessCharacteristic = lightbulbService + .addCharacteristic(new Characteristic.Brightness()) + .on('set', this.setBrightness.bind(this)); + // register with value update service + this.brightness_addresses = [this.brightness_group_address]; + this.brightness_addresses.concat(this.brightness_listen_addresses || []); // do not join anything if empty (do not add undefined) + this.knxregister(this.brightness_addresses, brightnessCharacteristic); + this.knxread(this.brightness_group_address); // issue a read request on the bus, maybe the device answers to that! + } + knxd.startMonitor({ host: this.knxd_ip, port: this.knxd_port }); + return [informationService, lightbulbService]; + } +}; diff --git a/platforms/KNX.js b/platforms/KNX.js new file mode 100644 index 0000000..0b4f765 --- /dev/null +++ b/platforms/KNX.js @@ -0,0 +1,206 @@ +/** Sample platform outline + * based on Sonos platform + */ +'use strict'; +var types = require("HAP-NodeJS/accessories/types.js"); +//var hardware = require('myHardwareSupport'); //require any additional hardware packages +var Connection = require('eibd').connection; + +function KNXPlatform(log, config){ + this.log = log; + this.config = config; +// this.property1 = config.property1; +// this.property2 = config.property2; + + + // initiate connection to bus for listening ==> done with first shim + +}; + +MyPlatform.prototype = { + accessories: function(callback) { + this.log("Fetching myPlatform devices."); + var that = this; + + + // iterate through all devices the platform my offer + // for each device, create an accessory + + // read accessories from file !!!!! + var foundAccessories = this.config.accessories; + + + //create array of accessories + var myAccessories = []; + + for (var int = 0; int < foundAccessories.length; int++) { + + // instantiate and push to array + if (foundAccessories[i].accessory-type === "knxlamp") { + + myAccessories.push(new require('../accessories/knxlamp.js').accessory(this.log,foundAccessories[i])); + } else { + // do something else + this.log("unkown accessory type found") + } //etc. + + }; + // if done, return the array to callback function + callback(myAccessories); + } +}; + + +// the signature of the constructor has to be adopted to the accessory you need in your platform! These are the first lines from the sonos platform +function myAccessoryType1(log, config, device, description /* add or remove parms as you need*/ ) { + + this.log = log; + this.config = config; + this.device = device; + this.description = description; + // more initialization if required + +} + +myAccessoryType1.prototype = { + // see https shim wiki page for details. Accessory definition is discussed there. +} + +// more + + +/** + * The buscallbacks module is to expose a simple function to listen on the bus and register callbacks for value changes + * of registered addresses. + * + * Usage: +* You can start the monitoring process at any time + startMonitor({host: name-ip, port: port-num }); + +* You can add addresses to the subscriptions using + +registerGA(groupAddress, callback) + +* groupAddress has to be an groupAddress in common knx notation string '1/2/3' +* the callback has to be a +* var f = function(value) { handle value update;} +* so you can do a +* registerGA('1/2/3', function(value){ +* console.log('1/2/3 got a hit with '+value); +* }); +* but of course it is meant to be used programmatically, not literally, otherwise it has no advantage +* +* You can also use arrays of addresses if your callback is supposed to listen to many addresses: + +registerGA(groupAddresses[], callback) + +* as in +* registerGA(['1/2/3','1/0/0'], function(value){ +* console.log('1/2/3 or 1/0/0 got a hit with '+value); +* }); +* if you are having central addresses like "all lights off" or additional response objects +* +* +* callbacks can have a signature of +* function(value, src, dest, type) but do not have to support these parameters (order matters) +* src = physical address such as '1.1.20' +* dest = groupAddress hit (you subscribed to that address, remember?), as '1/2/3' +* type = Data point type, as 'DPT1' +* +* +*/ + + + +// array of registered addresses and their callbacks +var subscriptions = []; +// check variable to avoid running two listeners +var running; + +function groupsocketlisten(opts, callback) { + var conn = Connection(); + conn.socketRemote(opts, function() { + conn.openGroupSocket(0, callback); + }); +} + + +var registerSingleGA = function registerSingleGA (groupAddress, callback) { + subscriptions.push({address: groupAddress, callback: callback }); +} + +/* + * public busMonitor.startMonitor() + * starts listening for telegrams on KNX bus + * + */ +var startMonitor = function startMonitor(opts) { // using { host: name-ip, port: port-num } options object + if (!running) { + running = true; + } else { + return null; + } + + groupsocketlisten(opts, function(parser) { + //console.log("knxfunctions.read: in callback parser"); + parser.on('write', function(src, dest, type, val){ + // search the registered group addresses + for (var i = 0; i < subscriptions.length; i++) { + // iterate through all registered addresses + if (subscriptions[i].address === dest) { + // found one, notify + //console.log('HIT: Write from '+src+' to '+dest+': '+val+' ['+type+']'); + subscriptions[i].callback(val, src, dest, type); + } + } + }); + + parser.on('response', function(src, dest, type, val) { + // search the registered group addresses + for (var i = 0; i < subscriptions.length; i++) { + // iterate through all registered addresses + if (subscriptions[i].address === dest) { + // found one, notify + //console.log('HIT: Response from '+src+' to '+dest+': '+val+' ['+type+']'); + subscriptions[i].callback(val, src, dest, type); + } + } + + }); + + //dont care about reads here +// parser.on('read', function(src, dest) { +// console.log('Read from '+src+' to '+dest); +// }); + //console.log("knxfunctions.read: in callback parser at end"); + }); // groupsocketlisten parser +}; //startMonitor + + +/* + * public registerGA(groupAdresses[], callback(value)) + * parameters + * callback: function(value, src, dest, type) called when a value is sent on the bus + * groupAddresses: (Array of) string(s) for group addresses + * + * + * + */ +var registerGA = function (groupAddresses, callback) { + // check if the groupAddresses is an array + if (groupAddresses.constructor.toString().indexOf("Array") > -1) { + // handle multiple addresses + for (var i = 0; i < groupAddresses.length; i++) { + registerSingleGA (groupAddresses[i], callback); + } + } else { + // it's only one + registerSingleGA (groupAddresses, callback); + } +}; + + + +module.exports.platform = myPlatform; +module.exports.registerGA = registerGA; +module.exports.startMonitor = startMonitor; \ No newline at end of file From 1a98a6c9ac2feb35d8b9656ab3478df101ad08cd Mon Sep 17 00:00:00 2001 From: Snowdd1 Date: Fri, 4 Sep 2015 16:35:11 +0200 Subject: [PATCH 05/18] got it straight does not use PR#15 for node-eibd any more. --- accessories/knxlamp.js | 10 ++++++++-- platforms/KNX.js | 24 +++++++++++++++--------- 2 files changed, 23 insertions(+), 11 deletions(-) diff --git a/accessories/knxlamp.js b/accessories/knxlamp.js index 337904c..738164c 100644 --- a/accessories/knxlamp.js +++ b/accessories/knxlamp.js @@ -6,6 +6,8 @@ var Service = require("HAP-NodeJS").Service; var Characteristic = require("HAP-NodeJS").Characteristic; var knxd = require("eibd"); +var knxd_registerGA = require('../platforms/KNX.js').registerGA; +var knxd_startMonitor = require('../platforms/KNX.js').startMonitor; @@ -21,6 +23,10 @@ function KNXlampAccessory(log, config) { this.brightness_listen_addresses = config.brightness_listen_addresses; this.knxd_ip = config.knxd_ip ; // eg 127.0.0.1 if running on localhost this.knxd_port = config.knxd_port || 6720; // eg 6720 default knxd port + if (config.name) { + this.name = config.name; + } + log("Accessory constructor called"); } @@ -90,7 +96,7 @@ KNXlampAccessory.prototype = { knxregister: function(addresses, characteristic) { console.log("knx registering " + addresses); - knxd.registerGA(addresses, function(value){ + knxd_registerGA(addresses, function(value){ // parameters do not match this.log("Getting value from bus:"+value); characteristic.setValue(value, undefined, 'fromKNXBus'); @@ -174,7 +180,7 @@ KNXlampAccessory.prototype = { this.knxregister(this.brightness_addresses, brightnessCharacteristic); this.knxread(this.brightness_group_address); // issue a read request on the bus, maybe the device answers to that! } - knxd.startMonitor({ host: this.knxd_ip, port: this.knxd_port }); + knxd_startMonitor({ host: this.knxd_ip, port: this.knxd_port }); return [informationService, lightbulbService]; } }; diff --git a/platforms/KNX.js b/platforms/KNX.js index 0b4f765..0b5c659 100644 --- a/platforms/KNX.js +++ b/platforms/KNX.js @@ -4,7 +4,7 @@ 'use strict'; var types = require("HAP-NodeJS/accessories/types.js"); //var hardware = require('myHardwareSupport'); //require any additional hardware packages -var Connection = require('eibd').connection; +var knxd = require('eibd'); function KNXPlatform(log, config){ this.log = log; @@ -17,9 +17,9 @@ function KNXPlatform(log, config){ }; -MyPlatform.prototype = { +KNXPlatform.prototype = { accessories: function(callback) { - this.log("Fetching myPlatform devices."); + this.log("Fetching KNX devices."); var that = this; @@ -34,11 +34,16 @@ MyPlatform.prototype = { var myAccessories = []; for (var int = 0; int < foundAccessories.length; int++) { - + this.log("parsing acc " + int + " of " + foundAccessories.length); // instantiate and push to array - if (foundAccessories[i].accessory-type === "knxlamp") { - - myAccessories.push(new require('../accessories/knxlamp.js').accessory(this.log,foundAccessories[i])); + if (foundAccessories[int].accessory_type === "knxlamp") { + this.log("push new lamp with "+foundAccessories[int].name); + foundAccessories[int].knxd_ip = this.config.knxd_ip; + foundAccessories[int].knxd_port = this.config.knxd_port; + var accConstructor = require('./../accessories/knxlamp.js'); + var acc = new accConstructor.accessory(this.log,foundAccessories[int]); + this.log("created "+acc.name+" accessory"); + myAccessories.push(acc); } else { // do something else this.log("unkown accessory type found") @@ -46,6 +51,7 @@ MyPlatform.prototype = { }; // if done, return the array to callback function + this.log("returning "+myAccessories.length+" accessories"); callback(myAccessories); } }; @@ -118,7 +124,7 @@ var subscriptions = []; var running; function groupsocketlisten(opts, callback) { - var conn = Connection(); + var conn = knxd.Connection(); conn.socketRemote(opts, function() { conn.openGroupSocket(0, callback); }); @@ -201,6 +207,6 @@ var registerGA = function (groupAddresses, callback) { -module.exports.platform = myPlatform; +module.exports.platform = KNXPlatform; module.exports.registerGA = registerGA; module.exports.startMonitor = startMonitor; \ No newline at end of file From 116dd1b3159d06ba6a006f0734cfb1c0458bea48 Mon Sep 17 00:00:00 2001 From: Snowdd1 Date: Fri, 4 Sep 2015 17:39:46 +0200 Subject: [PATCH 06/18] still WIP with Thermostat stub. Somehow stops working after a few lines of console.log --- accessories/knxlamp.js | 4 +- accessories/knxthermostat.js | 178 +++++++++++++++++++++++++++++++++++ platforms/KNX.js | 35 ++++--- 3 files changed, 203 insertions(+), 14 deletions(-) create mode 100644 accessories/knxthermostat.js diff --git a/accessories/knxlamp.js b/accessories/knxlamp.js index 738164c..7f06529 100644 --- a/accessories/knxlamp.js +++ b/accessories/knxlamp.js @@ -1,6 +1,6 @@ /* - * This is a demo KNX lamp accessory shim. - * It can switch a light on and off, and optionally set a brightness if configured to do so + * This is a demo KNX thermostat accessory shim. + * It can * */ var Service = require("HAP-NodeJS").Service; diff --git a/accessories/knxthermostat.js b/accessories/knxthermostat.js new file mode 100644 index 0000000..e0f408a --- /dev/null +++ b/accessories/knxthermostat.js @@ -0,0 +1,178 @@ +/* + * This is a demo KNX lamp accessory shim. + * It can switch a light on and off, and optionally set a brightness if configured to do so + * + */ +var Service = require("HAP-NodeJS").Service; +var Characteristic = require("HAP-NodeJS").Characteristic; +var knxd = require("eibd"); +var knxd_registerGA = require('../platforms/KNX.js').registerGA; +var knxd_startMonitor = require('../platforms/KNX.js').startMonitor; + + + +function KNXthermoAccessory(log, config) { + this.log = log; + this.config=config; + + // knx information on object + this.curr_temp_address = config.curr_temp_address; + this.curr_temp_listen_addresses = config.curr_temp_listen_addresses; // supposed to be undefined, an array of strings, or single string + this.target_temp_address = config.target_temp_address; + this.knxd_ip = config.knxd_ip ; // eg 127.0.0.1 if running on localhost + this.knxd_port = config.knxd_port || 6720; // eg 6720 default knxd port + if (config.name) { + this.name = config.name; + } + log("Accessory constructor called"); + +} + + +module.exports = { + accessory: KNXthermoAccessory + }; + + +KNXthermoAccessory.prototype = { + + + knxwrite: function(callback, groupAddress, dpt, value) { + // this.log("DEBUG in knxwrite"); + var knxdConnection = new knxd.Connection(); + // this.log("DEBUG in knxwrite: created empty connection, trying to connect socket to "+this.knxd_ip+":"+this.knxd_port); + knxdConnection.socketRemote({ host: this.knxd_ip, port: this.knxd_port }, function() { + var dest = knxd.str2addr(groupAddress); + // this.log("DEBUG got dest="+dest); + knxdConnection.openTGroup(dest, 1, function(err) { + if (err) { + this.log("[ERROR] knxwrite:openTGroup: " + err); + callback(err); + } else { + // this.log("DEBUG opened TGroup "); + var msg = knxd.createMessage('write', dpt, parseFloat(value)); + knxdConnection.sendAPDU(msg, function(err) { + if (err) { + this.log("[ERROR] knxwrite:sendAPDU: " + err); + callback(err); + } else { + this.log("knx data sent"); + callback(); + } + }.bind(this)); + } + }.bind(this)); + }.bind(this)); + }, + + // issues a read request on the knx bus + // DOES NOT WAIT for an answer. Please register the address with a callback using registerGA() function + knxread: function(groupAddress){ + // this.log("DEBUG in knxread"); + var knxdConnection = new knxd.Connection(); + // this.log("DEBUG in knxread: created empty connection, trying to connect socket to "+this.knxd_ip+":"+this.knxd_port); + knxdConnection.socketRemote({ host: this.knxd_ip, port: this.knxd_port }, function() { + var dest = knxd.str2addr(groupAddress); + // this.log("DEBUG got dest="+dest); + knxdConnection.openTGroup(dest, 1, function(err) { + if (err) { + this.log("[ERROR] knxread:openTGroup: " + err); + } else { + // this.log("DEBUG knxread: opened TGroup "); + var msg = knxd.createMessage('read', 'DPT1', 0); + knxdConnection.sendAPDU(msg, function(err) { + if (err) { + this.log("[ERROR] knxread:sendAPDU: " + err); + } else { + this.log("knx request sent"); + } + }.bind(this)); + } + }.bind(this)); + }.bind(this)); + }, + + knxregister: function(addresses, characteristic) { + console.log("knx registering " + addresses); + knxd_registerGA(addresses, function(value){ + // parameters do not match + this.log("Getting value from bus:"+value); + characteristic.setValue(value, undefined, 'fromKNXBus'); + }.bind(this)); + }, + + + + identify: function(callback) { + this.log("Identify requested!"); + callback(); // success + }, + + getServices: function() { + + // you can OPTIONALLY create an information service if you wish to override + // the default values for things like serial number, model, etc. + var informationService = new Service.AccessoryInformation(); + + informationService + .setCharacteristic(Characteristic.Manufacturer, "Opensource Community") + .setCharacteristic(Characteristic.Model, "KNX Thermostat") + .setCharacteristic(Characteristic.SerialNumber, "Version 1"); + + var myService = new Service.Thermostat(); + +// +// // Required Characteristics +// this.addCharacteristic(Characteristic.CurrentHeatingCoolingState); +// this.addCharacteristic(Characteristic.TargetHeatingCoolingState); +// this.addCharacteristic(Characteristic.CurrentTemperature); //check +// this.addCharacteristic(Characteristic.TargetTemperature); // +// this.addCharacteristic(Characteristic.TemperatureDisplayUnits); +// +// // Optional Characteristics +// this.addOptionalCharacteristic(Characteristic.CurrentRelativeHumidity); +// this.addOptionalCharacteristic(Characteristic.TargetRelativeHumidity); +// this.addOptionalCharacteristic(Characteristic.CoolingThresholdTemperature); +// this.addOptionalCharacteristic(Characteristic.HeatingThresholdTemperature); +// this.addOptionalCharacteristic(Characteristic.Name); + + + var CurrentTemperatureCharacteristic = myService + .getCharacteristic(Characteristic.CurrentTemperature) + // .on('set', this.setPowerState.bind(this)); + CurrentTemperatureCharacteristic.supportsEventNotification=true; + // register with value update service + this.addresses1 = [this.curr_temp_address]; + this.addresses1 = this.addresses1.concat(this.curr_temp_listen_addresses || []); // do not join anything if empty (do not add undefined) + this.knxregister(this.addresses1, CurrentTemperatureCharacteristic); + this.knxread(this.curr_temp_address); // issue a read request on the bus, maybe the device answers to that! + + var TargetTemperatureCharacteristic = myService + .getCharacteristic(Characteristic.TargetTemperature) + .on('set', function(value, callback, context) { + if (context === 'fromKNXBus') { + this.log("event ping pong, exit!"); + if (callback) { + callback(); + } + } else { + console.log("Setting temperature to %s", value); + var numericValue = 0.0; + if (value) { + numericValue = 0+value; // need to be numeric + } + this.knxwrite(callback, this.target_temp_address,'DPT9',numericValue); + } + }.bind(this)); + TargetTemperatureCharacteristic.supportsEventNotification=true; + // register with value update service + this.addresses2 = [this.target_temp_address]; + this.addresses2 = this.addresses2.concat(this.target_temp_listen_addresses || []); // do not join anything if empty (do not add undefined) + this.knxregister(this.addresses2, TargetTemperatureCharacteristic); + this.knxread(this.target_temp_address); // issue a read request on the bus, maybe the device answers to that! + + + knxd_startMonitor({ host: this.knxd_ip, port: this.knxd_port }); + return [informationService, myService]; + } +}; diff --git a/platforms/KNX.js b/platforms/KNX.js index 0b5c659..841c03d 100644 --- a/platforms/KNX.js +++ b/platforms/KNX.js @@ -36,18 +36,29 @@ KNXPlatform.prototype = { for (var int = 0; int < foundAccessories.length; int++) { this.log("parsing acc " + int + " of " + foundAccessories.length); // instantiate and push to array - if (foundAccessories[int].accessory_type === "knxlamp") { - this.log("push new lamp with "+foundAccessories[int].name); - foundAccessories[int].knxd_ip = this.config.knxd_ip; - foundAccessories[int].knxd_port = this.config.knxd_port; - var accConstructor = require('./../accessories/knxlamp.js'); - var acc = new accConstructor.accessory(this.log,foundAccessories[int]); - this.log("created "+acc.name+" accessory"); - myAccessories.push(acc); - } else { - // do something else - this.log("unkown accessory type found") - } //etc. + switch (foundAccessories[int].accessory_type) { + case "knxlamp": + this.log("push new lamp with "+foundAccessories[int].name); + foundAccessories[int].knxd_ip = this.config.knxd_ip; + foundAccessories[int].knxd_port = this.config.knxd_port; + var accConstructor = require('./../accessories/knxlamp.js'); + var acc = new accConstructor.accessory(this.log,foundAccessories[int]); + this.log("created "+acc.name+" accessory"); + myAccessories.push(acc); + break; + case "knxthermostat": + this.log("push new thermostat with "+foundAccessories[int].name); + foundAccessories[int].knxd_ip = this.config.knxd_ip; + foundAccessories[int].knxd_port = this.config.knxd_port; + var accConstructor = require('./../accessories/knxthermostat.js'); + var acc = new accConstructor.accessory(this.log,foundAccessories[int]); + this.log("created "+acc.name+" accessory"); + myAccessories.push(acc); + break; + default: + // do something else + this.log("unkown accessory type found") + } }; // if done, return the array to callback function From fe4cd285d096f6d8d24d5e1bc487df90461f87c6 Mon Sep 17 00:00:00 2001 From: Nick Farina Date: Fri, 4 Sep 2015 10:05:37 -0700 Subject: [PATCH 07/18] Use once() to guard multiple callbacks Mentioned in #95 --- app.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/app.js b/app.js index 3f0c275..8a92123 100644 --- a/app.js +++ b/app.js @@ -8,6 +8,7 @@ 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..."); @@ -118,7 +119,7 @@ function loadPlatforms() { function loadPlatformAccessories(platformInstance, log) { asyncCalls++; - platformInstance.accessories(function(foundAccessories){ + platformInstance.accessories(once(function(foundAccessories){ asyncCalls--; // loop through accessories adding them to the list and registering them @@ -137,7 +138,7 @@ function loadPlatformAccessories(platformInstance, log) { // were we the last callback? if (asyncCalls === 0 && !asyncWait) publish(); - }); + })); } function createAccessory(accessoryInstance, displayName) { From ea1c1f6fcec6c496ea16ca8fd02ad28989433950 Mon Sep 17 00:00:00 2001 From: Snowdd1 Date: Sat, 5 Sep 2015 19:08:28 +0200 Subject: [PATCH 08/18] Working Version Refactored version. KNX.js is used as platform, while knxdevice is called for each accessory to add. --- accessories/knxdevice.js | 562 +++++++++++++++++++++++++++++++++++ accessories/knxlamp.js | 186 ------------ accessories/knxthermostat.js | 178 ----------- platforms/KNX.js | 276 +++++++++-------- 4 files changed, 699 insertions(+), 503 deletions(-) create mode 100644 accessories/knxdevice.js delete mode 100644 accessories/knxlamp.js delete mode 100644 accessories/knxthermostat.js diff --git a/accessories/knxdevice.js b/accessories/knxdevice.js new file mode 100644 index 0000000..c07e2ac --- /dev/null +++ b/accessories/knxdevice.js @@ -0,0 +1,562 @@ +/* + * This is a KNX universal accessory shim. + * + * + */ +var Service = require("HAP-NodeJS").Service; +var Characteristic = require("HAP-NodeJS").Characteristic; +var knxd = require("eibd"); +var knxd_registerGA = require('../platforms/KNX.js').registerGA; +var knxd_startMonitor = require('../platforms/KNX.js').startMonitor; + + + +function KNXDevice(log, config) { + this.log = log; + // everything in one object, do not copy individually + this.config = config; + log("Accessory constructor called"); + if (config.name) { + this.name = config.name; + } + if (config.knxd_ip){ + this.knxd_ip = config.knxd_ip; + } else { + throw new Error("MISSING KNXD IP"); + } + if (config.knxd_port){ + this.knxd_port = config.knxd_port; + } else { + throw new Error("MISSING KNXD PORT"); + } + +} + + +//debugging helper only +//inspects an object and prints its properties (also inherited properties) +var iterate = function nextIteration(myObject, path){ + // this function iterates over all properties of an object and print them to the console + // when finding objects it goes one level deeper + var name; + if (!path){ + console.log("---iterating--------------------") + } + for (name in myObject) { + if (typeof myObject[name] !== 'function') { + if (typeof myObject[name] !== 'object' ) { + console.log((path || "") + name + ': ' + myObject[name]); + } else { + nextIteration(myObject[name], path ? path + name + "." : name + "."); + } + } else { + console.log((path || "") + name + ': (function)' ); + } + } + if (!path) { + console.log("================================"); + } +}; + + +module.exports = { + accessory: KNXDevice +}; + + +KNXDevice.prototype = { + + // all purpose / all types write function + knxwrite: function(callback, groupAddress, dpt, value) { + // this.log("DEBUG in knxwrite"); + var knxdConnection = new knxd.Connection(); + // this.log("DEBUG in knxwrite: created empty connection, trying to connect socket to "+this.knxd_ip+":"+this.knxd_port); + knxdConnection.socketRemote({ host: this.knxd_ip, port: this.knxd_port }, function() { + var dest = knxd.str2addr(groupAddress); + // this.log("DEBUG got dest="+dest); + knxdConnection.openTGroup(dest, 1, function(err) { + if (err) { + this.log("[ERROR] knxwrite:openTGroup: " + err); + callback(err); + } else { + // this.log("DEBUG opened TGroup "); + var msg = knxd.createMessage('write', dpt, parseFloat(value)); + knxdConnection.sendAPDU(msg, function(err) { + if (err) { + this.log("[ERROR] knxwrite:sendAPDU: " + err); + callback(err); + } else { + // this.log("knx data sent"); + callback(); + } + }.bind(this)); + } + }.bind(this)); + }.bind(this)); + }, + + // issues an all purpose read request on the knx bus + // DOES NOT WAIT for an answer. Please register the address with a callback using registerGA() function + knxread: function(groupAddress){ + // this.log("DEBUG in knxread"); + if (!groupAddress) { + return null; + } + var knxdConnection = new knxd.Connection(); + // this.log("DEBUG in knxread: created empty connection, trying to connect socket to "+this.knxd_ip+":"+this.knxd_port); + knxdConnection.socketRemote({ host: this.knxd_ip, port: this.knxd_port }, function() { + var dest = knxd.str2addr(groupAddress); + // this.log("DEBUG got dest="+dest); + knxdConnection.openTGroup(dest, 1, function(err) { + if (err) { + this.log("[ERROR] knxread:openTGroup: " + err); + } else { + // this.log("DEBUG knxread: opened TGroup "); + var msg = knxd.createMessage('read', 'DPT1', 0); + knxdConnection.sendAPDU(msg, function(err) { + if (err) { + this.log("[ERROR] knxread:sendAPDU: " + err); + } else { + this.log("knx request sent for "+groupAddress); + } + }.bind(this)); + } + }.bind(this)); + }.bind(this)); + }, + + // issuing multiple read requests at once + knxreadarray: function (groupAddresses) { + if (groupAddresses.constructor.toString().indexOf("Array") > -1) { + // handle multiple addresses + for (var i = 0; i < groupAddresses.length; i++) { + if (groupAddresses[i]) { // do not bind empty addresses + this.knxread (groupAddresses[i]); + } + } + } else { + // it's only one + this.knxread (groupAddresses); + } + }, + + // special types + knxwrite_percent: function(callback, groupAddress, value) { + var numericValue = 0; + if (value && value>=0 && value <= 100) { + numericValue = 255*value/100; // convert 1..100 to 1..255 for KNX bus + } else { + this.log("[ERROR] Percentage value ot of bounds "); + numericValue = 0; + } + this.knxwrite(callback, groupAddress,'DPT5',numericValue); + }, + + + // need to spit registers into types + + // boolean: get 0 or 1 from the bus, write boolean + knxregister_bool: function(addresses, characteristic) { + this.log("knx registering BOOLEAN " + addresses); + knxd_registerGA(addresses, function(val, src, dest, type){ + this.log("Received value from bus:"+val+ " for " +dest+ " from "+src+" of type"+type + " for " + characteristic.displayName); +// iterate(characteristic); + characteristic.setValue(val ? 1 : 0, undefined, 'fromKNXBus'); + }.bind(this)); + }, + + // percentage: get 0..255 from the bus, write 0..100 to characteristic + knxregister_percent: function(addresses, characteristic) { + this.log("knx registering PERCENT " + addresses); + knxd_registerGA(addresses, function(val, src, dest, type){ + this.log("Received value from bus:"+val+ " for " +dest+ " from "+src+" of type"+type+ " for " + characteristic.displayName); + if (type !== "DPT5") { + this.log("[ERROR] Received value cannot be a percentage value"); + } else { + characteristic.setValue(Math.round(val/255*100), undefined, 'fromKNXBus'); + } + }.bind(this)); + }, + + // float + knxregister_float: function(addresses, characteristic) { + this.log("knx registering FLOAT " + addresses); + knxd_registerGA(addresses, function(val, src, dest, type){ + this.log("Received value from bus:"+val+ " for " +dest+ " from "+src+" of type"+type+ " for " + characteristic.displayName); + characteristic.setValue(val, undefined, 'fromKNXBus'); + }.bind(this)); + }, + + // what about HVAC heating cooling types? + knxregister_HVAC: function(addresses, characteristic) { + this.log("knx registering HVAC " + addresses); + knxd_registerGA(addresses, function(val, src, dest, type){ + this.log("Received value from bus:"+val+ " for " +dest+ " from "+src+" of type"+type+ " for " + characteristic.displayName); + var HAPvalue = 0; + switch (val){ + case 0: + HAPvalue = 3; + break; + case 1: + HAPvalue = 3; + break; + case 2: + HAPvalue = 3; + break; + case 3: + HAPvalue = 3; + break; + case 4: + HAPvalue = 0; + break; + default: + HAPvalue = 0; + } + characteristic.setValue(HAPvalue, undefined, 'fromKNXBus'); + }.bind(this)); + }, + // to do! KNX: DPT 20.102 = One Byte like DPT5 +// 0 = Auto +// 1 = Comfort +// 2 = Standby +// 3 = Night +// 4 = Freezing/Heat Protection +// 5 – 255 = not allowed” + // The value property of TargetHeatingCoolingState must be one of the following: +// Characteristic.TargetHeatingCoolingState.OFF = 0; +// Characteristic.TargetHeatingCoolingState.HEAT = 1; +// Characteristic.TargetHeatingCoolingState.COOL = 2; +// Characteristic.TargetHeatingCoolingState.AUTO = 3; + + + // undefined, has to match! + knxregister: function(addresses, characteristic) { + this.log("knx registering " + addresses); + knxd_registerGA(addresses, function(val, src, dest, type){ + this.log("Received value from bus:"+val+ " for " +dest+ " from "+src+" of type"+type+ " for " + characteristic.displayName); + characteristic.setValue(val, undefined, 'fromKNXBus'); + }.bind(this)); + }, + + /* + * set methods used for creating callbacks, such as + * var Characteristic = myService.addCharacteristic(new Characteristic.Brightness()) + * .on('set', function(value, callback, context) { + * this.setPercentage(value, callback, context, this.config[index].Set) + * }.bind(this)); + * + */ + setBooleanState: function(value, callback, context, gaddress) { + if (context === 'fromKNXBus') { + this.log(gaddress + " event ping pong, exit!"); + if (callback) { + callback(); + } + } else { + var numericValue = 0; + if (value) { + numericValue = 1; // need 0 or 1, not true or something + } + this.log("Setting "+gaddress+" Boolean to %s", numericValue); + this.knxwrite(callback, gaddress,'DPT1',numericValue); + } + + }, + + + setPercentage: function(value, callback, context, gaddress) { + if (context === 'fromKNXBus') { + this.log("event ping pong, exit!"); + if (callback) { + callback(); + } + } else { + var numericValue = 0; + if (value) { + numericValue = Math.round(255*value/100); // convert 1..100 to 1..255 for KNX bus + } + this.log("Setting "+gaddress+" percentage to %s (%s)", value, numericValue); + this.knxwrite(callback, gaddress,'DPT5',numericValue); + } + }, + + setFloat: function(value, callback, context, gaddress) { + if (context === 'fromKNXBus') { + this.log(gaddress + " event ping pong, exit!"); + if (callback) { + callback(); + } + } else { + var numericValue = 0; + if (value) { + numericValue = value; // need 0 or 1, not true or something + } + this.log("Setting "+gaddress+" Float to %s", numericValue); + this.knxwrite(callback, gaddress,'DPT9',numericValue); + } + }, + + setHVACState: function(value, callback, context, gaddress) { + if (context === 'fromKNXBus') { + this.log(gaddress + " event ping pong, exit!"); + if (callback) { + callback(); + } + } else { + var numericValue = 0; + switch (value){ + case 0: + KNXvalue = 4; + break; + case 1: + KNXvalue = 1; + break; + case 2: + KNXvalue = 1; + break; + case 3: + KNXvalue = 1; + break; + default: + KNXvalue = 1; + } + + this.log("Setting "+gaddress+" HVAC to %s", KNXvalue); + this.knxwrite(callback, gaddress,'DPT5',KNXvalue); + } + + }, + + + identify: function(callback) { + this.log("Identify requested!"); + callback(); // success + }, + + + /* + * function getXXXXXXXService(config) + * + * returns a configured service object to the caller (accessory/device) + * + * the config section is supposed to look like that for the complete device + * "devices": [ + { + "accessory_type": "knxdevice", + "name": "Living Room North Lamp", + "services": [ + { + "type": "Light", + "name": "Living Room North Lamp", + "On": { + "Set": "1/1/6", + "Listen": [ + "1/1/63" + ] + }, + "Brightness": { + "Set": "1/1/62", + "Listen": [ + "1/1/64" + ] + } + } + ] + }, + + * for each service it is supposed to have a + * type + * name + * and service specific objects + */ + + bindCharacteristic: function(myService, characteristicType, valueType, config) { + var myCharacteristic = myService.getCharacteristic(characteristicType); + if (myCharacteristic === undefined) { + throw new Error("unknown characteristics cannot be bound"); + } + if (config.Set) { + // can write + switch (valueType) { + case "Bool": + myCharacteristic.on('set', function(value, callback, context) { +// this.log("ITERATE DEBUG"); +// iterate(config); + this.setBooleanState(value, callback, context, config.Set); + }.bind(this)); + break; + case "Percent": + myCharacteristic.on('set', function(value, callback, context) { + this.setPercentage(value, callback, context, config.Set); + }.bind(this)); + break; + case "Float": + myCharacteristic.on('set', function(value, callback, context) { + this.setFloat(value, callback, context, config.Set); + }.bind(this)); + break; + case "HVAC": + myCharacteristic.on('set', function(value, callback, context) { + this.setHVACState(value, callback, context, config.Set); + }.bind(this)); + break; + default: + this.log("[ERROR] unknown type passed"); + throw new Error("[ERROR] unknown type passed"); + } + } + if ([config.Set].concat(config.Listen || []).length>0) { + this.log("Binding LISTEN"); + // can read + switch (valueType) { + case "Bool": + this.knxregister_bool([config.Set].concat(config.Listen || []), myCharacteristic); + break; + case "Percent": + this.knxregister_percent([config.Set].concat(config.Listen || []), myCharacteristic); + break; + case "Float": + this.knxregister_float([config.Set].concat(config.Listen || []), myCharacteristic); + break; + case "HVAC": + this.knxregister_HVAC([config.Set].concat(config.Listen || []), myCharacteristic); + break; + default: + this.log("[ERROR] unknown type passed"); + throw new Error("[ERROR] unknown type passed"); + } + this.log("Issuing read requests on the KNX bus..."); + this.knxreadarray([config.Set].concat(config.Listen || [])); + } + return myCharacteristic; // for chaining or whatsoever + }, + + getLightbulbService: function(config) { + // some sanity checks + //this.config = config; + + if (config.type !== "Lightbulb") { + this.log("[ERROR] Lightbulb Service for non 'Lightbulb' service called"); + return undefined; + } + if (!config.name) { + this.log("[ERROR] Lightbulb Service without 'name' property called"); + return undefined; + } + var myService = new Service.Lightbulb(config.name,config.name); + // On (and Off) + if (config.On) { + this.log("Lightbulb on/off characteristic enabled"); + this.bindCharacteristic(myService, Characteristic.On, "Bool", config.On); + } // On characteristic + // Brightness if available + if (config.Brightness) { + this.log("Lightbulb Brightness characteristic enabled"); + myService.addCharacteristic(Characteristic.Brightness); // it's an optional + this.bindCharacteristic(myService, Characteristic.Brightness, "Percent", config.Brightness); + } + // Hue and Saturation could be added here if available in KNX lamps + //iterate(myService); + return myService; + }, + + getThermostatService: function(config) { + + +// // Required Characteristics +// this.addCharacteristic(Characteristic.CurrentHeatingCoolingState); +// this.addCharacteristic(Characteristic.TargetHeatingCoolingState); +// this.addCharacteristic(Characteristic.CurrentTemperature); //check +// this.addCharacteristic(Characteristic.TargetTemperature); // +// this.addCharacteristic(Characteristic.TemperatureDisplayUnits); + // +// // Optional Characteristics +// this.addOptionalCharacteristic(Characteristic.CurrentRelativeHumidity); +// this.addOptionalCharacteristic(Characteristic.TargetRelativeHumidity); +// this.addOptionalCharacteristic(Characteristic.CoolingThresholdTemperature); +// this.addOptionalCharacteristic(Characteristic.HeatingThresholdTemperature); + + + // some sanity checks + + + if (config.type !== "Thermostat") { + this.log("[ERROR] Thermostat Service for non 'Thermostat' service called"); + return undefined; + } + if (!config.name) { + this.log("[ERROR] Thermostat Service without 'name' property called"); + return undefined; + } + var myService = new Service.Thermostat(config.name,config.name); + // CurrentTemperature) + if (config.CurrentTemperature) { + this.log("Thermostat CurrentTemperature characteristic enabled"); + this.bindCharacteristic(myService, Characteristic.CurrentTemperature, "Float", config.CurrentTemperature); + } + // TargetTemperature if available + if (config.TargetTemperature) { + this.log("Thermostat TargetTemperature characteristic enabled"); + this.bindCharacteristic(myService, Characteristic.TargetTemperature, "Float", config.TargetTemperature); + } + // HVAC missing yet + if (config.CurrentHeatingCoolingState) { + this.log("Thermostat CurrentHeatingCoolingState characteristic enabled"); + this.bindCharacteristic(myService, Characteristic.CurrentHeatingCoolingState, "HVAC", config.CurrentHeatingCoolingState); + } + return myService; + }, + + + + /* assemble the device ***************************************************************************************************/ + + + getServices: function() { + + // you can OPTIONALLY create an information service if you wish to override + // the default values for things like serial number, model, etc. + + var accessoryServices = []; + + var informationService = new Service.AccessoryInformation(); + + informationService + .setCharacteristic(Characteristic.Manufacturer, "Opensource Community") + .setCharacteristic(Characteristic.Model, "KNX Universal Device") + .setCharacteristic(Characteristic.SerialNumber, "Version 1.1"); + + accessoryServices.push(informationService); + + iterate(this.config); +// throw new Error("STOP"); + if (!this.config.services){ + this.log("No services found in accessory?!") + } + var currServices = this.config.services; + this.log("Preparing Services: " + currServices.length) + // go through the config thing and look for services + for (var int = 0; int < currServices.length; int++) { + var configService = currServices[int]; + // services need to have type and name properties + if (!configService.type && !configService.name) { + this.log("[ERROR] must specify 'type' and 'name' properties for each service in config.json. KNX platform section fault "); + throw new Error("Must specify 'type' and 'name' properties for each service in config.json"); + } + switch (configService.type) { + case "Lightbulb": + accessoryServices.push(this.getLightbulbService(configService)); + break; + case "Thermostat": + accessoryServices.push(this.getThermostatService(configService)); + break; + default: + this.log("[ERROR] unknown 'type' property for service "+ configService.name + " in config.json. KNX platform section fault "); + //throw new Error("[ERROR] unknown 'type' property for service "+ configService.name + " in config.json. KNX platform section fault "); + } + } + // start listening for events on the bus (if not started yet - will prevent itself) + knxd_startMonitor({ host: this.knxd_ip, port: this.knxd_port }); + return accessoryServices; + } +}; diff --git a/accessories/knxlamp.js b/accessories/knxlamp.js deleted file mode 100644 index 7f06529..0000000 --- a/accessories/knxlamp.js +++ /dev/null @@ -1,186 +0,0 @@ -/* - * This is a demo KNX thermostat accessory shim. - * It can - * - */ -var Service = require("HAP-NodeJS").Service; -var Characteristic = require("HAP-NodeJS").Characteristic; -var knxd = require("eibd"); -var knxd_registerGA = require('../platforms/KNX.js').registerGA; -var knxd_startMonitor = require('../platforms/KNX.js').startMonitor; - - - -function KNXlampAccessory(log, config) { - this.log = log; - - - // knx information on object - this.group_address = config.group_address; - this.listen_addresses = config.listen_addresses; // supposed to be undefined, an array of strings, or single string - this.can_dim = config.can_dim; //supposed to be true or false - this.brightness_group_address = config.brightness_group_address; - this.brightness_listen_addresses = config.brightness_listen_addresses; - this.knxd_ip = config.knxd_ip ; // eg 127.0.0.1 if running on localhost - this.knxd_port = config.knxd_port || 6720; // eg 6720 default knxd port - if (config.name) { - this.name = config.name; - } - log("Accessory constructor called"); - -} - - -module.exports = { - accessory: KNXlampAccessory - }; - - -KNXlampAccessory.prototype = { - - - knxwrite: function(callback, groupAddress, dpt, value) { - // this.log("DEBUG in knxwrite"); - var knxdConnection = new knxd.Connection(); - // this.log("DEBUG in knxwrite: created empty connection, trying to connect socket to "+this.knxd_ip+":"+this.knxd_port); - knxdConnection.socketRemote({ host: this.knxd_ip, port: this.knxd_port }, function() { - var dest = knxd.str2addr(groupAddress); - // this.log("DEBUG got dest="+dest); - knxdConnection.openTGroup(dest, 1, function(err) { - if (err) { - this.log("[ERROR] knxwrite:openTGroup: " + err); - callback(err); - } else { - // this.log("DEBUG opened TGroup "); - var msg = knxd.createMessage('write', dpt, parseFloat(value)); - knxdConnection.sendAPDU(msg, function(err) { - if (err) { - this.log("[ERROR] knxwrite:sendAPDU: " + err); - callback(err); - } else { - // this.log("knx data sent"); - callback(); - } - }.bind(this)); - } - }.bind(this)); - }.bind(this)); - }, - - // issues a read request on the knx bus - // DOES NOT WAIT for an answer. Please register the address with a callback using registerGA() function - knxread: function(groupAddress){ - // this.log("DEBUG in knxread"); - var knxdConnection = new knxd.Connection(); - // this.log("DEBUG in knxread: created empty connection, trying to connect socket to "+this.knxd_ip+":"+this.knxd_port); - knxdConnection.socketRemote({ host: this.knxd_ip, port: this.knxd_port }, function() { - var dest = knxd.str2addr(groupAddress); - // this.log("DEBUG got dest="+dest); - knxdConnection.openTGroup(dest, 1, function(err) { - if (err) { - this.log("[ERROR] knxread:openTGroup: " + err); - } else { - // this.log("DEBUG knxread: opened TGroup "); - var msg = knxd.createMessage('read', 'DPT1', 0); - knxdConnection.sendAPDU(msg, function(err) { - if (err) { - this.log("[ERROR] knxread:sendAPDU: " + err); - } else { - this.log("knx request sent"); - } - }.bind(this)); - } - }.bind(this)); - }.bind(this)); - }, - - knxregister: function(addresses, characteristic) { - console.log("knx registering " + addresses); - knxd_registerGA(addresses, function(value){ - // parameters do not match - this.log("Getting value from bus:"+value); - characteristic.setValue(value, undefined, 'fromKNXBus'); - }.bind(this)); - }, - - setPowerState: function(value, callback, context) { - if (context === 'fromKNXBus') { - this.log("event ping pong, exit!"); - if (callback) { - callback(); - } - } else { - console.log("Setting power to %s", value); - var numericValue = 0; - if (value) { - numericValue = 1; // need 0 or 1, not true or something - } - this.knxwrite(callback, this.group_address,'DPT1',numericValue); - } - - }, - - - setBrightness: function(value, callback, context) { - if (context === 'fromKNXBus') { - this.log("event ping pong, exit!"); - if (callback) { - callback(); - } - } else { - this.log("Setting brightness to %s", value); - var numericValue = 0; - if (value) { - numericValue = 255*value/100; // convert 1..100 to 1..255 for KNX bus - } - this.knxwrite(callback, this.brightness_group_address,'DPT5',numericValue); - } - }, - - - - identify: function(callback) { - this.log("Identify requested!"); - callback(); // success - }, - - getServices: function() { - - // you can OPTIONALLY create an information service if you wish to override - // the default values for things like serial number, model, etc. - var informationService = new Service.AccessoryInformation(); - - informationService - .setCharacteristic(Characteristic.Manufacturer, "Opensource Community") - .setCharacteristic(Characteristic.Model, "KNX Light Switch with or without dimmer") - .setCharacteristic(Characteristic.SerialNumber, "Version 1"); - - var lightbulbService = new Service.Lightbulb(); - - var onCharacteristic = lightbulbService - .getCharacteristic(Characteristic.On) - .on('set', this.setPowerState.bind(this)); - onCharacteristic.supportsEventNotification=true; - // register with value update service - this.addresses = [this.group_address]; - this.log("DEBUG1 this.addresses = "+this.addresses); - this.log("DEBUG2 this.listen_addresses = "+this.listen_addresses); - this.addresses = this.addresses.concat(this.listen_addresses || []); // do not join anything if empty (do not add undefined) - this.log("DEBUG3 this.addresses = "+this.addresses); - this.knxregister(this.addresses, onCharacteristic); - this.knxread(this.group_address); // issue a read request on the bus, maybe the device answers to that! - - if (this.can_dim) { - var brightnessCharacteristic = lightbulbService - .addCharacteristic(new Characteristic.Brightness()) - .on('set', this.setBrightness.bind(this)); - // register with value update service - this.brightness_addresses = [this.brightness_group_address]; - this.brightness_addresses.concat(this.brightness_listen_addresses || []); // do not join anything if empty (do not add undefined) - this.knxregister(this.brightness_addresses, brightnessCharacteristic); - this.knxread(this.brightness_group_address); // issue a read request on the bus, maybe the device answers to that! - } - knxd_startMonitor({ host: this.knxd_ip, port: this.knxd_port }); - return [informationService, lightbulbService]; - } -}; diff --git a/accessories/knxthermostat.js b/accessories/knxthermostat.js deleted file mode 100644 index e0f408a..0000000 --- a/accessories/knxthermostat.js +++ /dev/null @@ -1,178 +0,0 @@ -/* - * This is a demo KNX lamp accessory shim. - * It can switch a light on and off, and optionally set a brightness if configured to do so - * - */ -var Service = require("HAP-NodeJS").Service; -var Characteristic = require("HAP-NodeJS").Characteristic; -var knxd = require("eibd"); -var knxd_registerGA = require('../platforms/KNX.js').registerGA; -var knxd_startMonitor = require('../platforms/KNX.js').startMonitor; - - - -function KNXthermoAccessory(log, config) { - this.log = log; - this.config=config; - - // knx information on object - this.curr_temp_address = config.curr_temp_address; - this.curr_temp_listen_addresses = config.curr_temp_listen_addresses; // supposed to be undefined, an array of strings, or single string - this.target_temp_address = config.target_temp_address; - this.knxd_ip = config.knxd_ip ; // eg 127.0.0.1 if running on localhost - this.knxd_port = config.knxd_port || 6720; // eg 6720 default knxd port - if (config.name) { - this.name = config.name; - } - log("Accessory constructor called"); - -} - - -module.exports = { - accessory: KNXthermoAccessory - }; - - -KNXthermoAccessory.prototype = { - - - knxwrite: function(callback, groupAddress, dpt, value) { - // this.log("DEBUG in knxwrite"); - var knxdConnection = new knxd.Connection(); - // this.log("DEBUG in knxwrite: created empty connection, trying to connect socket to "+this.knxd_ip+":"+this.knxd_port); - knxdConnection.socketRemote({ host: this.knxd_ip, port: this.knxd_port }, function() { - var dest = knxd.str2addr(groupAddress); - // this.log("DEBUG got dest="+dest); - knxdConnection.openTGroup(dest, 1, function(err) { - if (err) { - this.log("[ERROR] knxwrite:openTGroup: " + err); - callback(err); - } else { - // this.log("DEBUG opened TGroup "); - var msg = knxd.createMessage('write', dpt, parseFloat(value)); - knxdConnection.sendAPDU(msg, function(err) { - if (err) { - this.log("[ERROR] knxwrite:sendAPDU: " + err); - callback(err); - } else { - this.log("knx data sent"); - callback(); - } - }.bind(this)); - } - }.bind(this)); - }.bind(this)); - }, - - // issues a read request on the knx bus - // DOES NOT WAIT for an answer. Please register the address with a callback using registerGA() function - knxread: function(groupAddress){ - // this.log("DEBUG in knxread"); - var knxdConnection = new knxd.Connection(); - // this.log("DEBUG in knxread: created empty connection, trying to connect socket to "+this.knxd_ip+":"+this.knxd_port); - knxdConnection.socketRemote({ host: this.knxd_ip, port: this.knxd_port }, function() { - var dest = knxd.str2addr(groupAddress); - // this.log("DEBUG got dest="+dest); - knxdConnection.openTGroup(dest, 1, function(err) { - if (err) { - this.log("[ERROR] knxread:openTGroup: " + err); - } else { - // this.log("DEBUG knxread: opened TGroup "); - var msg = knxd.createMessage('read', 'DPT1', 0); - knxdConnection.sendAPDU(msg, function(err) { - if (err) { - this.log("[ERROR] knxread:sendAPDU: " + err); - } else { - this.log("knx request sent"); - } - }.bind(this)); - } - }.bind(this)); - }.bind(this)); - }, - - knxregister: function(addresses, characteristic) { - console.log("knx registering " + addresses); - knxd_registerGA(addresses, function(value){ - // parameters do not match - this.log("Getting value from bus:"+value); - characteristic.setValue(value, undefined, 'fromKNXBus'); - }.bind(this)); - }, - - - - identify: function(callback) { - this.log("Identify requested!"); - callback(); // success - }, - - getServices: function() { - - // you can OPTIONALLY create an information service if you wish to override - // the default values for things like serial number, model, etc. - var informationService = new Service.AccessoryInformation(); - - informationService - .setCharacteristic(Characteristic.Manufacturer, "Opensource Community") - .setCharacteristic(Characteristic.Model, "KNX Thermostat") - .setCharacteristic(Characteristic.SerialNumber, "Version 1"); - - var myService = new Service.Thermostat(); - -// -// // Required Characteristics -// this.addCharacteristic(Characteristic.CurrentHeatingCoolingState); -// this.addCharacteristic(Characteristic.TargetHeatingCoolingState); -// this.addCharacteristic(Characteristic.CurrentTemperature); //check -// this.addCharacteristic(Characteristic.TargetTemperature); // -// this.addCharacteristic(Characteristic.TemperatureDisplayUnits); -// -// // Optional Characteristics -// this.addOptionalCharacteristic(Characteristic.CurrentRelativeHumidity); -// this.addOptionalCharacteristic(Characteristic.TargetRelativeHumidity); -// this.addOptionalCharacteristic(Characteristic.CoolingThresholdTemperature); -// this.addOptionalCharacteristic(Characteristic.HeatingThresholdTemperature); -// this.addOptionalCharacteristic(Characteristic.Name); - - - var CurrentTemperatureCharacteristic = myService - .getCharacteristic(Characteristic.CurrentTemperature) - // .on('set', this.setPowerState.bind(this)); - CurrentTemperatureCharacteristic.supportsEventNotification=true; - // register with value update service - this.addresses1 = [this.curr_temp_address]; - this.addresses1 = this.addresses1.concat(this.curr_temp_listen_addresses || []); // do not join anything if empty (do not add undefined) - this.knxregister(this.addresses1, CurrentTemperatureCharacteristic); - this.knxread(this.curr_temp_address); // issue a read request on the bus, maybe the device answers to that! - - var TargetTemperatureCharacteristic = myService - .getCharacteristic(Characteristic.TargetTemperature) - .on('set', function(value, callback, context) { - if (context === 'fromKNXBus') { - this.log("event ping pong, exit!"); - if (callback) { - callback(); - } - } else { - console.log("Setting temperature to %s", value); - var numericValue = 0.0; - if (value) { - numericValue = 0+value; // need to be numeric - } - this.knxwrite(callback, this.target_temp_address,'DPT9',numericValue); - } - }.bind(this)); - TargetTemperatureCharacteristic.supportsEventNotification=true; - // register with value update service - this.addresses2 = [this.target_temp_address]; - this.addresses2 = this.addresses2.concat(this.target_temp_listen_addresses || []); // do not join anything if empty (do not add undefined) - this.knxregister(this.addresses2, TargetTemperatureCharacteristic); - this.knxread(this.target_temp_address); // issue a read request on the bus, maybe the device answers to that! - - - knxd_startMonitor({ host: this.knxd_ip, port: this.knxd_port }); - return [informationService, myService]; - } -}; diff --git a/platforms/KNX.js b/platforms/KNX.js index 841c03d..7070137 100644 --- a/platforms/KNX.js +++ b/platforms/KNX.js @@ -7,83 +7,75 @@ var types = require("HAP-NodeJS/accessories/types.js"); var knxd = require('eibd'); function KNXPlatform(log, config){ - this.log = log; - this.config = config; -// this.property1 = config.property1; -// this.property2 = config.property2; - - - // initiate connection to bus for listening ==> done with first shim - + this.log = log; + this.config = config; +// this.property1 = config.property1; +// this.property2 = config.property2; + + + // initiate connection to bus for listening ==> done with first shim + }; KNXPlatform.prototype = { - accessories: function(callback) { - this.log("Fetching KNX devices."); - var that = this; - - - // iterate through all devices the platform my offer - // for each device, create an accessory - - // read accessories from file !!!!! - var foundAccessories = this.config.accessories; - - - //create array of accessories - var myAccessories = []; - - for (var int = 0; int < foundAccessories.length; int++) { - this.log("parsing acc " + int + " of " + foundAccessories.length); - // instantiate and push to array - switch (foundAccessories[int].accessory_type) { - case "knxlamp": - this.log("push new lamp with "+foundAccessories[int].name); - foundAccessories[int].knxd_ip = this.config.knxd_ip; - foundAccessories[int].knxd_port = this.config.knxd_port; - var accConstructor = require('./../accessories/knxlamp.js'); - var acc = new accConstructor.accessory(this.log,foundAccessories[int]); - this.log("created "+acc.name+" accessory"); - myAccessories.push(acc); - break; - case "knxthermostat": - this.log("push new thermostat with "+foundAccessories[int].name); - foundAccessories[int].knxd_ip = this.config.knxd_ip; - foundAccessories[int].knxd_port = this.config.knxd_port; - var accConstructor = require('./../accessories/knxthermostat.js'); - var acc = new accConstructor.accessory(this.log,foundAccessories[int]); - this.log("created "+acc.name+" accessory"); - myAccessories.push(acc); - break; - default: - // do something else - this.log("unkown accessory type found") - } - - }; - // if done, return the array to callback function - this.log("returning "+myAccessories.length+" accessories"); - callback(myAccessories); - } + accessories: function(callback) { + this.log("Fetching KNX devices."); + var that = this; + + + // iterate through all devices the platform my offer + // for each device, create an accessory + + // read accessories from file !!!!! + var foundAccessories = this.config.accessories; + + + //create array of accessories + var myAccessories = []; + + for (var int = 0; int < foundAccessories.length; int++) { + this.log("parsing acc " + int + " of " + foundAccessories.length); + // instantiate and push to array + switch (foundAccessories[int].accessory_type) { + case "knxdevice": + this.log("push new universal device "+foundAccessories[int].name); + // push knxd connection setting to each device from platform + foundAccessories[int].knxd_ip = this.config.knxd_ip; + foundAccessories[int].knxd_port = this.config.knxd_port; + var accConstructor = require('./../accessories/knxdevice.js'); + var acc = new accConstructor.accessory(this.log,foundAccessories[int]); + this.log("created "+acc.name+" universal accessory"); + myAccessories.push(acc); + break; + case "knxlamp": + this.log("push new lamp with "+foundAccessories[int].name ); + foundAccessories[int].knxd_ip = this.config.knxd_ip; + foundAccessories[int].knxd_port = this.config.knxd_port; + var accConstructor = require('./../accessories/knxlamp.js'); + var acc = new accConstructor.accessory(this.log,foundAccessories[int]); + this.log("created "+acc.name+" accessory"); + myAccessories.push(acc); + break; + case "knxthermostat": + this.log("push new thermostat with "+foundAccessories[int].name); + foundAccessories[int].knxd_ip = this.config.knxd_ip; + foundAccessories[int].knxd_port = this.config.knxd_port; + var accConstructor = require('./../accessories/knxthermostat.js'); + var acc = new accConstructor.accessory(this.log,foundAccessories[int]); + this.log("created "+acc.name+" accessory"); + myAccessories.push(acc); + break; + default: + // do something else + this.log("unkown accessory type found") + } + + }; + // if done, return the array to callback function + this.log("returning "+myAccessories.length+" accessories"); + callback(myAccessories); + } }; - - -// the signature of the constructor has to be adopted to the accessory you need in your platform! These are the first lines from the sonos platform -function myAccessoryType1(log, config, device, description /* add or remove parms as you need*/ ) { - - this.log = log; - this.config = config; - this.device = device; - this.description = description; - // more initialization if required - -} - -myAccessoryType1.prototype = { - // see https shim wiki page for details. Accessory definition is discussed there. -} - -// more /** @@ -91,47 +83,47 @@ myAccessoryType1.prototype = { * of registered addresses. * * Usage: -* You can start the monitoring process at any time + * You can start the monitoring process at any time startMonitor({host: name-ip, port: port-num }); - -* You can add addresses to the subscriptions using - + + * You can add addresses to the subscriptions using + registerGA(groupAddress, callback) - -* groupAddress has to be an groupAddress in common knx notation string '1/2/3' -* the callback has to be a -* var f = function(value) { handle value update;} -* so you can do a -* registerGA('1/2/3', function(value){ -* console.log('1/2/3 got a hit with '+value); -* }); -* but of course it is meant to be used programmatically, not literally, otherwise it has no advantage -* -* You can also use arrays of addresses if your callback is supposed to listen to many addresses: + + * groupAddress has to be an groupAddress in common knx notation string '1/2/3' + * the callback has to be a + * var f = function(value) { handle value update;} + * so you can do a + * registerGA('1/2/3', function(value){ + * console.log('1/2/3 got a hit with '+value); + * }); + * but of course it is meant to be used programmatically, not literally, otherwise it has no advantage + * + * You can also use arrays of addresses if your callback is supposed to listen to many addresses: registerGA(groupAddresses[], callback) -* as in -* registerGA(['1/2/3','1/0/0'], function(value){ -* console.log('1/2/3 or 1/0/0 got a hit with '+value); -* }); -* if you are having central addresses like "all lights off" or additional response objects -* -* -* callbacks can have a signature of -* function(value, src, dest, type) but do not have to support these parameters (order matters) -* src = physical address such as '1.1.20' -* dest = groupAddress hit (you subscribed to that address, remember?), as '1/2/3' -* type = Data point type, as 'DPT1' -* -* -*/ + * as in + * registerGA(['1/2/3','1/0/0'], function(value){ + * console.log('1/2/3 or 1/0/0 got a hit with '+value); + * }); + * if you are having central addresses like "all lights off" or additional response objects + * + * + * callbacks can have a signature of + * function(value, src, dest, type) but do not have to support these parameters (order matters) + * src = physical address such as '1.1.20' + * dest = groupAddress hit (you subscribed to that address, remember?), as '1/2/3' + * type = Data point type, as 'DPT1' + * + * + */ -// array of registered addresses and their callbacks +//array of registered addresses and their callbacks var subscriptions = []; -// check variable to avoid running two listeners +//check variable to avoid running two listeners var running; function groupsocketlisten(opts, callback) { @@ -145,7 +137,7 @@ function groupsocketlisten(opts, callback) { var registerSingleGA = function registerSingleGA (groupAddress, callback) { subscriptions.push({address: groupAddress, callback: callback }); } - + /* * public busMonitor.startMonitor() * starts listening for telegrams on KNX bus @@ -155,42 +147,45 @@ var startMonitor = function startMonitor(opts) { // using { host: name-ip, port if (!running) { running = true; } else { + console.log("<< knxd socket listener already running >>"); return null; } - - groupsocketlisten(opts, function(parser) { - //console.log("knxfunctions.read: in callback parser"); - parser.on('write', function(src, dest, type, val){ - // search the registered group addresses - for (var i = 0; i < subscriptions.length; i++) { - // iterate through all registered addresses - if (subscriptions[i].address === dest) { - // found one, notify - //console.log('HIT: Write from '+src+' to '+dest+': '+val+' ['+type+']'); - subscriptions[i].callback(val, src, dest, type); - } - } - }); + console.log(">>> knxd groupsocketlisten starting <<<"); + groupsocketlisten(opts, function(parser) { + //console.log("knxfunctions.read: in callback parser"); + parser.on('write', function(src, dest, type, val){ + // search the registered group addresses + //console.log('recv: Write from '+src+' to '+dest+': '+val+' ['+type+'], listeners:' + subscriptions.length); + for (var i = 0; i < subscriptions.length; i++) { + // iterate through all registered addresses + if (subscriptions[i].address === dest) { + // found one, notify + console.log('HIT: Write from '+src+' to '+dest+': '+val+' ['+type+']'); + subscriptions[i].callback(val, src, dest, type); + } + } + }); - parser.on('response', function(src, dest, type, val) { - // search the registered group addresses - for (var i = 0; i < subscriptions.length; i++) { - // iterate through all registered addresses - if (subscriptions[i].address === dest) { - // found one, notify - //console.log('HIT: Response from '+src+' to '+dest+': '+val+' ['+type+']'); - subscriptions[i].callback(val, src, dest, type); - } - } - - }); - - //dont care about reads here -// parser.on('read', function(src, dest) { -// console.log('Read from '+src+' to '+dest); -// }); + parser.on('response', function(src, dest, type, val) { + // search the registered group addresses +// console.log('recv: resp from '+src+' to '+dest+': '+val+' ['+type+']'); + for (var i = 0; i < subscriptions.length; i++) { + // iterate through all registered addresses + if (subscriptions[i].address === dest) { + // found one, notify +// console.log('HIT: Response from '+src+' to '+dest+': '+val+' ['+type+']'); + subscriptions[i].callback(val, src, dest, type); + } + } + + }); + + //dont care about reads here +// parser.on('read', function(src, dest) { +// console.log('Read from '+src+' to '+dest); +// }); //console.log("knxfunctions.read: in callback parser at end"); - }); // groupsocketlisten parser + }); // groupsocketlisten parser }; //startMonitor @@ -208,12 +203,15 @@ var registerGA = function (groupAddresses, callback) { if (groupAddresses.constructor.toString().indexOf("Array") > -1) { // handle multiple addresses for (var i = 0; i < groupAddresses.length; i++) { - registerSingleGA (groupAddresses[i], callback); + if (groupAddresses[i]) { // do not bind empty addresses + registerSingleGA (groupAddresses[i], callback); + } } } else { // it's only one registerSingleGA (groupAddresses, callback); } +// console.log("listeners now: " + subscriptions.length); }; From 64635833d6f68ba5743b5b8f90090147bf52ac3b Mon Sep 17 00:00:00 2001 From: Snowdd1 Date: Sat, 5 Sep 2015 20:11:06 +0200 Subject: [PATCH 09/18] Sample config file config-sample-knx.jsnon --- config-sample-knx.json | 88 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 config-sample-knx.json diff --git a/config-sample-knx.json b/config-sample-knx.json new file mode 100644 index 0000000..8e7d11e --- /dev/null +++ b/config-sample-knx.json @@ -0,0 +1,88 @@ +{ + "bridge": { + "name": "Homebridge", + "username": "CC:22:3D:E3:CE:30", + "port": 51826, + "pin": "031-45-154" + }, + "description": "This is an example configuration file for KNX platform shim", + "platforms": + [ + { + "platform": "KNX", + "name": "KNX", + "knxd_ip": "192.168.178.205", + "knxd_port": 6720, + "accessories": + [ + { + "accessory_type": "knxdevice", + "name": "Living Room North Lamp", + "services": + [ + { + "type": "Lightbulb", + "name": "Living Room North Lamp", + "On": { + "Set": "1/1/6", + "Listen": [ + "1/1/63" + ] + }, + "Brightness": { + "Set": "1/1/62", + "Listen": [ + "1/1/64" + ] + } + } + ] + }, + { + "accessory_type": "knxdevice", + "name": "Office", + "services": + [ + { + "type": "Lightbulb", + "name": "Office Lamp", + "On": { + "Set": "1/3/5" + } + }, + { + "type": "Thermostat", + "name": "Raumtemperatur", + "CurrentTemperature": { + "Listen": "3/3/44" + }, + "TargetTemperature": { + "Set": "3/3/94" + }, + "CurrentHeatingCoolingState": { + "Listen": "3/3/64" + } + }, + { + "type": "WindowCovering", + "name": "Rollo", + "Target": { + "Set": "address", + "Listen": "adresses" + }, + "Current": { + "Set": "address", + "Listen": "adresses" + }, + "PositionState": { + "Listen": "adresses" + } + } + ] + } + ] + } + ], + "accessories": [], + "prototyping": {} +} \ No newline at end of file From e2ef8fc0b6d71274477af7adde55c4f4da5f3055 Mon Sep 17 00:00:00 2001 From: Snowdd1 Date: Sat, 5 Sep 2015 21:31:48 +0200 Subject: [PATCH 10/18] package.json - removed subtypes to be passed by default. - package.json to inbclude "eibd" --- accessories/knxdevice.js | 37 ++++--------------------------------- package.json | 1 + 2 files changed, 5 insertions(+), 33 deletions(-) diff --git a/accessories/knxdevice.js b/accessories/knxdevice.js index c07e2ac..ffcb50e 100644 --- a/accessories/knxdevice.js +++ b/accessories/knxdevice.js @@ -339,35 +339,6 @@ KNXDevice.prototype = { * * returns a configured service object to the caller (accessory/device) * - * the config section is supposed to look like that for the complete device - * "devices": [ - { - "accessory_type": "knxdevice", - "name": "Living Room North Lamp", - "services": [ - { - "type": "Light", - "name": "Living Room North Lamp", - "On": { - "Set": "1/1/6", - "Listen": [ - "1/1/63" - ] - }, - "Brightness": { - "Set": "1/1/62", - "Listen": [ - "1/1/64" - ] - } - } - ] - }, - - * for each service it is supposed to have a - * type - * name - * and service specific objects */ bindCharacteristic: function(myService, characteristicType, valueType, config) { @@ -406,7 +377,7 @@ KNXDevice.prototype = { } } if ([config.Set].concat(config.Listen || []).length>0) { - this.log("Binding LISTEN"); + //this.log("Binding LISTEN"); // can read switch (valueType) { case "Bool": @@ -443,7 +414,7 @@ KNXDevice.prototype = { this.log("[ERROR] Lightbulb Service without 'name' property called"); return undefined; } - var myService = new Service.Lightbulb(config.name,config.name); + var myService = new Service.Lightbulb() //(config.name,config.name); // On (and Off) if (config.On) { this.log("Lightbulb on/off characteristic enabled"); @@ -477,7 +448,7 @@ KNXDevice.prototype = { // this.addOptionalCharacteristic(Characteristic.HeatingThresholdTemperature); - // some sanity checks + // some sanity checks if (config.type !== "Thermostat") { @@ -488,7 +459,7 @@ KNXDevice.prototype = { this.log("[ERROR] Thermostat Service without 'name' property called"); return undefined; } - var myService = new Service.Thermostat(config.name,config.name); + var myService = new Service.Thermostat() //(config.name,config.name); // CurrentTemperature) if (config.CurrentTemperature) { this.log("Thermostat CurrentTemperature characteristic enabled"); diff --git a/package.json b/package.json index e7fd564..9ee336f 100644 --- a/package.json +++ b/package.json @@ -15,6 +15,7 @@ "carwingsjs": "0.0.x", "color": "0.10.x", "elkington": "kevinohara80/elkington", + "eibd": "0.3.1", "hap-nodejs": "git+https://github.com/KhaosT/HAP-NodeJS#fff863d7a387636fc612cf27cb859e82d9ee3294", "harmonyhubjs-client": "^1.1.4", "harmonyhubjs-discover": "git+https://github.com/swissmanu/harmonyhubjs-discover.git", From 816728c0cf1957518a343118d5f658c1101b5128 Mon Sep 17 00:00:00 2001 From: Nick Farina Date: Sat, 5 Sep 2015 12:42:21 -0700 Subject: [PATCH 11/18] Upgrade WeMo accessory - Use new Service API - Experimental preliminary support for WeMo "Garage Door Openers" (via WeMo Maker). No Sensor support yet. --- accessories/WeMo.js | 261 +++++++++++++++++++------------------------- 1 file changed, 110 insertions(+), 151 deletions(-) diff --git a/accessories/WeMo.js b/accessories/WeMo.js index 273e4b3..d8e713d 100644 --- a/accessories/WeMo.js +++ b/accessories/WeMo.js @@ -1,166 +1,125 @@ -var types = require("HAP-NodeJS/accessories/types.js"); +var Service = require("HAP-NodeJS").Service; +var Characteristic = require("HAP-NodeJS").Characteristic; var wemo = require('wemo'); -// extend our search timeout from 5 seconds to 60 -wemo.SearchTimeout = 60000; -wemo.timeout = wemo.SearchTimeout // workaround for a bug in wemo.js v0.0.4 +module.exports = { + accessory: WeMoAccessory +} function WeMoAccessory(log, config) { this.log = log; - this.name = config["name"]; - this.wemoName = config["wemo_name"]; - this.device = null; + this.service = config["service"] || "Switch"; + this.wemoName = config["wemo_name"] || config["name"]; // fallback to "name" if you didn't specify an exact "wemo_name" + this.device = null; // instance of WeMo, for controlling the discovered device this.log("Searching for WeMo device with exact name '" + this.wemoName + "'..."); this.search(); } -WeMoAccessory.prototype = { - - search: function() { - var that = this; - - wemo.Search(this.wemoName, function(err, device) { - if (!err && device) { - that.log("Found '"+that.wemoName+"' device at " + device.ip); - that.device = new wemo(device.ip, device.port); - } - else { - that.log("Error finding device '" + that.wemoName + "': " + err); - that.log("Continuing search for WeMo device with exact name '" + that.wemoName + "'..."); - that.search(); - } - }); - }, - - setPowerState: function(powerOn) { - - if (!this.device) { - this.log("No '"+this.wemoName+"' device found (yet?)"); - return; +WeMoAccessory.prototype.search = function() { + wemo.Search(this.wemoName, function(err, device) { + if (!err && device) { + this.log("Found '"+this.wemoName+"' device at " + device.ip); + this.device = new wemo(device.ip, device.port); } - - var binaryState = powerOn ? 1 : 0; - var that = this; - - this.log("Setting power state on the '"+this.wemoName+"' to " + binaryState); - - this.device.setBinaryState(binaryState, function(err, result) { - if (!err) { - that.log("Successfully set power state on the '"+that.wemoName+"' to " + binaryState); - } - else { - that.log("Error setting power state on the '"+that.wemoName+"'") - } - }); - }, - - getPowerState: function(callback) { - - if (!this.device) { - this.log("No '"+this.wemoName+"' device found (yet?)"); - return; + else { + this.log("Error finding device '" + this.wemoName + "': " + err); + this.log("Continuing search for WeMo device with exact name '" + this.wemoName + "'..."); + this.search(); } + }.bind(this)); +} - var that = this; +WeMoAccessory.prototype.getPowerOn = function(callback) { - this.log("checking power state for: " + this.wemoName); - this.device.getBinaryState(function(err, result) { - if (!err) { - var binaryState = parseInt(result) - that.log("power state for " + that.wemoName + " is: " + binaryState) - callback(binaryState > 0 ? 1 : 0); - } - else { - that.log(err) - } - }); - }, - - getServices: function() { - var that = this; - return [{ - sType: types.ACCESSORY_INFORMATION_STYPE, - characteristics: [{ - cType: types.NAME_CTYPE, - onUpdate: null, - perms: ["pr"], - format: "string", - initialValue: this.name, - supportEvents: false, - supportBonjour: false, - manfDescription: "Name of the accessory", - designedMaxLength: 255 - },{ - cType: types.MANUFACTURER_CTYPE, - onUpdate: null, - perms: ["pr"], - format: "string", - initialValue: "WeMo", - supportEvents: false, - supportBonjour: false, - manfDescription: "Manufacturer", - designedMaxLength: 255 - },{ - cType: types.MODEL_CTYPE, - onUpdate: null, - perms: ["pr"], - format: "string", - initialValue: "Rev-1", - supportEvents: false, - supportBonjour: false, - manfDescription: "Model", - designedMaxLength: 255 - },{ - cType: types.SERIAL_NUMBER_CTYPE, - onUpdate: null, - perms: ["pr"], - format: "string", - initialValue: "A1S2NASF88EW", - supportEvents: false, - supportBonjour: false, - manfDescription: "SN", - designedMaxLength: 255 - },{ - cType: types.IDENTIFY_CTYPE, - onUpdate: null, - perms: ["pw"], - format: "bool", - initialValue: false, - supportEvents: false, - supportBonjour: false, - manfDescription: "Identify Accessory", - designedMaxLength: 1 - }] - },{ - sType: types.SWITCH_STYPE, - characteristics: [{ - cType: types.NAME_CTYPE, - onUpdate: null, - perms: ["pr"], - format: "string", - initialValue: this.name, - supportEvents: false, - supportBonjour: false, - manfDescription: "Name of service", - designedMaxLength: 255 - },{ - cType: types.POWER_STATE_CTYPE, - onUpdate: function(value) { that.setPowerState(value); }, - onRead: function(callback) { - that.getPowerState(function(powerState){ - callback(powerState); - }); - }, - perms: ["pw","pr","ev"], - format: "bool", - initialValue: false, - supportEvents: false, - supportBonjour: false, - manfDescription: "Change the power state of the WeMo", - designedMaxLength: 1 - }] - }]; + if (!this.device) { + this.log("No '%s' device found (yet?)", this.wemoName); + callback(new Error("Device not found")); + return; } -}; -module.exports.accessory = WeMoAccessory; + this.log("Getting power state on the '%s'...", this.wemoName); + + this.device.getBinaryState(function(err, result) { + if (!err) { + var binaryState = parseInt(result); + var powerOn = binaryState > 0; + this.log("Power state for the '%s' is %s", this.wemoName, binaryState); + callback(null, powerOn); + } + else { + this.log("Error getting power state on the '%s': %s", this.wemoName, err.message); + callback(err); + } + }.bind(this)); +} + +WeMoAccessory.prototype.setPowerOn = function(powerOn, callback) { + + if (!this.device) { + this.log("No '%s' device found (yet?)", this.wemoName); + callback(new Error("Device not found")); + return; + } + + var binaryState = powerOn ? 1 : 0; // wemo langauge + this.log("Setting power state on the '%s' to %s", this.wemoName, binaryState); + + this.device.setBinaryState(binaryState, function(err, result) { + if (!err) { + this.log("Successfully set power state on the '%s' to %s", this.wemoName, binaryState); + callback(null); + } + else { + this.log("Error setting power state to %s on the '%s'", binaryState, this.wemoName); + callback(err); + } + }.bind(this)); +} + +WeMoAccessory.prototype.setTargetDoorState = function(targetDoorState, callback) { + + if (!this.device) { + this.log("No '%s' device found (yet?)", this.wemoName); + callback(new Error("Device not found")); + return; + } + + this.log("Activating WeMo switch '%s'", this.wemoName); + + this.device.setBinaryState(1, function(err, result) { + if (!err) { + this.log("Successfully activated WeMo switch '%s'", this.wemoName); + callback(null); + } + else { + this.log("Error activating WeMo switch '%s'", this.wemoName); + callback(err); + } + }.bind(this)); +} + +WeMoAccessory.prototype.getServices = function() { + + if (this.service == "Switch") { + var switchService = new Service.Switch("Switch"); + + switchService + .getCharacteristic(Characteristic.On) + .on('get', this.getPowerOn.bind(this)) + .on('set', this.setPowerOn.bind(this)); + + return [switchService]; + } + else if (this.service == "GarageDoor") { + var garageDoorService = new Service.GarageDoorOpener("Garage Door Opener"); + + garageDoorService + .getCharacteristic(Characteristic.TargetDoorState) + .on('set', this.setTargetDoorState.bind(this)); + + return [garageDoorService]; + } + else { + throw new Error("Unknown service type '%s'", this.service); + } +} From 6c6b5bf85f0e4612ae745ab0943003b6cd835cfe Mon Sep 17 00:00:00 2001 From: Nick Farina Date: Sat, 5 Sep 2015 12:47:56 -0700 Subject: [PATCH 12/18] Pull in iControl fix --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index e7fd564..883ac7b 100644 --- a/package.json +++ b/package.json @@ -20,7 +20,7 @@ "harmonyhubjs-discover": "git+https://github.com/swissmanu/harmonyhubjs-discover.git", "mdns": "^2.2.4", "node-hue-api": "^1.0.5", - "node-icontrol": "^0.1.3", + "node-icontrol": "^0.1.4", "node-milight-promise": "0.0.x", "node-persist": "0.0.x", "request": "2.49.x", From 3cc1f381a393e1e960a4a013415f14a558e4356d Mon Sep 17 00:00:00 2001 From: Nick Farina Date: Sat, 5 Sep 2015 14:04:31 -0700 Subject: [PATCH 13/18] Fix for WeMo switch names --- accessories/WeMo.js | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/accessories/WeMo.js b/accessories/WeMo.js index d8e713d..26ef822 100644 --- a/accessories/WeMo.js +++ b/accessories/WeMo.js @@ -8,8 +8,9 @@ module.exports = { function WeMoAccessory(log, config) { this.log = log; + this.name = config["name"]; this.service = config["service"] || "Switch"; - this.wemoName = config["wemo_name"] || config["name"]; // fallback to "name" if you didn't specify an exact "wemo_name" + this.wemoName = config["wemo_name"] || this.name; // fallback to "name" if you didn't specify an exact "wemo_name" this.device = null; // instance of WeMo, for controlling the discovered device this.log("Searching for WeMo device with exact name '" + this.wemoName + "'..."); this.search(); @@ -33,7 +34,7 @@ WeMoAccessory.prototype.getPowerOn = function(callback) { if (!this.device) { this.log("No '%s' device found (yet?)", this.wemoName); - callback(new Error("Device not found")); + callback(new Error("Device not found"), false); return; } @@ -101,7 +102,7 @@ WeMoAccessory.prototype.setTargetDoorState = function(targetDoorState, callback) WeMoAccessory.prototype.getServices = function() { if (this.service == "Switch") { - var switchService = new Service.Switch("Switch"); + var switchService = new Service.Switch(this.name); switchService .getCharacteristic(Characteristic.On) @@ -115,7 +116,9 @@ WeMoAccessory.prototype.getServices = function() { garageDoorService .getCharacteristic(Characteristic.TargetDoorState) - .on('set', this.setTargetDoorState.bind(this)); + .on('set', this.setTargetDoorState.bind(this)) + .supportsEventNotification = false; + return [garageDoorService]; } From 94ef18c94d60e88bdd99a6a7563853a5cca5f657 Mon Sep 17 00:00:00 2001 From: Nick Farina Date: Sat, 5 Sep 2015 15:43:11 -0700 Subject: [PATCH 14/18] Bump HAP-NodeJS with fixes for WeMo --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 883ac7b..8da308c 100644 --- a/package.json +++ b/package.json @@ -15,7 +15,7 @@ "carwingsjs": "0.0.x", "color": "0.10.x", "elkington": "kevinohara80/elkington", - "hap-nodejs": "git+https://github.com/KhaosT/HAP-NodeJS#fff863d7a387636fc612cf27cb859e82d9ee3294", + "hap-nodejs": "git+https://github.com/KhaosT/HAP-NodeJS#6bf0f9eaaa2d87db8d1768114c61f4acbb095c41", "harmonyhubjs-client": "^1.1.4", "harmonyhubjs-discover": "git+https://github.com/swissmanu/harmonyhubjs-discover.git", "mdns": "^2.2.4", From 6f5e6b6a0b63b2faf8fa2aea6f364dcf2f0434f4 Mon Sep 17 00:00:00 2001 From: Snowdd1 Date: Sun, 6 Sep 2015 09:37:03 +0200 Subject: [PATCH 15/18] More devices for KNX bus Now supports - Lightbulb - Thermostat - TemperatureSensor (as of iOS 8.4.1) - LockMechanism --- accessories/knxdevice.js | 143 +++++++++++++++++++++++++++++++++++---- 1 file changed, 130 insertions(+), 13 deletions(-) diff --git a/accessories/knxdevice.js b/accessories/knxdevice.js index ffcb50e..efdbd4e 100644 --- a/accessories/knxdevice.js +++ b/accessories/knxdevice.js @@ -9,6 +9,7 @@ var knxd = require("eibd"); var knxd_registerGA = require('../platforms/KNX.js').registerGA; var knxd_startMonitor = require('../platforms/KNX.js').startMonitor; +var milliTimeout = 300; // used to block responses while swiping function KNXDevice(log, config) { @@ -164,7 +165,14 @@ KNXDevice.prototype = { characteristic.setValue(val ? 1 : 0, undefined, 'fromKNXBus'); }.bind(this)); }, - + knxregister_boolReverse: function(addresses, characteristic) { + this.log("knx registering BOOLEAN " + addresses); + knxd_registerGA(addresses, function(val, src, dest, type){ + this.log("Received value from bus:"+val+ " for " +dest+ " from "+src+" of type"+type + " for " + characteristic.displayName); +// iterate(characteristic); + characteristic.setValue(val ? 0 : 1, undefined, 'fromKNXBus'); + }.bind(this)); + }, // percentage: get 0..255 from the bus, write 0..100 to characteristic knxregister_percent: function(addresses, characteristic) { this.log("knx registering PERCENT " + addresses); @@ -173,7 +181,16 @@ KNXDevice.prototype = { if (type !== "DPT5") { this.log("[ERROR] Received value cannot be a percentage value"); } else { - characteristic.setValue(Math.round(val/255*100), undefined, 'fromKNXBus'); + if (!characteristic.timeout) { + if (characteristic.timeout < Date.now()) { + characteristic.setValue(Math.round(val/255*100), undefined, 'fromKNXBus'); + } else { + this.log("Blackout time"); + } + } else { + characteristic.setValue(Math.round(val/255*100), undefined, 'fromKNXBus'); + } // todo get the boolean logic right into one OR expresssion + } }.bind(this)); }, @@ -183,7 +200,13 @@ KNXDevice.prototype = { this.log("knx registering FLOAT " + addresses); knxd_registerGA(addresses, function(val, src, dest, type){ this.log("Received value from bus:"+val+ " for " +dest+ " from "+src+" of type"+type+ " for " + characteristic.displayName); - characteristic.setValue(val, undefined, 'fromKNXBus'); + var hk_value = Math.round(val*10)/10; + if (hk_value>=characteristic.minimumValue && hk_value<=characteristic.maximumValue) { + characteristic.setValue(hk_value, undefined, 'fromKNXBus'); // 1 decoimal for HomeKit + } else { + this.log("Value %s out of bounds %s...%s ",hk_value, characteristic.minimumValue, characteristic.maximumValue); + } + }.bind(this)); }, @@ -195,16 +218,16 @@ KNXDevice.prototype = { var HAPvalue = 0; switch (val){ case 0: - HAPvalue = 3; + HAPvalue = 1; break; case 1: - HAPvalue = 3; + HAPvalue = 1; break; case 2: - HAPvalue = 3; + HAPvalue = 1; break; case 3: - HAPvalue = 3; + HAPvalue = 1; break; case 4: HAPvalue = 0; @@ -262,7 +285,22 @@ KNXDevice.prototype = { } }, + setBooleanReverseState: function(value, callback, context, gaddress) { + if (context === 'fromKNXBus') { + this.log(gaddress + " event ping pong, exit!"); + if (callback) { + callback(); + } + } else { + var numericValue = 0; + if (!value) { + numericValue = 1; // need 0 or 1, not true or something + } + this.log("Setting "+gaddress+" Boolean to %s", numericValue); + this.knxwrite(callback, gaddress,'DPT1',numericValue); + } + }, setPercentage: function(value, callback, context, gaddress) { if (context === 'fromKNXBus') { @@ -289,7 +327,7 @@ KNXDevice.prototype = { } else { var numericValue = 0; if (value) { - numericValue = value; // need 0 or 1, not true or something + numericValue = value; // homekit expects precision of 1 decimal } this.log("Setting "+gaddress+" Float to %s", numericValue); this.knxwrite(callback, gaddress,'DPT9',numericValue); @@ -351,14 +389,18 @@ KNXDevice.prototype = { switch (valueType) { case "Bool": myCharacteristic.on('set', function(value, callback, context) { -// this.log("ITERATE DEBUG"); -// iterate(config); this.setBooleanState(value, callback, context, config.Set); }.bind(this)); break; + case "BoolReverse": + myCharacteristic.on('set', function(value, callback, context) { + this.setBooleanReverseState(value, callback, context, config.Set); + }.bind(this)); + break; case "Percent": myCharacteristic.on('set', function(value, callback, context) { this.setPercentage(value, callback, context, config.Set); + myCharacteristic.timeout = Date.now()+milliTimeout; }.bind(this)); break; case "Float": @@ -382,6 +424,9 @@ KNXDevice.prototype = { switch (valueType) { case "Bool": this.knxregister_bool([config.Set].concat(config.Listen || []), myCharacteristic); + break; + case "BoolReverse": + this.knxregister_boolReverse([config.Set].concat(config.Listen || []), myCharacteristic); break; case "Percent": this.knxregister_percent([config.Set].concat(config.Listen || []), myCharacteristic); @@ -414,7 +459,7 @@ KNXDevice.prototype = { this.log("[ERROR] Lightbulb Service without 'name' property called"); return undefined; } - var myService = new Service.Lightbulb() //(config.name,config.name); + var myService = new Service.Lightbulb(config.name,config.name); // On (and Off) if (config.On) { this.log("Lightbulb on/off characteristic enabled"); @@ -430,6 +475,45 @@ KNXDevice.prototype = { //iterate(myService); return myService; }, + + getLockMechanismService: function(config) { + // some sanity checks + //this.config = config; +// Characteristic.LockCurrentState.UNSECURED = 0; +// Characteristic.LockCurrentState.SECURED = 1; + + if (config.type !== "LockMechanism") { + this.log("[ERROR] LockMechanism Service for non 'LockMechanism' service called"); + return undefined; + } + if (!config.name) { + this.log("[ERROR] LockMechanism Service without 'name' property called"); + return undefined; + } + var myService = new Service.LockMechanism(config.name,config.name); + // LockCurrentState + if (config.LockCurrentState) { + // for normal contacts: Secured = 1 + this.log("LockMechanism LockCurrentState characteristic enabled"); + this.bindCharacteristic(myService, Characteristic.LockCurrentState, "Bool", config.LockCurrentState); + } else if (config.LockCurrentStateSecured0) { + // for reverse contacts Secured = 0 + this.log("LockMechanism LockCurrentState characteristic enabled"); + this.bindCharacteristic(myService, Characteristic.LockCurrentState, "BoolReverse", config.LockCurrentStateSecured0); + } + // LockTargetState + if (config.LockTargetState) { + this.log("LockMechanism LockTargetState characteristic enabled"); + this.bindCharacteristic(myService, Characteristic.LockTargetState, "Bool", config.LockTargetState); + } else if (config.LockTargetStateSecured0) { + this.log("LockMechanism LockTargetState characteristic enabled"); + this.bindCharacteristic(myService, Characteristic.LockTargetState, "BoolReverse", config.LockTargetStateSecured0); + } + + //iterate(myService); + return myService; + }, + getThermostatService: function(config) { @@ -459,15 +543,18 @@ KNXDevice.prototype = { this.log("[ERROR] Thermostat Service without 'name' property called"); return undefined; } - var myService = new Service.Thermostat() //(config.name,config.name); + var myService = new Service.Thermostat(config.name,config.name); // CurrentTemperature) if (config.CurrentTemperature) { this.log("Thermostat CurrentTemperature characteristic enabled"); this.bindCharacteristic(myService, Characteristic.CurrentTemperature, "Float", config.CurrentTemperature); } - // TargetTemperature if available + // TargetTemperature if available if (config.TargetTemperature) { this.log("Thermostat TargetTemperature characteristic enabled"); + // default boundary too narrow for thermostats + myService.getCharacteristic(Characteristic.TargetTemperature).minimumValue=0; // °C + myService.getCharacteristic(Characteristic.TargetTemperature).maximumValue=40; // °C this.bindCharacteristic(myService, Characteristic.TargetTemperature, "Float", config.TargetTemperature); } // HVAC missing yet @@ -477,9 +564,33 @@ KNXDevice.prototype = { } return myService; }, + + // temperature sensor type (iOS9 assumed) + getTemperatureSensorService: function(config) { + // some sanity checks + + + if (config.type !== "TemperatureSensor") { + this.log("[ERROR] TemperatureSensor Service for non 'TemperatureSensor' service called"); + return undefined; + } + if (!config.name) { + this.log("[ERROR] TemperatureSensor Service without 'name' property called"); + return undefined; + } + var myService = new Service.TemperatureSensor(config.name,config.name); + // CurrentTemperature) + if (config.CurrentTemperature) { + this.log("Thermostat CurrentTemperature characteristic enabled"); + this.bindCharacteristic(myService, Characteristic.CurrentTemperature, "Float", config.CurrentTemperature); + } + return myService; + }, + + /* assemble the device ***************************************************************************************************/ @@ -518,6 +629,12 @@ KNXDevice.prototype = { case "Lightbulb": accessoryServices.push(this.getLightbulbService(configService)); break; + case "LockMechanism": + accessoryServices.push(this.getLockMechanismService(configService)); + break; + case "TemperatureSensor": + accessoryServices.push(this.getTemperatureSensorService(configService)); + break; case "Thermostat": accessoryServices.push(this.getThermostatService(configService)); break; From 86d548b8d9621339d56329acc4906bd07407963a Mon Sep 17 00:00:00 2001 From: Snowdd1 Date: Sun, 6 Sep 2015 09:39:51 +0200 Subject: [PATCH 16/18] Resolve package.json conflict Merged WEMO update --- package.json | 4 ---- 1 file changed, 4 deletions(-) diff --git a/package.json b/package.json index c4dcfcb..217e71b 100644 --- a/package.json +++ b/package.json @@ -15,12 +15,8 @@ "carwingsjs": "0.0.x", "color": "0.10.x", "elkington": "kevinohara80/elkington", -<<<<<<< HEAD "eibd": "0.3.1", - "hap-nodejs": "git+https://github.com/KhaosT/HAP-NodeJS#fff863d7a387636fc612cf27cb859e82d9ee3294", -======= "hap-nodejs": "git+https://github.com/KhaosT/HAP-NodeJS#6bf0f9eaaa2d87db8d1768114c61f4acbb095c41", ->>>>>>> nfarina/master "harmonyhubjs-client": "^1.1.4", "harmonyhubjs-discover": "git+https://github.com/swissmanu/harmonyhubjs-discover.git", "mdns": "^2.2.4", From 09516acaf3ed112a6c4d2802533a74bd3db0bc93 Mon Sep 17 00:00:00 2001 From: Snowdd1 Date: Sun, 6 Sep 2015 11:11:58 +0200 Subject: [PATCH 17/18] package.json update eibd dependency to include future versions (^0.3.1) --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 217e71b..9fa4cb5 100644 --- a/package.json +++ b/package.json @@ -15,7 +15,7 @@ "carwingsjs": "0.0.x", "color": "0.10.x", "elkington": "kevinohara80/elkington", - "eibd": "0.3.1", + "eibd": "^0.3.1", "hap-nodejs": "git+https://github.com/KhaosT/HAP-NodeJS#6bf0f9eaaa2d87db8d1768114c61f4acbb095c41", "harmonyhubjs-client": "^1.1.4", "harmonyhubjs-discover": "git+https://github.com/swissmanu/harmonyhubjs-discover.git", From 9d8afc4bcb73e6b52a3855249b91b3f8656a71d0 Mon Sep 17 00:00:00 2001 From: Snowdd1 Date: Sun, 6 Sep 2015 11:41:33 +0200 Subject: [PATCH 18/18] cleanup of sample.json and platform shim removed non-existent old device types from platform shim. --- config-sample-knx.json | 205 ++++++++++++++++++++++++----------------- platforms/KNX.js | 18 ---- 2 files changed, 119 insertions(+), 104 deletions(-) diff --git a/config-sample-knx.json b/config-sample-knx.json index 8e7d11e..df0de60 100644 --- a/config-sample-knx.json +++ b/config-sample-knx.json @@ -1,88 +1,121 @@ { - "bridge": { - "name": "Homebridge", - "username": "CC:22:3D:E3:CE:30", - "port": 51826, - "pin": "031-45-154" - }, - "description": "This is an example configuration file for KNX platform shim", - "platforms": - [ - { - "platform": "KNX", - "name": "KNX", - "knxd_ip": "192.168.178.205", - "knxd_port": 6720, - "accessories": - [ - { - "accessory_type": "knxdevice", - "name": "Living Room North Lamp", - "services": - [ - { - "type": "Lightbulb", - "name": "Living Room North Lamp", - "On": { - "Set": "1/1/6", - "Listen": [ - "1/1/63" - ] - }, - "Brightness": { - "Set": "1/1/62", - "Listen": [ - "1/1/64" - ] - } - } - ] - }, - { - "accessory_type": "knxdevice", - "name": "Office", - "services": - [ - { - "type": "Lightbulb", - "name": "Office Lamp", - "On": { - "Set": "1/3/5" - } - }, - { - "type": "Thermostat", - "name": "Raumtemperatur", - "CurrentTemperature": { - "Listen": "3/3/44" - }, - "TargetTemperature": { - "Set": "3/3/94" - }, - "CurrentHeatingCoolingState": { - "Listen": "3/3/64" - } - }, - { - "type": "WindowCovering", - "name": "Rollo", - "Target": { - "Set": "address", - "Listen": "adresses" - }, - "Current": { - "Set": "address", - "Listen": "adresses" - }, - "PositionState": { - "Listen": "adresses" - } - } - ] - } - ] - } - ], - "accessories": [], - "prototyping": {} + "bridge": { + "name": "Homebridge", + "username": "CC:22:3D:E3:CE:30", + "port": 51826, + "pin": "031-45-154" + }, + "description": "This is an example configuration file for KNX platform shim", + "hint": "Always paste into jsonlint.com validation page before starting your homebridge, saves a lot of frustration", + "platforms": [ + { + "platform": "KNX", + "name": "KNX", + "knxd_ip": "192.168.178.205", + "knxd_port": 6720, + "accessories": [ + { + "accessory_type": "knxdevice", + "description": "Only generic type knxdevice is supported, all previous knx type have been merged into that.", + "name": "Living Room North Lamp", + "services": [ + { + "type": "Lightbulb", + "description": "iOS8 Lightbulb type, supports On (Switch) and Brightness", + "name": "Living Room North Lamp", + "On": { + "Set": "1/1/6", + "Listen": [ + "1/1/63" + ] + }, + "Brightness": { + "Set": "1/1/62", + "Listen": [ + "1/1/64" + ] + } + } + ], + "services-description": "Services is an array, you CAN have multiple service types in one accessory, though it is not fully supported in many iOS HK apps, such as EVE and myTouchHome" + }, + { + "accessory_type": "knxdevice", + "name": "Office Temperature", + "description": "iOS8.4.1 TemperatureSensor type, supports CurrentTemperature", + "services": [ + { + "type": "TemperatureSensor", + "name": "Raumtemperatur", + "CurrentTemperature": { + "Listen": "3/3/44" + } + } + ] + }, + { + "accessory_type": "knxdevice", + "name": "Office Window Lock", + "services": [ + { + "type": "LockMechanism", + "description": "iOS8 Lock mechanism, Supports LockCurrentStateSecured0 OR LockCurrentState, LockTargetStateSecured0 OR LockTargetState, use depending if LOCKED is 0 or 1", + "name": "Office Window Lock", + "LockCurrentStateSecured0": { + "Listen": "5/3/15" + }, + "LockTargetStateSecured0": { + "Listen": "5/3/15" + } + } + ] + }, + { + "accessory_type": "knxdevice", + "description":"sample device with multiple services. Multiple services of different types are widely supported" + "name": "Office", + "services": [ + { + "type": "Lightbulb", + "name": "Office Lamp", + "On": { + "Set": "1/3/5" + } + }, + { + "type": "Thermostat", + "description": "iOS8 Thermostat type, supports CurrentTemperature, TargetTemperature, CurrentHeatingCoolingState ", + "name": "Raumtemperatur", + "CurrentTemperature": { + "Listen": "3/3/44" + }, + "TargetTemperature": { + "Set": "3/3/94" + }, + "CurrentHeatingCoolingState": { + "Listen": "3/3/64" + } + }, + { + "type": "WindowCovering", + "description": "iOS9 Window covering (blinds etc) type, still WIP", + "name": "Blinds", + "Target": { + "Set": "address", + "Listen": "adresses" + }, + "Current": { + "Set": "address", + "Listen": "adresses" + }, + "PositionState": { + "Listen": "adresses" + } + } + ] + } + ] + } + ], + "accessories": [] } \ No newline at end of file diff --git a/platforms/KNX.js b/platforms/KNX.js index 7070137..573b3b9 100644 --- a/platforms/KNX.js +++ b/platforms/KNX.js @@ -47,24 +47,6 @@ KNXPlatform.prototype = { this.log("created "+acc.name+" universal accessory"); myAccessories.push(acc); break; - case "knxlamp": - this.log("push new lamp with "+foundAccessories[int].name ); - foundAccessories[int].knxd_ip = this.config.knxd_ip; - foundAccessories[int].knxd_port = this.config.knxd_port; - var accConstructor = require('./../accessories/knxlamp.js'); - var acc = new accConstructor.accessory(this.log,foundAccessories[int]); - this.log("created "+acc.name+" accessory"); - myAccessories.push(acc); - break; - case "knxthermostat": - this.log("push new thermostat with "+foundAccessories[int].name); - foundAccessories[int].knxd_ip = this.config.knxd_ip; - foundAccessories[int].knxd_port = this.config.knxd_port; - var accConstructor = require('./../accessories/knxthermostat.js'); - var acc = new accConstructor.accessory(this.log,foundAccessories[int]); - this.log("created "+acc.name+" accessory"); - myAccessories.push(acc); - break; default: // do something else this.log("unkown accessory type found")