-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
46 lines (36 loc) · 1.3 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
var rpio = require("rpio");
var Service, Characteristic;
module.exports = function(homebridge) {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
homebridge.registerAccessory("homebridge-gpio-switch", "Switch", SwitchAccessory);
}
function SwitchAccessory(log, config) {
this.log = log;
this.name = config['name'];
this.pin = config['pin'];
this.invert = config['invert'];
this.service = new Service.Switch(this.name);
this.infoService = new Service.AccessoryInformation();
this.infoService
.setCharacteristic(Characteristic.Manufacturer, "Radoslaw Sporny")
.setCharacteristic(Characteristic.Model, "RaspberryPi GPIO Switch")
.setCharacteristic(Characteristic.SerialNumber, "Version 1.2.1");
// use gpio pin numbering
rpio.init({
mapping: 'gpio'
});
rpio.open(this.pin, rpio.OUTPUT, this.invert ? rpio.LOW : rpio.HIGH);
this.service
.getCharacteristic(Characteristic.On)
.on('set', this.setPowerState.bind(this));
}
SwitchAccessory.prototype.setPowerState = function(value, callback) {
this.log("Setting switch to %s", value);
if (this.invert) value = !value;
rpio.write(this.pin, (value ? rpio.LOW : rpio.HIGH));
callback();
}
SwitchAccessory.prototype.getServices = function() {
return [this.infoService, this.service];
}