refactor to allow bundling

This commit is contained in:
David Ventura
2024-04-26 10:32:56 +02:00
parent 1c6312e0c9
commit 639a78419e
8 changed files with 656 additions and 188 deletions
+49
View File
@@ -0,0 +1,49 @@
import { hideBin } from "yargs/helpers";
import yargs from "yargs/yargs";
import { serveHttp } from "../http_server.js";
import { opt } from "../options.js";
import { pair } from "../pair.js";
yargs(hideBin(process.argv))
.command(
"http_server",
"start http server",
(yargs) => {
return yargs
.option("debug", { default: false })
.option("ansi", { default: false })
.option("audio", { default: false })
.boolean(["debug", "ansi", "audio"])
.option("discovery_ip", { default: "192.168.1.255" })
.option("attempt_to_fix_packet_loss", { default: false })
.option("port", { describe: "HTTP Port to listen on", default: 5000 })
.number(["port"])
.strict();
},
(argv) => {
const opts: opt = argv as opt;
serveHttp(opts, argv.port, argv.audio || false);
},
)
.command(
"pair",
"configure a camera",
(yargs) => {
return yargs
.option("debug", { default: false })
.option("ansi", { default: false })
.boolean(["debug", "ansi"])
.option("discovery_ip", { default: "192.168.1.255" })
.option("attempt_to_fix_packet_loss", { default: false })
.demandOption("ssid")
.demandOption("password")
.string(["ssid", "password"]);
},
(argv) => {
const opts: opt = argv as unknown as opt;
pair({ opts, ssid: argv.ssid, password: argv.password });
},
)
.demandCommand()
.parseSync();
-37
View File
@@ -1,37 +0,0 @@
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",
attempt_to_fix_packet_loss: false,
};
let sessions: Record<string, Session> = {};
let devEv = discoverDevices(opts);
if (process.env.SSID == undefined || process.env.PSK == undefined) {
throw new Error("Set `SSID` and `PSK` environment variables");
}
const onLogin = configureWifi(process.env.SSID, process.env.PSK);
console.log(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;
});