|
| 1 | +import AbstractApiClient from "./AbstractApiClient.js"; |
| 2 | +import {JRiverData} from "../common/infrastructure/config/source/jriver.js"; |
| 3 | +import request, {Request, Response} from 'superagent'; |
| 4 | +import xml2js from 'xml2js'; |
| 5 | +import {ErrorWithCause} from "pony-cause"; |
| 6 | + |
| 7 | +const parser = new xml2js.Parser({'async': true}); |
| 8 | + |
| 9 | +export const PLAYER_STATE: Record<string, PLAYER_STATE> = { |
| 10 | + STOPPED: '0', |
| 11 | + PAUSED: '1', |
| 12 | + PLAYING: '2' |
| 13 | +} |
| 14 | + |
| 15 | +export type PLAYER_STATE = '0' | '1' | '2'; |
| 16 | + |
| 17 | +interface JRiverResponseItem { |
| 18 | + _: string |
| 19 | + $: { |
| 20 | + Name: string |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +interface JRiverResponse { |
| 25 | + Response: { |
| 26 | + '$': { |
| 27 | + Status: string |
| 28 | + }, |
| 29 | + Item: JRiverResponseItem[] |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +export interface JRiverTransformedResponse<T> { |
| 34 | + status: string |
| 35 | + data?: T |
| 36 | +} |
| 37 | + |
| 38 | +export interface Alive { |
| 39 | + RuntimeGUID: string |
| 40 | + LibraryVersion: string |
| 41 | + ProgramName: string |
| 42 | + ProgramVersion: string |
| 43 | + FriendlyName: string |
| 44 | + AccessKey: string |
| 45 | + ProductVersion: string |
| 46 | + Platform: string |
| 47 | +} |
| 48 | +// state 0 = nothing? |
| 49 | +// 2 = playing |
| 50 | +// 1 = paused |
| 51 | + |
| 52 | +export interface Authenticate { |
| 53 | + Token: string |
| 54 | + ReadOnly: number |
| 55 | + PreLicensed: boolean |
| 56 | +} |
| 57 | + |
| 58 | +export interface Info { |
| 59 | + ZoneID: string |
| 60 | + ZoneName: string |
| 61 | + State: PLAYER_STATE |
| 62 | + PositionMS: number |
| 63 | + DurationMS: number |
| 64 | + Artist: string |
| 65 | + Album: string |
| 66 | + Name: string |
| 67 | + Status: string |
| 68 | + FileKey: string |
| 69 | +} |
| 70 | + |
| 71 | +export interface Zones { |
| 72 | + NumberZones: number |
| 73 | + CurrentZoneID: string |
| 74 | + CurrentZoneIndex: string |
| 75 | +} |
| 76 | + |
| 77 | +const jriverResponseTransform = <T>(val: JRiverResponse): JRiverTransformedResponse<T> => { |
| 78 | + const status = val.Response.$.Status; |
| 79 | + const items = val.Response.Item === undefined ? undefined : val.Response.Item.map(x => { |
| 80 | + return [x.$.Name, x._]; |
| 81 | + }); |
| 82 | + return { |
| 83 | + status, |
| 84 | + data: items.reduce((acc, curr) => { |
| 85 | + acc[curr[0]] = curr[1]; |
| 86 | + return acc; |
| 87 | + }, {}) as T |
| 88 | + }; |
| 89 | +} |
| 90 | + |
| 91 | +export class JRiverApiClient extends AbstractApiClient { |
| 92 | + |
| 93 | + declare config: JRiverData |
| 94 | + |
| 95 | + url: string; |
| 96 | + |
| 97 | + token?: string; |
| 98 | + |
| 99 | + constructor(name: any, config: JRiverData, options = {}) { |
| 100 | + super('JRiver', name, config, options); |
| 101 | + const { |
| 102 | + url = 'http://localhost:52199/MCWS/v1/' |
| 103 | + } = config; |
| 104 | + this.url = url; |
| 105 | + } |
| 106 | + |
| 107 | + callApi = async <T>(req: Request, retries = 0): Promise<Response & {body: T}> => { |
| 108 | + const { |
| 109 | + maxRequestRetries = 2, |
| 110 | + retryMultiplier = 1.5 |
| 111 | + } = this.config; |
| 112 | + |
| 113 | + if (this.token !== undefined) { |
| 114 | + req.query({token: this.token}); |
| 115 | + } |
| 116 | + |
| 117 | + try { |
| 118 | + const resp = await req as Response; |
| 119 | + if (resp.text !== '') { |
| 120 | + const rawBody = await parser.parseStringPromise(resp.text); |
| 121 | + resp.body = <T>jriverResponseTransform(rawBody); |
| 122 | + } |
| 123 | + return resp; |
| 124 | + } catch (e) { |
| 125 | + throw e; |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + testConnection = async () => { |
| 130 | + try { |
| 131 | + const resp = await this.callApi<Alive>(request.get(`${this.url}Alive`)); |
| 132 | + const {body: { data } = {}} = resp; |
| 133 | + this.logger.verbose(`Found ${data.ProgramName} ${data.ProgramVersion} (${data.FriendlyName})`); |
| 134 | + return true; |
| 135 | + } catch (e) { |
| 136 | + this.logger.error(new ErrorWithCause('Could not communicate with JRiver server. Verify your server URL is correct.', {cause: e})); |
| 137 | + return false; |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + testAuth = async () => { |
| 142 | + try { |
| 143 | + let req = request.get(`${this.url}Authenticate`); |
| 144 | + if (this.config.username !== undefined) { |
| 145 | + req.auth(this.config.username, this.config.password); |
| 146 | + } |
| 147 | + const resp = await this.callApi<Authenticate>(req); |
| 148 | + this.token = resp.body.data.Token; |
| 149 | + return true; |
| 150 | + } catch (e) { |
| 151 | + let msg = 'Authentication failed.'; |
| 152 | + if(this.config.username === undefined || this.config.password === undefined) { |
| 153 | + msg = 'Authentication failed. No username/password was provided in config! Did you mean to do this?'; |
| 154 | + } |
| 155 | + this.logger.error(new ErrorWithCause(msg, {cause: e})); |
| 156 | + return false; |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + getInfo = async (zoneId: string = '-1') => { |
| 161 | + return await this.callApi<Info>(request.get(`${this.url}Playback/Info`).query({Zone: zoneId})); |
| 162 | + } |
| 163 | + |
| 164 | + getZones = async () => { |
| 165 | + return await this.callApi<Zones>(request.get(`${this.url}Playback/Zones`)); |
| 166 | + } |
| 167 | +} |
0 commit comments