rewrite typeysation

This commit is contained in:
Darius
2025-11-21 16:49:12 +01:00
parent 27582e342f
commit e8459a7a7b
6 changed files with 131 additions and 514 deletions

View File

@@ -1,3 +1,4 @@
import type { HomeAssistantDeskPositionResult } from "@dpu/shared";
import { logInfo } from "@dpu/shared/dist/logger.js";
import type { FastifyReply, FastifyRequest } from "fastify";
import fastify from "fastify";
@@ -44,17 +45,20 @@ const tidalService = new TidalService(tidalClient);
// HOME ASSISTANT
server.get("/homeassistant/desk/position", async (_request, reply) => {
const result = await haService.getDeskPosition();
const service_result = await haService.getDeskPosition();
if (!result) {
reply.code(500);
return { error: "Failed to get desk position" };
if (!service_result.successful) {
reply.code(418);
return { error: service_result.result };
}
const position_result =
service_result.result as HomeAssistantDeskPositionResult;
return {
position: result.as_text(),
is_standing: result.as_boolean,
last_changed: result.last_changed.toReadable(true),
position: position_result.as_text(),
is_standing: position_result.as_boolean,
last_changed: position_result.last_changed.toReadable(true),
};
});
@@ -62,60 +66,60 @@ server.post(
"/homeassistant/desk/stand",
{ preHandler: verifyAPIKey },
async (_request, reply) => {
const result = await haService.startStandingAutomation();
const service_result = await haService.startStandingAutomation();
if (!result) {
reply.code(500);
return { error: "Failed to start stand automation" };
if (!service_result.successful) {
reply.code(418);
return { error: service_result.result };
}
return result;
return service_result.result;
},
);
server.get("/homeassistant/temperature", async (_request, reply) => {
const result = await haService.getTemperatureText();
const service_result = await haService.getTemperatureText();
if (!result) {
reply.code(500);
return { error: "Failed to get temperature" };
if (!service_result.successful) {
reply.code(418);
return { error: service_result.result };
}
return result;
return service_result.result;
});
// TIDAL
server.get("/tidal/song", async (_request, reply) => {
const result = await tidalService.getSong();
const service_result = await tidalService.getSong();
if (!result) {
reply.code(500);
return { error: "Failed to get song" };
if (!service_result.successful) {
reply.code(418);
return { error: service_result.result };
}
return result;
return service_result.result;
});
server.get("/tidal/songFormatted", async (_request, reply) => {
const result = await tidalService.getSongFormatted();
const service_result = await tidalService.getSongFormatted();
if (!result) {
reply.code(500);
return { error: "Failed to get song" };
if (!service_result.successful) {
reply.code(418);
return { error: service_result.result };
}
return result;
return service_result.result;
});
server.get("/tidal/volume", async (_request, reply) => {
const result = await tidalService.getVolume();
const service_result = await tidalService.getVolume();
if (!result) {
reply.code(500);
return { error: "Failed to get volume" };
if (!service_result.successful) {
reply.code(418);
return { error: service_result.result };
}
return result;
return service_result.result;
});
server.post(
@@ -123,14 +127,14 @@ server.post(
{ preHandler: verifyAPIKey },
async (request, reply) => {
const volume = request.body as string;
const result = await tidalService.setVolume(volume);
const service_result = await tidalService.setVolume(volume);
if (!result) {
reply.code(500);
return { error: "Failed to set volume" };
if (!service_result.successful) {
reply.code(418);
return { error: service_result.result };
}
return result;
return service_result.result;
},
);