-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.mjs
More file actions
271 lines (226 loc) · 7.65 KB
/
index.mjs
File metadata and controls
271 lines (226 loc) · 7.65 KB
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
import { Constants, NodeJSSerialConnection } from "@liamcottle/meshcore.js";
import { DOMParser } from 'linkedom';
import * as mqtt from 'mqtt';
import * as utils from './utils.mjs';
import config from './config.json' with { type: 'json' };
import Parser from 'rss-parser';
const optionsShort = {
month: '2-digit',
day: '2-digit',
hour: '2-digit',
minute: '2-digit',
hour12: false
};
const port = process.argv[2] ?? config.port;
const channels = {
alerts: null,
weather: null
};
const seen = {
blitz: {},
warnings: {},
};
let geoCache = {};
let blitzBuffer = [];
let meteoAlerts = []
console.log(`Connecting to ${port}`);
const connection = new NodeJSSerialConnection(port);
connection.on('connected', async () => {
console.log(`Connected to ${port}`);
for (const [channelType, channel] of Object.entries(config.channels)) {
channels[channelType] = await connection.findChannelByName(channel);
if (!channels[channelType]) {
console.log(`Channel ${channelType}: "${channel}" not found!`);
connection.close();
return;
}
}
await registerBlitzortungMqtt(blitzHandler, config.blitzArea);
utils.setAlarm(config.weatherAlarm, sendWeather);
setInterval(blitzWarning, config.timers.blitzCollection);
if (config.meteoAlerts.enabled) {
setInterval(checkMeteoAlerts, config.timers.meteoAlerts);
checkMeteoAlerts();
}
console.log('weatherBot ready.');
});
// listen for new messages
connection.on(Constants.PushCodes.MsgWaiting, async () => {
try {
const waitingMessages = await connection.getWaitingMessages();
for (const message of waitingMessages) {
if (message.contactMessage) {
await onContactMessageReceived(message.contactMessage);
} else if (message.channelMessage) {
await onChannelMessageReceived(message.channelMessage);
}
}
} catch (e) {
console.log(e);
}
});
async function checkMeteoAlerts() {
Object.keys(meteoAlerts).forEach(key => {
if (meteoAlerts[key] < Date.now() - (config.meteoAlerts.timeout * 60 * 1000)) {
delete meteoAlerts[key];
}
});
let parser = new Parser({
headers: { 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9' },
xml2jsOptions: {
explicitArray: false,
},
customFields: {
item: [
['cap:areaDesc', 'area'],
['cap:event', 'event'],
['cap:certainty', 'certainty'],
['cap:severity', 'severity'],
['cap:expires', 'end'],
['cap:identifier', 'identifier'],
['cap:onset', 'start']
]
}
});
let warnigs = [];
const feed = await parser.parseURL(config.meteoAlerts.url);
if (feed.items && feed.items.length > 0) {
feed.items.forEach((item) => {
if (config.meteoAlerts.regions.includes(item.area)) {
const endTime = new Date(item.end);
if (endTime < Date.now())
return;
if (meteoAlerts[item.identifier]
|| !config.meteoAlerts.certaintyFilter.includes(item.certainty.toLowerCase())
|| !config.meteoAlerts.severityFilter.includes(item.severity.toLowerCase()))
return;
warnigs.push({
id: item.identifier,
region: item.area,
certainty: item.certainty.toLowerCase(),
severity: item.severity.toLowerCase(),
event: parseEvent(item.event),
start: item.start,
end: item.end
});
}
});
}
if (warnigs.length > 0) {
const sorted = warnigs.sort((a, b) => new Date(a.start) - new Date(b.start));
sorted.forEach(item => {
const message = interpolate(config.meteoAlerts.messageTemplate, {
region: item.region,
start: formatDate(item.start),
end: formatDate(item.end),
event: config.meteoAlerts.events[item.event] ?? item.event,
severity: config.meteoAlerts.severity[item.severity],
certainty: config.meteoAlerts.certainty[item.certainty]
});
sendAlert(message, channels.weather);
meteoAlerts[item.id] = Date.now();
utils.sleep(30 * 1000);
});
}
}
function interpolate(str, data) {
return str.replace(/\{([^}]+)\}/g, (_, key) => {
return data[key] ?? "";
});
}
function parseEvent(event) {
const start = event.indexOf(' ');
const end = event.lastIndexOf(' ');
return event.substring(start + 1, end).trim().toLowerCase().replace('-', '');
}
function formatDate(date) {
const dt = new Date(date);
return dt.toLocaleString("sk-SK", optionsShort)
}
async function onContactMessageReceived(message) {
console.log('Received contact message', message);
}
async function onChannelMessageReceived(message) {
console.log(`Received channel message`, message);
}
async function sendWeather(date) {
const chunks = utils.splitStringToByteChunks(await getWeather(), 130);
if (chunks.length === 0) return;
for (const message of chunks) {
await sendAlert(message, channels.weather);
}
}
async function getWeather() {
let weather = '';
try {
const res = await fetch('https://www.shmu.sk/sk/?page=1&id=meteo_tpredpoved_ba');
const html = await res.text();
console.debug(`downloaded ${html.length} bytes from shmu.sk`);
const document = new DOMParser().parseFromString(html, 'text/html');
const weatherEl = document.querySelector('.mp-section');
const weatherBits = Array.from(weatherEl.childNodes).filter(n => n.nodeType === 3).map(n => n.textContent).filter(t => /\w/.test(t));
weatherBits.push(
...Array.from(weatherEl.querySelectorAll('p')).map(e => e.textContent).filter(t => !/^Formul|Rozhovor/.test(t))
);
weather = utils.trimAndNormalize(weatherBits.join(' '));
}
catch (e) {
console.error(e)
}
return weather;
}
async function registerBlitzortungMqtt(blitzCallback, blitzArea) {
const client = await mqtt.connectAsync('mqtt://blitzortung.ha.sed.pl:1883');
const decoder = new TextDecoder();
client.on('message', (_, data) => {
const json = decoder.decode(data);
const blitzData = JSON.parse(json);
if (blitzData.lat < blitzArea.minLat || blitzData.lon < blitzArea.minLon ||
blitzData.lat > blitzArea.maxLat || blitzData.lon > blitzArea.maxLon) { return }
blitzCallback(blitzData);
});
await client.subscribeAsync('blitzortung/1.1/#');
}
function blitzHandler(blitzData) {
const blitz = utils.calculateHeadingAndDistance(config.myPosition.lat, config.myPosition.lon, blitzData.lat, blitzData.lon);
blitzBuffer.push({
key: `${blitz.heading}|${(blitz.distance / 10) | 0}`,
heading: blitz.heading,
distance: blitz.distance,
lat: blitzData.lat,
lon: blitzData.lon
});
}
async function sendAlert(message, channel) {
await connection.sendChannelTextMessage(
channel.channelIdx,
utils.shortenToBytes(message, 155)
);
console.log(`Sent out [${channel.name}]: ${message}`);
await utils.sleep(30 * 1000);
}
async function geoCodeChached(key, lat, lon) {
if (geoCache[key]) return geoCache[key];
const location = await utils.geoCode(lat, lon);
if (location) geoCache[key] = location;
return location;
}
async function blitzWarning() {
const counter = {};
for (const blitz of blitzBuffer) {
counter[blitz.key] = counter[blitz.key]++ ?? 1;
}
for (const key of Object.keys(counter)) {
if (counter[key] < 10 || seen.blitz[key]) continue;
const [heading, distance] = key.split('|');
if (!(heading && distance)) continue;
var data = blitzBuffer.find(b => b.key == key);
if (!data) continue;
const location = await geoCodeChached(key, data.lat, data.lon);
if (!location) continue;
await sendAlert(`🌩️ ${location} (${distance * 10}km ${config.compasNames[heading]})`, channels.alerts);
seen.blitz[key] = 1;
}
blitzBuffer = [];
}
await connection.connect();