forked from n8henrie/homebridge-rcswitch-gpiomem
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
84 lines (71 loc) · 3.29 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
var rcswitch = require('rcswitch-gpiomem2');
var Service, Characteristic;
module.exports = function(homebridge) {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
homebridge.registerAccessory("homebridge-rcswitch-gpiomem2", "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-gpiomem2")
.setCharacteristic(Characteristic.Manufacturer, "fredericvl")
.setCharacteristic(Characteristic.Model, "v1.2.2")
.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.setProtocol(config.protocol || 1);
rcswitch.setPulseLength(config.pulseLength || 190);
rcswitch.setRepeatTransmit(config.repeats || 10);
if (state) {
log("Switching on " + config.onCode + " (" + config.name + ") at protocol " + config.protocol);
switchOn();
} 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;
}
}