diff --git a/src/routes/+layout.svelte b/src/routes/+layout.svelte index ce98e06..1f7c859 100644 --- a/src/routes/+layout.svelte +++ b/src/routes/+layout.svelte @@ -2,7 +2,7 @@ import { NavbarObject } from "../classes/NavObject"; import Navbar from "../components/navbar.svelte"; - let navbarElemenets: NavbarObject[] = [new NavbarObject('/', 'Home'), new NavbarObject('/aaaa', 'aaaa'), new NavbarObject('/test1', 'test1')] + let navbarElemenets: NavbarObject[] = [new NavbarObject('/', 'Home'), new NavbarObject('/temps', 'Temps'), new NavbarObject('/xema', 'Estacions'), new NavbarObject('/openmeteo', 'Dades Open Meteo')] diff --git a/src/routes/openmeteo/+page.server.ts b/src/routes/openmeteo/+page.server.ts new file mode 100644 index 0000000..476f98f --- /dev/null +++ b/src/routes/openmeteo/+page.server.ts @@ -0,0 +1,34 @@ +const geocodingApiBase: string = "https://geocoding-api.open-meteo.com/v1/search"; +const openMeteoApiBase: string = "https://api.open-meteo.com/v1/forecast"; +let location: string = "L'Hospitalet de Llobregat"; +let previousLocation = ''; + +export function load(): object { + let openMeteoData = getFromOpenMeteo(location); + let latitude: number = openMeteoData.then((data) => data.results[0].latitude); + let longitude: number = openMeteoData.then((data) => data.results[0].longitude); + let openMeteoWeather = getWeatherFromOpenMeteo(latitude, longitude); + return { location: openMeteoData.then( (data) => data.results[0].name ).catch( (_e) => previousLocation ), weatherData: openMeteoWeather, latitude, longitude } +}; + +function changeLocation(newLoc: string): void { + previousLocation = location; + location = newLoc; +} + +export const actions = { + changeLoc: async ({ request }) => { + const data: FormData = await request.formData(); + changeLocation(data.get('newloc')); + } +} + +async function getFromOpenMeteo(ubicacio: string): Promise { + let geoCodeResponse: Response = await fetch(`${geocodingApiBase}?name=${ubicacio}&count=1`); + return geoCodeResponse.json(); +} + +async function getWeatherFromOpenMeteo(latitude: any, longitude: any) { + let openMeteoResponse: Response = await fetch(`${openMeteoApiBase}?latitude=12&longitude=12`); + return openMeteoResponse.json() +} diff --git a/src/routes/openmeteo/+page.svelte b/src/routes/openmeteo/+page.svelte new file mode 100644 index 0000000..d27e089 --- /dev/null +++ b/src/routes/openmeteo/+page.svelte @@ -0,0 +1,28 @@ + +
+
{ loading = true; return async ( { update } ) => { await update(); loading = false; } } }> +

Change location

+
+ +
+
+ +
{ JSON.stringify(data) }
+ diff --git a/src/routes/aaaa/+page.server.ts b/src/routes/temps/+page.server.ts similarity index 58% rename from src/routes/aaaa/+page.server.ts rename to src/routes/temps/+page.server.ts index b329c55..e86e6d7 100644 --- a/src/routes/aaaa/+page.server.ts +++ b/src/routes/temps/+page.server.ts @@ -1,15 +1,17 @@ const headers = { 'X-API-KEY': '1FvqqqUpoeNgNpxZQxJX6FBDLbwKscx5hgXhZxUb', 'Content-type': 'application/json' } +const geocodingApiBase: string = "https://geocoding-api.open-meteo.com/v1/search"; +const openMeteoApiBase: string = "https://api.open-meteo.com/v1/forecast"; let location: string = "L'Hospitalet de Llobregat"; let municipis = await fetch('https://api.meteo.cat/referencia/v1/municipis', { method: 'GET', headers: new Headers(headers), credentials: 'include' }).then( (data) => data.json() ); -export function load(): { location: string, weatherData: Promise } { +export function load(): object { let objMunicipi = getCityCode( location ); if (objMunicipi === location) { - return { location, weatherData: getWeather(location).then( (data) => data.message ) } + return { location, weatherData: getWeather(location).then( (data) => data.message ), openMeteoData: getFromOpenMeteo(location) } } let codiMunicipi: string = objMunicipi.codi; let locFullName: string = objMunicipi.nom; - return { location: locFullName, weatherData: getWeather(codiMunicipi) } + return { location: locFullName, weatherData: getWeather(codiMunicipi), openMeteoData: getFromOpenMeteo(location), consums: checkConsum() } }; function changeLocation(newLoc: string): void { @@ -28,6 +30,11 @@ export const actions = { } } +async function checkConsum(): Promise { + let response: Response = await fetchApi('https://api.meteo.cat/quotes/v1/consum-actual'); + return response.json(); +} + async function getWeather(codiMunicipi: string): Promise { if (codiMunicipi !== '') { let response = await fetchApi(`https://api.meteo.cat/pronostic/v1/municipal/${codiMunicipi}`) @@ -36,6 +43,13 @@ async function getWeather(codiMunicipi: string): Promise { return ''; } +async function getFromOpenMeteo(ubicacio: string): Promise { + let geoCodeResponse: Response = await fetch(`${geocodingApiBase}?name=${ubicacio}&count=1`); + // let coordinates: { latitude: Promise, longitude: Promise } = { latitude: geoCodeResponse.json().then( (data) => data[0].latitude ), longitude: geoCodeResponse.json().then( (data) => data[0].longitude )}; + // let weatherData: Response = await fetch(`${openMeteoApiBase}?latitude=${coordinates.latitude}&longitude=${coordinates.longitude}&hourly=temperature_850hPa&models=best_match`); + return geoCodeResponse.json(); +} + function getCityCode(locName: string): object | string { let foundMunicipis: object[] = municipis.filter( municipi => municipi["nom"].toLowerCase().startsWith(locName.toLowerCase()) ) if ( foundMunicipis.length > 0 ) { diff --git a/src/routes/aaaa/+page.svelte b/src/routes/temps/+page.svelte similarity index 55% rename from src/routes/aaaa/+page.svelte rename to src/routes/temps/+page.svelte index f9241c4..7cb5218 100644 --- a/src/routes/aaaa/+page.svelte +++ b/src/routes/temps/+page.svelte @@ -1,26 +1,27 @@
-
+ { loading = true; return async ( { update } ) => { await update(); loading = false; } } }>

Change location

-
- +
+

El temps a: {data.location}

- +
{ JSON.stringify(data.weatherData) }
@@ -30,13 +31,22 @@ - +
+
{ JSON.stringify(data.openMeteoData) }
+
{ JSON.stringify(data.consums) ?? 'No consum available' }
+