implement pair command

This commit is contained in:
DavidVentura
2024-01-31 16:38:23 +01:00
parent a93c0badff
commit 5379266fc8
3 changed files with 39 additions and 0 deletions
+31
View File
@@ -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<string, Session> = {};
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;
});