Skip to content

Commit 315801b

Browse files
Scan for devices before connecting
Attempt to connect once we've found the expected number of devices or completed the discovery timeout
1 parent 49793a4 commit 315801b

File tree

2 files changed

+12
-13
lines changed

2 files changed

+12
-13
lines changed

index.js

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,12 @@ noble.on('stateChange', (state) => {
9696
setTimeout(() => {
9797
log('stopping scan after timeout');
9898
noble.stopScanning();
99-
if (Object.entries(devices).filter(([, device]) => device.connectionState === 'connected').length === 0) {
99+
if (Object.keys(devices).length === 0) {
100100
log('No devices found, exiting');
101101
process.exit(-2);
102102
}
103+
104+
Object.values(devices).forEach((device) => {device.connect();});
103105
}, 1000 * argv.discoveryTimeout);
104106
}
105107

@@ -171,17 +173,16 @@ noble.on('discover', peripheral => {
171173
devices[id] = new SomaShade(id, peripheral, noble);
172174
if (argv.debug) {devices[id].log.enabled = true;}
173175

174-
peripheral.on('connect', () => {
175-
log('connected to %s', id);
176-
if (argv.expectedDevices &&
177-
Object.entries(devices).filter(([, device]) => device.connectionState === 'connected').length === argv.expectedDevices) {
178-
if (!loggedStop) {
179-
log('all expected devices connected, stopping scan');
180-
loggedStop = true;
181-
}
182-
noble.stopScanning();
176+
log('discovered %s', id);
177+
if (argv.expectedDevices &&
178+
Object.keys(devices).length === argv.expectedDevices) {
179+
if (!loggedStop) {
180+
log('all expected devices connected, stopping scan');
181+
loggedStop = true;
183182
}
184-
});
183+
noble.stopScanning();
184+
Object.values(devices).forEach((device) => {device.connect();});
185+
}
185186

186187
if (mqttUrl) {
187188
new mqttBinding(devices[id], mqttUrl, baseTopic, mqttUsername, mqttPassword);

src/SomaShade.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,6 @@ class SomaShade extends EventEmitter {
6767
this.emit('connectionStateChanged', this.getState());
6868
}});
6969
this._connectionState = 'disconnected';
70-
71-
this.connect();
7270
}
7371

7472
move(position) {

0 commit comments

Comments
 (0)