forked from FWeinb/homebridge-rcswitch
-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
81 lines (68 loc) · 3.01 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
var rcswitch = require('rcswitch-gpiomem');
var Service, Characteristic;
module.exports = function(homebridge) {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
homebridge.registerAccessory("homebridge-rcswitch-gpiomem", "RCSwitch", RadioSwitch);
}
function RadioSwitch(log, config) {
if (config.name === undefined) {
return log("Name missing from configuration.");
}
if ((config.onCode !== undefined) && (config.offCode !== undefined)) {
if (typeof(config.onCode) === "string") {
// If code is a string, it should be binary and don't need bit length
var switchOn = rcswitch.send.bind(rcswitch, config.onCode);
} else if (typeof(config.onCode) === "number") {
var bLength = config.bitLength || 24;
var switchOn = rcswitch.send.bind(rcswitch, config.onCode, bLength);
}
if (typeof(config.offCode) === "string") {
// If code is a string, it should be binary and don't need bit length
var switchOff = rcswitch.send.bind(rcswitch, config.offCode);
} else if (typeof(config.offCode) === "number") {
var bLength = config.bitLength || 24;
var switchOff = rcswitch.send.bind(rcswitch, config.offCode, bLength);
}
} else if ((config.systemcode != undefined) && (config.unitcode != undefined)) {
var switchOn = rcswitch.switchOn.bind(rcswitch, config.systemcode, config.unitcode);
var switchOff = rcswitch.switchOff.bind(rcswitch, config.systemcode, config.unitcode);
} else {
return log("Configuration must include either both an on code and an off code OR " +
"both a systemcode and a unitcode (or can have all of them), but " +
"doesn't appear to include at least one of these pairs.");
}
var informationService = new Service.AccessoryInformation();
informationService
.setCharacteristic(Characteristic.Name, "node-rcswitch-gpiomem")
.setCharacteristic(Characteristic.Manufacturer, "n8henrie")
.setCharacteristic(Characteristic.Model, "v1.1.0")
.setCharacteristic(Characteristic.SerialNumber, "0000000001");
var state = false;
var switchService = new Service.Switch(config.name);
switchService
.getCharacteristic(Characteristic.On)
.on('set', function(value, callback) {
state = value;
rcswitch.enableTransmit(config.pin || 17);
rcswitch.setPulseLength(config.pulseLength || 190);
rcswitch.setRepeatTransmit(config.repeats || 10);
if (state) {
switchOn();
} else {
switchOff();
}
callback();
});
switchService
.getCharacteristic(Characteristic.On)
.on('get', function(callback){
callback(null, state);
});
this.services = [ informationService, switchService ];
}
RadioSwitch.prototype = {
getServices : function (){
return this.services;
}
}