EV3/Microbit critical fixes for code freeze ()

* Resolves - BLESession and BTSession should emit PERIPHERAL_SCAN_TIMEOUT .

* Resolves - BLESession should handle 'could not find service' error .

* Resolves - BTSession should handle 'no peripheral connected' error .

* Fixing a typo that caused device scan timeout bugs.

* Resolves - Add casting and clamping throughout the EV3 extension .

* Fixing a linting error.

* Further fixes for issue .
This commit is contained in:
Evelyn Eastmond 2018-07-17 16:03:06 -04:00 committed by Eric Rosenbaum
parent 1dcdfc9548
commit c4ee7065a2
6 changed files with 117 additions and 46 deletions

View file

@ -22,11 +22,11 @@ class BLESession extends JSONRPCWebSocket {
this._availablePeripherals = {};
this._connectCallback = connectCallback;
this._connected = false;
this._characteristicDidChangeCallback = null;
this._deviceOptions = deviceOptions;
this._discoverTimeoutID = null;
this._runtime = runtime;
this._connected = false;
}
/**
@ -35,7 +35,8 @@ class BLESession extends JSONRPCWebSocket {
*/
requestDevice () {
if (this._ws.readyState === 1) { // is this needed since it's only called on ws.onopen?
// TODO: start a 'discover' timeout
this._availablePeripherals = {};
this._discoverTimeoutID = window.setTimeout(this._sendDiscoverTimeout.bind(this), 15000);
this.sendRemoteRequest('discover', this._deviceOptions)
.catch(e => this._sendError(e)); // never reached?
}
@ -88,7 +89,10 @@ class BLESession extends JSONRPCWebSocket {
this._runtime.constructor.PERIPHERAL_LIST_UPDATE,
this._availablePeripherals
);
// TODO: cancel a discover timeout if one is active
if (this._discoverTimeoutID) {
// TODO: window?
window.clearTimeout(this._discoverTimeoutID);
}
break;
case 'characteristicDidChange':
this._characteristicDidChangeCallback(params.message);
@ -115,8 +119,10 @@ class BLESession extends JSONRPCWebSocket {
params.startNotifications = true;
}
this._characteristicDidChangeCallback = onCharacteristicChanged;
return this.sendRemoteRequest('read', params);
// TODO: handle error here
return this.sendRemoteRequest('read', params)
.catch(e => {
this._sendError(e);
});
}
/**
@ -132,7 +138,10 @@ class BLESession extends JSONRPCWebSocket {
if (encoding) {
params.encoding = encoding;
}
return this.sendRemoteRequest('write', params);
return this.sendRemoteRequest('write', params)
.catch(e => {
this._sendError(e);
});
}
_sendError (e) {
@ -140,6 +149,10 @@ class BLESession extends JSONRPCWebSocket {
log.error(`BLESession error: ${JSON.stringify(e)}`);
this._runtime.emit(this._runtime.constructor.PERIPHERAL_ERROR);
}
_sendDiscoverTimeout () {
this._runtime.emit(this._runtime.constructor.PERIPHERAL_SCAN_TIMEOUT);
}
}
module.exports = BLESession;