mirror of
https://github.com/mtan93/homebridge.git
synced 2026-05-12 14:26:05 +01:00
Update to modern API and return error messages on failed connections
This commit is contained in:
@@ -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;
|
var SerialPort = require("serialport").SerialPort;
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
@@ -6,120 +7,51 @@ module.exports = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function GenericRS232DeviceAccessory(log, config) {
|
function GenericRS232DeviceAccessory(log, config) {
|
||||||
this.log = log;
|
this.log = log;
|
||||||
this.id = config["id"];
|
this.id = config["id"];
|
||||||
this.name = config["name"];
|
this.name = config["name"];
|
||||||
this.model_name = config["model_name"];
|
this.model_name = config["model_name"];
|
||||||
this.manufacturer = config["manufacturer"];
|
this.manufacturer = config["manufacturer"];
|
||||||
this.on_command = config["on_command"];
|
this.on_command = config["on_command"];
|
||||||
this.off_command = config["off_command"];
|
this.off_command = config["off_command"];
|
||||||
this.device = config["device"];
|
this.device = config["device"];
|
||||||
this.baudrate = config["baudrate"];
|
this.baudrate = config["baudrate"];
|
||||||
}
|
}
|
||||||
|
|
||||||
GenericRS232DeviceAccessory.prototype = {
|
GenericRS232DeviceAccessory.prototype = {
|
||||||
getServices: function() {
|
setPowerState: function(powerOn, callback) {
|
||||||
var that = this;
|
var that = this;
|
||||||
return [
|
var command = powerOn ? that.on_command : that.off_command;
|
||||||
{
|
var serialPort = new SerialPort(that.device, { baudrate: that.baudrate }, false);
|
||||||
sType: types.ACCESSORY_INFORMATION_STYPE,
|
serialPort.open(function (error) {
|
||||||
characteristics: [
|
if (error) {
|
||||||
{
|
callback(new Error('Can not communicate with ' + that.name + " (" + error + ")"))
|
||||||
cType: types.NAME_CTYPE,
|
} else {
|
||||||
onUpdate: null,
|
serialPort.write(command, function(err, results) {
|
||||||
perms: ["pr"],
|
if (error) {
|
||||||
format: "string",
|
callback(new Error('Can not send power command to ' + that.name + " (" + err + ")"))
|
||||||
initialValue: that.name,
|
} else {
|
||||||
supportEvents: false,
|
callback()
|
||||||
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
|
|
||||||
}
|
|
||||||
]
|
|
||||||
}
|
}
|
||||||
]
|
});
|
||||||
|
},
|
||||||
|
|
||||||
|
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];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user