Skip to content

Commit

Permalink
Avoid re-requesting device info during discovery
Browse files Browse the repository at this point in the history
  • Loading branch information
gormanb committed Aug 6, 2023
1 parent c53f15e commit 04c718a
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/connectorhub/connector-device-discovery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,22 @@ const kDiscoveryIntervalMs = 5 * 60 * 1000;
// Sends GetDeviceListReq every kDiscoveryFrequencyMs for kDiscoveryDurationMs.
export function doDiscovery(hubIp: string, platform: ConnectorHubPlatform) {
Log.debug('Starting discovery for hub:', hubIp);
const discoveredDevices: string[] = [];
const hubTokens = {};

// Create a socket for this discovery session, and add listeners to it.
const socket = dgram.createSocket('udp4');
socket.on('message', (msg) => {
const recvMsg = tryParse(msg.toString());
if (recvMsg && recvMsg.msgType === 'GetDeviceListAck') {
// Extract the device list and record the token associated with this hub.
const deviceList = <GetDeviceListAck>(recvMsg);
hubTokens[deviceList.mac] = deviceList.token;
for (const devInfo of deviceList.data) {
// Filter out any devices that have already been discovered this session.
const undiscoveredDevices = deviceList.data.filter(
(devInfo) => !discoveredDevices.includes(devInfo.mac));
// For all as-yet undiscovered devices, request full device information.
for (const devInfo of undiscoveredDevices) {
// If this entry is the hub itself, skip over it and continue.
if (devInfo.deviceType !== DeviceType.kWiFiBridge) {
const readDevReq =
Expand All @@ -39,6 +45,7 @@ export function doDiscovery(hubIp: string, platform: ConnectorHubPlatform) {
} else if (recvMsg && recvMsg.msgType === 'ReadDeviceAck') {
const hubToken = hubTokens[extractHubMac(recvMsg.mac)];
platform.registerDevice(hubIp, recvMsg, hubToken);
discoveredDevices.push(recvMsg.mac);
} else if (recvMsg) {
Log.debug('Unexpected message during discovery:', recvMsg);
}
Expand Down

0 comments on commit 04c718a

Please sign in to comment.