47 lines
1.4 KiB
TypeScript
47 lines
1.4 KiB
TypeScript
import {
|
|
BaseService,
|
|
FullInformation,
|
|
HomeAssistantDeskPositionResult,
|
|
SseService,
|
|
TidalGetCurrent,
|
|
type ServiceResult,
|
|
} from "@dpu/shared";
|
|
import { logWarning } from "@dpu/shared/dist/logger.js";
|
|
import { HomeAssistantService } from "../homeassistant/service";
|
|
import { TidalService } from "../tidal/service";
|
|
|
|
export class HomepageService extends BaseService<null> {
|
|
private haService: HomeAssistantService;
|
|
private tidalService: TidalService;
|
|
private sseService: SseService;
|
|
|
|
constructor(haService: HomeAssistantService, tidalService: TidalService, sseService: SseService) {
|
|
super(null);
|
|
this.haService = haService;
|
|
this.tidalService = tidalService;
|
|
this.sseService = sseService;
|
|
}
|
|
|
|
async getFullInformation(): Promise<ServiceResult<FullInformation | string>> {
|
|
try {
|
|
const [desk, temp, song] = await Promise.all([
|
|
this.haService.getDeskPosition(),
|
|
this.haService.getTemperatureText(),
|
|
this.tidalService.getSong()
|
|
]);
|
|
|
|
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(result);
|
|
} catch {
|
|
const error_message = "error getting all information";
|
|
logWarning(error_message);
|
|
return this.getErrorResult(error_message);
|
|
}
|
|
}
|
|
}
|