forked from smartechru/homebridge-button
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
47 lines (39 loc) · 1.32 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
var Service, Characteristic;
var rpio = require('rpio');
module.exports = function(homebridge) {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
homebridge.registerAccessory("homebridge-button", "Button", ButtonAccessory);
}
function ButtonAccessory(log, config) {
this.log = log;
this.name = config["name"];
this.buttonState = 0; // button state, default is OFF
this.log("Creating a button with name '" + this.name + "'...");
this.pin = config["pin"];
this.outpin = config["out"];
// rpio.open(this.pin, rpio.INPUT, rpio.HIGH);
rpio.open(this.outpin, rpio.OUTPUT);
// this.search();
}
ButtonAccessory.prototype.getOn = function(callback) {
this.log("Input PIN:%d is %d", this.pin, this.buttonState);
callback(null, this.buttonState);
}
ButtonAccessory.prototype.setOn = function(on, callback) {
this.log("Output PIN:%d to %d", this.outpin, on);
this.buttonState = on;
if (on) {
rpio.write(this.outpin, rpio.HIGH);
} else {
rpio.write(this.outpin, rpio.LOW);
}
callback(null);
}
ButtonAccessory.prototype.getServices = function() {
var buttonService = new Service.Switch(this.name);
buttonService.getCharacteristic(Characteristic.On)
.on('get', this.getOn.bind(this))
.on('set', this.setOn.bind(this));
return [buttonService];
}