forked from alguevara7/homebridge-ssh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
62 lines (49 loc) · 1.67 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
var Service;
var Characteristic;
var ssh = require('ssh-exec'),
assign = require('object-assign');
module.exports = function(homebridge) {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
homebridge.registerAccessory('homebridge-ssh', 'SSH', SshAccessory);
}
function SshAccessory(log, config) {
this.log = log;
this.service = 'Switch';
this.name = config['name'];
this.command = config['command'];
this.ssh = assign({
user: config['user'],
host: config['host'],
password: config['password'],
key: config['key']
}, config['ssh']);
}
SshAccessory.prototype.setState = function(powerOn, callback) {
var accessory = this;
var state = powerOn ? 'on' : 'off';
var stream = ssh(accessory.command, accessory.ssh);
stream.on('error', function (err) {
accessory.log('Error: ' + err);
callback(err || new Error('Error setting ' + accessory.name + ' to ' + state));
});
stream.on('finish', function () {
accessory.log('Set ' + accessory.name + ' to ' + state);
callback(null);
});
}
SshAccessory.prototype.getState = function(callback) {
var accessory = this;
callback(null);
}
SshAccessory.prototype.getServices = function() {
var informationService = new Service.AccessoryInformation();
var switchService = new Service.Switch(this.name);
informationService
.setCharacteristic(Characteristic.Manufacturer, 'SSH Manufacturer')
.setCharacteristic(Characteristic.Model, 'SSH Model')
.setCharacteristic(Characteristic.SerialNumber, 'SSH Serial Number');
var characteristic = switchService.getCharacteristic(Characteristic.On)
.on('set', this.setState.bind(this));
return [switchService];
}