Implemented pingDevice() which, like the WeDo 2.0 extension, tries to read from the Boost Hub at a given interval. If it doesn't hear back from the hub, it assumes the device has been disconnected.

This commit is contained in:
Kevin Andersen 2019-03-13 14:35:53 -04:00
parent 873b56c985
commit 55ccc4e77a

View file

@ -40,6 +40,12 @@ const BoostBLE = {
const BoostMotorMaxPower = 100; const BoostMotorMaxPower = 100;
/**
* A time interval to wait (in milliseconds) in between battery check calls.
* @type {number}
*/
const BoostPingInterval = 5000;
/** /**
* Enum for Boost sensor and actuator types. * Enum for Boost sensor and actuator types.
* @readonly * @readonly
@ -594,9 +600,17 @@ class Boost {
*/ */
this._rateLimiter = new RateLimiter(BoostBLE.sendRateMax); this._rateLimiter = new RateLimiter(BoostBLE.sendRateMax);
/**
* An interval id for the battery check interval.
* @type {number}
* @private
*/
this._pingDeviceId = null;
this.disconnect = this.disconnect.bind(this); this.disconnect = this.disconnect.bind(this);
this._onConnect = this._onConnect.bind(this); this._onConnect = this._onConnect.bind(this);
this._onMessage = this._onMessage.bind(this); this._onMessage = this._onMessage.bind(this);
this._pingDevice = this._pingDevice.bind(this);
} }
/** /**
@ -698,13 +712,13 @@ class Boost {
} }
this._ble = new BLE(this._runtime, this._extensionId, { this._ble = new BLE(this._runtime, this._extensionId, {
filters: [{ filters: [{
services: [BoostBLE.service], services: [BoostBLE.service]/* ,
manufacturerData: { manufacturerData: {
0: { 0: {
dataPrefix: [0x97, 0x03, 0x00, 0x40], dataPrefix: [0x97, 0x03, 0x00, 0x40],
mask: [0xFF, 0xFF, 0, 0xFF] mask: [0xFF, 0xFF, 0, 0xFF]
} }
} } */
}], }],
optionalServices: [] optionalServices: []
}, this._onConnect, this.disconnect); }, this._onConnect, this.disconnect);
@ -736,6 +750,11 @@ class Boost {
if (this._ble) { if (this._ble) {
this._ble.disconnect(); this._ble.disconnect();
} }
if (this._pingDevice) {
window.clearInterval(this._pingDeviceId);
this._pingDeviceId = null;
}
} }
/** /**
@ -831,6 +850,7 @@ class Boost {
BoostBLE.characteristic, BoostBLE.characteristic,
this._onMessage this._onMessage
); );
this._pingDeviceId = window.setInterval(this._pingDevice, BoostPingInterval);
} }
/** /**
@ -925,6 +945,20 @@ class Boost {
} }
} }
/**
* Ping the Boost hub. If the Boost hub has disconnected
* for some reason, the BLE socket will get an error back and automatically
* close the socket.
*/
_pingDevice () {
this._ble.read(
BoostBLE.service,
BoostBLE.characteristic,
false
);
}
/** /**
* Register a new sensor or motor connected at a port. Store the type of * Register a new sensor or motor connected at a port. Store the type of
* sensor or motor internally, and then register for notifications on input * sensor or motor internally, and then register for notifications on input
@ -958,7 +992,7 @@ class Boost {
case BoostIO.LED: case BoostIO.LED:
mode = BoostMode.LED; mode = BoostMode.LED;
this.setLEDMode(); this.setLEDMode();
this.setLED(0x00FF00); this.setLED(0x0000FF);
break; break;
case BoostIO.TILT: case BoostIO.TILT:
mode = BoostMode.TILT; mode = BoostMode.TILT;