-
Notifications
You must be signed in to change notification settings - Fork 0
/
buzzer.js
52 lines (42 loc) · 1.32 KB
/
buzzer.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
const db = require("./db");
const SerialPort = require("serialport");
const Readline = require("@serialport/parser-readline");
const debug = require("debug")("buzzer");
// open connection to modem
const port = new SerialPort("/dev/ttyACM0");
exports.initializeBuzzer = () => {
const parser = new Readline({ delimiter: "\r\n" });
port.pipe(parser);
// log when the port is opened
port.on("open", () => {
debug("MODEM OPENED");
// when port is open, send reset command
port.write("ATZ\r", () => {
debug("RESET");
});
});
parser.on("data", data => {
debug("DATA RECEIVED: ", data);
// when a RING command is received
if (data.indexOf("RING") > -1) {
db.shouldOpenDoor().then(allow => {
if (allow) {
// 1) Pick up the call
port.write("ATH1\r");
// 2) Open the door (by dialing 6)
// Note: had to use a setTimeout, otherwise it wouldn't dial 6
setTimeout(() => port.write("ATD6\r"), 1000);
// 3) Record Entry
db.recordAllowedEntry();
} else {
// 1) Record Disallowed Entry
db.recordDisallowedEntry();
// 2) Hangup
port.write("ATH0\r");
}
});
}
});
parser.on("error", error => debug("ERROR: " + error));
parser.on("close", () => debug("CLOSED"));
}