From 7d480be373cc0cfc400b92be6e98fd8df6184772 Mon Sep 17 00:00:00 2001 From: Grace Carr Date: Wed, 9 Mar 2022 04:33:54 -0800 Subject: [PATCH] Support multiple playstations (#22) * support discovery of multiple playstation accessories * update cli script to support multiple devices * Fix lint errors Co-authored-by: Flavio De Stefano --- src/cli.ts | 74 +++++++++++++++++++++++++++++--------- src/playstationPlatform.ts | 12 ++++--- 2 files changed, 64 insertions(+), 22 deletions(-) diff --git a/src/cli.ts b/src/cli.ts index d221efd..549ab9c 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -1,20 +1,60 @@ #!/usr/bin/env node + import { DeviceOptions } from "playactor/dist/cli/options"; -const opt = new DeviceOptions(); -opt.dontAutoOpenUrls = true; -opt - .findDevice() - .then((device) => { - device - .openConnection() - .then((conn) => { - conn.close(); - console.log("Connection succeded, restart HomeBridge now!"); - }) - .catch((err) => { - console.error(err.message); - }); - }) - .catch((err) => { - console.error(err.message); +import { Discovery } from "playactor/dist/discovery"; +import readline from "readline"; + +const discover = async () => { + const discovery = new Discovery(); + const devices = discovery.discover(); + + let success = false; + for await (const device of devices) { + console.log("Discovered device:", device); + const confirmed = await confirm(device.name); + if (confirmed) { + // track if there were any successful connections + success = (await connect(device.id)) || success; + } + } + + if (success) { + console.log("Success, restart HomeBridge now!"); + } else { + console.error("Did not authenticate to any consoles."); + } +}; + +const confirm = (deviceName) => { + const input = readline.createInterface({ + input: process.stdin, + output: process.stdout, }); + + return new Promise((resolve) => { + input.question(`Authenticate to ${deviceName}? (y/n) `, (response) => { + input.close(); + resolve(response.toLowerCase() === "y"); + }); + }); +}; + +const connect = async (deviceId) => { + const opt = new DeviceOptions(); + opt.dontAutoOpenUrls = true; + opt.deviceHostId = deviceId; + + try { + const device = await opt.findDevice(); + const conn = await device.openConnection(); + console.log("Connection succeeded!"); + await conn.close(); + return true; + } catch (err) { + const message = err instanceof Error ? err.message : err; + console.error(message); + return false; + } +}; + +discover().catch((err) => console.error(err)); diff --git a/src/playstationPlatform.ts b/src/playstationPlatform.ts index 0f63b82..2a7302d 100644 --- a/src/playstationPlatform.ts +++ b/src/playstationPlatform.ts @@ -8,7 +8,7 @@ import { } from "homebridge"; import { PlaystationAccessory } from "./playstationAccessory"; -import { Device } from "playactor/dist/device"; +import { Discovery } from "playactor/dist/discovery"; export interface PlaystationPlatformConfig extends PlatformConfig { pollInterval?: number; @@ -37,10 +37,12 @@ export class PlaystationPlatform implements IndependentPlatformPlugin { } async discoverDevices() { - const device = await Device.any(); - const deviceInformation = await device.discover(); - this.log.info("Discovered device:", deviceInformation); + const discovery = new Discovery(); + const devices = discovery.discover(); - new PlaystationAccessory(this, deviceInformation); + for await (const deviceInformation of devices) { + this.log.info("Discovered device:", deviceInformation); + new PlaystationAccessory(this, deviceInformation); + } } }