This repository has been archived by the owner on Jan 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMMM-Weather.js
203 lines (165 loc) · 5.38 KB
/
MMM-Weather.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
Module.register("MMM-Weather", {
defaults: {
location: false,
locationID: false,
apiKey: "",
updateInterval: 10 * 60 * 10000,
animationSpeed: 1000,
units: config.units,
lang: config.language,
tableClass: "MMM-Weather",
initialLoadDelay: 0,
retryDelay: 2500,
apiVersion: "2.5",
apiBase: "https://api.openweathermap.org/data/",
weatherEndpoint: "weather"
},
firstEvent: false,
fetchedLocationName: "",
getScripts: function () {
return ["moment.js"];
},
getStyles: function () {
return ["weather-icons.css", "MMM-Weather.css"];
},
start: function () {
Log.info("Starting module: " + this.name);
moment.locale(config.language);
this.temperature = null;
this.weatherType = null;
this.loaded = false;
this.scheduleUpdate(this.config.initialLoadDelay);
},
getDom: function () {
var wrapper = document.createElement("div");
wrapper.className = this.config.tableClass;
if (this.config.apiKey === "") {
wrapper.innerHTML =
"Please set the correct openweather <i>apiKey</i> in config.";
wrapper.className = "dimmed light small";
return wrapper;
}
if (!this.loaded) {
wrapper.innerHTML = this.translate("LOADING");
wrapper.className = "dimmed light small";
return wrapper;
}
var large = document.createElement("div");
large.className = "module_content";
var firstLine = document.createElement("div");
firstLine.className = "first-line large";
var secondLine = document.createElement("div");
secondLine.className = "second-line medium";
var weatherIconSource = this.file(
"./weather-icons/" + this.weatherType + ".svg"
);
var weatherIcon = document.createElement("img");
weatherIcon.src = weatherIconSource;
weatherIcon.className = "weather-icon";
firstLine.appendChild(weatherIcon);
var temperature = document.createElement("span");
temperature.className = "bright";
temperature.innerHTML = " " + this.temperature + "°";
firstLine.appendChild(temperature);
var conditions = document.createElement("span");
conditions.className = "dimmed weather-conditions";
conditions.innerHTML = this.conditions;
secondLine.appendChild(conditions);
large.appendChild(firstLine);
large.appendChild(secondLine);
wrapper.appendChild(large);
return wrapper;
},
getHeader: function () {
return "";
},
updateWeather: function () {
if (this.config.apiKey === "") {
Log.error("MMM-Weather: API-Key not set!");
return;
}
var url =
this.config.apiBase +
this.config.apiVersion +
"/" +
this.config.weatherEndpoint +
this.getParams();
var self = this;
var retry = true;
var weatherRequest = new XMLHttpRequest();
weatherRequest.open("GET", url, true);
weatherRequest.onreadystatechange = function () {
if (this.readyState === 4) {
if (this.status === 200) {
self.processWeather(JSON.parse(this.response));
} else if (this.status === 401) {
self.updateDom(self.config.animationSpeed);
Log.error(self.name + ": Incorrect API-Key.");
retry = true;
} else {
Log.error(self.name + ": Could not load weather.");
}
if (retry) {
self.scheduleUpdate(self.loaded ? -1 : self.config.retryDelay);
}
}
};
weatherRequest.send();
},
getParams: function () {
var params = "?";
if (this.config.locationID) {
params += "id=" + this.config.locationID;
} else if (this.config.location) {
params += "q=" + this.config.location;
} else if (this.firstEvent && this.firstEvent.geo) {
params +=
"lat=" + this.firstEvent.geo.lat + "&lon=" + this.firstEvent.geo.lon;
} else if (this.firstEvent && this.firstEvent.location) {
params += "q=" + this.firstEvent.location;
} else {
this.hide(this.config.animationSpeed, { lockString: this.identifier });
return;
}
params += "&units=" + this.config.units;
params += "&lang=" + this.config.lang;
params += "&APPID=" + this.config.apiKey;
return params;
},
processWeather: function (data) {
if (!data || !data.main || typeof data.main.temp === "undefined") {
return;
}
this.humidity = parseFloat(data.main.humidity);
this.temperature = this.roundValue(data.main.temp);
this.fetchedLocationName = data.name;
this.weatherType = data.weather[0].icon;
this.conditions = data.weather[0].description;
var now = new Date();
this.show(this.config.animationSpeed, { lockString: this.identifier });
this.loaded = true;
this.updateDom(this.config.animationSpeed);
this.sendNotification("MMM-WEATHER_DATA", { data: data });
},
scheduleUpdate: function (delay) {
var nextLoad = this.config.updateInterval;
if (typeof delay !== "undefined" && delay >= 0) {
nextLoad = delay;
}
var self = this;
setTimeout(function () {
self.updateWeather();
}, nextLoad);
},
/* function(temperature)
* Rounds a temperature to 1 decimal or integer (depending on config.roundTemp).
*
* argument temperature number - Temperature.
*
* return string - Rounded Temperature.
*/
roundValue: function (temperature) {
var decimals = this.config.roundTemp ? 0 : 1;
return parseFloat(temperature).toFixed(decimals);
}
});