updated weather service with correct date manipulation.
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-03 10:19:12 +02:00
parent e7a53ba7b5
commit ce56dd9e9f
3 changed files with 25 additions and 17 deletions

View File

@ -1,5 +1,13 @@
{ {
"name": "@farmmaps/common", "name": "@farmmaps/common",
"version": "0.0.1", "version": "0.0.1",
"lockfileVersion": 1 "lockfileVersion": 1,
"requires": true,
"dependencies": {
"moment": {
"version": "2.27.0",
"resolved": "https://registry.npmjs.org/moment/-/moment-2.27.0.tgz",
"integrity": "sha512-al0MUK7cpIcglMv3YF13qSgdAIqxHTO7brRtaz3DlSULbqfazqkc5kEjNrLDOM7fsjshoFIihnU8snrP7zUvhQ=="
}
}
} }

View File

@ -18,5 +18,8 @@
"font-awesome": "^4.7.0", "font-awesome": "^4.7.0",
"ngx-uploadx": "^3.3.4", "ngx-uploadx": "^3.3.4",
"angular-oauth2-oidc": "^9.1" "angular-oauth2-oidc": "^9.1"
},
"dependencies": {
"moment": "^2.27.0"
} }
} }

View File

@ -2,9 +2,9 @@ import {Injectable} from '@angular/core';
import {HttpClient} from '@angular/common/http'; import {HttpClient} from '@angular/common/http';
import {Observable} from 'rxjs'; import {Observable} from 'rxjs';
import {GeoJSON} from 'ol/format'; import {GeoJSON} from 'ol/format';
import {map, switchMap} from 'rxjs/operators'; import {map, switchMap, tap} from 'rxjs/operators';
import {getCenter} from 'ol/extent'; import {getCenter} from 'ol/extent';
import {DatePipe} from '@angular/common'; import * as moment from 'moment';
import {AppConfig} from '../shared/app.config'; import {AppConfig} from '../shared/app.config';
import {WeatherCurrentObservation} from '../models/weatherCurrentObservation'; import {WeatherCurrentObservation} from '../models/weatherCurrentObservation';
import {IItem} from '../models/item'; import {IItem} from '../models/item';
@ -20,7 +20,7 @@ export class WeatherService {
private format: GeoJSON; private format: GeoJSON;
constructor(public httpClient: HttpClient, public appConfig: AppConfig, private datePipe: DatePipe) { constructor(public httpClient: HttpClient, public appConfig: AppConfig) {
this.format = new GeoJSON(); this.format = new GeoJSON();
} }
@ -38,11 +38,15 @@ export class WeatherService {
const startDate = new Date(); const startDate = new Date();
startDate.setDate(startDate.getDate() - daysBefore); 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(); const endDate = new Date();
endDate.setDate(endDate.getDate() + daysAfter); 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); return this.getWeatherRange(centroid, startDateString, endDateString);
} }
@ -52,11 +56,11 @@ export class WeatherService {
const apiKey = this.appConfig.getConfig('weatherApiKey'); const apiKey = this.appConfig.getConfig('weatherApiKey');
// 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(startDate);
const edHistoricalDate = new Date(); const edHistoricalDate = new Date();
const edHistoricalString = `${this.datePipe.transform(edHistoricalDate, 'yyyy-MM-dd')}T${edHistoricalDate.getHours()}:00:00`; edHistoricalDate.setHours(edHistoricalDate.getHours(), 0, 0, 0);
const edHistorical = encodeURIComponent(edHistoricalString); 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 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}`;
@ -66,6 +70,7 @@ export class WeatherService {
switchMap(h => { switchMap(h => {
return this.httpClient.get<any[]>(forecast) return this.httpClient.get<any[]>(forecast)
.pipe( .pipe(
tap(v => console.log(v)),
map(f => [...h, ...f.filter(fd => fd.time <= endDate)] )); 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;
}
} }