Aktueller Stand - Datumsberechnungen

This commit is contained in:
Tobias Peper 2025-04-12 22:49:14 +02:00
parent 574398344a
commit 4cf340412a
3 changed files with 54 additions and 10 deletions

View File

@ -1,4 +1,7 @@
<main class="main">
<div class="container">
{{testDate}}
</div>
<div class="row row-cols-auto">
<div class="col">
<div ngbDropdown class="d-inline-block" #houseDrop="ngbDropdown">

View File

@ -5,7 +5,10 @@ import { ChartComponent } from './chart/chart.component';
import { RestService } from './rest.service';
import { NgbModule } from '@ng-bootstrap/ng-bootstrap';
import { NgbDropdownModule } from '@ng-bootstrap/ng-bootstrap';
import { endOfMonth, format, startOfMonth, subMonths } from 'date-fns';
import { addMonths, endOfMonth, format, startOfMonth, subMonths, subYears } from 'date-fns';
import { Statistics } from './statistics';
import { firstValueFrom } from 'rxjs';
import { formatNumber } from '@angular/common';
@Component({
selector: 'app-root',
@ -19,11 +22,17 @@ export class AppComponent implements OnInit, OnChanges {
constructor(private rest: RestService) {
}
ngOnChanges(changes: SimpleChanges): void {
this.testFunction().then((result: string) => {
this.testDate = result;
});
}
ngOnInit(): void {
this.houseId = 1;
this.duration = 120;
this.testFunction().then((result: string) => {
this.testDate = result;
});
}
getHouseName(): string {
@ -50,20 +59,28 @@ export class AppComponent implements OnInit, OnChanges {
}
}
testFunction(): string {
let startThisMonth: Date = subMonths(startOfMonth(new Date()), 1);
async testFunction(): Promise<string> {
let startThisMonth: Date = subYears(startOfMonth(new Date()), 1);
let returnValue: string = '';
for (let i = 0; i < 12; i++) {
let endThisMonth: Date = endOfMonth(startThisMonth);
let endThisMonth: Date = addMonths(startThisMonth, 1);
// REST-Service aufrufen.
let stats: Statistics = await firstValueFrom(this.rest.getStatsFromTimeframe(this.houseId, startThisMonth, endThisMonth))
returnValue += ' / ' + format(startThisMonth, 'dd.MM.yyyy HH:mm:ss')
+ ' - ' + format(endThisMonth, 'dd.MM.yyyy HH:mm:ss')
+ ' (' + startThisMonth.getTime() + ' - ' + endThisMonth.getTime() + ')<br>';
startThisMonth = subMonths(startThisMonth, 1);
+ ' (' + startThisMonth.getTime() + ' - ' + endThisMonth.getTime() + '): '
+ stats.producedEnergy + ' #### '
;
console.log(format(startThisMonth, 'MMM yyyy')
+ ': ' + formatNumber(stats.producedEnergy/1000000, 'de-DE', '1.2-2') + 'kWh' );
startThisMonth = addMonths(startThisMonth, 1);
}
return returnValue;
}
houseId: number | undefined = undefined;
houseId: number = 1;
duration: number | undefined = undefined;
startThisMonth: Date = startOfMonth(new Date());

View File

@ -17,14 +17,38 @@ export class RestService {
}
getLatestData(houseId: Number, duration: Number): Observable<Aggregate[]> {
return this.http.get<Aggregate[]>('https://vz.home.peper.info/rest-vz/latest-data?houseId=' + houseId + '&timeWindow=' + duration);
return this.http.get<Aggregate[]>('https://vz.home.peper.info/rest-vz/latest-data', {
params: {
houseId: houseId + '',
timeWindow: duration + '',
}
});
}
getStatsFromTimeframe(houseId: number, start: Date, end: Date) : Observable<Statistics> {
return this.http.get<Statistics>('https://vz.home.peper.info/rest-vz/stats', {
params: {
houseId: houseId + '',
timestampStart: start.getTime() + '',
timestampEnd: end.getTime() + '',
}
});
}
getStatisticsWithDuration(duration: string | undefined, houseId: Number): Observable<Statistics> {
if (duration) {
return this.http.get<Statistics>('https://vz.home.peper.info/rest-vz/stats?houseId=' + houseId + '&duration=' + duration);
return this.http.get<Statistics>('https://vz.home.peper.info/rest-vz/stats', {
params: {
houseId: houseId + '',
duration: duration,
}
});
} else {
return this.http.get<Statistics>('https://vz.home.peper.info/rest-vz/stats?houseId=' + houseId);
return this.http.get<Statistics>('https://vz.home.peper.info/rest-vz/stats', {
params: {
houseId: houseId + '',
}
});
}
}