-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
157 lines (146 loc) · 4.04 KB
/
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
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
"use strict";
const http = require("http");
const exec = require("child_process").exec;
const isRouteBlocked = require("./scannor.js");
const Tools = require("./tools.js");
const express = require("express");
const archive = require("./archive.js");
const moment = require("moment-timezone");
const Common = require("./common.js");
const SmsSender = require("./sms-sender.js");
/*
* Main switches
*/
const ENABLE_CRON = true;
const ENABLE_FRONTEND = true;
const ENABLE_DIRECT_CALL = false;
const FRONTEND_BASE_DIR = "frontend";
const FRONTEND_PORT = 8081;
const PLAYBACK_APP = "aplay"; // afplay on osx, aplay on raspian
const mApp = express();
var mCurrentAlarmProcess = null;
var mAlarmReportedToday = false;
var mKeepAlertAlive = false;
var mReportText = "";
// Global init and config
function init() {
Tools.initPolyfill();
Tools.log("Starting service, setting up ...");
if(ENABLE_CRON) {
setInterval(function() {
const scanTime = Common.getScanTime();
const now = moment.tz();
if(Tools.correctRange(now, Common.SCAN_WEEK_DAYS, scanTime) === false) {
Tools.trace(" <------------------------: -");
return;
}
Tools.trace( " <------------------------: +");
if(Tools.eq(now, scanTime[0]) === true) {
cleanJob();
}
if(Tools.eq(now, scanTime[1]) === true) {
endOfTodaysScanJob();
}
trafficScanJob();
}, 60000); // check minutely
const trafficScanJob = function() {
Tools.trace("Beginning scan.");
isRouteBlocked(reactOnBlockage);
Tools.trace("End scan.");
};
const endOfTodaysScanJob = function() {
Tools.log("Cleaning up for today.");
mAlarmReportedToday = false;
mReportText = "";
killAlert();
};
const cleanJob = function() {
archive.clear();
Tools.log("Deleted yesterdays data.");
};
}
if(ENABLE_FRONTEND) {
http.createServer(mApp).listen(FRONTEND_PORT);
mApp.use(express.static(FRONTEND_BASE_DIR));
mApp.get("/", (req, res) => send(res, "index.html"));
mApp.get("/reason", (req, res) => res.send(mReportText));
mApp.get("/deactivateAlarm", (req, res) => {
killAlert();
send(res, "done.html");
});
mApp.get("/reportMissedCall", (req, res) => {
reportMissedCall();
send(res, "done.html");
});
mApp.get("/getPreparedSMS", (req, res) => send(res, "preparedMessages.txt", "data"));
mApp.get("/sendGroupSMS", (req, res) => {
try {
const index = req.query.sms;
const sms = Tools.readLines("data/preparedMessages.txt")[index];
if(!sms || 0 === sms.length)
throw "No value found.";
const ok = SmsSender.broadcast(sms);
res.end(ok ? "OK" : "ERROR");
} catch(ex) {
res.end("ERROR");
}
});
Tools.log("Server runs at port " + FRONTEND_PORT);
}
Tools.log("Init finalized.");
function send(res, item, rootFolder=FRONTEND_BASE_DIR) {
res.sendFile(item, {root: rootFolder});
}
}
// Speaks a message over the alarm system.
function reportMissedCall() {
const command = "espeak \"Seine herrliche Lordschaft hatte versucht anzurufen.\" -v german";
exec(command);
}
function reactOnBlockage(reportText) {
if(mAlarmReportedToday) {
return; // do not alarm again
}
mAlarmReportedToday = true;
mKeepAlertAlive = true;
mReportText = `[${new Date()}]${reportText}`;
const logMsg = "Reporting incident: " + reportText;
Tools.log(logMsg);
//Tools.sendMail(logMsg);
const script = [
`${PLAYBACK_APP} data/sound0.wav`,
"sleep 1",
`${PLAYBACK_APP} data/sound0.wav`,
"sleep 1",
`${PLAYBACK_APP} data/sound0.wav`,
"sleep 1",
`espeak "${reportText}" -v german`,
"sleep 1",
`${PLAYBACK_APP} data/sound2.wav`,
"sleep 1"
];
var index = 0;
const fork = (cb) => exec(script[index++ % script.length], cb);
const cb = (err) => {
if (err) {
Tools.log("FAILED to report incident: " + err);
killAlert();
return;
}
if(mKeepAlertAlive) {
mCurrentAlarmProcess = fork(cb);
}
};
mCurrentAlarmProcess = fork(cb);
}
function killAlert() {
mKeepAlertAlive = false;
if(mCurrentAlarmProcess !== null) {
mCurrentAlarmProcess.kill();
mCurrentAlarmProcess = null;
}
}
init();
if(ENABLE_DIRECT_CALL) {
isRouteBlocked(reactOnBlockage);
}