update for partial polling, add grist
This commit is contained in:
@@ -1,28 +1,40 @@
|
||||
import {
|
||||
type API_HA_DeskPosition,
|
||||
BaseService,
|
||||
type ComponentUpdate,
|
||||
type FullInformation,
|
||||
type GristRecord_PersonalGoals,
|
||||
type HomeAssistantDeskPositionResult,
|
||||
type ServiceResult,
|
||||
type TidalGetCurrent,
|
||||
type WsService,
|
||||
} from "@dpu/shared";
|
||||
import { logInfo, logWarning } from "@dpu/shared/dist/logger.js";
|
||||
import type { GristService } from "../grist/service";
|
||||
import type { HomeAssistantService } from "../homeassistant/service";
|
||||
import type { TidalService } from "../tidal/service";
|
||||
|
||||
export class HomepageService extends BaseService<null> {
|
||||
private gristService: GristService;
|
||||
private haService: HomeAssistantService;
|
||||
private tidalService: TidalService;
|
||||
private wsService: WsService;
|
||||
private pollingInterval: ReturnType<typeof setInterval> | null = null;
|
||||
private lastPoll: FullInformation | null = null;
|
||||
private pollingIntervals: ReturnType<typeof setInterval>[] = [];
|
||||
private lastPoll: FullInformation = {
|
||||
ha_desk_position: null,
|
||||
ha_temp: null,
|
||||
tidal_current: null,
|
||||
grist_personal_goals: null,
|
||||
};
|
||||
|
||||
constructor(
|
||||
gristService: GristService,
|
||||
haService: HomeAssistantService,
|
||||
tidalService: TidalService,
|
||||
wsService: WsService,
|
||||
) {
|
||||
super(null);
|
||||
this.gristService = gristService;
|
||||
this.haService = haService;
|
||||
this.tidalService = tidalService;
|
||||
this.wsService = wsService;
|
||||
@@ -31,23 +43,21 @@ export class HomepageService extends BaseService<null> {
|
||||
|
||||
async getFullInformation(): Promise<ServiceResult<FullInformation | string>> {
|
||||
try {
|
||||
const [desk, temp, song] = await Promise.all([
|
||||
this.haService.getDeskPosition(),
|
||||
this.haService.getTemperatureText(),
|
||||
this.tidalService.getSong(),
|
||||
const [desk, temp, tidal, personal_goals] = await Promise.all([
|
||||
this._getDesk(),
|
||||
this._getTemp(),
|
||||
this._getTidal(),
|
||||
this._getGristPG(),
|
||||
]);
|
||||
|
||||
const result = {
|
||||
ha_desk_position: desk.successful
|
||||
? this.haService.convertPosResultToApiAnswer(
|
||||
desk.result as HomeAssistantDeskPositionResult,
|
||||
)
|
||||
: null,
|
||||
ha_temp: temp.successful ? temp.result : null,
|
||||
tidal_current: song ? (song.result as TidalGetCurrent) : null,
|
||||
};
|
||||
|
||||
return this.getSuccessfulResult(this.updateLastPoll(result));
|
||||
return this.getSuccessfulResult(
|
||||
this.updateFull({
|
||||
ha_desk_position: desk,
|
||||
ha_temp: temp,
|
||||
tidal_current: tidal,
|
||||
grist_personal_goals: personal_goals,
|
||||
}),
|
||||
);
|
||||
} catch {
|
||||
const error_message = "error getting all information";
|
||||
logWarning(error_message);
|
||||
@@ -55,34 +65,39 @@ export class HomepageService extends BaseService<null> {
|
||||
}
|
||||
}
|
||||
|
||||
updateLastPoll(newPoll: FullInformation) {
|
||||
const updates = [];
|
||||
if (
|
||||
this.lastPoll?.ha_desk_position?.is_standing !==
|
||||
newPoll.ha_desk_position?.is_standing
|
||||
) {
|
||||
updates.push({
|
||||
component: "ha_desk_position",
|
||||
data: newPoll.ha_desk_position,
|
||||
});
|
||||
}
|
||||
private async _getDesk(): Promise<API_HA_DeskPosition | null> {
|
||||
const desk = await this.haService.getDeskPosition();
|
||||
return desk.successful
|
||||
? this.haService.convertPosResultToApiAnswer(
|
||||
desk.result as HomeAssistantDeskPositionResult,
|
||||
)
|
||||
: null;
|
||||
}
|
||||
|
||||
if (this.lastPoll?.ha_temp !== newPoll.ha_temp) {
|
||||
updates.push({
|
||||
component: "ha_temp",
|
||||
data: newPoll.ha_temp,
|
||||
});
|
||||
}
|
||||
private async _getTemp(): Promise<string | null> {
|
||||
const temp = await this.haService.getTemperatureText();
|
||||
return temp.successful ? temp.result : null;
|
||||
}
|
||||
|
||||
if (
|
||||
this.lastPoll?.tidal_current?.title !== newPoll.tidal_current?.title ||
|
||||
this.lastPoll?.tidal_current?.status !== newPoll.tidal_current?.status
|
||||
) {
|
||||
updates.push({
|
||||
component: "tidal_current",
|
||||
data: newPoll.tidal_current,
|
||||
});
|
||||
}
|
||||
private async _getTidal(): Promise<TidalGetCurrent | null> {
|
||||
const tidal = await this.tidalService.getSong();
|
||||
return tidal ? (tidal.result as TidalGetCurrent) : null;
|
||||
}
|
||||
|
||||
private async _getGristPG(): Promise<GristRecord_PersonalGoals | null> {
|
||||
const personal_goals = await this.gristService.getToday();
|
||||
return personal_goals.successful
|
||||
? (personal_goals.result as GristRecord_PersonalGoals)
|
||||
: null;
|
||||
}
|
||||
|
||||
updateFull(newPoll: FullInformation): FullInformation {
|
||||
const updates: ComponentUpdate[] = [];
|
||||
|
||||
this.updateHaDesk(newPoll.ha_desk_position, updates);
|
||||
this.updateHaTemp(newPoll.ha_temp, updates);
|
||||
this.updateTidal(newPoll.tidal_current, updates);
|
||||
this.updateGristPG(newPoll.grist_personal_goals, updates);
|
||||
|
||||
if (updates.length > 0) {
|
||||
logInfo("Updating clients");
|
||||
@@ -92,16 +107,117 @@ export class HomepageService extends BaseService<null> {
|
||||
});
|
||||
}
|
||||
|
||||
this.lastPoll = newPoll;
|
||||
return newPoll;
|
||||
}
|
||||
|
||||
async updatePartial(components: string[]): Promise<void> {
|
||||
const updates: ComponentUpdate[] = [];
|
||||
for (const component of components) {
|
||||
switch (component) {
|
||||
case "desk": {
|
||||
this.updateHaDesk(await this._getDesk(), updates);
|
||||
break;
|
||||
}
|
||||
case "temp":
|
||||
this.updateHaTemp(await this._getTemp(), updates);
|
||||
break;
|
||||
case "tidal":
|
||||
this.updateTidal(await this._getTidal(), updates);
|
||||
break;
|
||||
case "grist":
|
||||
this.updateGristPG(await this._getGristPG(), updates);
|
||||
break;
|
||||
default:
|
||||
}
|
||||
}
|
||||
|
||||
this.wsService.broadcast({
|
||||
type: "update",
|
||||
data: updates,
|
||||
});
|
||||
}
|
||||
|
||||
private updateHaDesk(
|
||||
new_ha_desk_position: API_HA_DeskPosition | null,
|
||||
updates: ComponentUpdate[],
|
||||
): void {
|
||||
if (
|
||||
this.lastPoll.ha_desk_position?.is_standing !==
|
||||
new_ha_desk_position?.is_standing
|
||||
) {
|
||||
this.lastPoll.ha_desk_position = new_ha_desk_position;
|
||||
updates.push({
|
||||
component: "ha_desk_position",
|
||||
data: new_ha_desk_position,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
private 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({
|
||||
component: "ha_temp",
|
||||
data: new_ha_temp,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
private updateTidal(
|
||||
new_tidal_current: TidalGetCurrent | null,
|
||||
updates: ComponentUpdate[],
|
||||
): void {
|
||||
if (
|
||||
this.lastPoll.tidal_current?.title !== new_tidal_current?.title ||
|
||||
this.lastPoll.tidal_current?.artists !== new_tidal_current?.artists ||
|
||||
this.lastPoll.tidal_current?.status !== new_tidal_current?.status ||
|
||||
this.lastPoll.tidal_current?.volume !== new_tidal_current?.volume
|
||||
) {
|
||||
this.lastPoll.tidal_current = new_tidal_current;
|
||||
updates.push({
|
||||
component: "tidal_current",
|
||||
data: new_tidal_current,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
private updateGristPG(
|
||||
new_grist_personal_goals: GristRecord_PersonalGoals | null,
|
||||
updates: ComponentUpdate[],
|
||||
): void {
|
||||
if (
|
||||
this.lastPoll.grist_personal_goals?.went_outside !==
|
||||
new_grist_personal_goals?.went_outside ||
|
||||
this.lastPoll.grist_personal_goals?.standing !==
|
||||
new_grist_personal_goals?.standing ||
|
||||
this.lastPoll.grist_personal_goals?.steps !==
|
||||
new_grist_personal_goals?.steps ||
|
||||
this.lastPoll.grist_personal_goals?.pushups !==
|
||||
new_grist_personal_goals?.pushups ||
|
||||
this.lastPoll.grist_personal_goals?.squats !==
|
||||
new_grist_personal_goals?.squats ||
|
||||
this.lastPoll.grist_personal_goals?.leg_raises !==
|
||||
new_grist_personal_goals?.leg_raises ||
|
||||
this.lastPoll.grist_personal_goals?.stairs !==
|
||||
new_grist_personal_goals?.stairs
|
||||
) {
|
||||
this.lastPoll.grist_personal_goals = new_grist_personal_goals;
|
||||
updates.push({
|
||||
component: "grist_personal_goals",
|
||||
data: new_grist_personal_goals,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
listenForClientChange(): void {
|
||||
this.wsService.onClientChange((clients: number) => {
|
||||
if (clients === 0) {
|
||||
this.stopPolling();
|
||||
} else {
|
||||
if (!this.pollingInterval) {
|
||||
if (this.pollingIntervals.length === 0) {
|
||||
this.startPolling();
|
||||
}
|
||||
}
|
||||
@@ -110,17 +226,24 @@ export class HomepageService extends BaseService<null> {
|
||||
|
||||
startPolling(): void {
|
||||
logInfo("Polling started");
|
||||
this.pollingInterval = setInterval(() => {
|
||||
logInfo("Polling now");
|
||||
this.getFullInformation();
|
||||
}, 30000);
|
||||
|
||||
const config: [string[], number][] = [
|
||||
[["tidal"], 20_000],
|
||||
[["desk"], 60_000],
|
||||
[["temp", "grist"], 600_000],
|
||||
];
|
||||
|
||||
this.pollingIntervals = config.map(([components, interval]) =>
|
||||
setInterval(() => {
|
||||
logInfo(`Polling ${components.join(", ")}`);
|
||||
this.updatePartial(components);
|
||||
}, interval),
|
||||
);
|
||||
}
|
||||
|
||||
stopPolling(): void {
|
||||
logInfo("Polling ended");
|
||||
if (this.pollingInterval) {
|
||||
clearInterval(this.pollingInterval);
|
||||
this.pollingInterval = null;
|
||||
}
|
||||
this.pollingIntervals.forEach(clearInterval);
|
||||
this.pollingIntervals = [];
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user