fixed weather service
Some checks failed
FarmMaps.Develop/FarmMapsLib/pipeline/head There was a failure building this commit

This commit is contained in:
Mark van der Wal 2020-09-02 15:23:54 +02:00
parent 80522a3a2f
commit cdc900d5fd

View File

@ -1,19 +1,19 @@
import {Injectable} from '@angular/core'; import {Injectable} from '@angular/core';
import {HttpClient} from '@angular/common/http'; import {HttpClient} from '@angular/common/http';
import {AppConfig} from '../shared/app.config';
import {Observable} from 'rxjs'; 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 {GeoJSON} from 'ol/format';
import {map, switchMap} from 'rxjs/operators'; import {map, switchMap} from 'rxjs/operators';
import {getCenter} from 'ol/extent'; 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 {IItem} from '../models/item';
import {WeatherData} from '../models/WeatherData';
@Injectable({ @Injectable({
providedIn: 'root', providedIn: 'root',
}) })
export class WeatherService { export class WeatherTempService {
private apiCurrentObservationUrl = 'currentobservation'; private apiCurrentObservationUrl = 'currentobservation';
private apiObservation = 'observation'; private apiObservation = 'observation';
private apiForecast = 'forecast'; private apiForecast = 'forecast';
@ -36,14 +36,15 @@ export class WeatherService {
const geometry = this.format.readGeometry(item.geometry); const geometry = this.format.readGeometry(item.geometry);
const centroid = getCenter(geometry.getExtent()); const centroid = getCenter(geometry.getExtent());
const currentDate = new Date(Date.now()); const startDate = new Date();
const sd = new Date(Date.now()); startDate.setDate(startDate.getDate() - daysBefore);
const ed = new Date(Date.now()); const startDateString = `${this.datePipe.transform(startDate, 'yyyy-MM-dd')}T00:00:00`;
sd.setDate((currentDate.getDate() - daysBefore));
ed.setDate((currentDate.getDate() + daysAfter));
return this.getWeatherRange(centroid, this.datePipe.transform(sd, 'yyyy-MM-ddThh:mm:ss'), const endDate = new Date();
this.datePipe.transform(ed, 'yyyy-MM-ddThh:mm:ss')); 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<WeatherData[]> { public getWeatherRange(centroid: number[], startDate: string, endDate: string): Observable<WeatherData[]> {
@ -52,9 +53,12 @@ export class WeatherService {
// weather does not support UTC format, also remove Z // weather does not support UTC format, also remove Z
const sd = encodeURIComponent(this.removeUTCZ(startDate)); 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}`; const forecast = `${endpoint}${this.apiForecast}/?c=${centroid[0]},${centroid[1]}&interval=hourly&key=${apiKey}`;
return this.httpClient.get<any[]>(historical).pipe( return this.httpClient.get<any[]>(historical).pipe(
@ -62,13 +66,12 @@ export class WeatherService {
switchMap(h => { switchMap(h => {
return this.httpClient.get<any[]>(forecast) return this.httpClient.get<any[]>(forecast)
.pipe( .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]); const days = this.groupBy(hourlyWeatherData, hi => hi.time.split('T')[0]);
return Object.entries(days) return Object.entries(days)
.reduce ( (result, entry) => { .reduce ( (result, entry) => {