-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
66 lines (52 loc) · 1.94 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
63
64
65
66
var Service, Characteristic;
var telnetClient = require('telnet-client');
module.exports = function (homebridge) {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
homebridge.registerAccessory("homebridge-telnet", "telnet", telnet);
}
function telnet(log, config) {
this.log = log;
this.host = config["host"];
this.port = config["port"];
this.shellPrompt = config["shellPrompt"];
this.timeout = config["timeout"];
this.name = config["name"];
this.cmd = config["cmd"];
}
telnet.prototype = {
getPowerState: function (callback) {
callback(null, !this.default_state_off);
},
setPowerState: function(powerOn, callback) {
var connection = new telnetClient();
connection.on('ready', function(prompt){
connection.exec(this.cmd, function(){
connection.end().then(function(){
callback();
});
});
});
connection.on('timeout', function(){
callback(error);
});
connection.connect({ host: this.host, port: this.port, shellPrompt: this.shellPrompt, timeout: this.timeout});
},
identify: function (callback) {
this.log("Identify requested!");
callback();
},
getServices: function () {
var informationService = new Service.AccessoryInformation();
informationService
.setCharacteristic(Characteristic.Manufacturer, "skiilaa")
.setCharacteristic(Characteristic.Model, "homebridge-telnet")
.setCharacteristic(Characteristic.SerialNumber, "I don't even know anymore.");
switchService = new Service.Switch(this.name);
switchService
.getCharacteristic(Characteristic.On)
.on('get', this.getPowerState.bind(this))
.on('set', this.setPowerState.bind(this));
return [switchService];
}
};