mirror of
https://github.com/wkulhanek/bonob.git
synced 2025-12-22 09:53:32 +01:00
Add simple listing of sonos devices and basic Dockefile for running it
This commit is contained in:
48
src/sonos.ts
Normal file
48
src/sonos.ts
Normal file
@@ -0,0 +1,48 @@
|
||||
import { SonosManager } from "@svrooij/sonos";
|
||||
|
||||
import logger from "./logger";
|
||||
|
||||
type Device = {
|
||||
name: string;
|
||||
group: string;
|
||||
ip: string;
|
||||
port: number;
|
||||
};
|
||||
|
||||
interface Sonos
|
||||
{
|
||||
devices: () => Device[];
|
||||
}
|
||||
|
||||
class RealSonos implements Sonos {
|
||||
manager: SonosManager;
|
||||
|
||||
constructor(manager: SonosManager) {
|
||||
this.manager = manager;
|
||||
}
|
||||
|
||||
devices = (): Device[] => {
|
||||
const devices = this.manager.Devices.map((d) => ({
|
||||
name: d.Name,
|
||||
group: d.GroupName || "",
|
||||
ip: d.Host,
|
||||
port: d.Port,
|
||||
}));
|
||||
logger.debug({ devices })
|
||||
return devices;
|
||||
}
|
||||
}
|
||||
|
||||
const SonosDisabled: Sonos = {
|
||||
devices: () => [],
|
||||
};
|
||||
|
||||
export default function (): Promise<Sonos> {
|
||||
const manager = new SonosManager();
|
||||
return manager
|
||||
.InitializeWithDiscovery(10)
|
||||
.then((it) => (it ? new RealSonos(manager) : SonosDisabled))
|
||||
.catch((_) => {
|
||||
return SonosDisabled;
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user