1
0
Fork 0
mirror of https://github.com/pawelmalak/flame.git synced 2025-07-19 03:29:37 +02:00

fix celcius/fahrenheit

This commit is contained in:
sylvain.chateau 2024-09-10 22:56:21 +02:00
parent 6a054c76cd
commit 408ed1e317

View file

@ -3,32 +3,37 @@ const axios = require('axios');
const loadConfig = require('./loadConfig');
const getExternalWeather = async () => {
const { lat, long } = await loadConfig();
const { lat, long, isCelsius } = await loadConfig();
const params = {
latitude: lat,
longitude: long,
current: ['is_day', 'temperature_2m', 'relative_humidity_2m', 'cloud_cover', 'wind_speed_10m', 'weathercode'],
};
if (!isCelsius) {
params.temperature_unit = "fahrenheit";
}
// Fetch data from external API
try {
const res = await axios.get('https://api.open-meteo.com/v1/forecast', { params: {
latitude: lat,
longitude: long,
current: ['is_day', 'temperature_2m', 'relative_humidity_2m', 'cloud_cover', 'wind_speed_10m', 'weathercode'],
} });
const res = await axios.get('https://api.open-meteo.com/v1/forecast', { params });
// TODO weather code
// Save weather data
const cursor = res.data.current;
const weatherData = await Weather.create({
externalLastUpdate: "externalLastUpdate",
tempC: cursor.temperature_2m, // can not be 0 put like minus kelvin
externalLastUpdate: cursor.time,
tempC: cursor.temperature_2m,
tempF: cursor.temperature_2m,
isDay: Boolean(cursor.is_day),
cloud: cursor.cloud_cover,
conditionText: "conditionText",
conditionCode: 1,
conditionText: '',
conditionCode: 1, // cursor.weathercode
humidity: cursor.relative_humidity_2m,
windK: cursor.wind_speed_10m,
windM: cursor.wind_speed_10m,
});
return weatherData;
} catch (err) {
// TODO to update
throw new Error(err);
//throw new Error('External API request failed');
}