-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathnode_helper.js
146 lines (129 loc) · 3.92 KB
/
node_helper.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
/* MagicMirror² Module: MMM-2Day-NOAA-Forecast helper
* Version: 0.2.0
*
* By Jinserk Baik https://github.com/jinserk/
* MIT Licensed.
*/
const url = require("url");
const NodeHelper = require("node_helper");
const Log = require("logger");
module.exports = NodeHelper.create({
start: function () {
Log.log(`Starting node helper for: ${this.name}`);
},
getWeatherData: function (payload) {
let _this = this;
fetch(payload)
.then(async (response) => {
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
const data = await response.json();
const forecastUrl = data.properties.forecast;
_this.getForecastData(payload, forecastUrl);
})
.catch(function (error) {
Log.error(error);
});
},
getForecastData: function (url1, url2) {
let _this = this;
let forecast = [];
fetch(url2)
.then(async (response) => {
if (response.status === 200) {
const data = await response.json();
const periods = data.properties.periods;
if (periods.length > 4) {
forecast = _this.parseData(periods);
Log.info("Got forecast data from api.weather.gov");
} else {
Log.error("Got forecast data but something wrong");
forecast = _this.fillEmptyData();
}
} else {
Log.error("Error fetching forecast data:", response.status);
forecast = _this.fillEmptyData();
}
})
.catch(function (error) {
Log.error("Error fetching forecast data:", error);
forecast = _this.fillEmptyData();
})
.finally(function () {
//Log.info(forecast);
_this.sendSocketNotification("GOT-2DAY-NOAA-FORECAST", {
url: url1,
forecast: forecast
});
});
},
parseData: function (data) {
let forecast = [];
data.slice(0, 4).forEach((element, i) => {
forecast.push({
name: element.name,
date: element.startTime,
isDay: element.isDaytime,
icon: this.parseIcon(element.icon),
conditions: element.shortForecast,
temp: element.temperature,
pop: element.probabilityOfPrecipitation.value
? element.probabilityOfPrecipitation.value
: 0,
// Removed per https://www.weather.gov/media/notification/pdf_2023_24/scn24-55_api_v1.13.pdf
// humid: element.relativeHumidity.value,
wspd: element.windSpeed
.replace("to ", "")
.split(" ")
.slice(0, -1)
.map(Number),
wdir: element.windDirection
});
});
return forecast;
},
parseIcon: function (icon_url) {
let data = url.parse(icon_url).pathname.split("/").slice(4);
if (data.length === 1) {
let d0 = data[0].split(",");
return d0.length === 2 ? d0[0] : data[0];
} else {
let d0 = data[0].split(",");
let d1 = data[1].split(",");
if (d0.length === 2) {
return parseInt(d0[1], 10) > 50 ? d0[0] : d1[0];
} else if (d1.length === 2) {
return parseInt(d1[1], 10) > 50 ? d1[0] : d0[0];
} else {
// no pop, so choose the first
return d0[0];
}
}
},
fillEmptyData: function () {
let forecast = [];
for (let i = 0; i < 4; i++) {
forecast.push({
name: "--",
date: "--",
isDay: "--",
icon: ["not_available"],
conditions: "No weather data",
temp: "--",
pop: "--",
// Removed per https://www.weather.gov/media/notification/pdf_2023_24/scn24-55_api_v1.13.pdf
// humid: "--",
wspd: ["--"],
wdir: "--"
});
}
return forecast;
},
socketNotificationReceived: function (notification, payload) {
// Check this is for us and if it is let's get the weather data
if (notification === "GET-2DAY-NOAA-FORECAST") {
this.getWeatherData(payload);
}
}
});