commit d4d89ba30f94706851968f2fa284640b51ee5f07 Author: cheetah Date: Thu Nov 3 23:17:23 2022 +0100 first commit diff --git a/config.json b/config.json new file mode 100644 index 0000000..851e79a --- /dev/null +++ b/config.json @@ -0,0 +1,32 @@ +{ + "pager": { + "url": "http://127.0.0.1:3000/api/message/advanced", + "params": { + "type": "simple", + "routing": { + "device": "birdyslim", + "connectors": [ + [ + "ecityruf", + "1234567" + ] + ] + } + } + }, + "menuSupport": false, + "regions": [ + { + "name": "muc", + "active": true, + "dwdID": "mxx", + "_id": "eyJuYW1lIjoibXVjIiwiYWN0aXZlIjp0cnVlLCJkd2RJRCI6Im14eCIsIl9pZCI6ImV5SnVZVzFsSWpvaWJYVmpJaXdpWVdOMGFYWmxJanAwY25WbExDSmtkMlJKUkNJNkltMTRlQ0lzSWw5cFpDSTZJbVY1U25WWlZ6RnNTV3B2YVdKWVZtcEphWGRwV1ZkT01HRllXbXhKYW5Bd1kyNVdiRXhEU210a01sSktVa05KTmtsdE1YUmxRMGx6U1d3NWNGcERTVFpKYlZZMVUyNVdXbFo2Um5OVFYzQjJZVmRLV1ZadGNFcGhXR1J3VjFaa1QwMUhSbGxYYlhoS1lXNUJkMWt5TlZkaVJYaEVVMjEwYTAxc1NrdFZhMDVLVG10c2RFMVlVbXhSTUc4MVNXNHdQU0o5In0=" + }, + { + "name": "oberallgaeu", + "active": true, + "dwdID": "oax", + "_id": "eyJuYW1lIjoib2JlcmFsbGdhZXUiLCJhY3RpdmUiOnRydWUsImR3ZElEIjoib2F4IiwiX2lkIjoiZXlKdVlXMWxJam9pYjNOMFlXeHNaMkZsZFNJc0ltRmpkR2wyWlNJNmRISjFaU3dpWkhka1NVUWlPaUp2WVd3aWZRPT0ifQ==" + } + ] +} diff --git a/index.js b/index.js new file mode 100644 index 0000000..5d9e0a7 --- /dev/null +++ b/index.js @@ -0,0 +1,108 @@ +const fs = require('fs') + +const config = require('./config.json') + +const io = require("socket.io-client") +const axios = require('axios') +const socket = io(new URL(config.pager.url).origin) + +const { read } = require('feed-reader') + +const stateMachine = {} +socket.on('msgmgr:event', async (eventType, eventData) => { + console.log(eventType, eventData) +}) + +const umlautMap = { + '\u00dc': 'U', + '\u00c4': 'A', + '\u00d6': 'O', + '\u00fc': 'u', + '\u00e4': 'a', + '\u00f6': 'o', + '\u00df': 'ss', +} + +function replaceUmlaute(str) { + 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(payload) { + console.log(payload) + return (await axios.post(config.pager.url, Object.assign({ ...config.pager.params }, { payload }))) +} +async function checkDWD() { + for (region of config.regions) { + if (!region.active) continue + try { + let rssData = await read('https://wettwarn.de/rss/' + region.dwdID + '.rss') + 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(checkDWD, 0) + setInterval(checkDWD, 5 * 60 * 1e3) +} + + +main() + + + + + +const express = require('express') +const appConfig = express() +appConfig.use(express.json()) +appConfig.use(express.static('html')) + +/** CONFIG Routes */ + +appConfig.get('/config', async (req, res) => { + return res.json(JSON.parse(fs.readFileSync('config.json'))) +}) +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(3050, '0.0.0.0' || config.host || '127.0.0.1') diff --git a/package.json b/package.json new file mode 100644 index 0000000..367d48a --- /dev/null +++ b/package.json @@ -0,0 +1,25 @@ +{ + "name": "msg-email", + "version": "1.0.0", + "description": "", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/smartpager-network/msg-email.git" + }, + "author": "catSIXe", + "license": "ISC", + "bugs": { + "url": "https://github.com/smartpager-network/msg-email/issues" + }, + "homepage": "https://github.com/smartpager-network/msg-email#readme", + "dependencies": { + "axios": "^0.21.1", + "express": "^4.17.1", + "feed-reader": "^6.1.2", + "socket.io-client": "^4.4.1" + } +}