diff --git a/accessories/GenericRS232Device.js b/accessories/GenericRS232Device.js new file mode 100644 index 0000000..b84e4cc --- /dev/null +++ b/accessories/GenericRS232Device.js @@ -0,0 +1,58 @@ +var Service = require("HAP-NodeJS").Service; +var Characteristic = require("HAP-NodeJS").Characteristic; +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 = { + 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() + } + }); + } + }); + }, + + getServices: function() { + var switchService = new Service.Switch(this.name); + 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]; + } +} + +module.exports.accessory = GenericRS232DeviceAccessory; \ No newline at end of file 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]; } diff --git a/app.js b/app.js index 8a92123..a8c2006 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) { @@ -201,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, 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" } - ] }