FIX: XMLHttpRequest blocked by CORS policy

* move http query to `node_helper`
* replace `XMLHttpRequest` by `node-fetch`
* implement SocketNotifications
This commit is contained in:
Sebastian Mark 2021-09-01 19:33:04 +02:00
parent e125808c57
commit 741d2d1838
2 changed files with 38 additions and 23 deletions

View file

@ -9,11 +9,10 @@
Module.register("MMM-MyCovid19", { Module.register("MMM-MyCovid19", {
defaults: { defaults: {
region: "Bonn", region: "Bonn",
updateInterval: 3 * 60 * 60 * 1000, // 4h updateInterval: 3 * 60 * 60 * 1000, // 3h
fadeSpeed: 0, fadeSpeed: 0,
showHeader: true, showHeader: true,
appendLocationNameToHeader: true, appendLocationNameToHeader: true,
data_url: "https://rp-online.de/app/interaktiv_server/data/rki_nrw_mapped.json.php",
}, },
cases: { cases: {
new7: "N/A", new7: "N/A",
@ -27,13 +26,13 @@ Module.register("MMM-MyCovid19", {
start: function() { start: function() {
// init module and set update intervall // init module and set update intervall
var self = this; var self = this
Log.log(this.name + ' is started!'); Log.log(self.name + ' is started!');
self.update_data(); self.sendSocketNotification("{{MODULE_NAME}}-get_cases", self.config.region);
setInterval(function() { setInterval(function() {
self.update_data(); self.sendSocketNotification("{{MODULE_NAME}}-get_cases", self.config.region);
self.updateDom(this.config.fadeSpeed); self.updateDom(self.config.fadeSpeed);
}, this.config.updateInterval); }, self.config.updateInterval);
}, },
getHeader: function() { getHeader: function() {
@ -62,20 +61,9 @@ Module.register("MMM-MyCovid19", {
return { cases: this.cases } return { cases: this.cases }
}, },
update_data: function() { socketNotificationReceived: function(notification, payload) {
// get and parse json data from source // receive cases from node_helper
var xmlHttp = new XMLHttpRequest(); this.cases = payload;
xmlHttp.open( "GET", this.config.data_url, false ); // false for synchronous request this.updateDom(this.config.fadeSpeed);
xmlHttp.send( null );
data = JSON.parse(xmlHttp.responseText);
// extract data
this.cases.new7 = data[this.config.region]["last_7"];
this.cases.new7_100k = data[this.config.region]["last_7_100000"];
this.cases.active = data[this.config.region]["active"];
this.cases.total = data[this.config.region]["cases"];
this.cases.death = data[this.config.region]["tot"];
this.cases.cured = data[this.config.region]["gesund"];
this.cases.ts = data[this.config.region]["date"];
}, },
}); });

27
node_helper.js Normal file
View file

@ -0,0 +1,27 @@
var NodeHelper = require("node_helper");
const fetch = require("node-fetch");
var data_url = "https://rp-online.de/app/interaktiv_server/data/rki_nrw_mapped.json.php";
var cases = {}
module.exports = NodeHelper.create({
socketNotificationReceived: function (notification, region) {
// get and parse json data from source
res = fetch(data_url)
.then(response=>response.json())
.then(data=>{
// extract data
cases.new7 = data[region]["last_7"];
cases.new7_100k = data[region]["last_7_100000"];
cases.active = data[region]["active"];
cases.total = data[region]["cases"];
cases.death = data[region]["tot"];
cases.cured = data[region]["gesund"];
cases.ts = data[region]["date"];
// send data back to module
this.sendSocketNotification("{{MODULE_NAME}}-set_cases", cases)
})
},
});