-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmmm-uk-pollen-forecast.js
172 lines (126 loc) · 5.64 KB
/
mmm-uk-pollen-forecast.js
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
171
172
Module.register("mmm-uk-pollen-forecast", {
// Default module config.
defaults: {
update_interval_hours : 2, // minimum is 0.25
region: 'se', // region, required
first_display_date_DD_MM: '01-03', // optional, show the calendar between particular days
// format: DD-MM, e.g. 01-03 ==> display from 1st March
last_display_date_DD_MM: '01-09', // optional
// format: DD-MM, e.g. 01-09 ==> hide after 1st September
// special cases: '' or 'always' will always show the module
days_to_show: 5, // optional, choose how many forecast days to show. range is 0 - 5, where
// 0 ==> no icons, 5 ==> 5 icons. default is 5. min is 0, max is 5.
show_forecast_text: 'true', // show the forecast text. optional. default is true.
show_forecast_last_issued_text: 'true' // show when the forecast was last issued. optional. default is true.
},
// Define required scripts.
getScripts: function() {
return ["buffer.js, moment.js"];
},
start: function() {
Log.info("Starting module: " + this.name);
Log.info(this.name, ' config: ', this.config);
// days to show range
if(self.config.days_to_show < 0) {
self.config.days_to_show = 0;
}
if(self.config.days_to_show > 5 ) {
self.config.days_to_show = 5;
}
this.getPollen();
self = this;
if(self.config.update_interval_hours < 0.25) {
self.config.update_interval_hours = 0.25;
}
setInterval(function() {
self.getPollen();
}, self.config.update_interval_hours * 3600000 );
},
// Define required scripts.
getScripts: function() {
return [];
},
getStyles: function() {
return ["pollen-forecast.css", "pollen-forecast-icons.css"];
},
getPollen: function() {
self = this;
if(self.moduleIsVisible() == false) {
this.hide();
return;
} else {
this.show();
Log.info("mmm-uk-pollen-forecast: Getting allergies.");
this.sendSocketNotification("GET_POLLEN_DATA", {
config: this.config
});
}
},
socketNotificationReceived: function(notification, payload) {
if (notification === "ACHHOOOOO") {
self = this;
this.srcHtml = payload;
Log.info("pollen data gathering: ", payload);
if (typeof this.srcHtml !== "undefined") {
this.loaded = true;
this.updateDom();
}
}
},
// Override dom generator.
getDom: async function() {
var wrapper = document.createElement("div");
wrapper.className = "pollen-container";
if (typeof this.srcHtml !== "undefined") {
// load the html we scraped :)
wrapper.innerHTML = await this.fetchHtmlAsText('/modules/mmm-uk-pollen-forecast/cache/' + this.srcHtml);
// fiddle it
// add some header text
var region_heading = wrapper.getElementsByClassName("region-heading")[0];
region_heading.insertAdjacentHTML('afterbegin', "Pollen forecast for ");
// make header look like a magic mirror header
region_heading.classList.add("module-header");
var new_header = document.createElement('header');
new_header.innerHTML = region_heading.innerHTML;
region_heading.parentNode.replaceChild(new_header, region_heading);
// add line breaks to forecast text
if(self.config.show_forecast_text) {
var forecast = wrapper.getElementsByTagName("p")[0];
forecast.innerHTML=forecast.childNodes[0].nodeValue.replace(new RegExp('\\. ', 'g'), '. <br>');
}
// remove forecast icons if required
this.fiddleForecastTable(wrapper);
}
return wrapper;
},
fetchHtmlAsText: async function (url) {
const response = await fetch(url);
return await response.text();
},
fiddleForecastTable: function(wrapper){
self = this;
const days_to_show = self.config.days_to_show;
if(days_to_show == 5) { // do nothing
return;
}
var forecast_th = wrapper.getElementsByTagName("thead")[0].firstElementChild;
var forecast_tr = wrapper.getElementsByTagName("tbody")[0].firstElementChild;
for(i = days_to_show; i < 5 ; i++) { // remove unwanted days
forecast_th.removeChild(forecast_th.lastElementChild);
forecast_tr.removeChild(forecast_tr.lastElementChild);
}
},
moduleIsVisible: function () {
self = this;
if(self.config.first_display_date_DD_MM == 'always' || self.config.first_display_date_DD_MM == '' ) {
return true;
}
if(self.config.last_display_date_DD_MM == 'always' || self.config.last_display_date_DD_MM == '' ) {
return true;
}
var now = moment();
var firstDay = moment(self.config.first_display_date_DD_MM + "-" + now.year().toString(), "DD-MM-YYYY");
var lastDay = moment(self.config.last_display_date_DD_MM + "-" + now.year().toString(), "DD-MM-YYYY");
return now.isBetween(firstDay, lastDay, 'days', '[]');
},
});