Skip to content

Commit

Permalink
Updated Weather module Met Office API Gateway migration MagicMirrorOr…
Browse files Browse the repository at this point in the history
  • Loading branch information
serena-fan committed Apr 20, 2024
1 parent 118e212 commit 1ed8c50
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
1 change: 1 addition & 0 deletions MagicMirror
Submodule MagicMirror added at 118e21
48 changes: 48 additions & 0 deletions modules/default/weather/providers/metoffice.js
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);
}
}

0 comments on commit 1ed8c50

Please sign in to comment.