update grist every night

This commit is contained in:
Darius
2026-02-10 01:03:03 +01:00
parent c98304fe8e
commit 772a54aca8
2 changed files with 73 additions and 25 deletions

View File

@@ -2,20 +2,21 @@ import {
type API_HA_DeskPosition, type API_HA_DeskPosition,
BaseService, BaseService,
type ComponentUpdate, type ComponentUpdate,
createComponentUpdate,
type FullInformation, type FullInformation,
type GristRecord_PersonalGoals, type GristRecord_PersonalGoals,
type HomeAssistantDeskPositionResult, type HomeAssistantDeskPositionResult,
type ServiceResult, type ServiceResult,
StatusComponent,
type TidalGetCurrent, type TidalGetCurrent,
type WsService, type WsService,
createComponentUpdate,
StatusComponent,
} from "@dpu/shared"; } from "@dpu/shared";
import { logInfo, logWarning } from "@dpu/shared/dist/logger.js"; import { logInfo, logWarning } from "@dpu/shared/dist/logger.js";
import { DateTime } from "luxon";
import type { WebSocket } from "ws";
import type { GristService } from "../grist/service"; import type { GristService } from "../grist/service";
import type { HomeAssistantService } from "../homeassistant/service"; import type { HomeAssistantService } from "../homeassistant/service";
import type { TidalService } from "../tidal/service"; import type { TidalService } from "../tidal/service";
import type { WebSocket } from "ws";
export class HomepageService extends BaseService<null> { export class HomepageService extends BaseService<null> {
private gristService: GristService; private gristService: GristService;
@@ -42,6 +43,34 @@ export class HomepageService extends BaseService<null> {
this.wsService = wsService; this.wsService = wsService;
} }
scheduleMidnightGristUpdate(): void {
const now = DateTime.now().setZone("Europe/Berlin");
const nextMidnight = now.plus({ days: 1 }).startOf("day");
setTimeout(async () => {
await this.fetchAndBroadcastGrist();
this.scheduleMidnightGristUpdate();
}, nextMidnight.diff(now).toMillis());
}
private async fetchAndBroadcastGrist(): Promise<void> {
try {
if (this.wsService.getClientCount() > 0) {
const updates: ComponentUpdate[] = [];
this.updateGristPG(await this._getGristPG(), updates);
if (updates.length > 0) {
this.wsService.broadcast({
type: "update",
data: updates,
});
}
}
} catch (error) {
logWarning("Error fetching midnight Grist update", error);
}
}
async sendFullInformationToSocket(socket: WebSocket): Promise<void> { async sendFullInformationToSocket(socket: WebSocket): Promise<void> {
try { try {
const [desk, temp, tidal, personal_goals] = await this._getAll(); const [desk, temp, tidal, personal_goals] = await this._getAll();
@@ -49,12 +78,17 @@ export class HomepageService extends BaseService<null> {
createComponentUpdate(StatusComponent.HA_DESK_POSITION, desk), createComponentUpdate(StatusComponent.HA_DESK_POSITION, desk),
createComponentUpdate(StatusComponent.HA_TEMP, temp), createComponentUpdate(StatusComponent.HA_TEMP, temp),
createComponentUpdate(StatusComponent.TIDAL_CURRENT, tidal), createComponentUpdate(StatusComponent.TIDAL_CURRENT, tidal),
createComponentUpdate(StatusComponent.GRIST_PERSONAL_GOALS, personal_goals) createComponentUpdate(
StatusComponent.GRIST_PERSONAL_GOALS,
personal_goals,
),
]; ];
socket.send(JSON.stringify({ socket.send(
JSON.stringify({
type: "update", type: "update",
data: updates data: updates,
})) }),
);
} catch (error) { } catch (error) {
logWarning("error getting all information for socket update.", error); logWarning("error getting all information for socket update.", error);
} }
@@ -184,7 +218,12 @@ export class HomepageService extends BaseService<null> {
new_ha_desk_position?.is_standing new_ha_desk_position?.is_standing
) { ) {
this.lastPoll.ha_desk_position = new_ha_desk_position; this.lastPoll.ha_desk_position = new_ha_desk_position;
updates.push(createComponentUpdate(StatusComponent.HA_DESK_POSITION, new_ha_desk_position)); updates.push(
createComponentUpdate(
StatusComponent.HA_DESK_POSITION,
new_ha_desk_position,
),
);
} }
} }
@@ -206,7 +245,9 @@ export class HomepageService extends BaseService<null> {
this.lastPoll.tidal_current?.volume !== new_tidal_current?.volume this.lastPoll.tidal_current?.volume !== new_tidal_current?.volume
) { ) {
this.lastPoll.tidal_current = new_tidal_current; this.lastPoll.tidal_current = new_tidal_current;
updates.push(createComponentUpdate(StatusComponent.TIDAL_CURRENT, new_tidal_current)); updates.push(
createComponentUpdate(StatusComponent.TIDAL_CURRENT, new_tidal_current),
);
} }
} }
@@ -231,7 +272,12 @@ export class HomepageService extends BaseService<null> {
new_grist_personal_goals?.stairs new_grist_personal_goals?.stairs
) { ) {
this.lastPoll.grist_personal_goals = new_grist_personal_goals; this.lastPoll.grist_personal_goals = new_grist_personal_goals;
updates.push(createComponentUpdate(StatusComponent.GRIST_PERSONAL_GOALS, new_grist_personal_goals)); updates.push(
createComponentUpdate(
StatusComponent.GRIST_PERSONAL_GOALS,
new_grist_personal_goals,
),
);
} }
} }
} }

View File

@@ -154,6 +154,8 @@ const hpService = new HomepageService(
wsService, wsService,
); );
hpService.scheduleMidnightGristUpdate();
async function verifyAPIKey( async function verifyAPIKey(
request: FastifyRequest, request: FastifyRequest,
reply: FastifyReply, reply: FastifyReply,