-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathpair.ts
63 lines (55 loc) · 2.23 KB
/
pair.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import { RemoteInfo } from "dgram";
import { config } from "./settings.js";
import { discoverDevices } from "./discovery.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";
export const pair = ({ ssid, password }: { ssid: string; password: string }) => {
logger.info(`Will configure any devices found to join ${ssid}`);
let sessions: Record<string, Session> = {};
let devEv = discoverDevices(config.discovery_ips);
if (password == "") {
throw new Error("You must set a non-zero-length password");
}
const onLogin = (s: Session) => {
logger.info(`Scanning for Wifi networks on ${s.devName} -- this may time out`);
// configureWifi(ssid, password, 0)(s);
s.send(SendListWifi(s));
};
devEv.on("discover", (rinfo: RemoteInfo, dev: DevSerial) => {
if (dev.devId in sessions) {
logger.info(`Camera ${dev.devId} at ${rinfo.address} already discovered, ignoring`);
return;
}
logger.info(`Discovered camera ${dev.devId} at ${rinfo.address}`);
const s = makeSession(Handlers, dev, rinfo, onLogin, 10000);
let configured = {};
s.eventEmitter.on("disconnect", () => {
logger.info(`Camera ${dev.devId} disconnected`);
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`);
});
});
};