change for api route

This commit is contained in:
Darius
2025-11-18 22:08:00 +01:00
parent 22f8e28ed3
commit 4b31edd310
2 changed files with 105 additions and 100 deletions

View File

@@ -37,105 +37,110 @@ const haService = new HomeAssistantService(haClient);
const tidalClient = new TidalClient(server.axios.tidal);
const tidalService = new TidalService(tidalClient);
// HOME ASSISTANT
server.get("/homeassistant/desk/position", async (_request, reply) => {
const result = await haService.getDeskPosition();
server.register(
async (fastify) => {
// HOME ASSISTANT
fastify.get("/homeassistant/desk/position", async (_request, reply) => {
const result = await haService.getDeskPosition();
if (!result) {
reply.code(500);
return { error: "Failed to get desk position" };
}
if (!result) {
reply.code(500);
return { error: "Failed to get desk position" };
}
return {
position: result.as_text(),
is_standing: result.as_boolean,
last_changed: result.last_changed.toReadable(true),
};
});
return {
position: result.as_text(),
is_standing: result.as_boolean,
last_changed: result.last_changed.toReadable(true),
};
});
server.post(
"/homeassistant/desk/stand",
{ preHandler: verifyAPIKey },
async (_request, reply) => {
const result = await haService.startStandingAutomation();
fastify.post(
"/homeassistant/desk/stand",
{ preHandler: verifyAPIKey },
async (_request, reply) => {
const result = await haService.startStandingAutomation();
if (!result) {
reply.code(500);
return { error: "Failed to get desk position" };
}
if (!result) {
reply.code(500);
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({ port: 8080 }, (err, address) => {
if (err) {
console.error(err);

View File

@@ -1,48 +1,48 @@
### Simple GET Request
GET http://localhost:8080/ping
GET http://localhost:8080/api/ping
###
#################### HA #######################
### Simple GET Desk Position
GET http://localhost:8080/homeassistant/desk/position
GET http://localhost:8080/api/homeassistant/desk/position
###
### Simple GET Stand Automation
GET http://localhost:8080/homeassistant/desk/stand
GET http://localhost:8080/api/homeassistant/desk/stand
###
### Simple POST Stand Automation
POST http://localhost:8080/homeassistant/desk/stand
POST http://localhost:8080/api/homeassistant/desk/stand
###
### Simple GET Temps
GET http://localhost:8080/homeassistant/temperature
GET http://localhost:8080/api/homeassistant/temperature
###
#################### TIDAL #######################
### Simple GET Song
GET http://localhost:8080/tidal/song
GET http://localhost:8080/api/tidal/song
###
### Simple GET Song Formatted
GET http://localhost:8080/tidal/songFormatted
GET http://localhost:8080/api/tidal/songFormatted
###
### Simple GET Volume
GET http://localhost:8080/tidal/volume
GET http://localhost:8080/api/tidal/volume
###
### Simple SET Volume
POST http://localhost:8080/tidal/volume
POST http://localhost:8080/api/tidal/volume
###