generated from homebridge/homebridge-plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
Showing
2 changed files
with
64 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters