35 lines
1.9 KiB
JavaScript
35 lines
1.9 KiB
JavaScript
// modules/data/weather.js — Open-Meteo weather fetch
|
|
// Writes to: weatherState (returned), scene effects applied by caller
|
|
|
|
const WEATHER_LAT = 43.2897;
|
|
const WEATHER_LON = -72.1479;
|
|
const WEATHER_REFRESH_MS = 15 * 60 * 1000;
|
|
|
|
function weatherCodeToLabel(code) {
|
|
if (code === 0) return { condition: 'Clear', icon: '☀️' };
|
|
if (code <= 2) return { condition: 'Partly Cloudy', icon: '⛅' };
|
|
if (code === 3) return { condition: 'Overcast', icon: '☁️' };
|
|
if (code >= 45 && code <= 48) return { condition: 'Fog', icon: '🌫️' };
|
|
if (code >= 51 && code <= 57) return { condition: 'Drizzle', icon: '🌦️' };
|
|
if (code >= 61 && code <= 67) return { condition: 'Rain', icon: '🌧️' };
|
|
if (code >= 71 && code <= 77) return { condition: 'Snow', icon: '❄️' };
|
|
if (code >= 80 && code <= 82) return { condition: 'Showers', icon: '🌦️' };
|
|
if (code >= 85 && code <= 86) return { condition: 'Snow Showers', icon: '🌨️' };
|
|
if (code >= 95 && code <= 99) return { condition: 'Thunderstorm', icon: '⛈️' };
|
|
return { condition: 'Unknown', icon: '🌀' };
|
|
}
|
|
|
|
export async function fetchWeatherData() {
|
|
const url = `https://api.open-meteo.com/v1/forecast?latitude=${WEATHER_LAT}&longitude=${WEATHER_LON}¤t=temperature_2m,weather_code,wind_speed_10m,cloud_cover&temperature_unit=fahrenheit&wind_speed_unit=mph&forecast_days=1`;
|
|
const res = await fetch(url);
|
|
if (!res.ok) throw new Error('weather fetch failed');
|
|
const data = await res.json();
|
|
const cur = data.current;
|
|
const code = cur.weather_code;
|
|
const { condition, icon } = weatherCodeToLabel(code);
|
|
const cloudcover = typeof cur.cloud_cover === 'number' ? cur.cloud_cover : 50;
|
|
return { code, temp: cur.temperature_2m, wind: cur.wind_speed_10m, condition, icon, cloudcover };
|
|
}
|
|
|
|
export { WEATHER_REFRESH_MS };
|