conectado nearby
This commit is contained in:
36
src/helpers/OpenWeather.ts
Normal file
36
src/helpers/OpenWeather.ts
Normal file
@ -0,0 +1,36 @@
|
||||
import OWMResponse from "../interfaces/OWMResponse";
|
||||
|
||||
/**
|
||||
* Wrapper para recibir los datos del clima de OpenWeatherMap
|
||||
*/
|
||||
export default class OpenWeather {
|
||||
static async week(lat: string, lon: string): Promise<string> {
|
||||
const res = await fetch('https://api.openweathermap.org/data/2.5/forecast?lat=' + lat + '&lon=' + lon + '&appid=' + import.meta.env.VITE_OWM_KEY);
|
||||
if (!res.ok) {
|
||||
throw new Error("API Error");
|
||||
}
|
||||
|
||||
const json = await res.json() as OWMResponse;
|
||||
|
||||
let rains = 0;
|
||||
|
||||
json.list.forEach((el) => {
|
||||
if (el.weather[0].id === 500) {
|
||||
rains++;
|
||||
}
|
||||
});
|
||||
|
||||
let probability = '';
|
||||
|
||||
if (rains > json.list.length / 2) {
|
||||
// Bien
|
||||
probability = 'Sabiendo que ha llovido un total de ' + rains + ' días, podemos predecir que el caudal será alto';
|
||||
} else {
|
||||
// Mal
|
||||
probability = 'Sabiendo que ha llovido un total de ' + rains + ' días, podemos predecir que el caudal será bajo';
|
||||
|
||||
}
|
||||
|
||||
return probability;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user