-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
31 lines (26 loc) · 794 Bytes
/
app.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
const { calcTimeout } = require('./utils');
const { triggerEmailSend } = require('./emails');
const { getSensorData } = require('./purple_air');
const express = require('express');
const app = express();
let currentTimout = null;
let lastReading = null;
async function run() {
aqi = await getSensorData();
console.log(aqi, 'returned aqi');
triggerEmailSend(aqi, lastReading);
return aqi;
}
async function setSleepInt() {
const aqi = await run();
const timeout = calcTimeout(aqi, lastReading, currentTimout);
lastReading = aqi;
currentTimout = timeout;
console.log(`Waiting ${timeout} minutes between checks`);
setTimeout(setSleepInt, timeout * 60000);
}
setSleepInt();
const port = 8080;
app.listen(port, () => {
console.log(`aqiapp: listening on port ${port}`);
});