Skip to content

Commit

Permalink
Dev (#6)
Browse files Browse the repository at this point in the history
* ModType v2. Refactoring

* Internal refactoring

Co-authored-by: Jan Litzenburger <jan.litzenburger@sap.com>
  • Loading branch information
jalibu and Jan Litzenburger authored Oct 3, 2021
1 parent 0249716 commit c060bdd
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 32 deletions.
4 changes: 2 additions & 2 deletions MMM-NINA.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*! *****************************************************************************
mmm-nina
Version 1.1.1
Version 1.2.0
Simple Client Implementierung für die NINA Warn App für die MagicMirror² Plattform.
Please submit bugs at https://github.com/jalibu/MMM-NINA/issues
Expand All @@ -11,4 +11,4 @@
This file is auto-generated. Do not edit.
***************************************************************************** */

!function(){"use strict";Module.register("MMM-NINA",{defaults:{ags:"110000000000",maxAgeInHours:6,maxWidth:null,showIcon:!0,showNoWarning:!1,updateIntervalInSeconds:120},getStyles:()=>["font-awesome.css","MMM-NINA.css"],getScripts:()=>["moment.js"],getTranslations:()=>({en:"translations/en.json",de:"translations/de.json"}),getTemplate:()=>"templates/MMM-NINA.njk",getTemplateData(){return{config:this.config,alerts:this.alerts}},start(){this.loadData(),this.scheduleUpdate(),this.updateDom()},scheduleUpdate(){setInterval((()=>{this.loadData()}),1e3*this.config.updateIntervalInSeconds)},loadData(){this.sendSocketNotification("GET_NINA_ALERTS",this.config)},socketNotificationReceived(t,e){"NINA_ALERTS_RESPONSE"===t&&(this.alerts=e.map((t=>(t.date=moment(new Date(t.sent)).format("DD.MM.YYYY - HH:mm"),t))),this.updateDom(),console.debug("Alerts",this.alerts))}})}();
!function(t){"use strict";function e(t){if(t&&t.__esModule)return t;var e=Object.create(null);return t&&Object.keys(t).forEach((function(s){if("default"!==s){var a=Object.getOwnPropertyDescriptor(t,s);Object.defineProperty(e,s,a.get?a:{enumerable:!0,get:function(){return t[s]}})}})),e.default=t,Object.freeze(e)}var s=e(t);Module.register("MMM-NINA",{defaults:{ags:"110000000000",maxAgeInHours:6,maxWidth:null,showIcon:!0,showNoWarning:!1,updateIntervalInSeconds:120},getStyles:()=>["font-awesome.css","MMM-NINA.css"],getScripts:()=>["moment.js"],getTranslations:()=>({en:"translations/en.json",de:"translations/de.json"}),getTemplate:()=>"templates/MMM-NINA.njk",getTemplateData(){return{config:this.config,alerts:this.alerts}},start(){this.loadData(),this.scheduleUpdate(),this.updateDom()},scheduleUpdate(){setInterval((()=>{this.loadData()}),1e3*this.config.updateIntervalInSeconds)},loadData(){this.sendSocketNotification("NINA_ALERTS_REQUEST",this.config)},socketNotificationReceived(t,e){if("NINA_ALERTS_RESPONSE"===t){const t=e;this.alerts=t.map((t=>(t.date=moment(new Date(t.sent)).format("DD.MM.YYYY - HH:mm"),t))),this.updateDom(),s.log("Alerts",this.alerts)}}})}(Log);
6 changes: 3 additions & 3 deletions node_helper.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*! *****************************************************************************
mmm-nina
Version 1.1.1
Version 1.2.0
Simple Client Implementierung für die NINA Warn App für die MagicMirror² Plattform.
Please submit bugs at https://github.com/jalibu/MMM-NINA/issues
Expand All @@ -11,7 +11,7 @@
This file is auto-generated. Do not edit.
***************************************************************************** */

"use strict";var e=require("node_helper"),t=require("node-fetch");function n(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}function r(e){if(e&&e.__esModule)return e;var t=Object.create(null);return e&&Object.keys(e).forEach((function(n){if("default"!==n){var r=Object.getOwnPropertyDescriptor(e,n);Object.defineProperty(t,n,r.get?r:{enumerable:!0,get:function(){return e[n]}})}})),t.default=e,Object.freeze(t)}var o=r(e),i=n(t);
"use strict";var e=require("node_helper"),t=require("logger"),n=require("node-fetch");function r(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}function i(e){if(e&&e.__esModule)return e;var t=Object.create(null);return e&&Object.keys(e).forEach((function(n){if("default"!==n){var r=Object.getOwnPropertyDescriptor(e,n);Object.defineProperty(t,n,r.get?r:{enumerable:!0,get:function(){return e[n]}})}})),t.default=e,Object.freeze(t)}var a=i(e),o=i(t),s=r(n);
/*! *****************************************************************************
Copyright (c) Microsoft Corporation.
Expand All @@ -26,4 +26,4 @@ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
***************************************************************************** */
function a(e,t,n,r){return new(n||(n=Promise))((function(o,i){function a(e){try{c(r.next(e))}catch(e){i(e)}}function s(e){try{c(r.throw(e))}catch(e){i(e)}}function c(e){var t;e.done?o(e.value):(t=e.value,t instanceof n?t:new n((function(e){e(t)}))).then(a,s)}c((r=r.apply(e,t||[])).next())}))}module.exports=o.create({start(){console.log(`${this.name} helper method started...`)},transformNinaAlerts(e,t){const n=(new Date).getTime();return e.filter((e=>(n-Date.parse(e.sent))/36e5<=t.maxAgeInHours))},harmonizeAgs:e=>`${e.substring(0,e.length-7)}0000000`,socketNotificationReceived(e,t){return a(this,void 0,void 0,(function*(){if("GET_NINA_ALERTS"===e){const e=yield i.default(`https://warnung.bund.de/api31/dashboard/${this.harmonizeAgs(t.ags)}.json`);if(e.ok)try{const n=yield e.json();this.sendSocketNotification("NINA_ALERTS_RESPONSE",this.transformNinaAlerts(n,t))}catch(e){console.warn("There was a problem requesting the NINA API",e)}else console.warn(`There was a problem ${e.status} requesting the NINA API:`,e.statusText)}else console.warn(`${e} is invalid notification`)}))}});
function c(e,t,n,r){return new(n||(n=Promise))((function(i,a){function o(e){try{c(r.next(e))}catch(e){a(e)}}function s(e){try{c(r.throw(e))}catch(e){a(e)}}function c(e){var t;e.done?i(e.value):(t=e.value,t instanceof n?t:new n((function(e){e(t)}))).then(o,s)}c((r=r.apply(e,t||[])).next())}))}class u{static transformNinaAlerts(e,t){const n=(new Date).getTime();return e.filter((e=>(n-Date.parse(e.sent))/36e5<=t.maxAgeInHours))}static harmonizeAgs(e){return`${e.substring(0,e.length-7)}0000000`}}module.exports=a.create({start(){o.log(`${this.name} helper method started...`)},bla:()=>5,socketNotificationReceived(e,t){return c(this,void 0,void 0,(function*(){if("NINA_ALERTS_REQUEST"===e){const e=t,n=yield s.default(`https://warnung.bund.de/api31/dashboard/${u.harmonizeAgs(e.ags)}.json`);if(n.ok)try{const t=yield n.json();this.sendSocketNotification("NINA_ALERTS_RESPONSE",u.transformNinaAlerts(t,e))}catch(e){o.warn("There was a problem requesting the NINA API",e)}else o.warn(`Error ${n.status} requesting the NINA API:`,n.statusText)}else o.warn(`${e} is invalid notification`)}))}});
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mmm-nina",
"version": "1.1.1",
"version": "1.2.0",
"description": "Simple Client Implementierung für die NINA Warn App für die MagicMirror² Plattform.",
"main": "MMM-NINA.js",
"repository": {
Expand Down
10 changes: 7 additions & 3 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,20 @@ const bannerText = `/*! ********************************************************
export default [
{
input: './src/frontend/Frontend.ts',
external: ['logger'],
plugins: [typescript({ module: 'ESNext' }), nodeResolve(), commonjs(), terser(), banner2(() => bannerText)],
output: {
file: './' + pkg.main,
format: 'iife'
format: 'iife',
globals: {
logger: 'Log'
}
}
},
{
input: './src/backend/Backend.ts',
external: ['node_helper', 'node-fetch'],
plugins: [typescript({ module: 'ESNext' }), terser(), banner2(() => bannerText)],
external: ['node_helper', 'logger', 'node-fetch'],
plugins: [typescript({ module: 'ESNext' }), nodeResolve(), terser(), banner2(() => bannerText)],
output: {
file: './node_helper.js',
format: 'cjs'
Expand Down
34 changes: 15 additions & 19 deletions src/backend/Backend.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,35 @@
import * as NodeHelper from 'node_helper'
import * as Log from 'logger'
import fetch from 'node-fetch'
import { Config } from '../types/Config'
import { Alert } from '../types/Alert'
import Utils from './Utils'

module.exports = NodeHelper.create({
start() {
console.log(`${this.name} helper method started...`)
Log.log(`${this.name} helper method started...`)
},

transformNinaAlerts(alerts: any[], config: Config) {
const now = new Date().getTime()

const filtered = alerts.filter((alert) => (now - Date.parse(alert.sent)) / (1000 * 60 * 60) <= config.maxAgeInHours)

return filtered
},

harmonizeAgs(ags: string): string {
return `${ags.substring(0, ags.length - 7)}0000000`
bla() {
return 5
},

async socketNotificationReceived(notification, payload) {
if (notification === 'GET_NINA_ALERTS') {
const response = await fetch(`https://warnung.bund.de/api31/dashboard/${this.harmonizeAgs(payload.ags)}.json`)
async socketNotificationReceived(notification: string, payload: unknown) {
if (notification === 'NINA_ALERTS_REQUEST') {
const config = payload as Config
const response = await fetch(`https://warnung.bund.de/api31/dashboard/${Utils.harmonizeAgs(config.ags)}.json`)
if (response.ok) {
try {
const alerts = await response.json()
this.sendSocketNotification('NINA_ALERTS_RESPONSE', this.transformNinaAlerts(alerts, payload))
const alerts: Alert[] = await response.json()
this.sendSocketNotification('NINA_ALERTS_RESPONSE', Utils.transformNinaAlerts(alerts, config))
} catch (err) {
console.warn(`There was a problem requesting the NINA API`, err)
Log.warn(`There was a problem requesting the NINA API`, err)
}
} else {
console.warn(`There was a problem ${response.status} requesting the NINA API:`, response.statusText)
Log.warn(`Error ${response.status} requesting the NINA API:`, response.statusText)
}
} else {
console.warn(`${notification} is invalid notification`)
Log.warn(`${notification} is invalid notification`)
}
}
})
16 changes: 16 additions & 0 deletions src/backend/Utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Alert } from '../types/Alert'
import { Config } from '../types/Config'

export default class Utils {
static transformNinaAlerts(alerts: Alert[], config: Config): Alert[] {
const now = new Date().getTime()

const filtered = alerts.filter((alert) => (now - Date.parse(alert.sent)) / (1000 * 60 * 60) <= config.maxAgeInHours)

return filtered
}

static harmonizeAgs(ags: string): string {
return `${ags.substring(0, ags.length - 7)}0000000`
}
}
13 changes: 9 additions & 4 deletions src/frontend/Frontend.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import * as Log from 'logger'
import { Config } from '../types/Config'
import { Alert } from '../types/Alert'

// Global or injected variable declarations
// eslint-disable-next-line @typescript-eslint/no-explicit-any
declare const moment: any

Module.register<Config>('MMM-NINA', {
Expand Down Expand Up @@ -52,19 +55,21 @@ Module.register<Config>('MMM-NINA', {
},

loadData() {
this.sendSocketNotification('GET_NINA_ALERTS', this.config)
this.sendSocketNotification('NINA_ALERTS_REQUEST', this.config)
},

socketNotificationReceived(notificationIdentifier: string, payload: any) {
socketNotificationReceived(notificationIdentifier: string, payload: unknown) {
if (notificationIdentifier === 'NINA_ALERTS_RESPONSE') {
this.alerts = payload.map((alert) => {
const alerts = payload as Alert[]
this.alerts = alerts.map((alert: Alert) => {
// eslint-disable-next-line no-param-reassign
alert.date = moment(new Date(alert.sent)).format('DD.MM.YYYY - HH:mm')

return alert
})
this.updateDom()

console.debug('Alerts', this.alerts)
Log.log('Alerts', this.alerts)
}
}
})
4 changes: 4 additions & 0 deletions src/types/Alert.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export type Alert = {
sent: string
date: string
}

0 comments on commit c060bdd

Please sign in to comment.