updated weather service with correct date manipulation.
Some checks failed
FarmMaps.Develop/FarmMapsLib/pipeline/head There was a failure building this commit
Some checks failed
FarmMaps.Develop/FarmMapsLib/pipeline/head There was a failure building this commit
This commit is contained in:
@@ -2,9 +2,9 @@ import {Injectable} from '@angular/core';
|
||||
import {HttpClient} from '@angular/common/http';
|
||||
import {Observable} from 'rxjs';
|
||||
import {GeoJSON} from 'ol/format';
|
||||
import {map, switchMap} from 'rxjs/operators';
|
||||
import {map, switchMap, tap} from 'rxjs/operators';
|
||||
import {getCenter} from 'ol/extent';
|
||||
import {DatePipe} from '@angular/common';
|
||||
import * as moment from 'moment';
|
||||
import {AppConfig} from '../shared/app.config';
|
||||
import {WeatherCurrentObservation} from '../models/weatherCurrentObservation';
|
||||
import {IItem} from '../models/item';
|
||||
@@ -20,7 +20,7 @@ export class WeatherService {
|
||||
|
||||
private format: GeoJSON;
|
||||
|
||||
constructor(public httpClient: HttpClient, public appConfig: AppConfig, private datePipe: DatePipe) {
|
||||
constructor(public httpClient: HttpClient, public appConfig: AppConfig) {
|
||||
this.format = new GeoJSON();
|
||||
}
|
||||
|
||||
@@ -38,11 +38,15 @@ export class WeatherService {
|
||||
|
||||
const startDate = new Date();
|
||||
startDate.setDate(startDate.getDate() - daysBefore);
|
||||
const startDateString = `${this.datePipe.transform(startDate, 'yyyy-MM-dd')}T00:00:00`;
|
||||
startDate.setHours(0, 0, 0, 0);
|
||||
const startDateString = moment(startDate).local().format('YYYY-MM-DD[T]HH:mm:ss');
|
||||
console.log(startDateString);
|
||||
|
||||
const endDate = new Date();
|
||||
endDate.setDate(endDate.getDate() + daysAfter);
|
||||
const endDateString = `${this.datePipe.transform(endDate, 'yyyy-MM-dd')}T24:00:00`;
|
||||
endDate.setHours(23, 0, 0, 0);
|
||||
const endDateString = moment(endDate).local().format('YYYY-MM-DD[T]HH:mm:ss');
|
||||
console.log(endDateString);
|
||||
|
||||
return this.getWeatherRange(centroid, startDateString, endDateString);
|
||||
}
|
||||
@@ -52,11 +56,11 @@ export class WeatherService {
|
||||
const apiKey = this.appConfig.getConfig('weatherApiKey');
|
||||
|
||||
// weather does not support UTC format, also remove Z
|
||||
const sd = encodeURIComponent(this.removeUTCZ(startDate));
|
||||
const sd = encodeURIComponent(startDate);
|
||||
|
||||
const edHistoricalDate = new Date();
|
||||
const edHistoricalString = `${this.datePipe.transform(edHistoricalDate, 'yyyy-MM-dd')}T${edHistoricalDate.getHours()}:00:00`;
|
||||
const edHistorical = encodeURIComponent(edHistoricalString);
|
||||
edHistoricalDate.setHours(edHistoricalDate.getHours(), 0, 0, 0);
|
||||
const edHistorical = moment(edHistoricalDate).local().format('YYYY-MM-DD[T]HH:mm:ss');
|
||||
|
||||
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}`;
|
||||
@@ -66,6 +70,7 @@ export class WeatherService {
|
||||
switchMap(h => {
|
||||
return this.httpClient.get<any[]>(forecast)
|
||||
.pipe(
|
||||
tap(v => console.log(v)),
|
||||
map(f => [...h, ...f.filter(fd => fd.time <= endDate)] ));
|
||||
})
|
||||
);
|
||||
@@ -117,12 +122,4 @@ export class WeatherService {
|
||||
{},
|
||||
);
|
||||
}
|
||||
|
||||
private removeUTCZ(dateFormat: string): string {
|
||||
if (dateFormat[dateFormat.length - 1] === 'Z') {
|
||||
return dateFormat.substring(0, dateFormat.length - 1);
|
||||
}
|
||||
|
||||
return dateFormat;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user