Adding BONOB_SONOS_DEVICE_DISCOVERY env var for enabling/disabling auto-discovery

This commit is contained in:
simojenki
2021-03-11 21:36:13 +11:00
parent 9f484d5c01
commit 9e16dbd96e
4 changed files with 37 additions and 31 deletions

View File

@@ -8,6 +8,10 @@ const PORT = +(process.env["BONOB_PORT"] || 4534);
const WEB_ADDRESS =
process.env["BONOB_WEB_ADDRESS"] || `http://localhost:${PORT}`;
const SONOS_DEVICE_DISCOVERY =
(process.env["BONOB_SONOS_DEVICE_DISCOVERY"] || "true") == "true";
const SONOS_SEED_HOST = process.env["BONOB_SONOS_SEED_HOST"];
const bonob = bonobService(
process.env["BONOB_SONOS_SERVICE_NAME"] || "bonob",
Number(process.env["BONOS_SONOS_SERVICE_ID"] || "246"),
@@ -15,10 +19,13 @@ const bonob = bonobService(
"AppLink"
);
const app = server(
sonos(process.env["BONOB_SONOS_SEED_HOST"]),
sonos(SONOS_DEVICE_DISCOVERY, SONOS_SEED_HOST),
bonob,
WEB_ADDRESS,
new Navidrome(process.env["BONOB_NAVIDROME_URL"] || "http://localhost:4533", encryption(process.env["BONOB_SECRET"] || "bonob"))
new Navidrome(
process.env["BONOB_NAVIDROME_URL"] || "http://localhost:4533",
encryption(process.env["BONOB_SECRET"] || "bonob")
)
);
app.listen(PORT, () => {

View File

@@ -4,8 +4,8 @@ import { parse } from "node-html-parser";
import { MusicService } from "@svrooij/sonos/lib/services";
import { head } from "underscore";
import logger from "./logger";
import STRINGS from './strings';
import { SOAP_PATH, STRINGS_ROUTE, PRESENTATION_MAP_ROUTE } from './smapi';
import STRINGS from "./strings";
import { SOAP_PATH, STRINGS_ROUTE, PRESENTATION_MAP_ROUTE } from "./smapi";
export type Device = {
name: string;
@@ -22,7 +22,7 @@ export type Service = {
strings: { uri?: string; version?: string };
presentation: { uri?: string; version?: string };
pollInterval?: number;
authType: 'Anonymous' | 'AppLink' | 'DeviceLink' | 'UserId';
authType: "Anonymous" | "AppLink" | "DeviceLink" | "UserId";
};
const stripTailingSlash = (url: string) =>
@@ -32,7 +32,7 @@ export const bonobService = (
name: string,
sid: number,
bonobRoot: string,
authType: 'Anonymous' | 'AppLink' | 'DeviceLink' | 'UserId' = 'AppLink'
authType: "Anonymous" | "AppLink" | "DeviceLink" | "UserId" = "AppLink"
): Service => ({
name,
sid,
@@ -150,9 +150,9 @@ export function autoDiscoverySonos(sonosSeedHost?: string): Sonos {
const anyDevice = await sonosDevices().then((devices) => head(devices));
if (!anyDevice) {
logger.warn("Failed to find a device to register with...")
return false
};
logger.warn("Failed to find a device to register with...");
return false;
}
const customd = `http://${anyDevice.Host}:${anyDevice.Port}/customsd`;
@@ -164,9 +164,11 @@ export function autoDiscoverySonos(sonosSeedHost?: string): Sonos {
);
if (!csrfToken) {
logger.warn(`Failed to find csrfToken at GET -> ${customd}, cannot register service`)
return false
};
logger.warn(
`Failed to find csrfToken at GET -> ${customd}, cannot register service`
);
return false;
}
return axios
.post(customd, new URLSearchParams(asCustomdForm(csrfToken, service)), {
@@ -179,14 +181,10 @@ export function autoDiscoverySonos(sonosSeedHost?: string): Sonos {
};
}
export default function sonos(
const sonos = (
discoveryEnabled: boolean = true,
sonosSeedHost: string | undefined = undefined
): Sonos {
switch (sonosSeedHost) {
case "disabled":
logger.info("Sonos device discovery disabled");
return SONOS_DISABLED;
default:
return autoDiscoverySonos(sonosSeedHost);
}
}
): Sonos =>
discoveryEnabled ? autoDiscoverySonos(sonosSeedHost) : SONOS_DISABLED;
export default sonos;