From cdc900d5fd6d0de205f7a1e04cda6349617b97de Mon Sep 17 00:00:00 2001 From: Mark van der Wal Date: Wed, 2 Sep 2020 15:23:54 +0200 Subject: [PATCH] fixed weather service --- .../common/src/fm/services/weather.service.ts | 39 ++++++++++--------- 1 file changed, 21 insertions(+), 18 deletions(-) diff --git a/projects/common/src/fm/services/weather.service.ts b/projects/common/src/fm/services/weather.service.ts index f2402a9..6092cbf 100644 --- a/projects/common/src/fm/services/weather.service.ts +++ b/projects/common/src/fm/services/weather.service.ts @@ -1,19 +1,19 @@ import {Injectable} from '@angular/core'; import {HttpClient} from '@angular/common/http'; -import {AppConfig} from '../shared/app.config'; import {Observable} from 'rxjs'; -import {WeatherCurrentObservation} from '../models/weatherCurrentObservation'; -import {DatePipe} from '@angular/common'; -import {WeatherData} from '../models/WeatherData'; import {GeoJSON} from 'ol/format'; import {map, switchMap} from 'rxjs/operators'; import {getCenter} from 'ol/extent'; +import {DatePipe} from '@angular/common'; +import {AppConfig} from '../shared/app.config'; +import {WeatherCurrentObservation} from '../models/weatherCurrentObservation'; import {IItem} from '../models/item'; +import {WeatherData} from '../models/WeatherData'; @Injectable({ providedIn: 'root', }) -export class WeatherService { +export class WeatherTempService { private apiCurrentObservationUrl = 'currentobservation'; private apiObservation = 'observation'; private apiForecast = 'forecast'; @@ -36,14 +36,15 @@ export class WeatherService { const geometry = this.format.readGeometry(item.geometry); const centroid = getCenter(geometry.getExtent()); - const currentDate = new Date(Date.now()); - const sd = new Date(Date.now()); - const ed = new Date(Date.now()); - sd.setDate((currentDate.getDate() - daysBefore)); - ed.setDate((currentDate.getDate() + daysAfter)); + const startDate = new Date(); + startDate.setDate(startDate.getDate() - daysBefore); + const startDateString = `${this.datePipe.transform(startDate, 'yyyy-MM-dd')}T00:00:00`; - return this.getWeatherRange(centroid, this.datePipe.transform(sd, 'yyyy-MM-ddThh:mm:ss'), - this.datePipe.transform(ed, 'yyyy-MM-ddThh:mm:ss')); + const endDate = new Date(); + endDate.setDate(endDate.getDate() + daysAfter); + const endDateString = `${this.datePipe.transform(endDate, 'yyyy-MM-dd')}T24:00:00`; + + return this.getWeatherRange(centroid, startDateString, endDateString); } public getWeatherRange(centroid: number[], startDate: string, endDate: string): Observable { @@ -52,9 +53,12 @@ export class WeatherService { // weather does not support UTC format, also remove Z const sd = encodeURIComponent(this.removeUTCZ(startDate)); - const ed = encodeURIComponent(this.removeUTCZ(endDate)); - const historical = `${endpoint}${this.apiObservation}/?c=${centroid[0]},${centroid[1]}&sd=${sd}&ed=${ed}&t=observation&interval=hourly&key=${apiKey}`; + const edHistoricalDate = new Date(); + const edHistoricalString = `${this.datePipe.transform(edHistoricalDate, 'yyyy-MM-dd')}T${edHistoricalDate.getHours()}:00:00`; + const edHistorical = encodeURIComponent(edHistoricalString); + + const historical = `${endpoint}${this.apiObservation}/?c=${centroid[0]},${centroid[1]}&sd=${sd}&ed=${edHistorical}&t=observation&interval=hourly&key=${apiKey}`; const forecast = `${endpoint}${this.apiForecast}/?c=${centroid[0]},${centroid[1]}&interval=hourly&key=${apiKey}`; return this.httpClient.get(historical).pipe( @@ -62,13 +66,12 @@ export class WeatherService { switchMap(h => { return this.httpClient.get(forecast) .pipe( - map(f => this.hourlyToDaily([...h, ...f.filter(fd => fd.time <= endDate)]) - )); + map(f => [...h, ...f.filter(fd => fd.time <= endDate)] )); }) ); } - private hourlyToDaily(hourlyWeatherData: any[]): WeatherData[] { + public hourlyToDaily(hourlyWeatherData: any[]): WeatherData[] { const days = this.groupBy(hourlyWeatherData, hi => hi.time.split('T')[0]); return Object.entries(days) .reduce ( (result, entry) => { @@ -110,7 +113,7 @@ export class WeatherService { ...(result[group] || []), item, ], - });}, + }); }, {}, ); }