|
| 1 | +import { NodeCG } from "nodecg-types/types/server"; |
| 2 | +import { Result, emptySuccess, success, ServiceBundle, Logger } from "nodecg-io-core"; |
| 3 | +import { GoogleCastClient } from "./castClient"; |
| 4 | +import ChromecastAPI from "chromecast-api"; |
| 5 | + |
| 6 | +export interface GoogleCastConfig { |
| 7 | + name?: string; |
| 8 | + friendlyName?: string; |
| 9 | + host: string; |
| 10 | +} |
| 11 | + |
| 12 | +export { GoogleCastClient } from "./castClient"; |
| 13 | + |
| 14 | +module.exports = (nodecg: NodeCG) => { |
| 15 | + new GoogleCastService(nodecg).register(); |
| 16 | +}; |
| 17 | + |
| 18 | +class GoogleCastService extends ServiceBundle<GoogleCastConfig, GoogleCastClient> { |
| 19 | + private autoDiscoveryClient = new ChromecastAPI(); |
| 20 | + |
| 21 | + constructor(nodecg: NodeCG) { |
| 22 | + super(nodecg, "google-cast", __dirname, "../schema.json"); |
| 23 | + |
| 24 | + this.autoDiscoveryClient.on("device", (device) => { |
| 25 | + if (this.presets === undefined) this.presets = {}; |
| 26 | + |
| 27 | + this.presets[device.friendlyName] = { |
| 28 | + name: device.name, |
| 29 | + friendlyName: device.friendlyName, |
| 30 | + host: device.host, |
| 31 | + }; |
| 32 | + }); |
| 33 | + } |
| 34 | + |
| 35 | + async validateConfig(_: GoogleCastConfig): Promise<Result<void>> { |
| 36 | + return emptySuccess(); |
| 37 | + } |
| 38 | + |
| 39 | + async createClient(config: GoogleCastConfig, logger: Logger): Promise<Result<GoogleCastClient>> { |
| 40 | + const client = await GoogleCastClient.createClient(config); |
| 41 | + logger.info("Successfully created google-cast client."); |
| 42 | + return success(client); |
| 43 | + } |
| 44 | + |
| 45 | + stopClient(client: GoogleCastClient, logger: Logger): void { |
| 46 | + client.close(); |
| 47 | + logger.info("Successfully stopped google-cast client."); |
| 48 | + } |
| 49 | + |
| 50 | + removeHandlers(client: GoogleCastClient): void { |
| 51 | + client.removeAllListeners(); |
| 52 | + } |
| 53 | +} |
0 commit comments