even cooler now
This commit is contained in:
@@ -1,18 +1,21 @@
|
||||
import {
|
||||
type API_HA_DeskPosition,
|
||||
BaseService,
|
||||
type ComponentUpdate,
|
||||
type FullInformation,
|
||||
type GristRecord_PersonalGoals,
|
||||
type HomeAssistantDeskPositionResult,
|
||||
type ServiceResult,
|
||||
type TidalGetCurrent,
|
||||
type WsService,
|
||||
type API_HA_DeskPosition,
|
||||
BaseService,
|
||||
type ComponentUpdate,
|
||||
type FullInformation,
|
||||
type GristRecord_PersonalGoals,
|
||||
type HomeAssistantDeskPositionResult,
|
||||
type ServiceResult,
|
||||
type TidalGetCurrent,
|
||||
type WsService,
|
||||
createComponentUpdate,
|
||||
StatusComponent,
|
||||
} from "@dpu/shared";
|
||||
import { logInfo } from "@dpu/shared/dist/logger.js";
|
||||
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";
|
||||
import type { WebSocket } from "ws";
|
||||
|
||||
export class HomepageService extends BaseService<null> {
|
||||
private gristService: GristService;
|
||||
@@ -39,21 +42,34 @@ export class HomepageService extends BaseService<null> {
|
||||
this.wsService = wsService;
|
||||
}
|
||||
|
||||
async sendFullInformationToSocket(socket: WebSocket): Promise<void> {
|
||||
try {
|
||||
const [desk, temp, tidal, personal_goals] = await this._getAll();
|
||||
const updates: ComponentUpdate[] = [
|
||||
createComponentUpdate(StatusComponent.HA_DESK_POSITION, desk),
|
||||
createComponentUpdate(StatusComponent.HA_TEMP, temp),
|
||||
createComponentUpdate(StatusComponent.TIDAL_CURRENT, tidal),
|
||||
createComponentUpdate(StatusComponent.GRIST_PERSONAL_GOALS, personal_goals)
|
||||
];
|
||||
socket.send(JSON.stringify({
|
||||
type: "update",
|
||||
data: updates
|
||||
}))
|
||||
} catch (error) {
|
||||
logWarning("error getting all information for socket update.", error);
|
||||
}
|
||||
}
|
||||
|
||||
async getFullInformation(): Promise<ServiceResult<FullInformation | string>> {
|
||||
try {
|
||||
const [desk, temp, tidal, personal_goals] = await Promise.all([
|
||||
this._getDesk(),
|
||||
this._getTemp(),
|
||||
this._getTidal(),
|
||||
this._getGristPG(),
|
||||
]);
|
||||
const [desk, temp, tidal, personal_goals] = await this._getAll();
|
||||
|
||||
return this.getSuccessfulResult(
|
||||
this.updateFull({
|
||||
ha_desk_position: desk,
|
||||
ha_temp: temp,
|
||||
tidal_current: tidal,
|
||||
grist_personal_goals: personal_goals,
|
||||
ha_desk_position: desk as API_HA_DeskPosition,
|
||||
ha_temp: temp as string,
|
||||
tidal_current: tidal as TidalGetCurrent,
|
||||
grist_personal_goals: personal_goals as GristRecord_PersonalGoals,
|
||||
}),
|
||||
);
|
||||
} catch (error) {
|
||||
@@ -61,6 +77,15 @@ export class HomepageService extends BaseService<null> {
|
||||
}
|
||||
}
|
||||
|
||||
private async _getAll(): Promise<Iterable<unknown>> {
|
||||
return await Promise.all([
|
||||
this._getDesk(),
|
||||
this._getTemp(),
|
||||
this._getTidal(),
|
||||
this._getGristPG(),
|
||||
]);
|
||||
}
|
||||
|
||||
private async _getDesk(): Promise<API_HA_DeskPosition | null> {
|
||||
const desk = await this.haService.getDeskPosition();
|
||||
return desk.successful
|
||||
@@ -112,26 +137,26 @@ export class HomepageService extends BaseService<null> {
|
||||
const updates: ComponentUpdate[] = [];
|
||||
for (const component of components) {
|
||||
switch (component.component) {
|
||||
case "desk": {
|
||||
case StatusComponent.HA_DESK_POSITION: {
|
||||
this.updateHaDesk(
|
||||
(component.data as API_HA_DeskPosition) ?? (await this._getDesk()),
|
||||
updates,
|
||||
);
|
||||
break;
|
||||
}
|
||||
case "temp":
|
||||
case StatusComponent.HA_TEMP:
|
||||
this.updateHaTemp(
|
||||
(component.data as string) ?? (await this._getTemp()),
|
||||
updates,
|
||||
);
|
||||
break;
|
||||
case "tidal":
|
||||
case StatusComponent.TIDAL_CURRENT:
|
||||
this.updateTidal(
|
||||
(component.data as TidalGetCurrent) ?? (await this._getTidal()),
|
||||
updates,
|
||||
);
|
||||
break;
|
||||
case "grist":
|
||||
case StatusComponent.GRIST_PERSONAL_GOALS:
|
||||
this.updateGristPG(
|
||||
(component.data as GristRecord_PersonalGoals) ??
|
||||
(await this._getGristPG()),
|
||||
@@ -159,20 +184,14 @@ export class HomepageService extends BaseService<null> {
|
||||
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,
|
||||
});
|
||||
updates.push(createComponentUpdate(StatusComponent.HA_DESK_POSITION, new_ha_desk_position));
|
||||
}
|
||||
}
|
||||
|
||||
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,
|
||||
});
|
||||
updates.push(createComponentUpdate(StatusComponent.HA_TEMP, new_ha_temp));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -187,10 +206,7 @@ export class HomepageService extends BaseService<null> {
|
||||
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,
|
||||
});
|
||||
updates.push(createComponentUpdate(StatusComponent.TIDAL_CURRENT, new_tidal_current));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -215,10 +231,7 @@ export class HomepageService extends BaseService<null> {
|
||||
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,
|
||||
});
|
||||
updates.push(createComponentUpdate(StatusComponent.GRIST_PERSONAL_GOALS, new_grist_personal_goals));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user