You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

140 lines
3.9 KiB
JavaScript

2 years ago
const fs = require('fs')
const config = require('./config.json')
const axios = require('axios')
const stateMachine = {}
const umlautMapGermany = {
'\u00dc': ']',
'\u00c4': '[',
'\u00d6': "\\",
'\u00fc': '}',
'\u00e4': '{',
'\u00f6': '|',
'\u00df': '~',
}
const umlautMapIntl = {
'\u00dc': 'U',
'\u00c4': 'A',
'\u00d6': 'O',
'\u00fc': 'u',
'\u00e4': 'a',
'\u00f6': 'o',
'\u00df': 'ss',
}
function replaceUmlaute(str) {
let umlautMap = config.germanUmlautSupport
? umlautMapGermany
: umlautMapIntl
return str
.replace(/[\u00dc|\u00c4|\u00d6][a-z]/g, (a) => {
const big = umlautMap[a.slice(0, 1)];
return big.charAt(0) + big.charAt(1).toLowerCase() + a.slice(1);
})
.replace(new RegExp('['+Object.keys(umlautMap).join('|')+']',"g"),
(a) => umlautMap[a]
);
}
async function sendPage(preset, payload) {
console.log(preset, payload)
try {
await axios.post(new URL(config.pager.url).origin + '/api/message/' + (!!preset ? 'preset' : 'advanced'), Object.assign( !!preset
? { preset }
: { ...config.pager.params } // backward compatibility
, { payload: payload }))
} catch (e) {}
}
async function checkWOMAS() {
for (region of config.regions) {
if (!region.active) continue
try {
const regId = region.regionsID.substring(0, 6) + '000000'
let dashboardData = await axios.get('https://warnung.bund.de/api31/dashboard/' + regId + '.json')
//console.log('dashboardData', dashboardData.data)
for (let alarmHead of dashboardData.data) {
if (alarmHead.id.indexOf('dwd.') == 0) continue
2 years ago
if (!stateMachine[ alarmHead.id ]) {
let alarmData = await axios.get('https://nina.api.proxy.bund.dev/api31/warnings/' + alarmHead.id + '.json')
for (let infoData of alarmData.data.info) {
await sendPage(region.preset, replaceUmlaute(infoData.headline))
break
2 years ago
}
console.log(alarmHead.id, stateMachine[ alarmHead.id ])
2 years ago
stateMachine[ alarmHead.id ] = alarmData.data.info.length
}
//console.log('alarmData', alarmData.data)
}
/*let msg = ""
rssData.entries.sort((a,b) => new Date(b.published).valueOf() - new Date(a.published).valueOf())
if (rssData.entries.length > 0) {
msg = rssData.entries[0].description
msg = msg.replace('DWD WETTERWARNUNG:', 'DWD:')
msg = msg.replace(' in ', ' ')
msg = msg.replace(' von ', '/')
msg = msg.indexOf('Quelle:') > -1
? msg.split('Quelle:')[0]
: msg
msg = replaceUmlaute(msg)
if (!!stateMachine[ region.dwdID ]) {
if (stateMachine [ region.dwdID ] != msg) {
await sendPage( msg )
stateMachine [ region.dwdID ] = msg
}
} else {
stateMachine [ region.dwdID ] = msg
// if initial state is unknown and we have an alert, send it anyway
if (msg.indexOf('Es sind keine Warnungen') == -1) {
await sendPage( msg )
}
}
}
*/
} catch (e) { console.error(e) }
}
}
function main() {
// listen
setTimeout(checkWOMAS, 0)
setInterval(checkWOMAS, 5 * 60 * 1e3)
}
main()
const express = require('express')
const appConfig = express()
appConfig.use(express.json({ limit: '1mb' }))
2 years ago
appConfig.use(express.static('html'))
appConfig.use(express.static(__dirname + '/node_modules/@mdi/font'))
/** CONFIG Routes */
appConfig.get('/config', async (req, res) => {
return res.json(JSON.parse(fs.readFileSync('config.json')))
})
appConfig.get('/api/deliveryPresets', async (req, res) => {
const presets = await axios.get(new URL(config.pager.url).origin + '/api/deliveryPresets')
return res.json(presets.data)
})
appConfig.post('/config', async (req, res) => {
if (!(!!req.body.pager)) return res.status(403).json(false)
if (!(!!req.body.regions)) return res.status(403).json(false)
console.log(req.body)
fs.writeFileSync('config.json', JSON.stringify(req.body, null, "\t"))
return res.json(true)
})
appConfig.post('/restart', (req, res) => {
process.exit(1)
})
appConfig.listen(3090, '0.0.0.0' || config.host || '127.0.0.1')