-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathMMM-WeatherBackground.js
213 lines (195 loc) · 6.66 KB
/
MMM-WeatherBackground.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
/* eslint-disable prettier/prettier */
/* global Log */
//
// MMM-WeatherBackground
//
Module.register("MMM-WeatherBackground", {
defaults: {
verbose: false,
source: "weather",
size: null, // "1920x480", whatever....
hemisphere: "n", // 'n', 's' or null/false // will be deprecated. use monthMap instead.
monthMap: [
"NewYear",
"winter",
"spring",
"spring flower",
"bright",
"summer heat",
"summer beach",
"summer vacation",
"autumn",
"autumn leaves",
"winter",
"christmas"
],
targetDOM: ".fullscreen.below", //null or DomSelector for target. (if null, weather will be targeted.)
notification: null, //if you use other weather module, modify this.
payloadConverter: null,
defaultCollection: null, // When matched collection not found, this will be used.
externalCollections: "collections.json", // or null
collections: {},
clientID: "",
sources: {
weather: {
notification: "CURRENTWEATHER_TYPE",
payloadConverter: (payload) => {
return payload.type.replaceAll("_", " ");
}
},
"MMM-NOAA3": {
notification: "WEATHER",
payloadConverter: (payload) => {
var now = new Date();
if (payload.sunset != null) {
var sunset = payload.sunset.split(":");
var sunsetTime = new Date(
now.getFullYear(),
now.getMonth(),
now.getDate(),
sunset[0],
sunset[1]
);
} else {
var sunsetTime = new Date(
now.getFullYear(),
now.getMonth(),
now.getDate(), 17,1)
}
var n = now.getTime() >= sunsetTime.getTime() ? "night" : "day";
var ret = payload.icon;
var iconMap = {
tstorms: "thunderstorm",
chancesnow: "snow"
};
if (ret in iconMap) {
ret = iconMap[ret];
}
return ret + " " + n;
}
},
"MMM-DarkSkyForecast": {
notification: "DARK_SKY_FORECAST_WEATHER_UPDATE",
payloadConverter: (payload) => {
console.log("Payload: " + payload.currently.icon);
return payload.currently.icon;
}
},
"MMM-OpenWeatherForecast": {
notification: "OPENWEATHER_FORECAST_WEATHER_UPDATE",
payloadConverter: (payload) => {
console.log("Payload: " + payload.current.weather[0].description);
return payload.current.weather.description;
}
}
}
},
getStyles: function () {
return ["MMM-WeatherBackground.css"];
},
start: function () {
this.collections = {};
this.monthMap = this.config.monthMap;
this.source =
typeof this.config.sources[this.config.source] !== undefined
? this.config.sources[this.config.source]
: this.config.sources["weather"];
this.listenNotification = this.config.notification
? this.config.notification
: this.source.notification;
this.payloadConverter = this.config.payloadConverter
? this.config.payloadConverter
: this.source.payloadConverter;
if (this.config.verbose) console.log("Weather source set to " + this.source.notification);
if (typeof this.config.externalCollections === "string") {
fetch("modules/MMM-WeatherBackground/" + this.config.externalCollections)
.then(async (response) => {
if (!response.ok) {
throw new Error();
}
this.collections = await Object.assign(
{},
this.config.collections,
await response.json()
);
})
.catch((err) => {
if (config.verbose)
Log.err(
`[WHTBGR] External Collection Error: ${this.config.externalCollections}`
);
});
}
},
notificationReceived: async function (notification, payload, sender) {
var now = new Date();
var monthIndex = now.getMonth();
if (this.config.verbose && notification !== "CLOCK_SECOND") {
console.log("Received notification " + notification + " from " + sender);
console.log(payload);
}
if (notification === this.listenNotification) {
if (this.config.verbose) console.log("Weather notification received. Processing...");
var target = this.config.targetDOM
? this.config.targetDOM
: "#" + sender.data.identifier;
var monthKeyword = this.monthMap[monthIndex];
var description = this.payloadConverter(payload);
await this.loadImage(target, {monthKeyword, description});
}
},
//https://source.unsplash.com/collection/4733334/?winter,day,cloudy&s=1631542013371
loadImage: async function (target, { monthKeyword, description } = {}) {
if (this.config.verbose) console.log("this.collections", this.collections);
var seed = Date.now();
var convertedKeywords = (monthKeyword + " " + description).split(" ");
var score = 0;
var matchedCollection = this.config.defaultCollection ?? null;
if (this.config.verbose)
console.log(
"Keywords, converted keywords, intersection, score, matched collection"
);
for (let [keywords, collection] of Object.entries(this.collections)) {
var intersection = keywords
.split(" ")
.filter((x) => convertedKeywords.includes(x)).length;
if (this.config.verbose) {
console.log(
keywords,
convertedKeywords,
intersection,
score,
matchedCollection
);
}
if (score < intersection) {
score = intersection;
matchedCollection = collection;
}
}
const size = this.config.size ? `/${this.config.size}` : "";
const url = await this.getPhotoUrl(matchedCollection, size, convertedKeywords, seed)
var drawImage = (dom) => {
var timer = setTimeout(() => {
dom.classList.add("WTHBGR");
dom.style.backgroundImage = `url(${url})`;
if (this.config.verbose) console.log("[WTHBGR] new Image URL:", url);
}, 1000);
};
var doms = document.querySelectorAll(target);
if (doms.length <= 0) {
if (this.config.verbose) console.log("[WTHBGR] DOM not found.", target);
return;
}
doms.forEach((dom) => {
drawImage(dom);
});
},
getPhotoUrl: async function (matchedCollection, size, convertedKeywords, seed) {
const baseUrl = "https://api.unsplash.com/photos/random"
const query = convertedKeywords.join("+")
const response = await fetch(baseUrl + "?query=" + query + "&client_id=" + this.config.clientID)
const data = await response.json()
return data.urls.full
}
});