Skip to content

Commit

Permalink
Support multiple playstations (#22)
Browse files Browse the repository at this point in the history
* support discovery of multiple playstation accessories

* update cli script to support multiple devices

* Fix lint errors

Co-authored-by: Flavio De Stefano <destefano.flavio@gmail.com>
  • Loading branch information
grelca and kopiro authored Mar 9, 2022
1 parent d9040ef commit 7d480be
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 22 deletions.
74 changes: 57 additions & 17 deletions src/cli.ts
Original file line number Diff line number Diff line change
@@ -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));
12 changes: 7 additions & 5 deletions src/playstationPlatform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
}

0 comments on commit 7d480be

Please sign in to comment.