Ability to auto-regiter with sonos on startup

This commit is contained in:
simojenki
2021-03-13 10:31:06 +11:00
parent 3373ae773b
commit 283b319f87
4 changed files with 21 additions and 8 deletions

View File

@@ -45,3 +45,4 @@ BONOB_SONOS_SEED_HOST | undefined | sonos device seed host for discovery, or omm
BONOB_SONOS_SERVICE_NAME | bonob | service name for sonos
BONOB_SONOS_SERVICE_ID | 246 | service id for sonos
BONOB_NAVIDROME_URL | http://localhost:4533 | URL for navidrome
BONOB_SONOS_AUTO_REGISTER | false | Whether or not to try and auto-register on startup

View File

@@ -18,8 +18,18 @@ const bonob = bonobService(
WEB_ADDRESS,
"AppLink"
);
const sonosSystem = sonos(SONOS_DEVICE_DISCOVERY, SONOS_SEED_HOST);
if(process.env["BONOB_SONOS_AUTO_REGISTER"] == "true") {
sonosSystem.register(bonob).then(success => {
if(success) {
logger.info(`Successfully registered ${bonob.name}(SID:${bonob.sid}) with sonos`)
}
})
}
const app = server(
sonos(SONOS_DEVICE_DISCOVERY, SONOS_SEED_HOST),
sonosSystem,
bonob,
WEB_ADDRESS,
new Navidrome(

View File

@@ -19,7 +19,7 @@ export const BONOB_ACCESS_TOKEN_HEADER = "bonob-access-token";
function server(
sonos: Sonos,
bonobService: Service,
service: Service,
webAddress: string | "http://localhost:4534",
musicService: MusicService,
linkCodes: LinkCodes = new InMemoryLinkCodes(),
@@ -40,12 +40,12 @@ function server(
Promise.all([sonos.devices(), sonos.services()]).then(
([devices, services]) => {
const registeredBonobService = services.find(
(it) => it.sid == bonobService.sid
(it) => it.sid == service.sid
);
res.render("index", {
devices,
services,
bonobService,
bonobService: service,
registeredBonobService,
});
}
@@ -53,7 +53,7 @@ function server(
});
app.post("/register", (_, res) => {
sonos.register(bonobService).then((success) => {
sonos.register(service).then((success) => {
if (success) {
res.render("success", {
message: `Successfully registered`,
@@ -68,7 +68,7 @@ function server(
app.get(LOGIN_ROUTE, (req, res) => {
res.render("login", {
bonobService,
bonobService: service,
linkCode: req.query.linkCode,
loginRoute: LOGIN_ROUTE,
});
@@ -102,10 +102,10 @@ function server(
res.type("application/xml").send(`<?xml version="1.0" encoding="utf-8" ?>
<stringtables xmlns="http://sonos.com/sonosapi">
<stringtable rev="1" xml:lang="en-US">
<string stringId="AppLinkMessage">Linking sonos with ${bonobService.name}</string>
<string stringId="AppLinkMessage">Linking sonos with ${service.name}</string>
</stringtable>
<stringtable rev="1" xml:lang="fr-FR">
<string stringId="AppLinkMessage">Lier les sonos à la ${bonobService.name}</string>
<string stringId="AppLinkMessage">Lier les sonos à la ${service.name}</string>
</stringtable>
</stringtables>
`);

View File

@@ -156,6 +156,8 @@ export function autoDiscoverySonos(sonosSeedHost?: string): Sonos {
return false;
}
logger.info(`Registering ${service.name}(SID:${service.sid}) with sonos device ${anyDevice.Name} @ ${anyDevice.Host}`)
const customd = `http://${anyDevice.Host}:${anyDevice.Port}/customsd`;
const csrfToken = await axios.get(customd).then((response) =>