forked from b-seite/homebridge-rcswitch4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
100 lines (85 loc) · 4.08 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
var rcswitch = require('rcswitch4');
var Service, Characteristic;
module.exports = function(homebridge) {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
homebridge.registerAccessory("homebridge-rcswitch4", "RCSwitch", RadioSwitch);
}
function RadioSwitch(log, config) {
if (config.name === undefined) {
return log("Name missing from configuration.");
}
rcswitch.enableTransmit(config.pin === undefined ? 17 : config.pin);
rcswitch.setProtocol(config.protocol || 1);
rcswitch.setPulseLength(config.pulseLength || 190);
rcswitch.setRepeatTransmit(config.repeats || 10);
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-rcswitch4")
.setCharacteristic(Characteristic.Manufacturer, "jdrucey")
.setCharacteristic(Characteristic.Model, "v1.4.2");
if ((config.systemcode != undefined) && (config.unitcode != undefined)) {
informationService
.setCharacteristic(Characteristic.SerialNumber, config.systemcode + "|" + config.unitcode);
} else {
informationService
.setCharacteristic(Characteristic.SerialNumber, config.onCode + "|" + config.offCode);
}
var state = false;
var switchService = new Service.Switch(config.name);
switchService
.getCharacteristic(Characteristic.On)
.on('set', function(value, callback) {
state = value;
if (state) {
if ((config.systemcode != undefined) && (config.unitcode != undefined)) {
log("Switching on " + config.systemcode + "." + config.unitcode + " (" + config.name + ") at protocol " + config.protocol);
} else {
log("Switching on " + config.onCode + " (" + config.name + ") at protocol " + config.protocol);
}
switchOn();
} else {
if ((config.systemcode != undefined) && (config.unitcode != undefined)) {
log("Switching off " + config.systemcode + "." + config.unitcode + " (" + config.name + ") at protocol " + config.protocol);
} else {
log("Switching off " + config.offCode + " (" + config.name + ") at protocol " + config.protocol);
}
switchOff();
}
callback();
});
switchService
.getCharacteristic(Characteristic.On)
.on('get', function(callback){
callback(null, state);
});
this.services = [ informationService, switchService ];
}
RadioSwitch.prototype = {
getServices : function (){
return this.services;
}
}