Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions MMM-NOAAForecast.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,9 +208,9 @@ Module.register("MMM-NOAAForecast", {
//process weather data
this.dataRefreshTimeStamp = moment().format("x");
this.weatherData = {
daily: JSON.parse(payload.payload.forecast).properties.periods,
hourly: JSON.parse(payload.payload.forecastHourly).properties.periods,
grid: JSON.parse(payload.payload.forecastGridData).properties
daily: payload.payload.daily.properties.periods,
hourly: payload.payload.hourly.properties.periods,
grid: payload.payload.grid.properties
};

this.preProcessWeatherData();
Expand Down Expand Up @@ -1127,6 +1127,7 @@ Module.register("MMM-NOAAForecast", {
this.iconsets[this.config.iconset].format
}`
);
console.log("[MMM-NOAAForcast] main icon is " + this.config.mainIconset);
},

/*
Expand Down
115 changes: 67 additions & 48 deletions node_helper.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*********************************

Node Helper for MMM-NOAAForecast.
Node Helper for MMM-NOAAForecastDeluxe.
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The change to MMM-NOAAForecastDeluxe makes no sense in this PR.


This helper is responsible for the DarkSky-compatible data pull from NOAA.

Expand All @@ -17,7 +17,7 @@ var moment = require("moment");
module.exports = NodeHelper.create({
start: function () {
console.log(
`====================== Starting node_helper for module [${this.name}]`
`Starting node_helper for module ${this.name}`
);
},

Expand All @@ -36,69 +36,88 @@ module.exports = NodeHelper.create({
payload.longitude === ""
) {
console.log(
`[MMM-NOAAForecast] ${moment().format(
`[MMM-NOAAForecastDeluxe] ${moment().format(
"D-MMM-YY HH:mm"
)} ** ERROR ** Latitude and/or longitude not provided.`
);
} else {
var url = `https://api.weather.gov/points/${payload.latitude},${payload.longitude}`;

console.log(`[MMM-NOAAForecast] Getting data: ${url}`);
console.log(`[MMM-NOAAForecastDeluxe] Getting data: ${url}`);
needle.get(url, needleOptions, function (error, response, body) {
if (!error && response.statusCode === 200) {
var forecastData = {};
var parsedBody = typeof body === "string" ? JSON.parse(body) : body;
var properties = parsedBody.properties;

if (
properties &&
properties.forecast &&
properties.forecastHourly /* && properties.forecastGridData */
) {
var forecastUrls = [
{ key: "forecast", url: properties.forecast },
{ key: "forecastHourly", url: properties.forecastHourly },
{ key: "forecastGridData", url: properties.forecastGridData }
];
if (error || response.statusCode !== 200) {
console.error(
`[MMM-NOAAForecastDeluxe] ${moment().format(
"D-MMM-YY HH:mm"
)} ** ERROR ** Failed to get forecast URLs: ${error ? error.message : "HTTP Status Code " + response.statusCode}`
);
return;
}

var completedRequests = 0;
let parsedBody;
try {
parsedBody = JSON.parse(body);
} catch (e) {
console.error(
`[MMM-NOAAForecastDeluxe] ${moment().format(
"D-MMM-YY HH:mm"
)} ** ERROR ** Failed to parse response body as JSON: ${e.message}`
);
return;
}

if (parsedBody && parsedBody.properties && parsedBody.properties.forecastHourly) {
var forecastUrls = [{
key: "hourly",
url: parsedBody.properties.forecastHourly
}, {
key: "daily",
url: parsedBody.properties.forecast
}, {
key: "grid",
url: parsedBody.properties.forecastGridData
}];

forecastUrls.forEach(function (item) {
needle.get(item.url, needleOptions, function (err, res, data) {
if (!err && res.statusCode === 200) {
forecastData[item.key] = data;
console.log(`[MMM-NOAAForecast] Getting data: ${item.url}`);
} else {
var forecastData = {};
var completedRequests = 0;
forecastUrls.forEach(function (item) {
console.log(`[MMM-NOAAForecastDeluxe] Making request for ${item.key}: ${item.url}`);
needle.get(item.url, needleOptions, function (err, res, data) {
console.log(`[MMM-NOAAForecastDeluxe] Received data for ${item.key}. Data type: ${typeof data}`);
if (!err && res.statusCode === 200) {
try {
forecastData[item.key] = JSON.parse(data);
} catch (parseError) {
console.log(
`[MMM-NOAAForecast] ${moment().format(
`[MMM-NOAAForecastDeluxe] ${moment().format(
"D-MMM-YY HH:mm"
)} ** ERROR ** Failed to get ${item.key}: ${err}`
)} ** ERROR ** Failed to parse JSON for ${item.key}: ${parseError.message}`
);
}
} else {
console.log(
`[MMM-NOAAForecastDeluxe] ${moment().format(
"D-MMM-YY HH:mm"
)} ** ERROR ** Failed to get ${item.key}: ${err}`
);
}

completedRequests++;
if (completedRequests === forecastUrls.length) {
self.sendSocketNotification("NOAA_CALL_FORECAST_DATA", {
instanceId: payload.instanceId,
payload: forecastData
});
}
});
completedRequests++;
if (completedRequests === forecastUrls.length) {
console.log("[MMM-NOAAForecastDeluxe] All forecast data fetched. Sending to main module.");
// console.log("[MMM-NOAAWeatherForecast] Final payload:", JSON.stringify(forecastData));
self.sendSocketNotification("NOAA_CALL_FORECAST_DATA", {
instanceId: payload.instanceId,
payload: forecastData
});
}
});
} else {
console.log(
`[MMM-NOAAForecast] ${moment().format(
"D-MMM-YY HH:mm"
)} ** ERROR ** Missing forecast URLs in response: ${
error ? error : JSON.stringify(parsedBody)
}`
);
}
});
} else {
console.log(
`[MMM-NOAAForecast] ${moment().format(
console.error(
`[MMM-NOAAForecastDeluxe] ${moment().format(
"D-MMM-YY HH:mm"
)} ** ERROR ** ${error}`
)} ** ERROR ** API response does not contain forecast URLs.`
);
}
});
Expand Down