Compare commits
9 Commits
bd87929593
...
07719bbc1f
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
07719bbc1f | ||
|
|
7b6a056141 | ||
|
|
582c82d66f | ||
|
|
6e1c5e4e67 | ||
|
|
0043b75c95 | ||
|
|
4db61998bd | ||
|
|
7fd3e4b9d3 | ||
|
|
6b5b7a037d | ||
|
|
fd7a80f525 |
44
package-lock.json
generated
44
package-lock.json
generated
@@ -9,6 +9,7 @@
|
||||
"version": "1.0.0",
|
||||
"dependencies": {
|
||||
"@dpu/shared": "git+https://git.dariusbag.dev/DarDarBinks/dpu-shared.git",
|
||||
"@fastify/sse": "^0.4.0",
|
||||
"@fastify/swagger": "^9.6.1",
|
||||
"@fastify/swagger-ui": "^5.2.3",
|
||||
"dotenv": "^17.2.2",
|
||||
@@ -25,8 +26,8 @@
|
||||
}
|
||||
},
|
||||
"node_modules/@dpu/shared": {
|
||||
"version": "1.4.1",
|
||||
"resolved": "git+https://git.dariusbag.dev/DarDarBinks/dpu-shared.git#b55e1dd0a651c4cc3a7aabd8a44d49eb654f0189",
|
||||
"version": "1.6.4",
|
||||
"resolved": "git+https://git.dariusbag.dev/DarDarBinks/dpu-shared.git#60bcd23f5b77034777a792bbe8f33a62e92ba246",
|
||||
"dependencies": {
|
||||
"axios": "^1.7.9",
|
||||
"chalk": "^5.6.2",
|
||||
@@ -628,6 +629,21 @@
|
||||
"mime": "^3"
|
||||
}
|
||||
},
|
||||
"node_modules/@fastify/sse": {
|
||||
"version": "0.4.0",
|
||||
"resolved": "https://registry.npmjs.org/@fastify/sse/-/sse-0.4.0.tgz",
|
||||
"integrity": "sha512-bBV96iT2kHEw6h3i8IMkZGaqA7Gk81ugUzTNctXuE6N2BEC/qBnUuzlD/O17V43OkJP73h0/kf3Bp/asXlSuFA==",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"fastify-plugin": "^5.0.0"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=20"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"fastify": "^5.x"
|
||||
}
|
||||
},
|
||||
"node_modules/@fastify/static": {
|
||||
"version": "9.0.0",
|
||||
"resolved": "https://registry.npmjs.org/@fastify/static/-/static-9.0.0.tgz",
|
||||
@@ -736,9 +752,9 @@
|
||||
"license": "MIT"
|
||||
},
|
||||
"node_modules/@types/node": {
|
||||
"version": "24.10.10",
|
||||
"resolved": "https://registry.npmjs.org/@types/node/-/node-24.10.10.tgz",
|
||||
"integrity": "sha512-+0/4J266CBGPUq/ELg7QUHhN25WYjE0wYTPSQJn1xeu8DOlIOPxXxrNGiLmfAWl7HMMgWFWXpt9IDjMWrF5Iow==",
|
||||
"version": "24.10.11",
|
||||
"resolved": "https://registry.npmjs.org/@types/node/-/node-24.10.11.tgz",
|
||||
"integrity": "sha512-/Af7O8r1frCVgOz0I62jWUtMohJ0/ZQU/ZoketltOJPZpnb17yoNc9BSoVuV9qlaIXJiPNOpsfq4ByFajSArNQ==",
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
@@ -928,9 +944,9 @@
|
||||
}
|
||||
},
|
||||
"node_modules/dotenv": {
|
||||
"version": "17.2.3",
|
||||
"resolved": "https://registry.npmjs.org/dotenv/-/dotenv-17.2.3.tgz",
|
||||
"integrity": "sha512-JVUnt+DUIzu87TABbhPmNfVdBDt18BLOWjMUFJMSi/Qqg7NTYtabbvSNJGOJ7afbRuv9D/lngizHtP7QyLQ+9w==",
|
||||
"version": "17.2.4",
|
||||
"resolved": "https://registry.npmjs.org/dotenv/-/dotenv-17.2.4.tgz",
|
||||
"integrity": "sha512-mudtfb4zRB4bVvdj0xRo+e6duH1csJRM8IukBqfTRvHotn9+LBXB8ynAidP9zHqoRC/fsllXgk4kCKlR21fIhw==",
|
||||
"license": "BSD-2-Clause",
|
||||
"engines": {
|
||||
"node": ">=12"
|
||||
@@ -1309,9 +1325,9 @@
|
||||
}
|
||||
},
|
||||
"node_modules/get-tsconfig": {
|
||||
"version": "4.13.1",
|
||||
"resolved": "https://registry.npmjs.org/get-tsconfig/-/get-tsconfig-4.13.1.tgz",
|
||||
"integrity": "sha512-EoY1N2xCn44xU6750Sx7OjOIT59FkmstNc3X6y5xpz7D5cBtZRe/3pSlTkDJgqsOk3WwZPkWfonhhUJfttQo3w==",
|
||||
"version": "4.13.3",
|
||||
"resolved": "https://registry.npmjs.org/get-tsconfig/-/get-tsconfig-4.13.3.tgz",
|
||||
"integrity": "sha512-vp8Cj/+9Q/ibZUrq1rhy8mCTQpCk31A3uu9wc1C50yAb3x2pFHOsGdAZQ7jD86ARayyxZUViYeIztW+GE8dcrg==",
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
@@ -1804,9 +1820,9 @@
|
||||
"license": "BSD-3-Clause"
|
||||
},
|
||||
"node_modules/semver": {
|
||||
"version": "7.7.3",
|
||||
"resolved": "https://registry.npmjs.org/semver/-/semver-7.7.3.tgz",
|
||||
"integrity": "sha512-SdsKMrI9TdgjdweUSR9MweHA4EJ8YxHn8DFaDisvhVlUOe4BF1tLD7GAj0lIqWVl+dPb/rExr0Btby5loQm20Q==",
|
||||
"version": "7.7.4",
|
||||
"resolved": "https://registry.npmjs.org/semver/-/semver-7.7.4.tgz",
|
||||
"integrity": "sha512-vFKC2IEtQnVhpT78h1Yp8wzwrf8CM+MzKMHGJZfBtzhZNycRFnXsHk6E5TxIkkMsgNS7mdX3AGB7x2QM2di4lA==",
|
||||
"license": "ISC",
|
||||
"bin": {
|
||||
"semver": "bin/semver.js"
|
||||
|
||||
@@ -14,6 +14,7 @@
|
||||
"type": "module",
|
||||
"dependencies": {
|
||||
"@dpu/shared": "git+https://git.dariusbag.dev/DarDarBinks/dpu-shared.git",
|
||||
"@fastify/sse": "^0.4.0",
|
||||
"@fastify/swagger": "^9.6.1",
|
||||
"@fastify/swagger-ui": "^5.2.3",
|
||||
"dotenv": "^17.2.2",
|
||||
|
||||
@@ -27,6 +27,7 @@ export async function homeAssistantRoutes(
|
||||
position: z.string(),
|
||||
is_standing: z.boolean(),
|
||||
last_changed: z.string(),
|
||||
last_changed_seconds: z.number(),
|
||||
}),
|
||||
418: z.object({
|
||||
error: z.string(),
|
||||
@@ -42,14 +43,9 @@ export async function homeAssistantRoutes(
|
||||
return { error: service_result.result };
|
||||
}
|
||||
|
||||
const position_result =
|
||||
service_result.result as HomeAssistantDeskPositionResult;
|
||||
|
||||
return {
|
||||
position: position_result.as_text(),
|
||||
is_standing: position_result.as_boolean,
|
||||
last_changed: position_result.last_changed.toReadable(true),
|
||||
};
|
||||
return haService.convertPosResultToApiAnswer(
|
||||
service_result.result as HomeAssistantDeskPositionResult,
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import {
|
||||
type API_HA_DeskPosition,
|
||||
BaseService,
|
||||
type HomeAssistantDeskPositionResult,
|
||||
type HomeAssistantEntity,
|
||||
@@ -74,6 +75,17 @@ export class HomeAssistantService extends BaseService<HomeAssistantClient> {
|
||||
}
|
||||
}
|
||||
|
||||
convertPosResultToApiAnswer(
|
||||
position: HomeAssistantDeskPositionResult,
|
||||
): API_HA_DeskPosition {
|
||||
return {
|
||||
position: position.as_text(),
|
||||
is_standing: position.as_boolean,
|
||||
last_changed: position.last_changed.toReadable(true),
|
||||
last_changed_seconds: position.last_changed.seconds,
|
||||
};
|
||||
}
|
||||
|
||||
async getTemperatureText(): Promise<ServiceResult<string>> {
|
||||
try {
|
||||
const entities = await this.getTemperatures();
|
||||
|
||||
43
src/homepage/routes.ts
Normal file
43
src/homepage/routes.ts
Normal file
@@ -0,0 +1,43 @@
|
||||
import type { FastifyInstance } from "fastify";
|
||||
import { z } from "zod";
|
||||
import { type HomepageService } from "./service.js";
|
||||
import { type API_HA_DeskPosition, type TidalGetCurrent } from "@dpu/shared";
|
||||
|
||||
export async function homepageRoutes(
|
||||
fastify: FastifyInstance,
|
||||
{
|
||||
hpService,
|
||||
}: {
|
||||
hpService: HomepageService;
|
||||
},
|
||||
) {
|
||||
fastify.get(
|
||||
"/homepage/status",
|
||||
{
|
||||
schema: {
|
||||
description: "Get all current information for dpu status page",
|
||||
tags: ["homepage"],
|
||||
response: {
|
||||
200: z.object({
|
||||
ha_desk_position: z.custom<API_HA_DeskPosition>().nullable(),
|
||||
ha_temp: z.string().nullable(),
|
||||
tidal_current: z.custom<TidalGetCurrent>().nullable(),
|
||||
}),
|
||||
418: z.object({
|
||||
error: z.string(),
|
||||
}),
|
||||
},
|
||||
},
|
||||
},
|
||||
async (_request, reply) => {
|
||||
const service_result = await hpService.getFullInformation();
|
||||
|
||||
if (!service_result.successful) {
|
||||
reply.code(418);
|
||||
return { error: service_result.result };
|
||||
}
|
||||
|
||||
return service_result.result;
|
||||
},
|
||||
);
|
||||
}
|
||||
151
src/homepage/service.ts
Normal file
151
src/homepage/service.ts
Normal file
@@ -0,0 +1,151 @@
|
||||
import {
|
||||
BaseService,
|
||||
FullInformation,
|
||||
HomeAssistantDeskPositionResult,
|
||||
SseClientChangeEvent,
|
||||
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;
|
||||
private pollingInterval: ReturnType<typeof setInterval> | null = null;
|
||||
private songEndTimeout: ReturnType<typeof setTimeout> | null = null;
|
||||
private lastPoll: FullInformation | null = null;
|
||||
|
||||
constructor(
|
||||
haService: HomeAssistantService,
|
||||
tidalService: TidalService,
|
||||
sseService: SseService,
|
||||
) {
|
||||
super(null);
|
||||
this.haService = haService;
|
||||
this.tidalService = tidalService;
|
||||
this.sseService = sseService;
|
||||
this.listenForClientChange();
|
||||
}
|
||||
|
||||
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(this.updateLastPoll(result));
|
||||
} catch {
|
||||
const error_message = "error getting all information";
|
||||
logWarning(error_message);
|
||||
return this.getErrorResult(error_message);
|
||||
}
|
||||
}
|
||||
|
||||
updateLastPoll(newPoll: FullInformation) {
|
||||
const updates = [];
|
||||
if (
|
||||
this.lastPoll?.ha_desk_position?.is_standing !==
|
||||
newPoll.ha_desk_position?.is_standing
|
||||
) {
|
||||
updates.push({
|
||||
component: "ha_desk_position",
|
||||
data: newPoll.ha_desk_position,
|
||||
});
|
||||
}
|
||||
|
||||
if (this.lastPoll?.ha_temp !== newPoll.ha_temp) {
|
||||
updates.push({
|
||||
component: "ha_temp",
|
||||
data: newPoll.ha_temp,
|
||||
});
|
||||
}
|
||||
|
||||
if (
|
||||
this.lastPoll?.tidal_current?.title !== newPoll.tidal_current?.title ||
|
||||
this.lastPoll?.tidal_current?.status !== newPoll.tidal_current?.status
|
||||
) {
|
||||
updates.push({
|
||||
component: "tidal_current",
|
||||
data: newPoll.tidal_current,
|
||||
});
|
||||
}
|
||||
|
||||
if (updates.length > 0) {
|
||||
this.sseService.notifyClients({
|
||||
type: "update",
|
||||
data: updates,
|
||||
});
|
||||
}
|
||||
|
||||
this.lastPoll = newPoll;
|
||||
this.scheduleSongEndPoll(newPoll.tidal_current);
|
||||
return newPoll;
|
||||
}
|
||||
|
||||
private scheduleSongEndPoll(tidal: TidalGetCurrent | null): void {
|
||||
this.clearSongEndPoll();
|
||||
|
||||
if (!tidal || tidal.status === "paused") {
|
||||
return;
|
||||
}
|
||||
|
||||
const remainingSeconds = tidal.durationInSeconds - tidal.currentInSeconds;
|
||||
if (remainingSeconds > 0) {
|
||||
this.songEndTimeout = setTimeout(
|
||||
() => {
|
||||
this.songEndTimeout = null;
|
||||
this.getFullInformation();
|
||||
},
|
||||
(remainingSeconds + 1) * 1000,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
private clearSongEndPoll(): void {
|
||||
if (this.songEndTimeout) {
|
||||
clearTimeout(this.songEndTimeout);
|
||||
this.songEndTimeout = null;
|
||||
}
|
||||
}
|
||||
|
||||
listenForClientChange(): void {
|
||||
this.sseService.onClientChange((clientChange: SseClientChangeEvent) => {
|
||||
if (clientChange.clientCount === 0) {
|
||||
this.stopPolling();
|
||||
} else {
|
||||
if (!this.pollingInterval) {
|
||||
this.startPolling();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
startPolling(): void {
|
||||
this.pollingInterval = setInterval(() => {
|
||||
this.getFullInformation();
|
||||
}, 30000);
|
||||
}
|
||||
|
||||
stopPolling(): void {
|
||||
if (this.pollingInterval) {
|
||||
clearInterval(this.pollingInterval);
|
||||
}
|
||||
this.clearSongEndPoll();
|
||||
}
|
||||
}
|
||||
15
src/index.ts
15
src/index.ts
@@ -17,8 +17,13 @@ import { HomeAssistantClient } from "./homeassistant/client.js";
|
||||
import { homeAssistantRoutes } from "./homeassistant/routes.js";
|
||||
import { HomeAssistantService } from "./homeassistant/service.js";
|
||||
import { TidalClient } from "./tidal/client.js";
|
||||
import { tidalRoutes } from "./tidal/routes.js";
|
||||
import { TidalService } from "./tidal/service.js";
|
||||
import { tidalRoutes } from "./tidal/routes.js";
|
||||
import { sseRoutes } from "./sse/routes.js";
|
||||
import { SseService } from "@dpu/shared";
|
||||
import { HomepageService } from "./homepage/service.js";
|
||||
import { homepageRoutes } from "./homepage/routes.js";
|
||||
import fastifySSE from "@fastify/sse";
|
||||
|
||||
const fastify = Fastify().withTypeProvider<ZodTypeProvider>();
|
||||
|
||||
@@ -69,12 +74,18 @@ await fastify.register(fastifyAxios, {
|
||||
},
|
||||
});
|
||||
|
||||
await fastify.register(fastifySSE);
|
||||
|
||||
const haClient = new HomeAssistantClient(fastify.axios.homeassistant);
|
||||
const haService = new HomeAssistantService(haClient);
|
||||
|
||||
const tidalClient = new TidalClient(fastify.axios.tidal);
|
||||
const tidalService = new TidalService(tidalClient);
|
||||
|
||||
const sseService = new SseService();
|
||||
|
||||
const hpService = new HomepageService(haService, tidalService, sseService);
|
||||
|
||||
async function verifyAPIKey(
|
||||
request: FastifyRequest,
|
||||
reply: FastifyReply,
|
||||
@@ -92,6 +103,8 @@ const port = parseInt(Config.port, 10);
|
||||
// Register routes
|
||||
await fastify.register(homeAssistantRoutes, { haService, verifyAPIKey });
|
||||
await fastify.register(tidalRoutes, { tidalService, verifyAPIKey });
|
||||
await fastify.register(sseRoutes, { sseService });
|
||||
await fastify.register(homepageRoutes, { hpService });
|
||||
|
||||
fastify.get(
|
||||
"/ping",
|
||||
|
||||
44
src/sse/routes.ts
Normal file
44
src/sse/routes.ts
Normal file
@@ -0,0 +1,44 @@
|
||||
import type { FastifyInstance } from "fastify";
|
||||
import { logInfo, type SseEvent, type SseService } from "@dpu/shared";
|
||||
import { randomUUID } from "node:crypto";
|
||||
|
||||
export async function sseRoutes(
|
||||
fastify: FastifyInstance,
|
||||
{
|
||||
sseService,
|
||||
}: {
|
||||
sseService: SseService;
|
||||
},
|
||||
) {
|
||||
fastify.get(
|
||||
"/dpu/events",
|
||||
{
|
||||
schema: {
|
||||
description: "Register for SSE",
|
||||
tags: ["sse"],
|
||||
hide: true,
|
||||
},
|
||||
sse: true,
|
||||
},
|
||||
async (_request, reply) => {
|
||||
reply.sse.keepAlive();
|
||||
|
||||
const clientId = randomUUID();
|
||||
const sendEvent = (data: SseEvent) => {
|
||||
reply.sse.send({
|
||||
data: JSON.stringify(data),
|
||||
});
|
||||
};
|
||||
|
||||
sseService.addClient({ id: clientId, send: sendEvent });
|
||||
|
||||
await reply.sse.send({ data: "Connected" });
|
||||
logInfo(`Connection for client ${clientId} established`);
|
||||
|
||||
reply.sse.onClose(() => {
|
||||
sseService.removeClient(clientId);
|
||||
logInfo(`Connection for client ${clientId} closed`);
|
||||
});
|
||||
},
|
||||
);
|
||||
}
|
||||
@@ -11,15 +11,17 @@ export class TidalService extends BaseService<TidalClient> {
|
||||
const req = await this.getSong();
|
||||
if (req.successful) {
|
||||
const song = req.result as TidalGetCurrent;
|
||||
const status = song.status === "playing" ? "▶️" : "⏸️";
|
||||
return this.getSuccessfulResult(
|
||||
`listening to ${song.title} by ${song.artists}. ${status} ${song.current}/${song.duration}. link: ${song.url}`,
|
||||
);
|
||||
return this.getSuccessfulResult(this.formatSong(song));
|
||||
} else {
|
||||
return this.getErrorResult(req.result as string);
|
||||
}
|
||||
}
|
||||
|
||||
formatSong(song: TidalGetCurrent): string {
|
||||
const status = song.status === "playing" ? "▶️" : "⏸️";
|
||||
return `listening to ${song.title} by ${song.artists}. ${status} ${song.current}/${song.duration}. link: ${song.url}`;
|
||||
}
|
||||
|
||||
async getSong(): Promise<ServiceResult<TidalGetCurrent | string>> {
|
||||
try {
|
||||
const response = await this.getClient().get<TidalGetCurrent>("current");
|
||||
|
||||
Reference in New Issue
Block a user