forked from mykle1/MMM-CoronaVirus
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMMM-CoronaVirus.js
More file actions
170 lines (145 loc) · 6.91 KB
/
Copy pathMMM-CoronaVirus.js
File metadata and controls
170 lines (145 loc) · 6.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/* Magic Mirror
* Module: MMM-CoronaVirus
*
* By Mykle1
*
*/
Module.register("MMM-CoronaVirus", {
// Module config defaults.
defaults: {
country: "",
totalCasesColor: "",
deathsColor: "",
newCasesColor: "",
recoveryColor: "",
criticalColor: "",
activeColor: "",
useHeader: false,
header: "",
maxWidth: "100%",
animationSpeed: 0,
initialLoadDelay: 1250,
retryDelay: 2500,
rotateInterval: 5 * 60 * 1000,
updateInterval: 15 * 60 * 1000,
},
getStyles: function() {
return ["MMM-CoronaVirus.css"];
},
getScripts: function() {
return ["moment.js"];
},
start: function() {
Log.info("Starting module: " + this.name);
this.sendSocketNotification('CONFIG', this.config);
// Set locale.
this.Virus = [];
this.World = {};
this.activeItem = 0;
this.rotateInterval = null;
this.scheduleUpdate();
},
getDom: function() {
var wrapper = document.createElement("div");
wrapper.className = "wrapper";
wrapper.style.maxWidth = this.config.maxWidth;
if (!this.loaded) {
wrapper.innerHTML = "Maintain Social Distance . . .";
wrapper.classList.add("bright", "light", "small");
return wrapper;
}
if (this.config.useHeader != false) {
var header = document.createElement("header");
header.classList.add("small", "bright", "header");
header.innerHTML = this.config.header;
wrapper.appendChild(header);
}
var people = this.World.cases; // to slow things down so data arrives
console.log(people);
// worldTotals
var worldTotals = document.createElement("div");
worldTotals.classList.add("small", "bright", "worldTotals");
worldTotals.innerHTML =
"World cases = " + '<font color='+this.config.totalCasesColor+'>' + people.toLocaleString() + '</font>' + "     " +
"World deaths = " + '<font color='+this.config.deathsColor+'>' + this.World.deaths.toLocaleString() + '</font>' + "     " +
"World recoveries = " + '<font color='+this.config.recoveryColor+'>' + this.World.recovered.toLocaleString() + '</font>' + "     " +
"Updated ~ " + moment.utc(this.World.updated).local().format('MMMM DD, YYYY ~ h:mm a');
wrapper.appendChild(worldTotals);
// singleCountry
var choice = this.choice;
var singleCountry = document.createElement("div");
singleCountry.classList.add("small", "bright", "singleCountry");
singleCountry.innerHTML = choice.country + "     " +
'<font color='+this.config.totalCasesColor+'>' + choice.cases.toLocaleString() + '</font>' + " total cases" + "     " +
'<font color='+this.config.newCasesColor+'>' + choice.todayCases.toLocaleString() + '</font>' + " new cases today" + "     " +
'<font color='+this.config.deathsColor+'>' + choice.deaths.toLocaleString() + '</font>' + " total deaths" + "     " +
'<font color='+this.config.deathsColor+'>' + choice.todayDeaths.toLocaleString() + '</font>' + " deaths today" + "     " +
'<font color='+this.config.recoveryColor+'>' + choice.recovered.toLocaleString() + '</font>' + " recoveries" + "     " +
'<font color='+this.config.activeColor+'>' + choice.active.toLocaleString() + '</font>' + " active cases" + "     " +
'<font color='+this.config.criticalColor+'>' + choice.critical.toLocaleString() + '</font>' + " in critical condition" + "     " +
choice.casesPerOneMillion.toLocaleString() + " cases per million";
wrapper.appendChild(singleCountry);
// loop through the obects
var keys = Object.keys(this.Virus);
if (keys.length > 0) {
if (this.activeItem >= keys.length) {
this.activeItem = 0;
}
var Virus = this.Virus[keys[this.activeItem]];
// totals by country
var countryTotals = document.createElement("div");
countryTotals.classList.add("small", "bright", "countryTotals");
countryTotals.innerHTML =
Virus.country + "     " +
'<font color='+this.config.totalCasesColor+'>' + Virus.cases.toLocaleString() + '</font>' + " total cases" + "     " +
'<font color='+this.config.newCasesColor+'>' + Virus.todayCases.toLocaleString() + '</font>' + " new cases today" + "     " +
'<font color='+this.config.deathsColor+'>' + Virus.deaths.toLocaleString() + '</font>' + " total deaths" + "     " +
'<font color='+this.config.deathsColor+'>' + Virus.todayDeaths.toLocaleString() + '</font>' + " deaths today" + "     " +
'<font color='+this.config.recoveryColor+'>' + Virus.recovered.toLocaleString() + '</font>' + " recoveries" + "     " +
'<font color='+this.config.activeColor+'>' + Virus.active.toLocaleString() + '</font>' + " active cases" + "     " +
'<font color='+this.config.criticalColor+'>' + Virus.critical.toLocaleString() + '</font>' + " in critical condition" + "     " +
Virus.casesPerOneMillion.toLocaleString() + " cases per million";
wrapper.appendChild(countryTotals);
}
return wrapper;
},
processVirus: function(data) {
this.Virus = data.result;
//console.log(this.Virus);
this.choice = data.choice;
//console.log(this.choice);
},
processWorld: function(data) {
this.World = data;
//console.log(this.World);
this.loaded = true;
},
scheduleCarousel: function() {
console.log("Carousel of CoronaVirus fucktion!");
this.rotateInterval = setInterval(() => {
this.activeItem++;
this.updateDom(this.config.animationSpeed);
}, this.config.rotateInterval);
},
scheduleUpdate: function() {
setInterval(() => {
this.getVirus();
}, this.config.updateInterval);
this.getVirus();
},
getVirus: function() {
this.sendSocketNotification('GET_VIRUS');
},
socketNotificationReceived: function(notification, payload) {
if (notification === "VIRUS_RESULT") {
this.processVirus(payload);
}
if (notification === "WORLD_RESULT") {
this.processWorld(payload);
}
if (this.rotateInterval == null) {
this.scheduleCarousel();
}
this.updateDom(this.config.initialLoadDelay);
},
});