UNPKG

@petro-kushchak/homebridge-homepod-radio

Version:

Homebridge accessory for streaming radio to Homepod Mini and Apple TV

123 lines 4.31 kB
import { AirPlayDevice } from './lib/airplayDevice.js'; import * as os from 'os'; import * as path from 'path'; import * as fs from 'fs'; // eslint-disable-next-line @typescript-eslint/no-unused-vars const fileExists = async (path) => !!(await fs.promises.stat(path).catch((e) => false)); export var WebActionType; (function (WebActionType) { WebActionType[WebActionType["PlayFile"] = 0] = "PlayFile"; WebActionType[WebActionType["Unsupported"] = 1] = "Unsupported"; })(WebActionType || (WebActionType = {})); export class HomepodRadioPlatformWebActions { config; playbackController; logger; device; constructor(config, playbackController, logger) { this.config = config; this.playbackController = playbackController; this.logger = logger; this.playbackController.addStreamer(this); this.device = new AirPlayDevice(this.config.homepodId, this.logger, this.config.verboseMode, this.streamerName()); } // eslint-disable-next-line @typescript-eslint/no-unused-vars async volumeUpdated(homepodId, volume) { return await Promise.resolve(); } isPlaying() { return false; } async startPlaying() { return Promise.resolve(); } async stopPlaying() { return Promise.resolve(); } // eslint-disable-next-line @typescript-eslint/no-unused-vars async stopRequested(source) { return Promise.resolve(); } async shutdownRequested() { return Promise.resolve(); } async platformLaunched() { return Promise.resolve(); } streamerName() { return 'PlatformWebActions'; } parseAction(actionUri) { const parts = actionUri.split('/'); if (parts.length < 2) { return { action: WebActionType.Unsupported, data: 'Unsupported request', }; } if (parts[1] === 'play') { // play mp3/wav from mediaPath by name // uri example: /play/<mp3/wav-file> // uri example: /play/<mp3/wav-file>/25 const fileName = parts[2]; const mediaPath = this.config.mediaPath || os.homedir(); const filePath = path.join(mediaPath, fileName); let volume = 0; if (parts.length > 3) { if (isNaN(Number(parts[3]))) { return { action: WebActionType.Unsupported, data: 'Unsupported request', }; } else { volume = Number(parts[3]); volume = Math.max(volume, 0); volume = Math.min(volume, 100); } } return { action: WebActionType.PlayFile, data: filePath, extra: volume, }; } return { action: WebActionType.Unsupported, data: 'Unsupported request', }; } async handleAction(actionUrl) { this.logger.info('Received request: %s', actionUrl); const webAction = this.parseAction(actionUrl); switch (webAction.action) { case WebActionType.PlayFile: { const filePath = webAction.data; const fileFound = await fileExists(filePath); if (!fileFound) { return { error: false, message: `File does not exist: ${filePath}`, }; } const volume = (webAction.extra) ? webAction.extra : 0; const message = `Started playing file: ${filePath}`; this.logger.info(message); await this.playbackController.requestStop(this); await this.device.playFile(filePath, volume); return { error: false, message: message, }; } // falls through case WebActionType.Unsupported: default: return { error: true, message: webAction.data, }; } } } //# sourceMappingURL=platformWebActions.js.map