-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathindex.js
106 lines (89 loc) · 3.17 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
101
102
103
104
105
106
/**
* Script file: index.js
* Created on: Feb 28, 2018
* Last modified on: Mar 31, 2021
*
* Comments:
* Raspberry Pi relay controller homebridge plugin
*/
var rpio = require('rpio');
let Service, Characteristic;
module.exports = function(homebridge) {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
homebridge.registerAccessory("homebridge-relays", "Relay", RelayAccessory);
}
class RelayAccessory {
constructor(log, config) {
/* log instance */
this.log = log;
/* read configuration */
this.name = config.name;
this.pin = config.pin;
this.invert = config.invert || false;
this.initialState = config.initial_state || 0;
this.timeout = config.timeout_ms || 0;
/* initialize variables */
this.timerId = -1;
/* GPIO initialization */
rpio.open(this.pin, rpio.OUTPUT, this.gpioValue(this.initialState));
/* run service */
this.relayService = new Service.Switch(this.name);
}
identify(callback) {
this.log.debug("Accessory identified");
callback(null);
}
gpioValue(val) {
if (this.invert) {
val = !val;
}
return val ? rpio.HIGH : rpio.LOW;
}
getRelayState() {
/* get relay state (ON, OFF) */
var val = this.gpioValue(rpio.read(this.pin) > 0);
return val == rpio.HIGH;
}
setRelayState(value) {
/* clear timeout if already exists */
if (this.timerId !== -1) {
clearTimeout(this.timerId);
this.timerId = -1;
}
/* GPIO write operation */
this.log.debug("Pin %d status: %s", this.pin, value);
rpio.write(this.pin, this.gpioValue(value));
/* turn off the relay if timeout is expired */
if (value && this.timeout > 0) {
this.timerId = setTimeout(() => {
this.log.debug("Pin %d timed out. Turned off", this.pin);
rpio.write(this.pin, this.gpioValue(false));
this.timerId = -1;
/* update relay status */
this.relayService
.getCharacteristic(Characteristic.On)
.updateValue(false);
}, this.timeout);
}
}
getServices() {
this.informationService = new Service.AccessoryInformation();
this.informationService
.setCharacteristic(Characteristic.Manufacturer, 'Smart Technology')
.setCharacteristic(Characteristic.Model, 'Multi-Relay Controller');
/* relay control */
this.relayService
.getCharacteristic(Characteristic.On)
.on('get', callback => {
this.state = this.getRelayState();
this.log.debug("Status:", this.state ? "ON" : "OFF");
callback(null, this.state);
})
.on('set', (value, callback) => {
this.setRelayState(value);
callback(null);
});
return [this.informationService, this.relayService];
}
}