Skip to content

Commit

Permalink
call configureWifi after scanning to include the channel
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidVentura committed May 7, 2024
1 parent 7540162 commit 2facd91
Showing 1 changed file with 29 additions and 11 deletions.
40 changes: 29 additions & 11 deletions pair.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import { RemoteInfo } from "dgram";

import { config } from "./settings.js";
import { discoverDevices } from "./discovery.js";
import { DevSerial, SendReboot, SendWifiSettings } from "./impl.js";
import { WifiListItem } from "./handlers.js";
import { DevSerial, SendListWifi } from "./impl.js";
import { Handlers, makeSession, Session, configureWifi } from "./session.js";
import { logger } from "./logger.js";

Expand All @@ -16,15 +17,10 @@ export const pair = ({ ssid, password }: { ssid: string; password: string }) =>
}

const onLogin = (s: Session) => {
logger.info(`Configuring camera ${s.devName}`);
configureWifi(ssid, password)(s);
logger.info(`WiFi config for camera ${s.devName} is done`);
logger.info(`Scanning for Wifi networks on ${s.devName} -- this may time out`);

logger.info(`Validating WiFi settings on ${s.devName}`);
s.send(SendWifiSettings(s));

logger.info(`Asking ${s.devName} to reboot`);
s.send(SendReboot(s));
// configureWifi(ssid, password, 0)(s);
s.send(SendListWifi(s));
};

devEv.on("discover", (rinfo: RemoteInfo, dev: DevSerial) => {
Expand All @@ -33,13 +29,35 @@ export const pair = ({ ssid, password }: { ssid: string; password: string }) =>
return;
}
logger.info(`Discovered camera ${dev.devId} at ${rinfo.address}`);
const s = makeSession(Handlers, dev, rinfo, onLogin);
const s = makeSession(Handlers, dev, rinfo, onLogin, 10000);
let configured = {};

s.eventEmitter.on("disconnect", () => {
logger.info(`Camera ${dev.devId} disconnected`);
logger.info("Press CONTROL+C if you're done setting up your cameras");
if (configured[dev.devId]) {
logger.info("Press CONTROL+C if you're done setting up your cameras");
}
delete sessions[dev.devId];
delete configured[dev.devId];
});
sessions[dev.devId] = s;

s.eventEmitter.on("ListWifi", (items: WifiListItem[]) => {
const matches = items.filter((i) => i.ssid == ssid);
if (matches.length == 0) {
logger.error(`Camera could not find SSID '${ssid}'`);
return;
}
if (configured[dev.devId]) {
logger.info(`Got two answers from camera, ignoring second`);
return;
}
const match = matches[0];
logger.info(`Configuring camera ${s.devName} on ${JSON.stringify(match)}`);
configureWifi(ssid, password, match.channel)(s);
configured[dev.devId] = true;
logger.info(`WiFi config for camera ${s.devName} is done`);
logger.info(`Camera should reboot now`);
});
});
};

0 comments on commit 2facd91

Please sign in to comment.