1
1
import { Injectable , inject } from "@angular/core" ;
2
2
import { HttpClient } from "@angular/common/http" ;
3
- import { Observable , forkJoin , of , switchMap } from "rxjs" ;
3
+ import { Observable , delay , forkJoin , of , switchMap } from "rxjs" ;
4
4
import { LangService } from "../lang/lang.service" ;
5
- import { GeolocationService } from "./geolocation.service" ;
6
5
import { IWeatherAqi , IWeatherOne } from "../interface/weather-owm.interface" ;
7
6
import { environment } from "../../../environments/environment.development" ;
8
7
@@ -23,11 +22,6 @@ export class WeatherService {
23
22
*/
24
23
private langService : LangService = inject ( LangService ) ;
25
24
26
- /**
27
- * Service to manage geolocation.
28
- */
29
- private geolocationService : GeolocationService = inject ( GeolocationService ) ;
30
-
31
25
/**
32
26
* API key for OpenWeatherMap.
33
27
*/
@@ -41,43 +35,45 @@ export class WeatherService {
41
35
/**
42
36
* URL for the OpenWeatherMap One Call endpoint.
43
37
*/
44
- private readonly weatherOwmOne : string = environment . WEATHER_OWM ;
38
+ private readonly weatherOwmOne_mock : string = environment . MOCK . WEATHER_OWM_ONE ;
39
+ private readonly weatherOwmOne_endpoint : string = environment . ENDPOINT . WEATHER_OWM_ONE ;
45
40
46
41
/**
47
42
* URL for the OpenWeatherMap AQI endpoint.
48
43
*/
49
- private readonly weatherOwmAqi : string = environment . WEATHER_OWM_AQI ;
44
+ private readonly weatherOwmAqi_mock : string = environment . MOCK . WEATHER_OWM_AQI ;
45
+ private readonly weatherOwmAqi_endpoint : string = environment . ENDPOINT . WEATHER_OWM_AQI ;
50
46
51
47
/**
52
48
* Retrieves comprehensive weather data in a single request.
53
49
* @returns {Observable<IWeatherOne> } An Observable emitting the weather data.
54
50
*/
55
- private getWeatherOne ( ) : Observable < IWeatherOne > {
51
+ private getWeatherOne ( lat : number , lon : number ) : Observable < IWeatherOne > {
56
52
const url = this . isProduction
57
- ? `${ this . weatherOwmOne } lat=${ this . geolocationService . lat ( ) } &lon=${ this . geolocationService . lon ( ) } &units=metric&lang=${ this . langService . currentLangSig ( ) } &appid=${ this . weatherOwmKey } `
58
- : this . weatherOwmOne ;
53
+ ? `${ this . weatherOwmOne_endpoint } lat=${ lat } &lon=${ lon } &units=metric&lang=${ this . langService . currentLangSig ( ) } &appid=${ this . weatherOwmKey } `
54
+ : this . weatherOwmOne_mock ;
59
55
return this . http . get < IWeatherOne > ( url ) ;
60
56
}
61
57
62
58
/**
63
59
* Retrieves air quality index (AQI) data.
64
60
* @returns {Observable<IWeatherAqi> } An Observable emitting the AQI data.
65
61
*/
66
- private getWeatherAqi ( ) : Observable < IWeatherAqi > {
62
+ private getWeatherAqi ( lat : number , lon : number ) : Observable < IWeatherAqi > {
67
63
const url = this . isProduction
68
- ? `${ this . weatherOwmAqi } lat=${ this . geolocationService . lon ( ) } &lon=${ this . geolocationService . lon ( ) } &appid=${ this . weatherOwmKey } `
69
- : this . weatherOwmAqi ;
64
+ ? `${ this . weatherOwmAqi_endpoint } lat=${ lat } &lon=${ lon } &appid=${ this . weatherOwmKey } `
65
+ : this . weatherOwmAqi_mock ;
70
66
return this . http . get < IWeatherAqi > ( url ) ;
71
67
}
72
68
73
69
/**
74
70
* Combines weather and AQI data into a single object.
75
71
* @returns {Observable<any> } An Observable emitting an object containing both weather data and AQI information.
76
72
*/
77
- public weatherMergeData ( ) : Observable < any > {
73
+ public weatherMergeData ( lat : number , lon : number ) : Observable < any > {
78
74
return forkJoin ( {
79
- weather : this . getWeatherOne ( ) ,
80
- aqi : this . getWeatherAqi ( ) ,
75
+ weather : this . getWeatherOne ( lat , lon ) ,
76
+ aqi : this . getWeatherAqi ( lat , lon ) ,
81
77
} ) . pipe (
82
78
switchMap ( ( results ) => {
83
79
const combinedData = {
0 commit comments