-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMMM-BMW-DS.js
221 lines (183 loc) · 13.2 KB
/
MMM-BMW-DS.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/* Magic Mirror
* Module: MMM-BMW-DS
* By Mykle1
* MIT Licensed
*/
Module.register("MMM-BMW-DS", {
// Module config defaults.
defaults: {
apiKey: "", // Get FREE API key from darksky.net
tempUnits: "C", // C or F
lat: "40.123456", // Latitude
lng: "-74.123456", // Longitude
css: "1", // 1=default, 2=Clean, 3=Lord of the Rings, 4=handwriting, 5=Julee, 6=Englebert
ownTitle: "Current Conditions", // Default = Current Conditions
playSounds: "yes", // yes = weather sounds, no = no weather sounds
useHeader: false, // true if you want a header
header: "Your Header", // Any text you want. useHeader must be true
maxWidth: "100%",
animationSpeed: 3000,
initialLoadDelay: 4250,
retryDelay: 2500,
updateInterval: 5 * 60 * 1000,
iconArray: {
"clear-day": "clear",
"clear-night": "nt_clear",
"partly-cloudy-day": "partlycloudy",
"partly-cloudy-night": "nt_partlycloudy",
"cloudy": "mostlycloudy",
"rain": "rain",
"sleet": "sleet",
"snow": "chancesnow",
"wind": "fog",
"fog": "fog"
}
},
// Gets correct css file from config.js
getStyles: function() {
if(this.config.css != ""){
return ["modules/MMM-BMW-DS/css/MMM-BMW-DS" + this.config.css + ".css"];
} else {
return ["modules/MMM-BMW-DS/css/MMM-BMW-DS1.css"]; // default.css
}
},
getScripts: function() {
return ["moment.js"];
},
start: function() {
Log.info("Starting module: " + this.name);
this.sendSocketNotification('CONFIG', this.config);
this.config.lang = this.config.lang || config.language;
// Set locale.
this.url = "https://api.darksky.net/forecast/" + this.config.apiKey + "/" + this.config.lat + "," + this.config.lng + "?lang=" + config.language;
this.forecast = {};
this.scheduleUpdate();
},
getDom: function() {
var forecast = this.forecast;
// convert fahrenheit to celcius function
function to_celcius(t) {
return (t - 32) * 5 / 9;
}
// 12 or 24 hour time function based on config.js timeFormat //
function getTime(gTime) {
if (this.config.timeFormat == "12") {
gTime = moment(forecast.time).format("h:mm a");
} else {
gTime = moment(forecast.time).format("H:MM");
}
return gTime
}
var wrapper = document.createElement("div");
wrapper.className = "wrapper";
wrapper.style.maxWidth = this.config.maxWidth;
if (!this.loaded) {
wrapper.innerHTML = "Acquiring your weather . . .";
wrapper.classList.add("bright", "light", "small");
return wrapper;
}
if (this.config.useHeader != false) {
var header = document.createElement("header");
header.classList.add("xsmall", "bright", "light");
header.innerHTML = this.config.header;
wrapper.appendChild(header);
}
var current = document.createElement("div");
current.classList.add("small", "bright", "current");
// Check if element exists, courtesy of @CBD
var numbnuts = forecast.minutely;
if (typeof numbnuts !== 'undefined') { // This checks if element exists courtesy of @CBD
if (this.config.tempUnits != "F") {
if (this.config.ownTitle !== "") {
current.innerHTML = this.config.ownTitle + "     " + "<img class = image src=./modules/MMM-BMW-DS/icons/" + forecast.minutely.icon + ".png>" + "     " + Math.round(to_celcius(forecast.currently.temperature)) + "°C   @   " + getTime() + "   " + forecast.minutely.summary;
} else {
current.innerHTML = "<img class = image src=./modules/MMM-BMW-DS/icons/" + forecast.minutely.icon + ".png>" + "     " + Math.round(to_celcius(forecast.currently.temperature)) + "°C   @   " + getTime() + "   " + forecast.minutely.summary;
}
wrapper.appendChild(current);
} else {
if (this.config.ownTitle !== "") {
current.innerHTML = this.config.ownTitle + "     " + "<img class = image src=./modules/MMM-BMW-DS/icons/" + forecast.minutely.icon + ".png>" + "     " + Math.round(forecast.currently.temperature) + "°F   @   " + getTime() + "   " + forecast.minutely.summary;
} else {
current.innerHTML = "<img class = image src=./modules/MMM-BMW-DS/icons/" + forecast.minutely.icon + ".png>" + "     " + Math.round(forecast.currently.temperature) + "°F   @   " + getTime() + "   " + forecast.minutely.summary;
}
wrapper.appendChild(current);
}
} else {
if (this.config.tempUnits != "F") {
current.innerHTML = this.config.ownTitle + "     " + "<img class = image src=./modules/MMM-BMW-DS/icons/" + forecast.currently.icon + ".png>" + "     " + Math.round(to_celcius(forecast.currently.temperature)) + "°C   @   " + getTime() + "   " + forecast.currently.summary;
wrapper.appendChild(current);
} else {
current.innerHTML = this.config.ownTitle + "     " + "<img class = image src=./modules/MMM-BMW-DS/icons/" + forecast.currently.icon + ".png>" + "     " + Math.round(forecast.currently.temperature) + "°F   @   " + getTime() + "   " + forecast.currently.summary;
wrapper.appendChild(current);
}
}
var summary = document.createElement("div");
summary.classList.add("xsmall", "bright", "summary");
summary.innerHTML = forecast.hourly.summary; // + "<img class = image src=./modules/MMM-BMW/icons/" + forecast.hourly.icon + ".png>";
wrapper.appendChild(summary);
// daily names, high/low and icons
var daily = document.createElement("div");
daily.classList.add("small", "bright", "daily");
daily.innerHTML = this.config.tempUnits != "C" ? /* ? shorthand if statement */
moment.unix(forecast.daily.data[0].time).local().format('ddd') + "  " + "<img class = image src=./modules/MMM-BMW-DS/icons/" + forecast.daily.data[0].icon + ".png>" + "  " + Math.round(forecast.daily.data[0].temperatureHigh) + "/" + Math.round(forecast.daily.data[0].temperatureLow) + "          " +
moment.unix(forecast.daily.data[1].time).local().format('ddd') + "  " + "<img class = image src=./modules/MMM-BMW-DS/icons/" + forecast.daily.data[1].icon + ".png>" + "  " + Math.round(forecast.daily.data[1].temperatureHigh) + "/" + Math.round(forecast.daily.data[1].temperatureLow) + "          " +
moment.unix(forecast.daily.data[2].time).local().format('ddd') + "  " + "<img class = image src=./modules/MMM-BMW-DS/icons/" + forecast.daily.data[2].icon + ".png>" + "  " + Math.round(forecast.daily.data[2].temperatureHigh) + "/" + Math.round(forecast.daily.data[2].temperatureLow) + "          " +
moment.unix(forecast.daily.data[3].time).local().format('ddd') + "  " + "<img class = image src=./modules/MMM-BMW-DS/icons/" + forecast.daily.data[3].icon + ".png>" + "  " + Math.round(forecast.daily.data[3].temperatureHigh) + "/" + Math.round(forecast.daily.data[3].temperatureLow) + "          " +
moment.unix(forecast.daily.data[4].time).local().format('ddd') + "  " + "<img class = image src=./modules/MMM-BMW-DS/icons/" + forecast.daily.data[4].icon + ".png>" + "  " + Math.round(forecast.daily.data[4].temperatureHigh) + "/" + Math.round(forecast.daily.data[4].temperatureLow) + "          " +
moment.unix(forecast.daily.data[5].time).local().format('ddd') + "  " + "<img class = image src=./modules/MMM-BMW-DS/icons/" + forecast.daily.data[5].icon + ".png>" + "  " + Math.round(forecast.daily.data[5].temperatureHigh) + "/" + Math.round(forecast.daily.data[5].temperatureLow) + "          " +
moment.unix(forecast.daily.data[6].time).local().format('ddd') + "  " + "<img class = image src=./modules/MMM-BMW-DS/icons/" + forecast.daily.data[6].icon + ".png>" + "  " + Math.round(forecast.daily.data[6].temperatureHigh) + "/" + Math.round(forecast.daily.data[6].temperatureLow) + "          "
:
/* : shorthand else */
moment.unix(forecast.daily.data[0].time).local().format('ddd') + "  " + "<img class = image src=./modules/MMM-BMW-DS/icons/" + forecast.daily.data[0].icon + ".png>" + "  " + Math.round(to_celcius(forecast.daily.data[0].temperatureHigh)) + "/" + Math.round(to_celcius(forecast.daily.data[0].temperatureLow)) + "          " +
moment.unix(forecast.daily.data[1].time).local().format('ddd') + "  " + "<img class = image src=./modules/MMM-BMW-DS/icons/" + forecast.daily.data[1].icon + ".png>" + "  " + Math.round(to_celcius(forecast.daily.data[1].temperatureHigh)) + "/" + Math.round(to_celcius(forecast.daily.data[1].temperatureLow)) + "          " +
moment.unix(forecast.daily.data[2].time).local().format('ddd') + "  " + "<img class = image src=./modules/MMM-BMW-DS/icons/" + forecast.daily.data[2].icon + ".png>" + "  " + Math.round(to_celcius(forecast.daily.data[2].temperatureHigh)) + "/" + Math.round(to_celcius(forecast.daily.data[2].temperatureLow)) + "          " +
moment.unix(forecast.daily.data[3].time).local().format('ddd') + "  " + "<img class = image src=./modules/MMM-BMW-DS/icons/" + forecast.daily.data[3].icon + ".png>" + "  " + Math.round(to_celcius(forecast.daily.data[3].temperatureHigh)) + "/" + Math.round(to_celcius(forecast.daily.data[3].temperatureLow)) + "          " +
moment.unix(forecast.daily.data[4].time).local().format('ddd') + "  " + "<img class = image src=./modules/MMM-BMW-DS/icons/" + forecast.daily.data[4].icon + ".png>" + "  " + Math.round(to_celcius(forecast.daily.data[4].temperatureHigh)) + "/" + Math.round(to_celcius(forecast.daily.data[4].temperatureLow)) + "          " +
moment.unix(forecast.daily.data[5].time).local().format('ddd') + "  " + "<img class = image src=./modules/MMM-BMW-DS/icons/" + forecast.daily.data[5].icon + ".png>" + "  " + Math.round(to_celcius(forecast.daily.data[5].temperatureHigh)) + "/" + Math.round(to_celcius(forecast.daily.data[5].temperatureLow)) + "          " +
moment.unix(forecast.daily.data[6].time).local().format('ddd') + "  " + "<img class = image src=./modules/MMM-BMW-DS/icons/" + forecast.daily.data[6].icon + ".png>" + "  " + Math.round(to_celcius(forecast.daily.data[6].temperatureHigh)) + "/" + Math.round(to_celcius(forecast.daily.data[6].temperatureLow)) + "          "
wrapper.appendChild(daily);
// Sound for rain, wind, thunder, etc.
if (forecast.hourly.data[0].icon == "rain" && this.config.playSounds == "yes"){
var sound = new Audio()
sound.src = 'modules/MMM-BMW-DS/sounds/rain.mp3'
sound.play()
} else if (forecast.hourly.data[0].icon == "thunder" && this.config.playSounds == "yes"){
var sound = new Audio();
sound.src = 'modules/MMM-BMW-DS/sounds/thunder.mp3';
sound.play();
} else if (forecast.hourly.data[0].icon == "wind" && this.config.playSounds == "yes"){
var sound = new Audio();
sound.src = 'modules/MMM-BMW-DS/sounds/wind.mp3';
sound.play();
}
return wrapper;
},
notificationReceived: function(notification, payload) {
if (notification === 'HIDE_WEATHER') {
this.hide(1000);
} else if (notification === 'SHOW_WEATHER') {
this.show(1000);
}
},
processWeather: function(data) {
this.forecast = data;
// console.log(this.forecast);
this.loaded = true;
},
scheduleUpdate: function() {
setInterval(() => {
this.getWeather();
}, this.config.updateInterval);
this.getWeather(this.config.initialLoadDelay);
},
getWeather: function() {
this.sendSocketNotification('GET_WEATHER', this.url);
},
socketNotificationReceived: function(notification, payload) {
if (notification === "WEATHER_RESULT") {
this.processWeather(payload);
this.updateDom(this.config.animationSpeed);
}
this.updateDom(this.config.initialLoadDelay);
},
});