ability to specify sonos service name and id

This commit is contained in:
simojenki
2021-01-31 10:08:31 +11:00
parent ad09a88de8
commit 1e8820b419
7 changed files with 59 additions and 38 deletions

View File

@@ -39,3 +39,5 @@ item | default value | description
---- | ------------- | ----------- ---- | ------------- | -----------
PORT | 4534 | Default http port for bonob to listen on PORT | 4534 | Default http port for bonob to listen on
BONOB_SONOS_SEED_HOST | undefined | sonos device seed host for auto-discovery, or 'disabled' to turn off device discovery entirely BONOB_SONOS_SEED_HOST | undefined | sonos device seed host for auto-discovery, or 'disabled' to turn off device discovery entirely
BONOB_SONOS_SERVICE_NAME | bonob | service name for sonos
BONOS_SONOS_SERVICE_ID | 246 | service id for sonos

View File

@@ -1,12 +1,20 @@
import sonos from "./sonos"; import sonos, { bonobService } from "./sonos";
import server from "./server"; import server from "./server";
import logger from "./logger"
const PORT = process.env["PORT"] || 4534; const PORT = process.env["PORT"] || 4534;
const app = server(sonos(process.env["BONOB_SONOS_SEED_HOST"])); const bonob = bonobService(
process.env["BONOB_SONOS_SERVICE_NAME"] || "bonob",
Number(process.env["BONOS_SONOS_SERVICE_ID"] || "246")
)
const app = server(
sonos(process.env["BONOB_SONOS_SEED_HOST"]),
bonob
);
app.listen(PORT, () => { app.listen(PORT, () => {
console.info(`Listening on ${PORT}`); logger.info(`Listening on ${PORT}`);
}); });
export default app; export default app;

View File

@@ -1,8 +1,8 @@
import express, { Express } from "express"; import express, { Express } from "express";
import * as Eta from "eta"; import * as Eta from "eta";
import { Sonos, servicesFrom, registrationStatus } from "./sonos"; import { Sonos, servicesFrom, registrationStatus, Service } from "./sonos";
function server(sonos: Sonos): Express { function server(sonos: Sonos, bonob: Service): Express {
const app = express(); const app = express();
app.use(express.static("./web/public")); app.use(express.static("./web/public"));
@@ -12,14 +12,15 @@ function server(sonos: Sonos): Express {
app.set("views", "./web/views"); app.set("views", "./web/views");
app.get("/", (_, res) => { app.get("/", (_, res) => {
sonos.devices().then(devices => { sonos.devices().then((devices) => {
const services = servicesFrom(devices) const services = servicesFrom(devices);
res.render("index", { res.render("index", {
devices, devices,
services, services,
registration: registrationStatus(services) bonob,
}) registration: registrationStatus(services, bonob),
}) });
});
}); });
return app; return app;

View File

@@ -18,10 +18,10 @@ export type Service = {
export type BonobRegistrationStatus = 'registered' | 'not-registered' export type BonobRegistrationStatus = 'registered' | 'not-registered'
export const BONOB_SERVICE: Service = { export const bonobService = (name: string, id: number): Service => ({
name: "bonob", name,
id: 245 id
} })
export interface Sonos { export interface Sonos {
devices: () => Promise<Device[]>; devices: () => Promise<Device[]>;
@@ -41,8 +41,8 @@ export const servicesFrom = (devices: Device[]) =>
"name" "name"
); );
export const registrationStatus = (services: Service[]): BonobRegistrationStatus => { export const registrationStatus = (services: Service[], bonob: Service): BonobRegistrationStatus => {
if(services.find(s => s.id == BONOB_SERVICE.id) != undefined) { if(services.find(s => s.id == bonob.id) != undefined) {
return "registered" return "registered"
} else { } else {
return "not-registered" return "not-registered"

View File

@@ -1,10 +1,15 @@
import request from "supertest"; import request from "supertest";
import makeServer from "../src/server"; import makeServer from "../src/server";
import { SONOS_DISABLED, Sonos, Device, BONOB_SERVICE } from "../src/sonos"; import { SONOS_DISABLED, Sonos, Device, Service } from "../src/sonos";
describe("index", () => { describe("index", () => {
const BONOB_FOR_TEST: Service = {
name: "test bonob",
id: 999
}
describe("when sonos integration is disabled", () => { describe("when sonos integration is disabled", () => {
const server = makeServer(SONOS_DISABLED); const server = makeServer(SONOS_DISABLED, BONOB_FOR_TEST);
describe("devices list", () => { describe("devices list", () => {
it("should be empty", async () => { it("should be empty", async () => {
@@ -55,7 +60,7 @@ describe("index", () => {
devices: () =>Promise.resolve([device1, device2]), devices: () =>Promise.resolve([device1, device2]),
}; };
const server = makeServer(fakeSonos); const server = makeServer(fakeSonos, BONOB_FOR_TEST);
describe("devices list", () => { describe("devices list", () => {
it("should contain the devices returned from sonos", async () => { it("should contain the devices returned from sonos", async () => {
@@ -89,7 +94,7 @@ describe("index", () => {
const res = await request(server).get("/").send(); const res = await request(server).get("/").send();
expect(res.status).toEqual(200); expect(res.status).toEqual(200);
expect(res.text).toMatch( expect(res.text).toMatch(
/Not registered/ /test bonob\s+\(999\) is not-registered/
); );
}) })
}); });
@@ -110,7 +115,7 @@ describe("index", () => {
name: "s2", name: "s2",
id: 2, id: 2,
}, },
BONOB_SERVICE BONOB_FOR_TEST
], ],
}; };
@@ -128,7 +133,7 @@ describe("index", () => {
name: "s4", name: "s4",
id: 4, id: 4,
}, },
BONOB_SERVICE BONOB_FOR_TEST
], ],
} }
@@ -136,14 +141,14 @@ describe("index", () => {
devices: () =>Promise.resolve([device1, device2]), devices: () =>Promise.resolve([device1, device2]),
}; };
const server = makeServer(fakeSonos); const server = makeServer(fakeSonos, BONOB_FOR_TEST);
describe("registration status", () => { describe("registration status", () => {
it("should be registered", async () => { it("should be registered", async () => {
const res = await request(server).get("/").send(); const res = await request(server).get("/").send();
expect(res.status).toEqual(200); expect(res.status).toEqual(200);
expect(res.text).toMatch( expect(res.text).toMatch(
/Registered/ /test bonob\s+\(999\) is registered/
); );
}) })
}); });

View File

@@ -12,7 +12,6 @@ import sonos, {
Device, Device,
servicesFrom, servicesFrom,
registrationStatus, registrationStatus,
BONOB_SERVICE,
} from "../src/sonos"; } from "../src/sonos";
const mockSonosManagerConstructor = <jest.Mock<SonosManager>>SonosManager; const mockSonosManagerConstructor = <jest.Mock<SonosManager>>SonosManager;
@@ -25,21 +24,31 @@ describe("sonos", () => {
describe("bonobRegistrationStatus", () => { describe("bonobRegistrationStatus", () => {
describe("when bonob is registered", () => { describe("when bonob is registered", () => {
it("should return 'registered'", () => { it("should return 'registered'", () => {
const bonob = {
name: "some bonob",
id: 123,
};
expect( expect(
registrationStatus([ registrationStatus(
{ id: 1, name: "not bonob" }, [
BONOB_SERVICE, { id: 1, name: "not bonob" },
{ id: 2, name: "also not bonob" }, bonob,
]) { id: 2, name: "also not bonob" },
],
bonob
)
).toBe("registered"); ).toBe("registered");
}); });
}); });
describe("when bonob is not registered", () => { describe("when bonob is not registered", () => {
it("should return not-registered", () => { it("should return not-registered", () => {
expect(registrationStatus([{ id: 1, name: "not bonob" }])).toBe( expect(
"not-registered" registrationStatus([{ id: 1, name: "not bonob" }], {
); name: "bonob",
id: 999,
})
).toBe("not-registered");
}); });
}); });
}); });

View File

@@ -2,11 +2,7 @@
<div id="content"> <div id="content">
<h1>bonob</h1> <h1>bonob</h1>
<% if(it.registration == "registered") { %> <h2><%= it.bonob.name %> (<%= it.bonob.id %>) is <%= it.registration %></h2>
<h2 class="registered">Registered</h2>
<% } else if(it.registration == "not-registered") { %>
<h2 class="not-registered">Not registered</h2>
<% } %>
<h2>Devices</h2> <h2>Devices</h2>
<ul> <ul>
<% it.devices.forEach(function(d){ %> <% it.devices.forEach(function(d){ %>