diff --git a/README.md b/README.md index c1deeb9..4b96ce7 100644 --- a/README.md +++ b/README.md @@ -39,3 +39,5 @@ item | default value | description ---- | ------------- | ----------- 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_SERVICE_NAME | bonob | service name for sonos +BONOS_SONOS_SERVICE_ID | 246 | service id for sonos diff --git a/src/app.ts b/src/app.ts index 5280130..949fecd 100644 --- a/src/app.ts +++ b/src/app.ts @@ -1,12 +1,20 @@ -import sonos from "./sonos"; +import sonos, { bonobService } from "./sonos"; import server from "./server"; +import logger from "./logger" 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, () => { - console.info(`Listening on ${PORT}`); + logger.info(`Listening on ${PORT}`); }); export default app; diff --git a/src/server.ts b/src/server.ts index 3692ded..60678f5 100644 --- a/src/server.ts +++ b/src/server.ts @@ -1,8 +1,8 @@ import express, { Express } from "express"; 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(); app.use(express.static("./web/public")); @@ -12,14 +12,15 @@ function server(sonos: Sonos): Express { app.set("views", "./web/views"); app.get("/", (_, res) => { - sonos.devices().then(devices => { - const services = servicesFrom(devices) + sonos.devices().then((devices) => { + const services = servicesFrom(devices); res.render("index", { devices, services, - registration: registrationStatus(services) - }) - }) + bonob, + registration: registrationStatus(services, bonob), + }); + }); }); return app; diff --git a/src/sonos.ts b/src/sonos.ts index a987166..e4b484f 100644 --- a/src/sonos.ts +++ b/src/sonos.ts @@ -18,10 +18,10 @@ export type Service = { export type BonobRegistrationStatus = 'registered' | 'not-registered' -export const BONOB_SERVICE: Service = { - name: "bonob", - id: 245 -} +export const bonobService = (name: string, id: number): Service => ({ + name, + id +}) export interface Sonos { devices: () => Promise; @@ -41,8 +41,8 @@ export const servicesFrom = (devices: Device[]) => "name" ); -export const registrationStatus = (services: Service[]): BonobRegistrationStatus => { - if(services.find(s => s.id == BONOB_SERVICE.id) != undefined) { +export const registrationStatus = (services: Service[], bonob: Service): BonobRegistrationStatus => { + if(services.find(s => s.id == bonob.id) != undefined) { return "registered" } else { return "not-registered" diff --git a/tests/index.test.ts b/tests/index.test.ts index f43dfd6..335391f 100644 --- a/tests/index.test.ts +++ b/tests/index.test.ts @@ -1,10 +1,15 @@ import request from "supertest"; 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", () => { + const BONOB_FOR_TEST: Service = { + name: "test bonob", + id: 999 + } + describe("when sonos integration is disabled", () => { - const server = makeServer(SONOS_DISABLED); + const server = makeServer(SONOS_DISABLED, BONOB_FOR_TEST); describe("devices list", () => { it("should be empty", async () => { @@ -55,7 +60,7 @@ describe("index", () => { devices: () =>Promise.resolve([device1, device2]), }; - const server = makeServer(fakeSonos); + const server = makeServer(fakeSonos, BONOB_FOR_TEST); describe("devices list", () => { it("should contain the devices returned from sonos", async () => { @@ -89,7 +94,7 @@ describe("index", () => { const res = await request(server).get("/").send(); expect(res.status).toEqual(200); expect(res.text).toMatch( - /Not registered/ + /test bonob\s+\(999\) is not-registered/ ); }) }); @@ -110,7 +115,7 @@ describe("index", () => { name: "s2", id: 2, }, - BONOB_SERVICE + BONOB_FOR_TEST ], }; @@ -128,7 +133,7 @@ describe("index", () => { name: "s4", id: 4, }, - BONOB_SERVICE + BONOB_FOR_TEST ], } @@ -136,14 +141,14 @@ describe("index", () => { devices: () =>Promise.resolve([device1, device2]), }; - const server = makeServer(fakeSonos); + const server = makeServer(fakeSonos, BONOB_FOR_TEST); describe("registration status", () => { it("should be registered", async () => { const res = await request(server).get("/").send(); expect(res.status).toEqual(200); expect(res.text).toMatch( - /Registered/ + /test bonob\s+\(999\) is registered/ ); }) }); diff --git a/tests/sonos.test.ts b/tests/sonos.test.ts index 92da265..044f0cd 100644 --- a/tests/sonos.test.ts +++ b/tests/sonos.test.ts @@ -12,7 +12,6 @@ import sonos, { Device, servicesFrom, registrationStatus, - BONOB_SERVICE, } from "../src/sonos"; const mockSonosManagerConstructor = >SonosManager; @@ -25,21 +24,31 @@ describe("sonos", () => { describe("bonobRegistrationStatus", () => { describe("when bonob is registered", () => { it("should return 'registered'", () => { + const bonob = { + name: "some bonob", + id: 123, + }; expect( - registrationStatus([ - { id: 1, name: "not bonob" }, - BONOB_SERVICE, - { id: 2, name: "also not bonob" }, - ]) + registrationStatus( + [ + { id: 1, name: "not bonob" }, + bonob, + { id: 2, name: "also not bonob" }, + ], + bonob + ) ).toBe("registered"); }); }); describe("when bonob is not registered", () => { it("should return not-registered", () => { - expect(registrationStatus([{ id: 1, name: "not bonob" }])).toBe( - "not-registered" - ); + expect( + registrationStatus([{ id: 1, name: "not bonob" }], { + name: "bonob", + id: 999, + }) + ).toBe("not-registered"); }); }); }); diff --git a/web/views/index.eta b/web/views/index.eta index be8eba3..b8454e1 100644 --- a/web/views/index.eta +++ b/web/views/index.eta @@ -2,11 +2,7 @@

bonob

- <% if(it.registration == "registered") { %> -

Registered

- <% } else if(it.registration == "not-registered") { %> -

Not registered

- <% } %> +

<%= it.bonob.name %> (<%= it.bonob.id %>) is <%= it.registration %>

Devices

    <% it.devices.forEach(function(d){ %>