-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathbackend.js
129 lines (106 loc) · 4.24 KB
/
backend.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
const express = require("express");
const cors = require("cors");
const path = require("path");
const { initCurrentConditions, getStationLatLong } = require("./current-conditions");
const { fetchWeatherForObservedCities, latestObservations } = require("./observations.js");
const { fetchWeatherForObservedUSCities, latestUSObservations } = require("./us-observations.js");
const { fetchForecastForSunspotCities, latestSunspotForecasts } = require("./sunspot");
const { initManitobaTracking, manitobaHighLow } = require("./manitoba.js");
const historicalDataAPI = ({
initHistoricalData,
lastYearObservation,
getSeasonPrecipData,
getSeasonPrecipNormalsData,
getLastMonthSummary,
} = require("./historical-data.js"));
const { fetchProvinceObservationData } = require("./province-today-observation.js");
const { startAlertMonitoring } = require("./alert-monitoring");
const { initAQHIObservation } = require("./aqhi-observation");
const { isWinterSeason } = require("./date-utils.js");
const config = require("./config/config");
const { getActivateInfoScreens } = require("./config/info-screens");
const { fetchAlternateRecordData } = require("./alternate-record-data");
const corsOptions = {
origin: "http://localhost:8080",
optionsSuccessStatus: 200,
};
const app = express()
.use(cors(corsOptions))
.use(express.json());
const port = 8600;
// load in the config for the weather channel
config.initWeatherChannel(app, startBackend);
async function startBackend() {
console.log("[RECW]", `Application started, listening on http://localhost:${port}`);
app.get("/api/init", (req, res) => {
const playlist = config.playlist();
const crawler = config.crawler();
res.send({
playlist: { files: playlist, file_count: playlist.length },
crawler: { messages: crawler, message_count: crawler.length },
showMBHighLow: config.isProvinceHighLowEnabled(),
infoScreens: getActivateInfoScreens(),
lookAndFeel: config.lookAndFeel(),
});
});
// alternate record source
if (config.misc().alternateRecordsSource) {
await fetchAlternateRecordData();
setInterval(fetchAlternateRecordData, 6 * 60 * 60 * 1000);
}
// current conditions info
initCurrentConditions(config.primaryLocation(), config.rejectInHourConditionUpdates(), app, historicalDataAPI);
// handling api requests
fetchWeatherForObservedCities();
setInterval(fetchWeatherForObservedCities, 5 * 60 * 1000);
// us city observations
fetchWeatherForObservedUSCities();
setInterval(fetchWeatherForObservedUSCities, 7.5 * 60 * 1000);
// sunspot city observations
fetchForecastForSunspotCities();
setInterval(fetchForecastForSunspotCities, 7.5 * 60 * 1000);
// air quality readings
initAQHIObservation(config.primaryLocation()?.name);
// MB regional high/low screen
// winnipeg, portage, brandon, dauphin, kenora, thompson
if (config.isProvinceHighLowEnabled()) initManitobaTracking();
// provincial today observations
fetchProvinceObservationData(config.primaryLocation()?.province);
setInterval(() => fetchProvinceObservationData(config.primaryLocation()?.province), 5 * 60 * 1000);
app.get("/api/climate/season/precip", (req, res) => {
res.send({
isWinter: isWinterSeason(),
totalPrecip: getSeasonPrecipData(),
normalPrecip: getSeasonPrecipNormalsData(),
});
});
app.get("/api/climate/lastmonth", (req, res) => {
res.send({
summary: getLastMonthSummary() || false,
});
});
app.get("/api/weather/surrounding", (req, res) => {
res.send({ observations: latestObservations });
});
app.get("/api/weather/usa", (req, res) => {
res.send({ observations: latestUSObservations });
});
app.get("/api/weather/sunspot", (req, res) => {
res.send({ sunspots: latestSunspotForecasts });
});
app.get("/api/weather/mb_highlow", (req, res) => {
if (!config.isProvinceHighLowEnabled()) return;
res.send(manitobaHighLow());
});
// start the amqp alert monitoring of cap
startAlertMonitoring(getStationLatLong(), app);
}
app.listen(port);
app.use(express.static("dist"));
app.use(express.static("music"));
app.get("/", (req, res) => {
res.sendFile(path.join(__dirname, "/dist/index.html"));
});
app.get("/music/*", (req, res) => {
res.sendFile(path.join(__dirname, decodeURI(req.url)));
});