-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller.js
51 lines (44 loc) · 1.3 KB
/
controller.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
/* GPIO functions */
const rpio = require('rpio');
function gpioInit(ENV_PINS) {
/****
PULL_DOWN = When pin connect with empty will get 0. if you don't use this, the
pin will get 0 or 1 in random.
PULL_UP = When pin connect with empty will get 1.
****/
// set lock's pin. Default is unlock
rpio.open(ENV_PINS.relay, rpio.OUTPUT, rpio.LOW);
// set state's pin. When door close, the pin will get HIGH.
rpio.open(ENV_PINS.state, rpio.INPUT, rpio.PULL_DOWN);
}
function gpioRead(PIN) {
return rpio.read(PIN);
}
function gpioSwitch(PIN) {
// read PIN state and can't change
const readPIN = rpio.read(PIN);
// action = close/open, method by rfid/button/app
let resultObject = {
"action": undefined,
"method": undefined,
"message": undefined
};
if (readPIN === rpio.HIGH) {
// Close relay
rpio.write(PIN, rpio.LOW);
resultObject.action = '開門';
} else {
// Open relay
rpio.write(PIN, rpio.HIGH);
resultObject.action = '關門';
}
// add method and message to object
resultObject.method = request.tokenTitle;
resultObject.message = request.body.message;
return resultObject;
}
module.exports = {
gpioInit,
gpioRead,
gpioSwitch
};