FIX: XMLHttpRequest blocked by CORS policy
* move http query to `node_helper` * replace `XMLHttpRequest` by `node-fetch` * implement SocketNotifications
This commit is contained in:
parent
e125808c57
commit
741d2d1838
2 changed files with 38 additions and 23 deletions
|
@ -9,11 +9,10 @@
|
|||
Module.register("MMM-MyCovid19", {
|
||||
defaults: {
|
||||
region: "Bonn",
|
||||
updateInterval: 3 * 60 * 60 * 1000, // 4h
|
||||
updateInterval: 3 * 60 * 60 * 1000, // 3h
|
||||
fadeSpeed: 0,
|
||||
showHeader: true,
|
||||
appendLocationNameToHeader: true,
|
||||
data_url: "https://rp-online.de/app/interaktiv_server/data/rki_nrw_mapped.json.php",
|
||||
},
|
||||
cases: {
|
||||
new7: "N/A",
|
||||
|
@ -27,13 +26,13 @@ Module.register("MMM-MyCovid19", {
|
|||
|
||||
start: function() {
|
||||
// init module and set update intervall
|
||||
var self = this;
|
||||
Log.log(this.name + ' is started!');
|
||||
self.update_data();
|
||||
var self = this
|
||||
Log.log(self.name + ' is started!');
|
||||
self.sendSocketNotification("{{MODULE_NAME}}-get_cases", self.config.region);
|
||||
setInterval(function() {
|
||||
self.update_data();
|
||||
self.updateDom(this.config.fadeSpeed);
|
||||
}, this.config.updateInterval);
|
||||
self.sendSocketNotification("{{MODULE_NAME}}-get_cases", self.config.region);
|
||||
self.updateDom(self.config.fadeSpeed);
|
||||
}, self.config.updateInterval);
|
||||
},
|
||||
|
||||
getHeader: function() {
|
||||
|
@ -62,20 +61,9 @@ Module.register("MMM-MyCovid19", {
|
|||
return { cases: this.cases }
|
||||
},
|
||||
|
||||
update_data: function() {
|
||||
// get and parse json data from source
|
||||
var xmlHttp = new XMLHttpRequest();
|
||||
xmlHttp.open( "GET", this.config.data_url, false ); // false for synchronous request
|
||||
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"];
|
||||
socketNotificationReceived: function(notification, payload) {
|
||||
// receive cases from node_helper
|
||||
this.cases = payload;
|
||||
this.updateDom(this.config.fadeSpeed);
|
||||
},
|
||||
});
|
||||
|
|
27
node_helper.js
Normal file
27
node_helper.js
Normal 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)
|
||||
})
|
||||
},
|
||||
});
|
Loading…
Reference in a new issue