-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
157 lines (144 loc) · 4.43 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
import sirv from "sirv";
import polka from "polka";
import { EventEmitter } from "node:events";
const departureEmitter = new EventEmitter();
const app = polka();
let config = {
port: process.env.PORT || 3000,
refreshrate: process.env.REALREFRESH || 30, //Number of seconds between refresh
siteid: process.env.SITEID || 9204, //Tekniska högskolan siteid
};
let stats = {
requests: 0,
nrofclients: 0,
};
let sldata = null;
app
.use("/public", sirv("public"))
.use("/", sirv("views"))
.use("/subscribe", (req, res) => {
res.writeHead(200, {
"access-control-allow-origin": "*",
"content-type": "text/event-stream",
"cache-control": "no-cache",
});
function slMetroHandler() {
res.write("event: slmetro\n");
res.write(`data: ${JSON.stringify(sldata.Metro)}\n\n`);
}
function slBusHandler() {
res.write("event: slbus\n");
res.write(`data: ${JSON.stringify(sldata.Bus)}\n\n`);
}
function slTramHandler() {
res.write("event: sltram\n");
res.write(`data: ${JSON.stringify(sldata.Tram)}\n\n`);
}
function statsHandler() {
res.write("event: stats\n");
res.write(`data: ${JSON.stringify(stats)}\n\n`);
}
stats.nrofclients++;
if (sldata) {
res.write("event: slmetro\n");
res.write(`data: ${JSON.stringify(sldata.Metro)}\n\n`);
res.write("event: slbus\n");
res.write(`data: ${JSON.stringify(sldata.Bus)}\n\n`);
res.write("event: sltram\n");
res.write(`data: ${JSON.stringify(sldata.Tram)}\n\n`);
res.write("event: stats\n");
res.write(`data: ${JSON.stringify(stats)}\n\n`);
}
departureEmitter.on("slmetro", slMetroHandler);
departureEmitter.on("slbus", slBusHandler);
departureEmitter.on("sltram", slTramHandler);
departureEmitter.on("stats", statsHandler);
req.once("close", () => {
departureEmitter.off("slmetro", slMetroHandler);
departureEmitter.off("slbus", slBusHandler);
departureEmitter.off("sltram", slTramHandler);
departureEmitter.off("stats", statsHandler);
res.end();
stats.nrofclients--;
});
})
.listen(config.port, () => {
console.log(`listening on *:${config.port}`);
});
async function updateDepartures() {
stats.requests++;
const response = await fetch(
`https://transport.integration.sl.se/v1/sites/${config.siteid}/departures`,
{
headers: {
"Content-Type": "application/json",
},
},
);
if (response.ok) {
const data = await response.json();
const busFour = [];
const busOther = [];
const metroNorth = [];
const metroSouth = [];
const tram = [];
data.departures.forEach((departure) => {
if (departure.line.transport_mode === "BUS") {
if (departure.line.id === 4 || departure.line.id === 94) {
busFour.push({
id: departure.line.id,
destination: departure.destination,
display: departure.display,
});
} else if (busOther.length < 10) {
busOther.push({
id: departure.line.id,
destination: departure.destination,
display: departure.display,
});
}
} else if (departure.line.transport_mode === "METRO") {
if (departure.direction_code === 1 && metroNorth.length < 4) {
metroNorth.push({
id: departure.line.id,
destination: departure.destination,
display: departure.display,
});
} else if (metroSouth.length < 4) {
metroSouth.push({
id: departure.line.id,
destination: departure.destination,
display: departure.display,
});
}
} else if (departure.line.transport_mode === "TRAM") {
if (tram.length < 8) {
tram.push({
id: departure.line.id,
destination: departure.destination,
display: departure.display,
});
}
}
});
sldata = {
Bus: {
four: busFour,
other: busOther,
},
Metro: {
north: metroNorth,
south: metroSouth,
},
Tram: tram,
};
departureEmitter.emit("slbus");
departureEmitter.emit("slmetro");
departureEmitter.emit("sltram");
departureEmitter.emit("stats");
} else {
console.error("Error fetching data", response);
}
}
setInterval(updateDepartures, 1000 * config.refreshrate); //Refreshrate is in seconds.
await updateDepartures();