-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
29 lines (22 loc) · 895 Bytes
/
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
const socketIoClient = require("socket.io-client");
const config = require("./config");
const { PiProvider } = require("./piProvider");
const piProvider = new PiProvider(console, config.garageOpenerOutputGpioPin, config.garageDoorSensorGpioPin);
const socket = socketIoClient(config.socketIoEndpoint);
socket.on("connect", () => {
console.log("socket.io connected");
socket.emit("registerGarageDoorOpener", config.garageDoorId);
});
socket.on("getGarageDoorStatus", async (message, callback) => {
const garageDoorStatus = await piProvider.getGarageDoorStatus();
callback(garageDoorStatus);
});
socket.on("toggleGarageDoorState", async () => {
await piProvider.activateGarageDoorOpener();
});
piProvider.watchGarageDoorOpenInput(status => {
socket.emit("garageDoorStatus", status);
});
setInterval(() => {
socket.emit("heartbeat");
}, config.heartbeatInterval);