diff --git a/Makefile b/Makefile index 1b5ec0e..e538de5 100644 --- a/Makefile +++ b/Makefile @@ -11,6 +11,9 @@ venv: requirements.txt run: node_modules ./node_modules/.bin/ts-node --esm http_server.ts +pair: node_modules + ./node_modules/.bin/ts-node --esm cmd/pair.ts + hook: bundle.js venv ./venv/bin/python3 -u loader3.py diff --git a/README.md b/README.md index 4b45a5b..7490908 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,11 @@ Per pictures of the [X5](https://github.com/DavidVentura/cam-reverse/blob/master To execute the HTTP server, run `make run`; you can access the JPEG stream at http://localhost:1234/ and a file `audio.pcm` will be created. The roundtrip delay when using MJPEG is [~350ms](pics/delay.jpg?raw=true). + +## Pairing a new camera + +Connect to the device's access point and run `SSID= PSK= make pair`. + ## Protocol The protocol is weirdly complex, though very little communication is necessary to use the device diff --git a/cmd/pair.ts b/cmd/pair.ts new file mode 100644 index 0000000..a0df990 --- /dev/null +++ b/cmd/pair.ts @@ -0,0 +1,31 @@ +import { RemoteInfo } from "dgram"; + +import { discoverDevices } from "../discovery.js"; +import { DevSerial } from "../impl.js"; +import { Handlers, makeSession, Session, configureWifi } from "../session.js"; + +const opts = { + debug: true, + ansi: true, + discovery_ip: "192.168.1.255", +}; + +let sessions: Record = {}; + +let devEv = discoverDevices(opts); +const onLogin = configureWifi(process.env.SSID, process.env.PSK); + +devEv.on("discover", (rinfo: RemoteInfo, dev: DevSerial) => { + if (dev.devId in sessions) { + console.log(`ignoring ${dev.devId} - ${rinfo.address}`); + return; + } + console.log(`discovered ${dev.devId} - ${rinfo.address}`); + const s = makeSession(Handlers, dev, rinfo, onLogin, opts); + + s.eventEmitter.on("disconnect", () => { + console.log("deleting from sessions"); + sessions[dev.devId] = undefined; + }); + sessions[dev.devId] = s; +});