extracted SeriesCacheService out of SeriesService

This commit is contained in:
Patrick Haßel 2024-10-21 09:45:43 +02:00
parent 3fd4da3797
commit f76b6cdec8
5 changed files with 159 additions and 142 deletions

View File

@ -0,0 +1,113 @@
import {Injectable} from '@angular/core';
import {Series} from "./Series";
import {ApiService} from "../api.service";
import {SeriesService} from "./series.service";
export function returnUpdatedSeriesIfNameAndNewer(fresh: Series, old: Series, name: string): Series {
if (fresh.name !== name) {
return old;
}
if (old === null || old.lastDate.getTime() <= fresh.lastDate.getTime()) {
return fresh;
}
return old;
}
@Injectable({
providedIn: 'root'
})
export class SeriesCacheService {
public series: Series[] = [];
public gridPurchased: Series = Series.EMPTY;
public gridDelivered: Series = Series.EMPTY;
public photovoltaicProduced: Series = Series.EMPTY;
public photovoltaicPower: Series = Series.EMPTY;
public gridPower: Series = Series.EMPTY;
public schlafzimmerTemperature: Series = Series.EMPTY;
public schlafzimmerHumidityRelative: Series = Series.EMPTY;
public schlafzimmerHumidityAbsolute: Series = Series.EMPTY;
public outdoorTemperature: Series = Series.EMPTY;
public outdoorHumidityRelative: Series = Series.EMPTY;
public outdoorHumidityAbsolute: Series = Series.EMPTY;
public heatingRoomTemperature: Series = Series.EMPTY;
public heatingRoomHumidityRelative: Series = Series.EMPTY;
public heatingRoomHumidityAbsolute: Series = Series.EMPTY;
public heatingExhaustTemperature: Series = Series.EMPTY;
public heatingBufferSupplyTemperature: Series = Series.EMPTY;
public heatingBufferReturnTemperature: Series = Series.EMPTY;
public heatingBufferColdTemperature: Series = Series.EMPTY;
public heatingBufferInnerTemperature: Series = Series.EMPTY;
public heatingBufferHotTemperature: Series = Series.EMPTY;
public heatingBufferCirculationTemperature: Series = Series.EMPTY;
public heatingLoopSupplyTemperature: Series = Series.EMPTY;
public heatingLoopReturnTemperature: Series = Series.EMPTY;
constructor(
private readonly api: ApiService,
private readonly seriesService: SeriesService,
) {
this.api.connected(() => {
this.seriesService.findAll(list => {
this.series = list;
list.forEach(s => this.seriesUpdate(s));
});
});
this.seriesService.subscribe(series => this.seriesUpdate(series));
}
private seriesUpdate(series: Series) {
this.gridPurchased = returnUpdatedSeriesIfNameAndNewer(series, this.gridPurchased, 'electricity.grid.purchase.energy');
this.gridDelivered = returnUpdatedSeriesIfNameAndNewer(series, this.gridDelivered, 'electricity.grid.delivery.energy');
this.gridPower = returnUpdatedSeriesIfNameAndNewer(series, this.gridPower, 'electricity.grid.power');
this.photovoltaicProduced = returnUpdatedSeriesIfNameAndNewer(series, this.photovoltaicProduced, 'electricity.photovoltaic.energy');
this.photovoltaicPower = returnUpdatedSeriesIfNameAndNewer(series, this.photovoltaicPower, 'electricity.photovoltaic.power');
this.schlafzimmerTemperature = returnUpdatedSeriesIfNameAndNewer(series, this.schlafzimmerTemperature, 'schlafzimmer.temperature');
this.schlafzimmerHumidityRelative = returnUpdatedSeriesIfNameAndNewer(series, this.schlafzimmerHumidityRelative, 'schlafzimmer.humidity_relative');
this.schlafzimmerHumidityAbsolute = returnUpdatedSeriesIfNameAndNewer(series, this.schlafzimmerHumidityAbsolute, 'schlafzimmer.humidity_absolute');
this.outdoorTemperature = returnUpdatedSeriesIfNameAndNewer(series, this.outdoorTemperature, 'garten.temperature');
this.outdoorHumidityRelative = returnUpdatedSeriesIfNameAndNewer(series, this.outdoorHumidityRelative, 'garten.humidity_relative');
this.outdoorHumidityAbsolute = returnUpdatedSeriesIfNameAndNewer(series, this.outdoorHumidityAbsolute, 'garten.humidity_absolute');
this.heatingRoomTemperature = returnUpdatedSeriesIfNameAndNewer(series, this.heatingRoomTemperature, 'heating.room.temperature');
this.heatingRoomHumidityRelative = returnUpdatedSeriesIfNameAndNewer(series, this.heatingRoomHumidityRelative, 'heating.room.humidity_relative');
this.heatingRoomHumidityAbsolute = returnUpdatedSeriesIfNameAndNewer(series, this.heatingRoomHumidityAbsolute, 'heating.room.humidity_absolute');
this.heatingExhaustTemperature = returnUpdatedSeriesIfNameAndNewer(series, this.heatingExhaustTemperature, 'heating.exhaust.temperature');
this.heatingBufferSupplyTemperature = returnUpdatedSeriesIfNameAndNewer(series, this.heatingBufferSupplyTemperature, 'heating.buffer.supply.temperature');
this.heatingBufferReturnTemperature = returnUpdatedSeriesIfNameAndNewer(series, this.heatingBufferReturnTemperature, 'heating.buffer.return.temperature');
this.heatingBufferColdTemperature = returnUpdatedSeriesIfNameAndNewer(series, this.heatingBufferColdTemperature, 'heating.buffer.cold.temperature');
this.heatingBufferInnerTemperature = returnUpdatedSeriesIfNameAndNewer(series, this.heatingBufferInnerTemperature, 'heating.buffer.inner.temperature');
this.heatingBufferHotTemperature = returnUpdatedSeriesIfNameAndNewer(series, this.heatingBufferHotTemperature, 'heating.buffer.hot.temperature');
this.heatingBufferCirculationTemperature = returnUpdatedSeriesIfNameAndNewer(series, this.heatingBufferCirculationTemperature, 'heating.buffer.circulation.temperature');
this.heatingLoopSupplyTemperature = returnUpdatedSeriesIfNameAndNewer(series, this.heatingLoopSupplyTemperature, 'heating.loop.supply.temperature');
this.heatingLoopReturnTemperature = returnUpdatedSeriesIfNameAndNewer(series, this.heatingLoopReturnTemperature, 'heating.loop.return.temperature');
}
}

View File

@ -1,113 +1,17 @@
import {Injectable} from '@angular/core';
import {Series} from "./Series";
import {ApiService} from "../api.service";
type Next<T> = (t: T) => any;
import {Next} from "../types";
@Injectable({
providedIn: 'root'
})
export class SeriesService {
public series: Series[] = [];
public gridPurchased: Series = Series.EMPTY;
public gridDelivered: Series = Series.EMPTY;
public photovoltaicProduced: Series = Series.EMPTY;
public photovoltaicPower: Series = Series.EMPTY;
public gridPower: Series = Series.EMPTY;
public schlafzimmerTemperature: Series = Series.EMPTY;
public schlafzimmerHumidityRelative: Series = Series.EMPTY;
public schlafzimmerHumidityAbsolute: Series = Series.EMPTY;
public outdoorTemperature: Series = Series.EMPTY;
public outdoorHumidityRelative: Series = Series.EMPTY;
public outdoorHumidityAbsolute: Series = Series.EMPTY;
public heatingRoomTemperature: Series = Series.EMPTY;
public heatingRoomHumidityRelative: Series = Series.EMPTY;
public heatingRoomHumidityAbsolute: Series = Series.EMPTY;
public heatingExhaustTemperature: Series = Series.EMPTY;
public heatingBufferSupplyTemperature: Series = Series.EMPTY;
public heatingBufferReturnTemperature: Series = Series.EMPTY;
public heatingBufferColdTemperature: Series = Series.EMPTY;
public heatingBufferInnerTemperature: Series = Series.EMPTY;
public heatingBufferHotTemperature: Series = Series.EMPTY;
public heatingBufferCirculationTemperature: Series = Series.EMPTY;
public heatingLoopSupplyTemperature: Series = Series.EMPTY;
public heatingLoopReturnTemperature: Series = Series.EMPTY;
constructor(
private readonly api: ApiService,
) {
this.api.connected(() => {
this.findAll(list => {
this.series = list;
list.forEach(s => this.seriesUpdate(s));
});
});
this.subscribe(series => this.seriesUpdate(series));
}
private seriesUpdate(series: Series) {
this.gridPurchased = this.returnSeriesIfName(series, this.gridPurchased, 'electricity.grid.purchase.energy');
this.gridDelivered = this.returnSeriesIfName(series, this.gridDelivered, 'electricity.grid.delivery.energy');
this.gridPower = this.returnSeriesIfName(series, this.gridPower, 'electricity.grid.power');
this.photovoltaicProduced = this.returnSeriesIfName(series, this.photovoltaicProduced, 'electricity.photovoltaic.energy');
this.photovoltaicPower = this.returnSeriesIfName(series, this.photovoltaicPower, 'electricity.photovoltaic.power');
this.schlafzimmerTemperature = this.returnSeriesIfName(series, this.schlafzimmerTemperature, 'schlafzimmer.temperature');
this.schlafzimmerHumidityRelative = this.returnSeriesIfName(series, this.schlafzimmerHumidityRelative, 'schlafzimmer.humidity_relative');
this.schlafzimmerHumidityAbsolute = this.returnSeriesIfName(series, this.schlafzimmerHumidityAbsolute, 'schlafzimmer.humidity_absolute');
this.outdoorTemperature = this.returnSeriesIfName(series, this.outdoorTemperature, 'garten.temperature');
this.outdoorHumidityRelative = this.returnSeriesIfName(series, this.outdoorHumidityRelative, 'garten.humidity_relative');
this.outdoorHumidityAbsolute = this.returnSeriesIfName(series, this.outdoorHumidityAbsolute, 'garten.humidity_absolute');
this.heatingRoomTemperature = this.returnSeriesIfName(series, this.heatingRoomTemperature, 'heating.room.temperature');
this.heatingRoomHumidityRelative = this.returnSeriesIfName(series, this.heatingRoomHumidityRelative, 'heating.room.humidity_relative');
this.heatingRoomHumidityAbsolute = this.returnSeriesIfName(series, this.heatingRoomHumidityAbsolute, 'heating.room.humidity_absolute');
this.heatingExhaustTemperature = this.returnSeriesIfName(series, this.heatingExhaustTemperature, 'heating.exhaust.temperature');
this.heatingBufferSupplyTemperature = this.returnSeriesIfName(series, this.heatingBufferSupplyTemperature, 'heating.buffer.supply.temperature');
this.heatingBufferReturnTemperature = this.returnSeriesIfName(series, this.heatingBufferReturnTemperature, 'heating.buffer.return.temperature');
this.heatingBufferColdTemperature = this.returnSeriesIfName(series, this.heatingBufferColdTemperature, 'heating.buffer.cold.temperature');
this.heatingBufferInnerTemperature = this.returnSeriesIfName(series, this.heatingBufferInnerTemperature, 'heating.buffer.inner.temperature');
this.heatingBufferHotTemperature = this.returnSeriesIfName(series, this.heatingBufferHotTemperature, 'heating.buffer.hot.temperature');
this.heatingBufferCirculationTemperature = this.returnSeriesIfName(series, this.heatingBufferCirculationTemperature, 'heating.buffer.circulation.temperature');
this.heatingLoopSupplyTemperature = this.returnSeriesIfName(series, this.heatingLoopSupplyTemperature, 'heating.loop.supply.temperature');
this.heatingLoopReturnTemperature = this.returnSeriesIfName(series, this.heatingLoopReturnTemperature, 'heating.loop.return.temperature');
}
private returnSeriesIfName(fresh: Series, old: Series, name: string): Series {
if (fresh.name !== name) {
return old;
}
if (old === null || old.lastDate.getTime() <= fresh.lastDate.getTime()) {
return fresh;
}
return old;
// -
}
subscribe(next: Next<Series>) {

View File

@ -1,7 +1,7 @@
import {Component, Input} from '@angular/core';
import {ValueListComponent} from "../../../shared/value-list/value-list.component";
import {SeriesService} from "../../../api/series/series.service";
import {DisplayValue, Display} from "../../../api/Value/Display";
import {Display, DisplayValue} from "../../../api/Value/Display";
import {SeriesCacheService} from "../../../api/series/series-cache.service";
@Component({
selector: 'app-dashboard-air-tile',
@ -18,30 +18,30 @@ export class DashboardAirTileComponent {
now!: Date;
constructor(
protected readonly seriesService: SeriesService,
protected readonly seriesCacheService: SeriesCacheService,
) {
// -
}
getDisplayList(): Display[] {
const bedroomVent = this.seriesService.schlafzimmerHumidityAbsolute.gt(this.seriesService.outdoorHumidityAbsolute);
const bedroomVent = this.seriesCacheService.schlafzimmerHumidityAbsolute.gt(this.seriesCacheService.outdoorHumidityAbsolute);
const bedroomVentColor = bedroomVent ? 'red' : '';
const heatingRoomVent = this.seriesService.heatingRoomHumidityAbsolute.gt(this.seriesService.outdoorHumidityAbsolute);
const heatingRoomVent = this.seriesCacheService.heatingRoomHumidityAbsolute.gt(this.seriesCacheService.outdoorHumidityAbsolute);
const heatingRoomVentColor = heatingRoomVent ? 'red' : '';
return [
new DisplayValue('Garten Temperatur', this.seriesService.outdoorTemperature, ''),
new DisplayValue('Garten Feucht. Relativ', this.seriesService.outdoorHumidityRelative, ''),
new DisplayValue('Garten Feucht. Absolut', this.seriesService.outdoorHumidityAbsolute, ''),
new DisplayValue('Garten Temperatur', this.seriesCacheService.outdoorTemperature, ''),
new DisplayValue('Garten Feucht. Relativ', this.seriesCacheService.outdoorHumidityRelative, ''),
new DisplayValue('Garten Feucht. Absolut', this.seriesCacheService.outdoorHumidityAbsolute, ''),
null,
new DisplayValue('Schlaf. Temperatur', this.seriesService.schlafzimmerTemperature, ''),
new DisplayValue('Schlaf. Feucht. Relativ', this.seriesService.schlafzimmerHumidityRelative, ''),
new DisplayValue('Schlaf. Feucht. Absolut', this.seriesService.schlafzimmerHumidityAbsolute, bedroomVentColor),
new DisplayValue('Schlaf. Temperatur', this.seriesCacheService.schlafzimmerTemperature, ''),
new DisplayValue('Schlaf. Feucht. Relativ', this.seriesCacheService.schlafzimmerHumidityRelative, ''),
new DisplayValue('Schlaf. Feucht. Absolut', this.seriesCacheService.schlafzimmerHumidityAbsolute, bedroomVentColor),
null,
new DisplayValue('Heiz. Temperatur', this.seriesService.heatingRoomTemperature, ''),
new DisplayValue('Heiz. Feucht. Relativ', this.seriesService.heatingRoomHumidityRelative, ''),
new DisplayValue('Heiz. Feucht. Absolut', this.seriesService.heatingRoomHumidityAbsolute, heatingRoomVentColor),
new DisplayValue('Heiz. Temperatur', this.seriesCacheService.heatingRoomTemperature, ''),
new DisplayValue('Heiz. Feucht. Relativ', this.seriesCacheService.heatingRoomHumidityRelative, ''),
new DisplayValue('Heiz. Feucht. Absolut', this.seriesCacheService.heatingRoomHumidityAbsolute, heatingRoomVentColor),
];
}

View File

@ -1,8 +1,8 @@
import {Component, Input} from '@angular/core';
import {ValueListComponent} from "../../../shared/value-list/value-list.component";
import {SeriesService} from "../../../api/series/series.service";
import {DisplayValue, Display} from "../../../api/Value/Display";
import {Display, DisplayValue} from "../../../api/Value/Display";
import {ValueConstant} from "../../../api/Value/ValueConstant";
import {SeriesCacheService} from "../../../api/series/series-cache.service";
const PURCHASING_MUCH = 200;
@ -23,45 +23,45 @@ export class DashboardElectricityTileComponent {
now!: Date;
constructor(
protected readonly seriesService: SeriesService,
protected readonly seriesCacheService: SeriesCacheService,
) {
// -
}
getDisplayList(): Display[] {
const consumptionPower = this.seriesService.photovoltaicPower.plus(this.seriesService.gridPower).clampNonNegative();
const producedAfterChange = this.seriesService.photovoltaicProduced.minus(PRODUCED_BEFORE_METER_CHANGE);
const selfAfterChange = producedAfterChange.minus(this.seriesService.gridDelivered);
const consumptionPower = this.seriesCacheService.photovoltaicPower.plus(this.seriesCacheService.gridPower).clampNonNegative();
const producedAfterChange = this.seriesCacheService.photovoltaicProduced.minus(PRODUCED_BEFORE_METER_CHANGE);
const selfAfterChange = producedAfterChange.minus(this.seriesCacheService.gridDelivered);
const selfRatio = selfAfterChange.div(producedAfterChange);
const selfConsumed = selfRatio.mul(this.seriesService.photovoltaicProduced);
const selfConsumed = selfRatio.mul(this.seriesCacheService.photovoltaicProduced);
const gridColor = this.getGridPowerColor();
const productionColor = this.getProductionPowerColor();
return [
new DisplayValue('Bezogen', this.seriesService.gridPurchased, ''),
new DisplayValue('Eingespeist', this.seriesService.gridDelivered, ''),
new DisplayValue('Produziert', this.seriesService.photovoltaicProduced, ''),
new DisplayValue('Bezogen', this.seriesCacheService.gridPurchased, ''),
new DisplayValue('Eingespeist', this.seriesCacheService.gridDelivered, ''),
new DisplayValue('Produziert', this.seriesCacheService.photovoltaicProduced, ''),
new DisplayValue('Selbst verbraucht', selfConsumed, ''),
null,
new DisplayValue('Produktion', this.seriesService.photovoltaicPower, productionColor),
new DisplayValue('Netz', this.seriesService.gridPower, gridColor),
new DisplayValue('Produktion', this.seriesCacheService.photovoltaicPower, productionColor),
new DisplayValue('Netz', this.seriesCacheService.gridPower, gridColor),
new DisplayValue('Verbrauch', consumptionPower, ''),
];
}
private getGridPowerColor(): string {
if (this.seriesService.gridPower !== null && this.seriesService.gridPower.value !== null) {
const deliveringAny = this.seriesService.gridPower.value < 0;
if (this.seriesCacheService.gridPower !== null && this.seriesCacheService.gridPower.value !== null) {
const deliveringAny = this.seriesCacheService.gridPower.value < 0;
if (deliveringAny) {
return 'magenta';
}
const purchasingMuch = this.seriesService.gridPower.value > PURCHASING_MUCH;
const purchasingMuch = this.seriesCacheService.gridPower.value > PURCHASING_MUCH;
if (purchasingMuch) {
return 'red';
}
const purchasingAny = this.seriesService.gridPower.value > 0;
const purchasingAny = this.seriesCacheService.gridPower.value > 0;
if (purchasingAny) {
return 'orange';
}
@ -72,11 +72,11 @@ export class DashboardElectricityTileComponent {
}
private getProductionPowerColor() {
if (this.seriesService.photovoltaicPower === null || this.seriesService.photovoltaicPower.value === null) {
if (this.seriesCacheService.photovoltaicPower === null || this.seriesCacheService.photovoltaicPower.value === null) {
return '';
}
const producingAny = this.seriesService.photovoltaicPower.value > 0;
const producingAny = this.seriesCacheService.photovoltaicPower.value > 0;
if (producingAny) {
return 'green';
}

View File

@ -1,7 +1,7 @@
import {Component, Input} from '@angular/core';
import {ValueListComponent} from "../../../shared/value-list/value-list.component";
import {SeriesService} from "../../../api/series/series.service";
import {DisplayValue, Display} from "../../../api/Value/Display";
import {Display, DisplayValue} from "../../../api/Value/Display";
import {SeriesCacheService} from "../../../api/series/series-cache.service";
const WARM = 25;
@ -22,25 +22,25 @@ export class DashboardHeatingTileComponent {
now!: Date;
constructor(
protected readonly seriesService: SeriesService,
protected readonly seriesCacheService: SeriesCacheService,
) {
// -
}
getDisplayList(): Display[] {
return [
new DisplayValue('Abgas', this.seriesService.heatingExhaustTemperature, this.seriesService.heatingExhaustTemperature.color(WARM, HOT, '')),
new DisplayValue('Abgas', this.seriesCacheService.heatingExhaustTemperature, this.seriesCacheService.heatingExhaustTemperature.color(WARM, HOT, '')),
null,
new DisplayValue('Heizkreis Vorlauf', this.seriesService.heatingLoopSupplyTemperature, this.seriesService.heatingLoopSupplyTemperature.color(WARM, HOT)),
new DisplayValue('Heizkreis Rücklauf', this.seriesService.heatingLoopReturnTemperature, this.seriesService.heatingLoopReturnTemperature.color(WARM, HOT)),
new DisplayValue('Heizkreis Vorlauf', this.seriesCacheService.heatingLoopSupplyTemperature, this.seriesCacheService.heatingLoopSupplyTemperature.color(WARM, HOT)),
new DisplayValue('Heizkreis Rücklauf', this.seriesCacheService.heatingLoopReturnTemperature, this.seriesCacheService.heatingLoopReturnTemperature.color(WARM, HOT)),
null,
new DisplayValue('Puffer Vorlauf', this.seriesService.heatingBufferSupplyTemperature, this.seriesService.heatingBufferSupplyTemperature.color(WARM, HOT)),
new DisplayValue('Puffer Rücklauf', this.seriesService.heatingBufferReturnTemperature, this.seriesService.heatingBufferReturnTemperature.color(WARM, HOT)),
new DisplayValue('Puffer Vorlauf', this.seriesCacheService.heatingBufferSupplyTemperature, this.seriesCacheService.heatingBufferSupplyTemperature.color(WARM, HOT)),
new DisplayValue('Puffer Rücklauf', this.seriesCacheService.heatingBufferReturnTemperature, this.seriesCacheService.heatingBufferReturnTemperature.color(WARM, HOT)),
null,
new DisplayValue('Puffer Kalt', this.seriesService.heatingBufferColdTemperature, this.seriesService.heatingBufferColdTemperature.color(WARM, HOT)),
new DisplayValue('Puffer Speicher', this.seriesService.heatingBufferInnerTemperature, this.seriesService.heatingBufferInnerTemperature.color(WARM, HOT)),
new DisplayValue('Puffer Warm', this.seriesService.heatingBufferHotTemperature, this.seriesService.heatingBufferHotTemperature.color(WARM, HOT)),
new DisplayValue('Puffer Zirkulation', this.seriesService.heatingBufferCirculationTemperature, this.seriesService.heatingBufferCirculationTemperature.color(WARM, HOT)),
new DisplayValue('Puffer Kalt', this.seriesCacheService.heatingBufferColdTemperature, this.seriesCacheService.heatingBufferColdTemperature.color(WARM, HOT)),
new DisplayValue('Puffer Speicher', this.seriesCacheService.heatingBufferInnerTemperature, this.seriesCacheService.heatingBufferInnerTemperature.color(WARM, HOT)),
new DisplayValue('Puffer Warm', this.seriesCacheService.heatingBufferHotTemperature, this.seriesCacheService.heatingBufferHotTemperature.color(WARM, HOT)),
new DisplayValue('Puffer Zirkulation', this.seriesCacheService.heatingBufferCirculationTemperature, this.seriesCacheService.heatingBufferCirculationTemperature.color(WARM, HOT)),
];
}