-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
68 lines (57 loc) · 2.1 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
var Service, Characteristic;
var rpi433 = require('rpi-433');
module.exports = function(homebridge) {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
homebridge.registerPlatform("homebridge-rfbuttons", "RFButtons", RFButtonsPlatform);
};
function RFButtonsPlatform(log, config) {
var self = this;
self.config = config;
self.log = log;
self.pin = config["pin"] || 2; // Listen on GPIO 2 (or Physical PIN 13)
self.delay = config["debounceDelay"] || 300; // Debounce delay
self.rfSniffer = rpi433.sniffer({
pin: self.pin,
debounceDelay: self.delay
});
self.rfSniffer.on('data', function (data) {
self.log('Code received: '+ data.code +' pulse length : ' + data.pulseLength);
if(self.accessories) {
self.accessories.forEach(function(accessory) {
accessory.notify.call(accessory, data.code);
});
}
});
}
RFButtonsPlatform.prototype.accessories = function(callback) {
var self = this;
self.accessories = [];
self.config.buttons.forEach(function(button) {
self.accessories.push(new RFButtonAccessory(button, self.log, self.config));
});
callback(self.accessories);
};
function RFButtonAccessory(button, log, config) {
var self = this;
self.name = button.name;
self.button = button;
self.log = log;
self.config = config;
self.service = new Service.StatelessProgrammableSwitch(self.name);
self.service.getCharacteristic(Characteristic.ProgrammableSwitchEvent).setProps({maxValue: 0}); // Single tap only
}
RFButtonAccessory.prototype.pressed = function() {
var self = this;
self.log("%s pressed", self.button.name);
self.service.getCharacteristic(Characteristic.ProgrammableSwitchEvent).setValue(0 /* BUTTON PRESSED EVENT */);
};
RFButtonAccessory.prototype.notify = function(code) {
var self = this;
if(self.button.codes.indexOf(code) !== -1) {
self.pressed();
}
};
RFButtonAccessory.prototype.getServices = function() {
return [this.service];
};