From 9685f8008ebfe6514159180a244de35bec5dbe5c Mon Sep 17 00:00:00 2001 From: DavidVentura Date: Wed, 31 Jan 2024 16:32:06 +0100 Subject: [PATCH] allow onLogin CB to be specified --- discovery.ts | 6 ++---- http_server.ts | 13 +++++++++---- options.ts | 1 + session.ts | 9 ++++++++- 4 files changed, 20 insertions(+), 9 deletions(-) diff --git a/discovery.ts b/discovery.ts index 394505f..7beea73 100644 --- a/discovery.ts +++ b/discovery.ts @@ -20,8 +20,6 @@ const handleIncomingPunch = (msg: Buffer, ee: EventEmitter, rinfo: RemoteInfo, o export const discoverDevices = (options: opt): EventEmitter => { const sock = createSocket("udp4"); - //const BCAST_IP = "192.168.1.255"; - const BCAST_IP = "192.168.40.101"; const SEND_PORT = 32108; const ee = new EventEmitter(); @@ -39,9 +37,9 @@ export const discoverDevices = (options: opt): EventEmitter => { let buf = create_LanSearch(); setInterval(() => { console.log("."); - sock.send(new Uint8Array(buf.buffer), SEND_PORT, BCAST_IP); + sock.send(new Uint8Array(buf.buffer), SEND_PORT, options.discovery_ip); }, 2000); - sock.send(new Uint8Array(buf.buffer), SEND_PORT, BCAST_IP); + sock.send(new Uint8Array(buf.buffer), SEND_PORT, options.discovery_ip); }); sock.bind(); diff --git a/http_server.ts b/http_server.ts index 8eca47a..916af9d 100644 --- a/http_server.ts +++ b/http_server.ts @@ -1,11 +1,16 @@ import { createWriteStream } from "node:fs"; import http from "node:http"; +import { RemoteInfo } from "dgram"; -import { Handlers, makeSession, Session } from "./session.js"; +import { Handlers, makeSession, Session, startVideoStream } from "./session.js"; import { discoverDevices } from "./discovery.js"; import { DevSerial } from "./impl.js"; -const opts = { debug: false, ansi: false }; +const opts = { + debug: false, + ansi: false, + discovery_ip: "192.168.40.101", //, "192.168.1.255"; +}; let BOUNDARY = "a very good boundary line"; let responses = []; @@ -38,13 +43,13 @@ const server = http.createServer((req, res) => { }); let devEv = discoverDevices(opts); -devEv.on("discover", (rinfo, dev: DevSerial) => { +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, opts); + const s = makeSession(Handlers, dev, rinfo, startVideoStream, opts); const withAudio = false; s.eventEmitter.on("frame", (frame: Buffer) => { let s = `--${BOUNDARY}\r\n`; diff --git a/options.ts b/options.ts index 66b5f86..2f281d6 100644 --- a/options.ts +++ b/options.ts @@ -1,4 +1,5 @@ export type opt = { debug: boolean; ansi: boolean; + discovery_ip: string; }; diff --git a/session.ts b/session.ts index 533bf24..09ca37f 100644 --- a/session.ts +++ b/session.ts @@ -45,6 +45,7 @@ export const makeSession = ( handlers: Record, dev: DevSerial, ra: RemoteInfo, + onLogin: (s: Session) => void, options: opt, ): Session => { const sock = createSocket("udp4"); @@ -111,7 +112,13 @@ export const makeSession = ( return session; }; -const startVideoStream = (s: Session) => { +export const configureWifi = (ssid: string, password: string) => { + return (s: Session) => { + [SendWifiDetails(s, ssid, password, true)].forEach(s.send); + }; +}; + +export const startVideoStream = (s: Session) => { [ ...SendVideoResolution(s, 2), // 640x480 SendStartVideo(s),