From 408ed1e317cd55dc4f08f5fdb5c45324b4a87485 Mon Sep 17 00:00:00 2001 From: "sylvain.chateau" Date: Tue, 10 Sep 2024 22:56:21 +0200 Subject: [PATCH] fix celcius/fahrenheit --- utils/getExternalWeather.js | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/utils/getExternalWeather.js b/utils/getExternalWeather.js index 9a9341f..9f1e65c 100644 --- a/utils/getExternalWeather.js +++ b/utils/getExternalWeather.js @@ -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'); }