-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* ModType v2. Refactoring * Internal refactoring Co-authored-by: Jan Litzenburger <jan.litzenburger@sap.com>
- Loading branch information
Showing
8 changed files
with
57 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`) | ||
} | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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` | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export type Alert = { | ||
sent: string | ||
date: string | ||
} |