change from poll to update from hooks
This commit is contained in:
@@ -9,7 +9,7 @@ import {
|
||||
type TidalGetCurrent,
|
||||
type WsService,
|
||||
} from "@dpu/shared";
|
||||
import { logInfo, logWarning } from "@dpu/shared/dist/logger.js";
|
||||
import { logInfo } from "@dpu/shared/dist/logger.js";
|
||||
import type { GristService } from "../grist/service";
|
||||
import type { HomeAssistantService } from "../homeassistant/service";
|
||||
import type { TidalService } from "../tidal/service";
|
||||
@@ -58,10 +58,8 @@ export class HomepageService extends BaseService<null> {
|
||||
grist_personal_goals: personal_goals,
|
||||
}),
|
||||
);
|
||||
} catch {
|
||||
const error_message = "error getting all information";
|
||||
logWarning(error_message);
|
||||
return this.getErrorResult(error_message);
|
||||
} catch (error) {
|
||||
return this.getErrorResult("error getting all information.", error);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -75,7 +73,7 @@ export class HomepageService extends BaseService<null> {
|
||||
}
|
||||
|
||||
private async _getTemp(): Promise<string | null> {
|
||||
const temp = await this.haService.getTemperatureText();
|
||||
const temp = await this.haService.getTemperature();
|
||||
return temp.successful ? temp.result : null;
|
||||
}
|
||||
|
||||
@@ -110,22 +108,37 @@ export class HomepageService extends BaseService<null> {
|
||||
return newPoll;
|
||||
}
|
||||
|
||||
async updatePartial(components: string[]): Promise<void> {
|
||||
async updatePartial(
|
||||
components: ComponentUpdate[],
|
||||
): Promise<ServiceResult<string>> {
|
||||
const updates: ComponentUpdate[] = [];
|
||||
for (const component of components) {
|
||||
switch (component) {
|
||||
switch (component.component) {
|
||||
case "desk": {
|
||||
this.updateHaDesk(await this._getDesk(), updates);
|
||||
this.updateHaDesk(
|
||||
(component.data as API_HA_DeskPosition) ?? (await this._getDesk()),
|
||||
updates,
|
||||
);
|
||||
break;
|
||||
}
|
||||
case "temp":
|
||||
this.updateHaTemp(await this._getTemp(), updates);
|
||||
this.updateHaTemp(
|
||||
(component.data as string) ?? (await this._getTemp()),
|
||||
updates,
|
||||
);
|
||||
break;
|
||||
case "tidal":
|
||||
this.updateTidal(await this._getTidal(), updates);
|
||||
this.updateTidal(
|
||||
(component.data as TidalGetCurrent) ?? (await this._getTidal()),
|
||||
updates,
|
||||
);
|
||||
break;
|
||||
case "grist":
|
||||
this.updateGristPG(await this._getGristPG(), updates);
|
||||
this.updateGristPG(
|
||||
(component.data as GristRecord_PersonalGoals) ??
|
||||
(await this._getGristPG()),
|
||||
updates,
|
||||
);
|
||||
break;
|
||||
default:
|
||||
}
|
||||
@@ -135,9 +148,11 @@ export class HomepageService extends BaseService<null> {
|
||||
type: "update",
|
||||
data: updates,
|
||||
});
|
||||
|
||||
return this.getSuccessfulResult("update broadcasted");
|
||||
}
|
||||
|
||||
private updateHaDesk(
|
||||
updateHaDesk(
|
||||
new_ha_desk_position: API_HA_DeskPosition | null,
|
||||
updates: ComponentUpdate[],
|
||||
): void {
|
||||
@@ -153,10 +168,7 @@ export class HomepageService extends BaseService<null> {
|
||||
}
|
||||
}
|
||||
|
||||
private updateHaTemp(
|
||||
new_ha_temp: string | null,
|
||||
updates: ComponentUpdate[],
|
||||
): void {
|
||||
updateHaTemp(new_ha_temp: string | null, updates: ComponentUpdate[]): void {
|
||||
if (this.lastPoll.ha_temp !== new_ha_temp) {
|
||||
this.lastPoll.ha_temp = new_ha_temp;
|
||||
updates.push({
|
||||
@@ -166,7 +178,7 @@ export class HomepageService extends BaseService<null> {
|
||||
}
|
||||
}
|
||||
|
||||
private updateTidal(
|
||||
updateTidal(
|
||||
new_tidal_current: TidalGetCurrent | null,
|
||||
updates: ComponentUpdate[],
|
||||
): void {
|
||||
@@ -184,7 +196,7 @@ export class HomepageService extends BaseService<null> {
|
||||
}
|
||||
}
|
||||
|
||||
private updateGristPG(
|
||||
updateGristPG(
|
||||
new_grist_personal_goals: GristRecord_PersonalGoals | null,
|
||||
updates: ComponentUpdate[],
|
||||
): void {
|
||||
@@ -227,10 +239,16 @@ export class HomepageService extends BaseService<null> {
|
||||
startPolling(): void {
|
||||
logInfo("Polling started");
|
||||
|
||||
const config: [string[], number][] = [
|
||||
[["tidal"], 20_000],
|
||||
[["desk"], 60_000],
|
||||
[["temp", "grist"], 600_000],
|
||||
const config: [ComponentUpdate[], number][] = [
|
||||
//[[{ component: "tidal", data: null }], 20_000],
|
||||
//[[{ component: "desk", data: null }], 60_000],
|
||||
[
|
||||
[
|
||||
//{ component: "temp", data: null },
|
||||
{ component: "grist", data: null },
|
||||
],
|
||||
600_000,
|
||||
],
|
||||
];
|
||||
|
||||
this.pollingIntervals = config.map(([components, interval]) =>
|
||||
|
||||
Reference in New Issue
Block a user