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:
parent
e7a53ba7b5
commit
ce56dd9e9f
10
projects/common/package-lock.json
generated
10
projects/common/package-lock.json
generated
@ -1,5 +1,13 @@
|
||||
{
|
||||
"name": "@farmmaps/common",
|
||||
"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=="
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -18,5 +18,8 @@
|
||||
"font-awesome": "^4.7.0",
|
||||
"ngx-uploadx": "^3.3.4",
|
||||
"angular-oauth2-oidc": "^9.1"
|
||||
},
|
||||
"dependencies": {
|
||||
"moment": "^2.27.0"
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user