forked from wstameris/homebridge-rfoutlets
-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
109 lines (92 loc) · 3.31 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
var Service, Characteristic, limiter;
var exec = require("child_process").exec,
RateLimiter = require('limiter').RateLimiter;
module.exports = function(homebridge) {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
limiter = new RateLimiter(1, 700); //limit requests to one per 700ms
homebridge.registerAccessory("homebridge-rfoutlets-protocol",
"RFOutlet",
RFOutletAccessory);
}
function RFOutletAccessory(log, config) {
this.log = log;
//Accessory information
this.name = config["name"];
this.type = config["type"];
this.manufacturer = config["manufacturer"];
this.model = config["model"];
this.serial = config["serial"];
//RF transmit inforamtion
this.rf_on = config["rf_on"];
this.rf_off = config["rf_off"];
if (config["pulselength"]) {
this.pulselength = config["pulselength"];
} else {
this.pulselength = 189; //Default to a pulse length of 189
}
if (config["protocol"]) {
this.protocol = config["protocol"];
} else {
this.protocol = 1; //Default protocol is 1
}
}
RFOutletAccessory.prototype = {
setPowerState: function(powerOn, callback) {
var state;
var cmd;
if (powerOn) {
cmd = __dirname + "/codesend " + this.rf_on + " " + this.protocol + " " + this.pulselength;
state = "on";
} else {
cmd = __dirname + "/codesend " + this.rf_off + " " + this.protocol + " " + this.pulselength;
state = "off";
}
this.log("Turning " + this.name + " " + state + " (" + cmd + ")");
limiter.removeTokens(1, function() {
exec(cmd, function(error, stdout, stderr) {
if (error) {
console.log(error);
}
callback();
})
});
},
identify: function(callback) {
this.log("HomeKit identify requested");
callback();
},
getServices: function() {
var informationService = new Service.AccessoryInformation();
informationService
.setCharacteristic(Characteristic.Manufacturer, this.manufacturer)
.setCharacteristic(Characteristic.Model, this.model)
.setCharacteristic(Characteristic.SerialNumber, this.serial);
var outletService;
switch (this.type) {
case "Switch":
this.outletService = new Service.Switch(this.name);
break;
case "Light":
this.outletService = new Service.Lightbulb(this.name);
break;
case "Fan":
this.outletService = new Service.Fan(this.name);
break;
case "Outlet":
this.outletService = new Service.Outlet(this.name);
break;
/* case "Speaker":
this.outletService = new Service.Speaker(this.name);
break;
//still no support for Speakers in iOS 11.2.1 (Jan18), may be shipped with HomePods
*/
default:
this.outletService = new Service.Switch(this.name);
}
this.outletService
.getCharacteristic(Characteristic.On)
.on('set', this.setPowerState.bind(this));
return [informationService, this.outletService];
}
};