From 0da4fe5d224f4903f7ad85b7f10cb65a651ef0f2 Mon Sep 17 00:00:00 2001 From: Snowdd1 Date: Wed, 16 Sep 2015 20:05:02 +0200 Subject: [PATCH 1/9] Welcome iOS9 More services, documentation, and cleanups. --- accessories/knxdevice.js | 210 +++++++++++++++++++++++++++++++++++++-- accessories/knxdevice.md | 126 +++++++++++++++++++++++ config-sample-knx.json | 29 ++++-- 3 files changed, 347 insertions(+), 18 deletions(-) create mode 100644 accessories/knxdevice.md diff --git a/accessories/knxdevice.js b/accessories/knxdevice.js index efdbd4e..824b9dd 100644 --- a/accessories/knxdevice.js +++ b/accessories/knxdevice.js @@ -1,6 +1,13 @@ /* * This is a KNX universal accessory shim. + * This is NOT the version for dynamic installation * +New 2015-09-16: Welcome iOS9.0 +new features includ: +services: +Window +WindowCovering +ContactSensor * */ var Service = require("HAP-NodeJS").Service; @@ -23,7 +30,7 @@ function KNXDevice(log, config) { if (config.knxd_ip){ this.knxd_ip = config.knxd_ip; } else { - throw new Error("MISSING KNXD IP"); + throw new Error("KNX configuration fault: MISSING KNXD IP"); } if (config.knxd_port){ this.knxd_port = config.knxd_port; @@ -87,7 +94,7 @@ KNXDevice.prototype = { this.log("[ERROR] knxwrite:sendAPDU: " + err); callback(err); } else { - // this.log("knx data sent"); + this.log("knx data sent: Value "+value+ " for GA "+groupAddress); callback(); } }.bind(this)); @@ -160,7 +167,7 @@ KNXDevice.prototype = { 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); + 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)); @@ -168,7 +175,7 @@ KNXDevice.prototype = { 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); + 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)); @@ -177,7 +184,7 @@ KNXDevice.prototype = { 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); + 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 { @@ -199,7 +206,7 @@ KNXDevice.prototype = { 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); + this.log("Received value from bus:"+val+ " for " +dest+ " from "+src+" of type "+type+ " for " + characteristic.displayName); 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 @@ -214,7 +221,7 @@ KNXDevice.prototype = { 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); + this.log("Received value from bus:"+val+ " for " +dest+ " from "+src+" of type "+type+ " for " + characteristic.displayName); var HAPvalue = 0; switch (val){ case 0: @@ -256,7 +263,7 @@ KNXDevice.prototype = { 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); + this.log("Received value from bus:"+val+ " for " +dest+ " from "+src+" of type "+type+ " for " + characteristic.displayName); characteristic.setValue(val, undefined, 'fromKNXBus'); }.bind(this)); }, @@ -552,16 +559,26 @@ KNXDevice.prototype = { // TargetTemperature if available if (config.TargetTemperature) { this.log("Thermostat TargetTemperature characteristic enabled"); + + // DEBUG + console.log("default value: " + myService.getCharacteristic(Characteristic.TargetTemperature).value); + // DEBUG + // 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 + // HVAC if (config.CurrentHeatingCoolingState) { this.log("Thermostat CurrentHeatingCoolingState characteristic enabled"); this.bindCharacteristic(myService, Characteristic.CurrentHeatingCoolingState, "HVAC", config.CurrentHeatingCoolingState); } + // HVAC + if (config.TargetHeatingCoolingState) { + this.log("Thermostat TargetHeatingCoolingState characteristic enabled"); + this.bindCharacteristic(myService, Characteristic.TargetHeatingCoolingState, "HVAC", config.TargetHeatingCoolingState); + } return myService; }, @@ -584,13 +601,174 @@ KNXDevice.prototype = { var myService = new Service.TemperatureSensor(config.name,config.name); // CurrentTemperature) if (config.CurrentTemperature) { - this.log("Thermostat CurrentTemperature characteristic enabled"); + this.log("TemperatureSensor CurrentTemperature characteristic enabled"); this.bindCharacteristic(myService, Characteristic.CurrentTemperature, "Float", config.CurrentTemperature); } return myService; }, + + // window type (iOS9 assumed) + getWindowService: function(config) { +// Service.Window = function(displayName, subtype) { +// Service.call(this, displayName, '0000008B-0000-1000-8000-0026BB765291', subtype); +// +// // Required Characteristics +// this.addCharacteristic(Characteristic.CurrentPosition); +// this.addCharacteristic(Characteristic.TargetPosition); +// this.addCharacteristic(Characteristic.PositionState); +// +// // Optional Characteristics +// this.addOptionalCharacteristic(Characteristic.HoldPosition); +// this.addOptionalCharacteristic(Characteristic.ObstructionDetected); +// this.addOptionalCharacteristic(Characteristic.Name); + + // Characteristic.PositionState.DECREASING = 0; +// Characteristic.PositionState.INCREASING = 1; +// Characteristic.PositionState.STOPPED = 2; + + + // some sanity checks + + + if (config.type !== "Window") { + this.log("[ERROR] Window Service for non 'Window' service called"); + return undefined; + } + if (!config.name) { + this.log("[ERROR] Window Service without 'name' property called"); + return undefined; + } + var myService = new Service.Window(config.name,config.name); + + if (config.CurrentPosition) { + this.log("Window CurrentPosition characteristic enabled"); + this.bindCharacteristic(myService, Characteristic.CurrentPosition, "Percent", config.CurrentPosition); + } + if (config.TargetPosition) { + this.log("Window TargetPosition characteristic enabled"); + this.bindCharacteristic(myService, Characteristic.TargetPosition, "Percent", config.TargetPosition); + } + if (config.PositionState) { + this.log("Window PositionState characteristic enabled"); + this.bindCharacteristic(myService, Characteristic.PositionState, "Float", config.PositionState); + } + return myService; + }, + + +// /** +// * Service "Window Covering" +// */ +// +// Service.WindowCovering = function(displayName, subtype) { +// Service.call(this, displayName, '0000008C-0000-1000-8000-0026BB765291', subtype); +// +// // Required Characteristics +// this.addCharacteristic(Characteristic.CurrentPosition); +// this.addCharacteristic(Characteristic.TargetPosition); +// this.addCharacteristic(Characteristic.PositionState); +// +// // Optional Characteristics +// this.addOptionalCharacteristic(Characteristic.HoldPosition); +// this.addOptionalCharacteristic(Characteristic.TargetHorizontalTiltAngle); +// this.addOptionalCharacteristic(Characteristic.TargetVerticalTiltAngle); +// this.addOptionalCharacteristic(Characteristic.CurrentHorizontalTiltAngle); +// this.addOptionalCharacteristic(Characteristic.CurrentVerticalTiltAngle); +// this.addOptionalCharacteristic(Characteristic.ObstructionDetected); +// this.addOptionalCharacteristic(Characteristic.Name); +// }; + getWindowCoveringService: function(config) { + + // some sanity checks + + + if (config.type !== "WindowCovering") { + this.log("[ERROR] WindowCovering Service for non 'WindowCovering' service called"); + return undefined; + } + if (!config.name) { + this.log("[ERROR] WindowCovering Service without 'name' property called"); + return undefined; + } + var myService = new Service.WindowCovering(config.name,config.name); + + if (config.CurrentPosition) { + this.log("WindowCovering CurrentPosition characteristic enabled"); + this.bindCharacteristic(myService, Characteristic.CurrentPosition, "Percent", config.CurrentPosition); + } + if (config.TargetPosition) { + this.log("WindowCovering TargetPosition characteristic enabled"); + this.bindCharacteristic(myService, Characteristic.TargetPosition, "Percent", config.TargetPosition); + } + if (config.PositionState) { + this.log("WindowCovering PositionState characteristic enabled"); + this.bindCharacteristic(myService, Characteristic.PositionState, "Float", config.PositionState); + } + return myService; + }, + +// Service.ContactSensor = function(displayName, subtype) { +// Service.call(this, displayName, '00000080-0000-1000-8000-0026BB765291', subtype); +// +// // Required Characteristics +// this.addCharacteristic(Characteristic.ContactSensorState); +// +// // Optional Characteristics +// this.addOptionalCharacteristic(Characteristic.StatusActive); +// this.addOptionalCharacteristic(Characteristic.StatusFault); +// this.addOptionalCharacteristic(Characteristic.StatusTampered); +// this.addOptionalCharacteristic(Characteristic.StatusLowBattery); +// this.addOptionalCharacteristic(Characteristic.Name); +// }; +// Characteristic.ContactSensorState.CONTACT_DETECTED = 0; +// Characteristic.ContactSensorState.CONTACT_NOT_DETECTED = 1; + getContactSenserService: function(config) { + // some sanity checks + if (config.type !== "ContactSensor") { + this.log("[ERROR] ContactSensor Service for non 'ContactSensor' service called"); + return undefined; + } + if (!config.name) { + this.log("[ERROR] ContactSensor Service without 'name' property called"); + return undefined; + } + var myService = new Service.ContactSensor(config.name,config.name); + + if (config.ContactSensorState) { + this.log("ContactSensor ContactSensorState characteristic enabled"); + this.bindCharacteristic(myService, Characteristic.ContactSensorState, "Bool", config.ContactSensorState); + } else if (config.ContactSensorStateContact1) { + this.log("ContactSensor ContactSensorStateContact1 characteristic enabled"); + this.bindCharacteristic(myService, Characteristic.ContactSensorState, "BoolReverse", config.ContactSensorStateContact1); + } + //optionals + if (config.StatusActive) { + this.log("ContactSensor StatusActive characteristic enabled"); + myService.addCharacteristic(Characteristic.StatusActive); + this.bindCharacteristic(myService, Characteristic.StatusActive, "Bool", config.StatusActive); + } + if (config.StatusFault) { + this.log("ContactSensor StatusFault characteristic enabled"); + myService.addCharacteristic(Characteristic.StatusFault); + this.bindCharacteristic(myService, Characteristic.StatusFault, "Bool", config.StatusFault); + } + if (config.StatusTampered) { + this.log("ContactSensor StatusTampered characteristic enabled"); + myService.addCharacteristic(Characteristic.StatusTampered); + this.bindCharacteristic(myService, Characteristic.StatusTampered, "Bool", config.StatusTampered); + } + if (config.StatusLowBattery) { + this.log("ContactSensor StatusLowBattery characteristic enabled"); + myService.addCharacteristic(Characteristic.StatusLowBattery); + this.bindCharacteristic(myService, Characteristic.StatusLowBattery, "Bool", config.StatusLowBattery); + } + return myService; + }, + + + /* assemble the device ***************************************************************************************************/ @@ -625,7 +803,11 @@ KNXDevice.prototype = { 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"); } + this.log("Preparing Service: " + int + " of type "+configService.type) switch (configService.type) { + case "ContactSensor": + accessoryServices.push(this.getContactSenserService(configService)); + break; case "Lightbulb": accessoryServices.push(this.getLightbulbService(configService)); break; @@ -638,8 +820,14 @@ KNXDevice.prototype = { case "Thermostat": accessoryServices.push(this.getThermostatService(configService)); break; + case "Window": + accessoryServices.push(this.getWindowService(configService)); + break; + case "WindowCovering": + accessoryServices.push(this.getWindowCoveringService(configService)); + break; default: - this.log("[ERROR] unknown 'type' property for service "+ configService.name + " in config.json. KNX platform section fault "); + this.log("[ERROR] unknown 'type' property of '"+configService.type+"' 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 "); } } diff --git a/accessories/knxdevice.md b/accessories/knxdevice.md new file mode 100644 index 0000000..c9131cc --- /dev/null +++ b/accessories/knxdevice.md @@ -0,0 +1,126 @@ +# Syntax of the config.json +In the platforms section, you can insert a KNX type platform. +You need to configure all devices directly in the config.json. + + "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", + "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" + ] + } + } + ] + } + } + +In the accessories section (the array within the brackets [ ]) you can insert as many objects as you like in the following form + { + "accessory_type": "knxdevice", + "name": "Here goes your display name, this will be shown in HomeKit apps", + "services": [ + { + } + ] + } + +You have to add services in the following syntax: + { + "type": "SERVICENAME", + "description": "This is just for you to remember things", + "name": "We need a name for each service, though it usually shows only if multiple services are present in one accessory", + "CHARACTERISTIC1": { + "Set": "1/1/6", + "Listen": [ + "1/1/63" + ] + }, + "CHARACTERISTIC2": { + "Set": "1/1/62", + "Listen": [ + "1/1/64" + ] + } + } +CHARACTERISTIC are properties that are dependent on the service type, so they are listed below. +Two kinds of addresses are supported: "Set":"1/2/3" is a writable group address, to which changes are sent if the service supports changing values. Changes on the bus are listened to, too. +"Listen":["1/2/3","1/2/4","1/2/5"] is an array of addresses that are listened to additionally. To these addresses never values get written, but the on startup the service will issue read requests to ALL addresses listed in Set: and in Listen: + + +# Supported Services and their characteristics + +## Lightbulb + On: DPT 1, 1 as on, 0 as off + Brightness: DPT5 percentage, 100% (=255) the brightest + + +## LockMechanism +LockCurrentState: DPT 1, 1 as secured +OR (but not both:) +LockCurrentStateSecured0: DPT 1, 0 as secured + +LockTargetState: DPT 1, 1 as secured +LockTargetStateSecured0: DPT 1, 0 as secured + +## Thermostat +CurrentTemperature: DPT9 in C [listen only] +TargetTemperature: DPT9, values 0..40C only, all others are ignored +CurrentHeatingCoolingState: DPT5 HVAC, because of the incompatible mapping only off and heating (=auto) are shown, [listen only] +TargetHeatingCoolingState: as above + + +## TemperatureSensor +CurrentTemperature: DPT9 in C [listen only] + +## Window +CurrentPosition: DPT5 percentage +TargetPosition: DPT5 percentage +PositionState: DPT5 value [listen only] + +## WindowCovering +CurrentPosition: DPT5 percentage +TargetPosition: DPT5 percentage +PositionState: DPT5 value [listen only] + +### not yet supported +HoldPosition +TargetHorizontalTiltAngle +TargetVerticalTiltAngle +CurrentHorizontalTiltAngle +CurrentVerticalTiltAngle +ObstructionDetected + +## ContactSensor +ContactSensorState: DPT 1, 0 as contact +OR +ContactSensorStateContact1: DPT 1, 1 as contact + +StatusActive: DPT 1, 1 as true +StatusFault: DPT 1, 1 as true +StatusTampered: DPT 1, 1 as true +StatusLowBattery: DPT 1, 1 as true + + +# DISCLAIMER +This is work in progress! + diff --git a/config-sample-knx.json b/config-sample-knx.json index a8e52b1..d74a14f 100644 --- a/config-sample-knx.json +++ b/config-sample-knx.json @@ -7,6 +7,8 @@ }, "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", + "hint2":"Replace all group addresses by current addresses of your installation, these are arbitrary examples!", + "hint3":"For valid services and their characteristics have a look at the knxdevice.md file in folder accessories!" "platforms": [ { "platform": "KNX", @@ -16,7 +18,7 @@ "accessories": [ { "accessory_type": "knxdevice", - "description": "Only generic type knxdevice is supported, all previous knx type have been merged into that.", + "description": "Only generic type knxdevice is supported, all previous knx types have been merged into that.", "name": "Living Room North Lamp", "services": [ { @@ -101,19 +103,32 @@ "description": "iOS9 Window covering (blinds etc) type, still WIP", "name": "Blinds", "Target": { - "Set": "address", - "Listen": "adresses" + "Set": "1/2/3", + "Listen": "1/2/4" }, "Current": { - "Set": "address", - "Listen": "adresses" + "Set": "1/3/1", + "Listen": "1/3/2" }, "PositionState": { - "Listen": "adresses" + "Listen": "2/7/1" } } ] - } + },{ + "accessory_type": "knxdevice", + + "description":"sample contact sensor device", + "name": "Office", + "services": [ + { + "type": "ContactSensor", + "name": "Office Door", + "ContactSensorState": { + "Listen": "5/3/5" + } + }, + ] } ], From ace364644e25b8dda0bd95a1a1678bada58ccae1 Mon Sep 17 00:00:00 2001 From: Snowdd1 Date: Wed, 16 Sep 2015 21:22:49 +0200 Subject: [PATCH 2/9] MOve config-sample-knx.json to platforms/ --- config-sample-knx.json => platforms/config-sample-knx.json | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename config-sample-knx.json => platforms/config-sample-knx.json (100%) diff --git a/config-sample-knx.json b/platforms/config-sample-knx.json similarity index 100% rename from config-sample-knx.json rename to platforms/config-sample-knx.json From aa823baa8ec6e76ca7aec723f0f5e69018567689 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Devran=20=C3=9Cnal?= Date: Thu, 17 Sep 2015 02:03:53 +0200 Subject: [PATCH 3/9] Add shim for devices with serial connection (video projectors, screens, receivers, ..) --- accessories/GenericRS232Device.js | 126 ++++++++++++++++++++++++++++++ config-sample.json | 13 ++- package.json | 1 + 3 files changed, 139 insertions(+), 1 deletion(-) create mode 100644 accessories/GenericRS232Device.js diff --git a/accessories/GenericRS232Device.js b/accessories/GenericRS232Device.js new file mode 100644 index 0000000..3b9ca36 --- /dev/null +++ b/accessories/GenericRS232Device.js @@ -0,0 +1,126 @@ +var types = require("HAP-NodeJS/accessories/types.js"); +var SerialPort = require("serialport").SerialPort; + +module.exports = { + accessory: GenericRS232DeviceAccessory +} + +function GenericRS232DeviceAccessory(log, config) { + this.log = log; + this.id = config["id"]; + this.name = config["name"]; + this.model_name = config["model_name"]; + this.manufacturer = config["manufacturer"]; + this.on_command = config["on_command"]; + this.off_command = config["off_command"]; + this.device = config["device"]; + this.baudrate = config["baudrate"]; +} + +GenericRS232DeviceAccessory.prototype = { + getServices: function() { + var that = this; + return [ + { + sType: types.ACCESSORY_INFORMATION_STYPE, + characteristics: [ + { + cType: types.NAME_CTYPE, + onUpdate: null, + perms: ["pr"], + format: "string", + initialValue: that.name, + supportEvents: false, + supportBonjour: false, + manfDescription: "Name of the accessory", + designedMaxLength: 255 + }, + { + cType: types.MANUFACTURER_CTYPE, + onUpdate: null, + perms: ["pr"], + format: "string", + initialValue: that.manufacturer, + supportEvents: false, + supportBonjour: false, + manfDescription: "Manufacturer", + designedMaxLength: 255 + }, + { + cType: types.MODEL_CTYPE, + onUpdate: null, + perms: ["pr"], + format: "string", + initialValue: that.model_name, + supportEvents: false, + supportBonjour: false, + manfDescription: "Model", + designedMaxLength: 255 + }, + { + cType: types.SERIAL_NUMBER_CTYPE, + onUpdate: null, + perms: ["pr"], + format: "string", + initialValue: that.id, + 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.serviceName, + supportEvents: false, + supportBonjour: false, + manfDescription: "Name of service", + designedMaxLength: 255 + }, + { + cType: types.POWER_STATE_CTYPE, + onUpdate: function(value) { + var command = (value == 1 ? that.on_command : that.off_command); + var serialPort = new SerialPort(that.device, { baudrate: that.baudrate }); + serialPort.on("open", function () { + serialPort.write(command, function(error, results) { + if(error) { + console.log('Errors ' + err); + } + }); + }); + + }, + perms: ["pw","pr","ev"], + format: "bool", + initialValue: false, + supportEvents: false, + supportBonjour: false, + manfDescription: "Set the Power state", + designedMaxLength: 1 + } + ] + } + ] + } +} + +module.exports.accessory = GenericRS232DeviceAccessory; \ No newline at end of file diff --git a/config-sample.json b/config-sample.json index 49fc923..6f24554 100644 --- a/config-sample.json +++ b/config-sample.json @@ -205,7 +205,18 @@ "window_seconds": 5, "sensor_type": "m", "inverse": false + }, + { + "accessory": "GenericRS232Device", + "name": "Projector", + "description": "Make sure you set a 'Siri-Name' for your iOS-Device (example: 'Home Cinema') otherwise it might not work.", + "id": "TYDYMU044UVNP", + "baudrate": 9600, + "device": "/dev/tty.usbserial", + "manufacturer": "Acer", + "model_name": "H6510BD", + "on_command": "* 0 IR 001\r", + "off_command": "* 0 IR 002\r" } - ] } diff --git a/package.json b/package.json index d0eb41c..f8b7225 100644 --- a/package.json +++ b/package.json @@ -30,6 +30,7 @@ "q": "1.4.x", "tough-cookie": "^2.0.0", "request": "2.49.x", + "serialport": "^1.7.4", "sonos": "0.8.x", "telldus-live": "0.2.x", "teslams": "1.0.1", From 65ec517fd89f1ce95bd04d379ee668149627e0cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Devran=20=C3=9Cnal?= Date: Thu, 17 Sep 2015 11:37:20 +0200 Subject: [PATCH 4/9] Update to modern API and return error messages on failed connections --- accessories/GenericRS232Device.js | 150 ++++++++---------------------- 1 file changed, 41 insertions(+), 109 deletions(-) diff --git a/accessories/GenericRS232Device.js b/accessories/GenericRS232Device.js index 3b9ca36..3bdfc18 100644 --- a/accessories/GenericRS232Device.js +++ b/accessories/GenericRS232Device.js @@ -1,4 +1,5 @@ -var types = require("HAP-NodeJS/accessories/types.js"); +var Service = require("HAP-NodeJS").Service; +var Characteristic = require("HAP-NodeJS").Characteristic; var SerialPort = require("serialport").SerialPort; module.exports = { @@ -6,120 +7,51 @@ module.exports = { } function GenericRS232DeviceAccessory(log, config) { - this.log = log; - this.id = config["id"]; - this.name = config["name"]; - this.model_name = config["model_name"]; + this.log = log; + this.id = config["id"]; + this.name = config["name"]; + this.model_name = config["model_name"]; this.manufacturer = config["manufacturer"]; - this.on_command = config["on_command"]; - this.off_command = config["off_command"]; - this.device = config["device"]; - this.baudrate = config["baudrate"]; + this.on_command = config["on_command"]; + this.off_command = config["off_command"]; + this.device = config["device"]; + this.baudrate = config["baudrate"]; } GenericRS232DeviceAccessory.prototype = { - getServices: function() { - var that = this; - return [ - { - sType: types.ACCESSORY_INFORMATION_STYPE, - characteristics: [ - { - cType: types.NAME_CTYPE, - onUpdate: null, - perms: ["pr"], - format: "string", - initialValue: that.name, - supportEvents: false, - supportBonjour: false, - manfDescription: "Name of the accessory", - designedMaxLength: 255 - }, - { - cType: types.MANUFACTURER_CTYPE, - onUpdate: null, - perms: ["pr"], - format: "string", - initialValue: that.manufacturer, - supportEvents: false, - supportBonjour: false, - manfDescription: "Manufacturer", - designedMaxLength: 255 - }, - { - cType: types.MODEL_CTYPE, - onUpdate: null, - perms: ["pr"], - format: "string", - initialValue: that.model_name, - supportEvents: false, - supportBonjour: false, - manfDescription: "Model", - designedMaxLength: 255 - }, - { - cType: types.SERIAL_NUMBER_CTYPE, - onUpdate: null, - perms: ["pr"], - format: "string", - initialValue: that.id, - 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 + setPowerState: function(powerOn, callback) { + var that = this; + var command = powerOn ? that.on_command : that.off_command; + var serialPort = new SerialPort(that.device, { baudrate: that.baudrate }, false); + serialPort.open(function (error) { + if (error) { + callback(new Error('Can not communicate with ' + that.name + " (" + error + ")")) + } else { + serialPort.write(command, function(err, results) { + if (error) { + callback(new Error('Can not send power command to ' + that.name + " (" + err + ")")) + } else { + callback() } - ] - }, - { - sType: types.SWITCH_STYPE, - characteristics: [ - { - cType: types.NAME_CTYPE, - onUpdate: null, - perms: ["pr"], - format: "string", - initialValue: this.serviceName, - supportEvents: false, - supportBonjour: false, - manfDescription: "Name of service", - designedMaxLength: 255 - }, - { - cType: types.POWER_STATE_CTYPE, - onUpdate: function(value) { - var command = (value == 1 ? that.on_command : that.off_command); - var serialPort = new SerialPort(that.device, { baudrate: that.baudrate }); - serialPort.on("open", function () { - serialPort.write(command, function(error, results) { - if(error) { - console.log('Errors ' + err); - } - }); - }); - - }, - perms: ["pw","pr","ev"], - format: "bool", - initialValue: false, - supportEvents: false, - supportBonjour: false, - manfDescription: "Set the Power state", - designedMaxLength: 1 - } - ] + }); } - ] + }); + }, + + getServices: function() { + var switchService = new Service.Switch(); + var informationService = new Service.AccessoryInformation(); + + informationService + .setCharacteristic(Characteristic.Manufacturer, this.manufacturer) + .setCharacteristic(Characteristic.Model, this.model_name) + .setCharacteristic(Characteristic.SerialNumber, this.id); + + switchService + .getCharacteristic(Characteristic.On) + .on('set', this.setPowerState.bind(this)); + + return [informationService, switchService]; } } From 72b9175b787a324aaebead838939f57e10fc7fb1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Devran=20=C3=9Cnal?= Date: Thu, 17 Sep 2015 15:22:02 +0200 Subject: [PATCH 5/9] Set siri name based on accessory name --- accessories/GenericRS232Device.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/accessories/GenericRS232Device.js b/accessories/GenericRS232Device.js index 3bdfc18..b84e4cc 100644 --- a/accessories/GenericRS232Device.js +++ b/accessories/GenericRS232Device.js @@ -39,7 +39,7 @@ GenericRS232DeviceAccessory.prototype = { }, getServices: function() { - var switchService = new Service.Switch(); + var switchService = new Service.Switch(this.name); var informationService = new Service.AccessoryInformation(); informationService From 2135e7eccbbc2aabc4476d3c8db4a9367dc90547 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Devran=20=C3=9Cnal?= Date: Thu, 17 Sep 2015 17:00:52 +0200 Subject: [PATCH 6/9] Print scannable setup code to terminal on startup --- app.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/app.js b/app.js index 8a92123..692fc1a 100644 --- a/app.js +++ b/app.js @@ -191,6 +191,16 @@ function createAccessory(accessoryInstance, displayName) { } } +// Returns the setup code in a scannable format. +function printPin(pin) { + console.log("Scan this code with your HomeKit App on your iOS device:"); + console.log("\x1b[30;47m%s\x1b[0m", " "); + console.log("\x1b[30;47m%s\x1b[0m", " ┌────────────┐ "); + console.log("\x1b[30;47m%s\x1b[0m", " │ " + pin + " │ "); + console.log("\x1b[30;47m%s\x1b[0m", " └────────────┘ "); + console.log("\x1b[30;47m%s\x1b[0m", " "); +} + // Returns a logging function that prepends messages with the given name in [brackets]. function createLog(name) { return function(message) { @@ -210,3 +220,5 @@ function publish() { } startup(); + +printPin(bridgeConfig.pin); From a05a4b6f714805968709e8e66a2c4be75e1f9be0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Devran=20=C3=9Cnal?= Date: Thu, 17 Sep 2015 17:36:29 +0200 Subject: [PATCH 7/9] Print the setup code just before publishing the bridge --- app.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/app.js b/app.js index 692fc1a..a8c2006 100644 --- a/app.js +++ b/app.js @@ -211,6 +211,7 @@ function createLog(name) { } function publish() { + printPin(bridgeConfig.pin); bridge.publish({ username: bridgeConfig.username || "CC:22:3D:E3:CE:30", port: bridgeConfig.port || 51826, @@ -220,5 +221,3 @@ function publish() { } startup(); - -printPin(bridgeConfig.pin); From 5720e82abd578cbd257bfa761eb636b6f7232902 Mon Sep 17 00:00:00 2001 From: Nick Farina Date: Thu, 17 Sep 2015 12:50:27 -0700 Subject: [PATCH 8/9] Remove test code from WeMo --- accessories/WeMo.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/accessories/WeMo.js b/accessories/WeMo.js index df16f56..c19e19f 100644 --- a/accessories/WeMo.js +++ b/accessories/WeMo.js @@ -140,9 +140,7 @@ WeMoAccessory.prototype.getServices = function() { garageDoorService .getCharacteristic(Characteristic.TargetDoorState) - .on('set', this.setTargetDoorState.bind(this)) - .supportsEventNotification = false; - + .on('set', this.setTargetDoorState.bind(this)); return [garageDoorService]; } From 73148b060d17586c20b492ec4922799e1a77ca79 Mon Sep 17 00:00:00 2001 From: Nick Farina Date: Thu, 17 Sep 2015 12:51:55 -0700 Subject: [PATCH 9/9] Remove serial port dependency for now --- package.json | 1 - 1 file changed, 1 deletion(-) diff --git a/package.json b/package.json index f8b7225..d0eb41c 100644 --- a/package.json +++ b/package.json @@ -30,7 +30,6 @@ "q": "1.4.x", "tough-cookie": "^2.0.0", "request": "2.49.x", - "serialport": "^1.7.4", "sonos": "0.8.x", "telldus-live": "0.2.x", "teslams": "1.0.1",