Merge pull request #182 from Cosmo/master

Add shim for devices with serial connection
This commit is contained in:
Nick Farina
2015-09-17 06:23:12 -07:00
3 changed files with 71 additions and 1 deletions

View File

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

View File

@@ -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"
}
]
}

View File

@@ -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",