-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_webserver.ts
69 lines (52 loc) · 2.59 KB
/
example_webserver.ts
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
import express from 'express';
import sqlLite from './src/sqlite_database';
import PDF_PROGRAMME from './src/pdf_programme';
import PDF_WZ_LANG from './src/pdf_wzlang';
import PDF_MQTT from './src/pdf_mqtt';
import { getSpectacleSubInfos, getWzLangInfos, getSpectacleState } from './src/utils';
import PDFProgrammeInterface from './types/pdf_programme';
const sqlite = new sqlLite();
const pdfProgramme = new PDF_PROGRAMME();
const pdfWzLang = new PDF_WZ_LANG();
const pdfMQTT = new PDF_MQTT();
const app = express();
async function webServer(programmeData: PDFProgrammeInterface[]) {
app.get('/', (req, res) => {
res.json(programmeData);
});
app.listen(3000, () => {
console.log('Server running on port 3000');
});
}
async function main() {
await sqlite.checkDatabase();
const sqliteConnection = await sqlite.getDatabaseConnection();
pdfMQTT.isMQTTConnected();
const programmeData = await pdfProgramme.getProgrammeData();
const wzLangData = await pdfWzLang.getWzLangData();
await getSpectacleSubInfos(programmeData, sqliteConnection);
getWzLangInfos(programmeData, wzLangData);
const eventsToSubscribe = programmeData.filter((event) => event.duree > 0).length;
process.setMaxListeners(eventsToSubscribe + 1); // Set max listeners to the number of events + 1 (for the 'done' event)
require('events').EventEmitter.prototype._maxListeners = eventsToSubscribe + 1;
for (const event of programmeData) {
if (event.duree != 0) {
if (event.pdfid == undefined) continue; // Skip events with no PDF ID (WZ Lang data not found
//console.log(`[PDF Programme] ${event.title} (${event.wzid})`);
//console.log(`[PDF Programme] Try to subscribe to topic ${event.pdfid}`);
pdfMQTT.addMQTTHook('fr/visitor/grandstand/affluence_real_time/' + event.pdfid, (message: any) => {
//console.log(`[PDF Programme] ${new Date().toLocaleString()} Received message for ${event.title} (${event.wzid})`);
//console.log(message.toString());
const messageData = JSON.parse(message.toString());
if (messageData.affluence) {
//console.log(`[PDF Programme] Affluence for ${event.title} (${event.wzid}): ${messageData.affluence}`);
event.affluence = messageData.affluence != '' ? Number(messageData.affluence) : 0;
event.state = getSpectacleState(event);
}
});
}
event.state = getSpectacleState(event);
}
webServer(programmeData);
}
export default main;