const config = require('./config.json') const axios = require('axios') const fs = require('fs') const moment = require('moment') moment.locale('yes', { week : { dow : 1 } }) const express = require('express') const appConfig = express() appConfig.use(express.json()) appConfig.use(express.static('html')) appConfig.use(express.static(__dirname + '/node_modules/@mdi/font')) async function minuteCheck() { let timeRN = moment() //console.log('timeRN', timeRN.format("HH:mm")) for (let alarm of config.alarms) { let alarmTime = moment() .hours(+alarm.alarmTime.split(':') [ 0 ]) .minutes(+alarm.alarmTime.split(':') [ 1 ]) let secDiff = alarmTime.diff(timeRN, 'seconds') // //console.log(alarmTime.format("HH:mm"), secDiff) if (alarmTime.format("HH:mm") == timeRN.format("HH:mm")) { //console.log("TRIGGER", alarm.alarmSchedulingMode) let alarmTrigger = false switch (alarm.alarmSchedulingMode) { case 'monthlyAtSpecificDate': alarmTrigger = (timeRN.date() == alarmTime.specificWeekday) break; case 'monthlyFirstOccWeekday': // wish Date = alarmTime.firstOccWeekday let thisMonthFirstWeekDayOccurance = -1 let monthFirstDay = moment().startOf('month') while (monthFirstDay.weekday() != alarm.firstOccWeekday) monthFirstDay.add(1, 'day') thisMonthFirstWeekDayOccurance = monthFirstDay.date() //console.log(monthFirstDay.date(), monthFirstDay.weekday()) //console.log('monthly first occurance for', alarm.firstOccWeekday, 'is', thisMonthFirstWeekDayOccurance) alarmTrigger = (timeRN.date() == thisMonthFirstWeekDayOccurance) break; case 'weekly': for (let weekDayKey of Object.keys(alarm.weekDay)) { //console.log(weekDayKey, alarm.weekDay) if (+weekDayKey === +timeRN.weekday()) { //console.log('weekday match', 'result is', alarm.weekDay[weekDayKey] ) alarmTrigger = alarm.weekDay[weekDayKey] break; } } break; } if (alarmTrigger === true) { await axios.post(new URL(config.pager.url).origin + '/api/message/' + (!!alarm.preset ? 'preset' : 'advanced'), Object.assign( !!alarm.preset ? { preset: alarm.preset } : { ...alarm.params } // backward compatibility , { payload: alarm.payload })) } } } } async function main() { let now = moment() await minuteCheck() setTimeout(() => setInterval(minuteCheck, 60e3), now.diff(moment().add(1, 'minute'), 'seconds') * 1e3 + 5e3) } main() /** 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.alarms)) return res.status(403).json(false) if (!(!!req.body.pager)) 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(3070, '0.0.0.0' || config.host || '127.0.0.1')