From 22e7bd89ddc1a051e86a9c5acea7ca4144dc751e Mon Sep 17 00:00:00 2001 From: Sebastian Mark Date: Sun, 26 Jul 2020 10:50:34 +0200 Subject: [PATCH] Genesis --- .gitignore | 1 + MMM-covid19.css | 14 ++++++ MMM-covid19.js | 129 ++++++++++++++++++++++++++++++++++++++++++++++++ README.md | 39 +++++++++++++++ 4 files changed, 183 insertions(+) create mode 100644 .gitignore create mode 100644 MMM-covid19.css create mode 100644 MMM-covid19.js create mode 100644 README.md diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..37f05f2 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.import diff --git a/MMM-covid19.css b/MMM-covid19.css new file mode 100644 index 0000000..93bbf9e --- /dev/null +++ b/MMM-covid19.css @@ -0,0 +1,14 @@ +.covid19 { + text-align: left; + font-size: smaller; + line-height: 1.255em; +} + +.covid19 .ts { + font-size: small; + text-align: right; +} + +.covid19 .new7 .good { color: darkgreen; } +.covid19 .new7 .warn { color: darkorange; } +.covid19 .new7 .bad { color: darkred; } diff --git a/MMM-covid19.js b/MMM-covid19.js new file mode 100644 index 0000000..c82bf45 --- /dev/null +++ b/MMM-covid19.js @@ -0,0 +1,129 @@ +/* Magic Mirror + * Module: MMM-covid19 + * + * Author: Sebastian Mark + * CC-BY-SA (https://creativecommons.org/licenses/by-sa/4.0/deed.de) + * for civil use only +*/ + +Module.register("MMM-covid19", { + defaults: { + region: "Bonn", + updateInterval: 3 * 60 * 60 * 1000, // 4h + 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", + active: "N/A", + total: "N/A", + death: "N/A", + cured: "N/A", + trend: "N/A", + ts: "N/A", + }, + + start: function() { + // init module and set update intervall + var self = this; + Log.log(this.name + ' is started!'); + setInterval(function() { + self.updateDom(this.config.fadeSpeed); + }, this.config.updateInterval); + }, + + getStyles: function () { + // load custom css for module + return ["MMM-covid19.css"]; + }, + + getHeader: function() { + // set module header + if (this.config.showHeader) { + this.data.header = "COVID-19 Daten"; + if (this.config.appendLocationNameToHeader) { + this.data.header += " für " + this.config.region; + } + } + return this.data.header; + }, + + getDom: function() { + // get/update covid data + this.update_data(); + + // create layout and output + var wrapper = document.createElement("div"); + var new7_wrapper = document.createElement("span"); + var active_wrapper = document.createElement("span"); + var total_wrapper = document.createElement("span"); + var death_wrapper = document.createElement("span"); + var cured_wrapper = document.createElement("span"); + var ts_wrapper = document.createElement("div"); + + + wrapper.className = "covid19"; + + new7_wrapper.className = "new7"; + new7_wrapper.innerHTML = "Neu: " + this.cases.new7 + " " + this.cases.trend + "
"; + wrapper.appendChild(new7_wrapper); + + active_wrapper.className = "active"; + active_wrapper.innerHTML = "Aktiv: " + this.cases.active + "
"; + wrapper.appendChild(active_wrapper); + + total_wrapper.className = "total"; + total_wrapper.innerHTML = "Gesamt: " + this.cases.total + "
"; + wrapper.appendChild(total_wrapper); + + death_wrapper.className = "death"; + death_wrapper.innerHTML = "Tote: " + this.cases.death + "
"; + wrapper.appendChild(death_wrapper); + + cured_wrapper.className = "cured"; + cured_wrapper.innerHTML = "Genesen: " + this.cases.cured + "
"; + wrapper.appendChild(cured_wrapper); + + ts_wrapper.className = "ts"; + ts_wrapper.innerHTML = this.cases.ts; + wrapper.appendChild(ts_wrapper); + + return wrapper; + }, + + 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.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"]; + + // determine trend: + // calc this and last weeks cases and substract + // results the different of new cases between this and last week + // +10: red (arrow up) + // +0: orange (arrow up) + // 0: normal (arrow right) + // -0: green (arrow down) + currentToLastWeekDiff = (data[this.config.region]["last_7"] * 2) - data[this.config.region]["last_14"] + if (currentToLastWeekDiff < 0) { + this.cases.trend = ""; + } else if (currentToLastWeekDiff == 0) { + this.cases.trend = "&#rarr;"; + } else if (currentToLastWeekDiff > 10) { + this.cases.trend = ""; + } else if (currentToLastWeekDiff > 0) { + this.cases.trend = ""; + } + }, +}); diff --git a/README.md b/README.md new file mode 100644 index 0000000..6c351cb --- /dev/null +++ b/README.md @@ -0,0 +1,39 @@ +# Module: MMM-covid19 + +This module displays COVID-19 data aggregated by [rp-online](https://rp-online.de). + +## Installation + +1. Navigate to the `MagicMirror/modules` directory. +1. Clone the module +1. Configure the module as per below +1. Restart MagicMirror + + +## Using the module +```javascript +modules: [ + { + module: "MMM-covid19", + position: "top_left", + config: { + // The config property is optional. + // If no config is set, an example calendar is shown. + // See 'Configuration options' for more information. + } + } +] +``` + +## Configuration options + +| Option | Description | +| ------ | ----------- | +| `region` | Choose region to be fetched
**Default:** `Bonn` | +| `updateInterval` | How often does the content needs to be fetched? (Milliseconds)
**Default:** 10800000 (4 hours) | +| `showHeader` | Show `COVID-19 Daten` as module header?
**Values:** `true` or `/false`
**Default:** `true` | +| `appendLocationNameToHeader` | Append region name to header?
**Values:** `true` or `/false`
**Default:** `true` | + +### List available regions + +`curl https://rp-online.de/app/interaktiv_server/data/rki_nrw_mapped.json.php | jq | awk '/^ "/ {print}'`