1
0
Fork 0
mirror of https://github.com/pawelmalak/flame.git synced 2025-07-19 11:39:36 +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 loadConfig = require('./loadConfig');
const getExternalWeather = async () => { 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 // Fetch data from external API
try { try {
const res = await axios.get('https://api.open-meteo.com/v1/forecast', { params: { const res = await axios.get('https://api.open-meteo.com/v1/forecast', { params });
latitude: lat, // TODO weather code
longitude: long,
current: ['is_day', 'temperature_2m', 'relative_humidity_2m', 'cloud_cover', 'wind_speed_10m', 'weathercode'],
} });
// Save weather data // Save weather data
const cursor = res.data.current; const cursor = res.data.current;
const weatherData = await Weather.create({ const weatherData = await Weather.create({
externalLastUpdate: "externalLastUpdate", externalLastUpdate: cursor.time,
tempC: cursor.temperature_2m, // can not be 0 put like minus kelvin tempC: cursor.temperature_2m,
tempF: cursor.temperature_2m, tempF: cursor.temperature_2m,
isDay: Boolean(cursor.is_day), isDay: Boolean(cursor.is_day),
cloud: cursor.cloud_cover, cloud: cursor.cloud_cover,
conditionText: "conditionText", conditionText: '',
conditionCode: 1, conditionCode: 1, // cursor.weathercode
humidity: cursor.relative_humidity_2m, humidity: cursor.relative_humidity_2m,
windK: cursor.wind_speed_10m, windK: cursor.wind_speed_10m,
windM: cursor.wind_speed_10m, windM: cursor.wind_speed_10m,
}); });
return weatherData; return weatherData;
} catch (err) { } catch (err) {
// TODO to update
throw new Error(err); throw new Error(err);
//throw new Error('External API request failed'); //throw new Error('External API request failed');
} }