diff --git a/src/bluOs/player.ts b/src/bluOs/player.ts index bbb2f33..e4d3f69 100644 --- a/src/bluOs/player.ts +++ b/src/bluOs/player.ts @@ -16,7 +16,7 @@ import { xml2js } from "xml-js" import * as zod from "zod" import { Logger } from "pino" import { createEtagCache } from "./etagCache.js" -import { discoverPlayersObservable, Player } from "./serviceDiscovery.js" +import { Player } from "./serviceDiscovery.js" const xmlTextField = zod.object({ _text: zod.string(), @@ -155,26 +155,7 @@ const parseBluOsStatus = (bluOsXml: string): StatusQueryResponse => { } } -export const createDiscoveredPlayersStatusObservable = ( - logger: Logger, -): Observable => { - return discoverPlayersObservable().pipe( - tap((players: Player[]) => logger.debug({ players }, "Discovered players")), - mergeMap((players: Player[]) => - merge( - ...players.map((p) => - createBluOsStatusObservable({ - ...p, - logger: logger.child({ component: "bluOS" }), - }), - ), - ), - ), - share(), - ) -} - -export const createBluOsStatusObservable = ({ +const createBluOsStatusObservable = ({ ip, port, logger, @@ -210,3 +191,23 @@ export const createBluOsStatusObservable = ({ return bluOsStatus } + +export const createPlayersStatusObservable = ( + logger: Logger, + playersObservable: Observable, +): Observable => { + return playersObservable.pipe( + tap((players: Player[]) => logger.debug({ players }, "Discovered players")), + mergeMap((players: Player[]) => + merge( + ...players.map((p) => + createBluOsStatusObservable({ + ...p, + logger: logger.child({ component: "bluOS" }), + }), + ), + ), + ), + share(), + ) +} diff --git a/src/scrobbler.ts b/src/scrobbler.ts index ceacbca..051bbb1 100644 --- a/src/scrobbler.ts +++ b/src/scrobbler.ts @@ -1,10 +1,9 @@ -import { Observable, filter, map } from "rxjs" +import { Observable, filter, map, of } from "rxjs" import { Logger } from "pino" import { + createPlayersStatusObservable, PlayingTrack, StatusQueryResponse, - createBluOsStatusObservable, - createDiscoveredPlayersStatusObservable, } from "./bluOs/player.js" import { SubmitScrobbleResult, @@ -14,6 +13,7 @@ import { } from "./submitTrack.js" import { Configuration } from "./configuration.js" import { LastFmApi } from "./lastFm.js" +import { discoverPlayersObservable } from "./bluOs/serviceDiscovery.js" export interface ScrobblerDeps { config: Configuration @@ -33,12 +33,11 @@ export const createScrobbler = async ({ lastFm, sessionToken, }: ScrobblerDeps): Promise => { - const bluOsStatus: Observable = config.bluOs - ? createBluOsStatusObservable({ - ...config.bluOs, - logger: logger.child({ component: "bluOS" }), - }) - : createDiscoveredPlayersStatusObservable(logger) + const playersObservable = config.bluOs + ? of([{ ...config.bluOs }]) + : discoverPlayersObservable() + const bluOsStatus: Observable = + createPlayersStatusObservable(logger, playersObservable) const playingTrack = bluOsStatus.pipe( map((s) => s.playingTrack),