Commit 8a5622f1 authored by Donald Chen's avatar Donald Chen
Browse files

handle reconnecting devices correctly

parent 1159af26
......@@ -8,8 +8,13 @@ class Device {
this.id = bleDevice.address.replace(/:/g, '');
this.bleDevice = bleDevice;
this.fwVersion = null;
this.timer = null;
}
resetState() {
this.fwVersion = null;
this.timer = null;
}
/**
* Returns a Promise that will resolve with the ble device's firmware revision,
* or reject with an error
......@@ -37,11 +42,27 @@ class Device {
*/
setUp() {
return new Promise((resolve, reject) => {
this.timer = setTimeout(() => {
console.log(`connection timeout: ${this.id}`)
this.bleDevice.disconnect();
reject("connection timeout")
}, 10000)
this.bleDevice.connectAndSetUp((setupError) => {
if (setupError) {
reject(new Error(setupError));
}
if(this.timer == null) {
console.log(`duplicate connection timeout: ${this.id}`)
this.bleDevice.disconnect();
reject("connection already timed out")
} else {
clearTimeout(this.timer)
this.timer = null
}
this.readFirmware()
.then(this.enableLuxometer.bind(this))
.then(() => {
......@@ -49,7 +70,7 @@ class Device {
})
.catch((err) => {
console.log(`DEVICE SETUP ERROR: ${err}`);
throw err;
reject(err)
});
});
});
......
......@@ -23,7 +23,7 @@ function startScanning() {
// Restart scanning
// https://github.com/sandeepmistry/noble/issues/223
if (noble.state === 'poweredOn') {
noble.startScanning();
noble.startScanning([], true);
} else {
throw new Error('BLE poweredOff');
}
......@@ -36,50 +36,58 @@ function stopScanning() {
/**
* Called when a new device is discovered
*/
var connected = false
function onDiscover(beaconInst) {
const id = beaconInst.address.replace(/:/g, '');
// Ignore duplicates
if (connected) {
return
}
var device = null;
// must reuse device objects!
if (devices[id]) {
console.log(`${id}: Duplicate device found, ignoring`);
return;
device = devices[id]
} else {
const device = new Device(beaconInst);
console.log(`Discovered: ${device.id}`);
//must stop scanning to initiate a connection
stopScanning()
// Connect to device and setup
device.setUp()
.then(() => {
device.onLuxometerChange((d, lux) => {
const telemetry = {
deviceID: d.id,
light: lux.toFixed(1),
};
provider.sendTelemetry(telemetry);
});
// Store in list of known devices
devices[device.id] = device;
// Set disconnect handler
device.bleDevice.on('disconnect', function disconnectHandler() {
console.log(`${device.id}: disconnected`);
delete devices[device.id];
device.bleDevice.removeListener('disconnect', disconnectHandler);
});
// Wait for as long as possible before scanning again to avoid race
// conditions
device = new Device(beaconInst);
}
console.log(`Connecting: ${device.id}`);
//must stop scanning to initiate a connection
connected = true
stopScanning()
// Connect to device and setup
device.setUp()
.then(() => {
device.onLuxometerChange((d, lux) => {
const telemetry = {
deviceID: d.id,
light: lux.toFixed(1),
};
provider.sendTelemetry(telemetry);
});
// Store in list of known devices
devices[device.id] = device;
// Set disconnect handler
device.bleDevice.once('disconnect', function disconnectHandler() {
console.log(`${device.id}: disconnected`);
device.bleDevice.removeListener('disconnect', disconnectHandler);
device.resetState();
connected = false
startScanning();
})
.catch((error) => {
console.log(error)
process.exit(1)
});
}
})
.catch((error) => {
console.log(error)
device.bleDevice.disconnect();
connected = false
startScanning();
});
}
noble.on('scanStop', () => {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment