mirror of
https://github.com/wkulhanek/bonob.git
synced 2025-12-21 17:33:29 +01:00
ability to specify sonos service name and id
This commit is contained in:
@@ -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
|
||||
|
||||
14
src/app.ts
14
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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
12
src/sonos.ts
12
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<Device[]>;
|
||||
@@ -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"
|
||||
|
||||
@@ -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/
|
||||
);
|
||||
})
|
||||
});
|
||||
|
||||
@@ -12,7 +12,6 @@ import sonos, {
|
||||
Device,
|
||||
servicesFrom,
|
||||
registrationStatus,
|
||||
BONOB_SERVICE,
|
||||
} from "../src/sonos";
|
||||
|
||||
const mockSonosManagerConstructor = <jest.Mock<SonosManager>>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([
|
||||
registrationStatus(
|
||||
[
|
||||
{ id: 1, name: "not bonob" },
|
||||
BONOB_SERVICE,
|
||||
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");
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -2,11 +2,7 @@
|
||||
|
||||
<div id="content">
|
||||
<h1>bonob</h1>
|
||||
<% if(it.registration == "registered") { %>
|
||||
<h2 class="registered">Registered</h2>
|
||||
<% } else if(it.registration == "not-registered") { %>
|
||||
<h2 class="not-registered">Not registered</h2>
|
||||
<% } %>
|
||||
<h2><%= it.bonob.name %> (<%= it.bonob.id %>) is <%= it.registration %></h2>
|
||||
<h2>Devices</h2>
|
||||
<ul>
|
||||
<% it.devices.forEach(function(d){ %>
|
||||
|
||||
Reference in New Issue
Block a user