Skip to content

Commit

Permalink
Improve discovery for multiple hubs
Browse files Browse the repository at this point in the history
  • Loading branch information
gormanb committed Aug 6, 2023
1 parent 8cbb096 commit c53f15e
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
2 changes: 1 addition & 1 deletion config.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
},
"hubIps": {
"title": "Hub / Wifi Device IPs",
"description": "If you have more than one hub or WiFi motor device, or if you have a single hub/device but UDP multicast auto-discovery is not working, specify the IPs of each device here.",
"description": "If UDP multicast auto-discovery is not working, specify the IPs of each hub or WiFi motor device here.",
"type": "array",
"required": true,
"minLength": 0,
Expand Down
12 changes: 7 additions & 5 deletions src/connectorhub/connector-device-discovery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {Log} from '../util/log';

import {DeviceModel, DeviceType, GetDeviceListAck, ReadDeviceAck} from './connector-hub-api';
import {kSendPort} from './connector-hub-constants';
import {makeGetDeviceListRequest, makeReadDeviceRequest, TDBUType, tryParse} from './connector-hub-helpers';
import {extractHubMac, makeGetDeviceListRequest, makeReadDeviceRequest, TDBUType, tryParse} from './connector-hub-helpers';

// These constants determine how long each discovery period lasts for, and how
// often we send GetDeviceList requests during that period.
Expand All @@ -19,14 +19,15 @@ 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);
let deviceList: GetDeviceListAck;
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') {
deviceList = <GetDeviceListAck>(recvMsg);
const deviceList = <GetDeviceListAck>(recvMsg);
hubTokens[deviceList.mac] = deviceList.token;
for (const devInfo of deviceList.data) {
// If this entry is the hub itself, skip over it and continue.
if (devInfo.deviceType !== DeviceType.kWiFiBridge) {
Expand All @@ -36,7 +37,8 @@ export function doDiscovery(hubIp: string, platform: ConnectorHubPlatform) {
}
}
} else if (recvMsg && recvMsg.msgType === 'ReadDeviceAck') {
platform.registerDevice(hubIp, recvMsg, deviceList.token);
const hubToken = hubTokens[extractHubMac(recvMsg.mac)];
platform.registerDevice(hubIp, recvMsg, hubToken);
} else if (recvMsg) {
Log.debug('Unexpected message during discovery:', recvMsg);
}
Expand All @@ -55,7 +57,7 @@ export function doDiscovery(hubIp: string, platform: ConnectorHubPlatform) {
// When we have exceeded the discovery duration...
if ((new Date()).getTime() - kStartTime > kDiscoveryDurationMs) {
// ... if we didn't hear back from the hub, keep going...
if (!deviceList) {
if (!Object.keys(hubTokens).length) {
Log.warn(`Device discovery failed to reach hub ${hubIp}, retrying...`);
kStartTime = (new Date()).getTime();
return;
Expand Down
5 changes: 5 additions & 0 deletions src/connectorhub/connector-hub-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,11 @@ export function getDeviceModel(
return basicModel + tdbuType;
}

// Given a device's MAC, extract the MAC of its parent hub.
export function extractHubMac(deviceMac: string): string {
return deviceMac.slice(0, kMacAddrLength);
}

export function makeDeviceName(devInfo: ExtendedDeviceInfo): string {
// The format of a device's MAC is [hub_mac][device_num] where the former is a
// 12-character hex string and the latter is a 4-digit hex string. If this is
Expand Down

0 comments on commit c53f15e

Please sign in to comment.