rework tidal api to be nicer now
This commit is contained in:
@@ -81,9 +81,7 @@ export async function tidalRoutes(
|
||||
description: "Get current volume level",
|
||||
tags: ["tidal"],
|
||||
response: {
|
||||
200: z.object({
|
||||
volume: z.number(),
|
||||
}),
|
||||
200: z.number(),
|
||||
418: z.object({
|
||||
error: z.string(),
|
||||
}),
|
||||
@@ -110,13 +108,9 @@ export async function tidalRoutes(
|
||||
description:
|
||||
"Set volume level (accepts absolute number or relative +/- value)",
|
||||
tags: ["tidal"],
|
||||
body: z.object({
|
||||
volume: z.string(),
|
||||
}),
|
||||
body: z.string(),
|
||||
response: {
|
||||
200: z.object({
|
||||
volume: z.number(),
|
||||
}),
|
||||
200: z.number(),
|
||||
401: z.object({
|
||||
error: z.literal("Invalid API key"),
|
||||
}),
|
||||
|
||||
@@ -2,7 +2,6 @@ import {
|
||||
BaseService,
|
||||
type ServiceResult,
|
||||
type TidalGetCurrent,
|
||||
type TidalPutVolume,
|
||||
} from "@dpu/shared";
|
||||
import { logWarning } from "@dpu/shared/dist/logger.js";
|
||||
import type { TidalClient } from "./client.js";
|
||||
@@ -33,13 +32,13 @@ export class TidalService extends BaseService<TidalClient> {
|
||||
}
|
||||
}
|
||||
|
||||
async getVolume(): Promise<ServiceResult<TidalPutVolume | string>> {
|
||||
async getVolume(): Promise<ServiceResult<number | string>> {
|
||||
try {
|
||||
const response = await this.getClient().get<TidalGetCurrent>("current");
|
||||
|
||||
return this.getSuccessfulResult({
|
||||
volume: response.volume,
|
||||
});
|
||||
return this.getSuccessfulResult(
|
||||
this.decimalToPercentage(response.volume),
|
||||
);
|
||||
} catch {
|
||||
const error_message = "error getting volume from tidal";
|
||||
logWarning(error_message);
|
||||
@@ -48,20 +47,18 @@ export class TidalService extends BaseService<TidalClient> {
|
||||
}
|
||||
|
||||
clamp(value: number): number {
|
||||
return Math.min(Math.max(value, 0), 100);
|
||||
return Math.min(Math.max(value, 0.0), 1.0);
|
||||
}
|
||||
|
||||
async setVolume(
|
||||
argument: string,
|
||||
): Promise<ServiceResult<TidalPutVolume | string>> {
|
||||
const value = this.percentageToDecimal(argument);
|
||||
async setVolume(argument: string): Promise<ServiceResult<number | string>> {
|
||||
const value = this.percentageToDecimal(parseFloat(argument));
|
||||
// relative
|
||||
const adjustMatch = argument.match(/^([+-])/);
|
||||
if (adjustMatch) {
|
||||
const req = await this.getVolume();
|
||||
if (req.successful) {
|
||||
const volume = req.result as TidalPutVolume;
|
||||
const wantedVolume = volume.volume + value;
|
||||
const volume = req.result as number;
|
||||
const wantedVolume = volume + value;
|
||||
const clampWantedVolume = this.clamp(wantedVolume);
|
||||
return await this.setVolumeToTidal(clampWantedVolume);
|
||||
}
|
||||
@@ -81,16 +78,11 @@ export class TidalService extends BaseService<TidalClient> {
|
||||
|
||||
async setVolumeToTidal(
|
||||
volume: number,
|
||||
): Promise<ServiceResult<TidalPutVolume | string>> {
|
||||
): Promise<ServiceResult<number | string>> {
|
||||
try {
|
||||
const response = await this.getClient().put<TidalPutVolume>(
|
||||
`player/volume?volume=${volume}`,
|
||||
{},
|
||||
);
|
||||
await this.getClient().put<number>(`player/volume?volume=${volume}`, {});
|
||||
|
||||
return this.getSuccessfulResult(
|
||||
this.decimalToPercentage(response.volume).toString(),
|
||||
);
|
||||
return this.getSuccessfulResult(this.decimalToPercentage(volume));
|
||||
} catch {
|
||||
const error_message = "error setting volume from tidal";
|
||||
logWarning(error_message);
|
||||
@@ -102,8 +94,7 @@ export class TidalService extends BaseService<TidalClient> {
|
||||
return decimal * 100;
|
||||
}
|
||||
|
||||
percentageToDecimal(percentage: string): number {
|
||||
const num = parseFloat(percentage);
|
||||
return num / 100;
|
||||
percentageToDecimal(percentage: number): number {
|
||||
return percentage / 100;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user