fix routes

This commit is contained in:
Darius
2025-11-18 22:25:29 +01:00
parent 99b9f7d40c
commit 55f4e90fea

View File

@@ -37,110 +37,105 @@ const haService = new HomeAssistantService(haClient);
const tidalClient = new TidalClient(server.axios.tidal); const tidalClient = new TidalClient(server.axios.tidal);
const tidalService = new TidalService(tidalClient); const tidalService = new TidalService(tidalClient);
server.register( // HOME ASSISTANT
async (fastify) => { server.get("/homeassistant/desk/position", async (_request, reply) => {
// HOME ASSISTANT const result = await haService.getDeskPosition();
fastify.get("/homeassistant/desk/position", async (_request, reply) => {
const result = await haService.getDeskPosition();
if (!result) { if (!result) {
reply.code(500); reply.code(500);
return { error: "Failed to get desk position" }; return { error: "Failed to get desk position" };
} }
return { return {
position: result.as_text(), position: result.as_text(),
is_standing: result.as_boolean, is_standing: result.as_boolean,
last_changed: result.last_changed.toReadable(true), last_changed: result.last_changed.toReadable(true),
}; };
}); });
fastify.post( server.post(
"/homeassistant/desk/stand", "/homeassistant/desk/stand",
{ preHandler: verifyAPIKey }, { preHandler: verifyAPIKey },
async (_request, reply) => { async (_request, reply) => {
const result = await haService.startStandingAutomation(); const result = await haService.startStandingAutomation();
if (!result) { if (!result) {
reply.code(500); reply.code(500);
return { error: "Failed to get desk position" }; return { error: "Failed to get desk position" };
} }
return { result }; return { result };
},
);
fastify.get("/homeassistant/temperature", async (_request, reply) => {
const result = await haService.getTemperatureText();
if (!result) {
reply.code(500);
return { error: "Failed to get desk position" };
}
return {
temperature: result,
};
});
// TIDAL
fastify.get("/tidal/song", async (_request, reply) => {
const result = await tidalService.getSong();
if (!result) {
reply.code(500);
return { error: "Failed to get song" };
}
return { result };
});
fastify.get("/tidal/songFormatted", async (_request, reply) => {
const result = await tidalService.getSongFormatted();
if (!result) {
reply.code(500);
return { error: "Failed to get song" };
}
return { result };
});
fastify.get("/tidal/volume", async (_request, reply) => {
const result = await tidalService.getVolume();
if (!result) {
reply.code(500);
return { error: "Failed to get volume" };
}
return { result };
});
fastify.post(
"/tidal/volume",
{ preHandler: verifyAPIKey },
async (request, reply) => {
const volume = request.body as string;
const result = await tidalService.setVolume(volume);
if (!result) {
reply.code(500);
return { error: "Failed to set volume" };
}
return { result };
},
);
// Default
fastify.get("/ping", async (_request, _reply) => {
return "pong\n";
});
}, },
{ prefix: "/api" },
); );
server.get("/homeassistant/temperature", async (_request, reply) => {
const result = await haService.getTemperatureText();
if (!result) {
reply.code(500);
return { error: "Failed to get desk position" };
}
return {
temperature: result,
};
});
// TIDAL
server.get("/tidal/song", async (_request, reply) => {
const result = await tidalService.getSong();
if (!result) {
reply.code(500);
return { error: "Failed to get song" };
}
return { result };
});
server.get("/tidal/songFormatted", async (_request, reply) => {
const result = await tidalService.getSongFormatted();
if (!result) {
reply.code(500);
return { error: "Failed to get song" };
}
return { result };
});
server.get("/tidal/volume", async (_request, reply) => {
const result = await tidalService.getVolume();
if (!result) {
reply.code(500);
return { error: "Failed to get volume" };
}
return { result };
});
server.post(
"/tidal/volume",
{ preHandler: verifyAPIKey },
async (request, reply) => {
const volume = request.body as string;
const result = await tidalService.setVolume(volume);
if (!result) {
reply.code(500);
return { error: "Failed to set volume" };
}
return { result };
},
);
// Default
server.get("/ping", async (_request, _reply) => {
return "pong\n";
});
server.listen( server.listen(
{ port: parseInt(Config.port, 10), host: "0.0.0.0" }, { port: parseInt(Config.port, 10), host: "0.0.0.0" },
(err, address) => { (err, address) => {