forked from MagicMirrorOrg/MagicMirror
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updated Weather module Met Office API Gateway migration MagicMirrorOr…
- Loading branch information
1 parent
118e212
commit 1ed8c50
Showing
2 changed files
with
49 additions
and
0 deletions.
There are no files selected for viewing
Submodule MagicMirror
added at
118e21
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// Update the base URL for the Met Office API | ||
const baseURL = 'https://api-metoffice.new-endpoint.com/' | ||
|
||
// Function to fetch current weather data | ||
function fetchCurrentWeather() { | ||
const url = `${baseURL}/current?location=${this.config.location}&appid=${this.config.apiKey}`; | ||
|
||
url.searchParams.append('location', this.config.location); | ||
url.searchParams.append('appid', this.config.apiKey); | ||
|
||
// Using fetch to make the API call | ||
fetch(url) | ||
.then(response => { | ||
if (!response.ok) { | ||
throw new Error('Network response was not ok ' + response.statusText); | ||
} | ||
return response.json(); // Convert the response body to JSON | ||
}) | ||
.then(data => { | ||
this.processWeatherData(data); | ||
}) | ||
.catch(error => { | ||
// Handle any errors that occurred during the fetch | ||
console.error('Unable to fetch Met Office weather data:', error); | ||
});etch(url) | ||
|
||
|
||
} | ||
|
||
function processWeatherData(data) { | ||
|
||
if (data && data.temperature && data.conditions && data.wind) { | ||
|
||
const currentTemperature = data.temperature.value; | ||
const weatherConditions = data.conditions[0].description; | ||
const windSpeed = data.wind.speed; | ||
|
||
const temperatureCelsius = currentTemperature - 273.15; | ||
|
||
console.log(`Current Temperature: ${temperatureCelsius.toFixed(1)}°C`); | ||
console.log(`Weather Conditions: ${weatherConditions}`); | ||
console.log(`Wind Speed: ${windSpeed} m/s`); | ||
|
||
|
||
} else { | ||
console.error("Unexpected API response structure:", data); | ||
} | ||
} |