/
MMM-MarsWeather.js
62 lines (52 loc) · 2.15 KB
/
MMM-MarsWeather.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
Module.register("MMM-MarsWeather", {
defaults: {
updateInterval: 600000, // 10 minutes
apiKey: 'your_api_key', // Your NASA API key
},
start: function() {
Log.info("Starting module: " + this.name);
this.marsWeather = null;
this.getData();
this.scheduleUpdate();
},
getData: function() {
const url = `https://api.nasa.gov/insight_weather/?api_key=${this.config.apiKey}&feedtype=json&ver=1.0`;
axios.get(url)
.then(response => {
this.marsWeather = response.data;
this.updateDom();
})
.catch(error => console.error("Error fetching Mars weather data:", error));
},
getDom: function() {
var wrapper = document.createElement("div");
if (this.marsWeather) {
var title = document.createElement("h2");
title.innerHTML = "Mars Weather";
wrapper.appendChild(title);
var content = document.createElement("p");
if (this.marsWeather.sol_keys && this.marsWeather.sol_keys.length > 0) {
var latestSol = this.marsWeather.sol_keys[this.marsWeather.sol_keys.length - 1];
var weatherData = this.marsWeather[latestSol];
content.innerHTML = "Sol: " + latestSol + "<br>" +
"Average Temperature: " + weatherData.AT.av + " °C<br>" +
"Wind Speed: " + weatherData.HWS.av + " m/s<br>" +
"Pressure: " + weatherData.PRE.av + " Pa";
} else {
content.innerHTML = "No weather data available"; // You should put something clever here, but that's lame
}
wrapper.appendChild(content);
} else {
wrapper.innerHTML = "Loading Mars Weather Data...";
}
return wrapper;
},
scheduleUpdate: function() {
setInterval(() => {
this.getData();
}, this.config.updateInterval);
},
getScripts: function() {
return ["axios.min.js"];
},
});