-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
123 lines (118 loc) · 4.57 KB
/
server.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import _ from 'lodash';
import {initStore, getSetting} from './store';
initStore();
import {
sampleExternalSensor,
setupPhysicalPins,
sleep,
openGateTemporarily, getTemperature, getCPUVoltage,
} from './helpers/rpiHelper';
import {
setupTelegram,
sendTelegramGroupMessage,
sendTelegramAdminMessage,
camerasSnapshot,
} from './helpers/telegramHelper';
import {killProcess, pickRandomFromArray} from './utils';
import {setupBlynk} from './helpers/blynkHelper';
import {fetchCalendarAvailability, isCalendarBusy} from "./helpers/calendarHelper";
async function init() {
await setupPhysicalPins().catch((e) => killProcess(e));
// await setupBlynk().catch((e) => killProcess(e));
await setupTelegram().catch((e) => {
console.error('setupTelegram err ', e);
// killProcess(e);
});
fetchCalendarAvailability().catch(e => console.log('fetchCalendarAvailability err', e));
externalSensorPolling().catch((e) => {
console.error('externalSensorPolling err ', e);
// killProcess(e)
});
loadTempAndVoltage().catch((e) => {
console.error('externalSensorPolling err ', e);
// killProcess(e)
});
}
const TIME_STEP = 0.5; // seconds
const COUNT_TRIGGER = 1;
const COOL_DOWN_TIME = 120; // seconds
const SPAM_THRESHOLD = 3; // Notifications
async function externalSensorPolling() {
let spamCounter = 0;
let triggerCounter = 0;
let coolDownNotificationsCounter = 0;
while (true) {
const sensorIsBlocked = await sampleExternalSensor();
if (sensorIsBlocked) {
// console.log("Ext. sensor triggered. counter ", triggerCounter);
if (triggerCounter >= COUNT_TRIGGER) {
const extTriggerEnabled = getSetting({setting: 'extTriggerEnabled'});
const shouldNotifyOnExtTrigger = getSetting({
setting: 'shouldNotifyOnExtTrigger',
});
// console.log("Ext. sensor triggered. Opening gate");
triggerCounter = 0;
if (extTriggerEnabled || shouldNotifyOnExtTrigger) {
if (!isCalendarBusy() && extTriggerEnabled) await openGateTemporarily().catch(e => console.error('openGateTemporarily err', e));
else console.log('calendar busy, not opening gate')
const day = new Date().getDay();
let response = pickRandomFromArray([
'External sensor. Opening gate',
// "Ext. sensor triggered, maybe a new package?? So exciting..",
// "Ext. sensor triggered, is Rox checking for mail again?",
]);
if (day === 0) response = 'External sensor. Tomorrow is garbage day!';
// if (day === 6)
// response =
// "External sensor. It could have been a Saturday tour! If not for this virus.. I'll spin up my antivirus";
if (coolDownNotificationsCounter <= 0) {
coolDownNotificationsCounter = _.floor(COOL_DOWN_TIME / TIME_STEP);
if (shouldNotifyOnExtTrigger) {
await sendTelegramGroupMessage(response).catch((e) =>
console.error('err sendTelegramGroupMessage', e)
);
await camerasSnapshot().catch((e) =>
console.error('err camerasSnapshot', e)
);
}
// else {
// await sendTelegramAdminMessage(response).catch((e) =>
// console.error(
// 'extTriggerEnabled',
// extTriggerEnabled,
// 'err sendTelegramAdminMessage',
// e
// )
// );
// await camerasSnapshot().catch((e) =>
// console.error(
// 'extTriggerEnabled',
// extTriggerEnabled,
// 'err camerasSnapshot',
// e
// )
// );
// fetchCalendarAvailability().catch(e => console.error('fetchCalendarAvailability err', e));
// }
} else console.log('coolDownNotificationsCounter', coolDownNotificationsCounter, 'triggerCounter', triggerCounter)
}
// console.log('coolDownNotificationsCounter', coolDownNotificationsCounter, 'triggerCounter',triggerCounter)
} else {
triggerCounter += 1;
}
}
await sleep(TIME_STEP * 1000);
if (coolDownNotificationsCounter > 0) {
--coolDownNotificationsCounter;
}
}
}
async function loadTempAndVoltage() {
while (true) {
await sleep(600000);
const temp = await getTemperature();
const volt = await getCPUVoltage();
await sendTelegramAdminMessage(`CPU temperature - ${temp} \nCPU voltage - ${volt}`)
}
}
init().catch((e) => console.error('err in setup', e));