Adding reset and resetCallback to BLE and Boost extension only for now.

This commit is contained in:
Evelyn Eastmond 2019-06-06 15:24:26 -04:00
parent b88d5d05b0
commit 56fdd45242
2 changed files with 19 additions and 15 deletions

View file

@ -678,7 +678,7 @@ class Boost {
*/
this._pingDeviceId = null;
this.disconnect = this.disconnect.bind(this);
this.reset = this.reset.bind(this);
this._onConnect = this._onConnect.bind(this);
this._onMessage = this._onMessage.bind(this);
this._pingDevice = this._pingDevice.bind(this);
@ -809,7 +809,7 @@ class Boost {
} commented out until feature is enabled in scratch-link */
}],
optionalServices: []
}, this._onConnect, this.disconnect);
}, this._onConnect, this.reset);
}
/**
@ -826,6 +826,15 @@ class Boost {
* Disconnects from the current BLE socket.
*/
disconnect () {
if (this._ble) {
this._ble.disconnect();
}
}
/**
* Reset all the state and timeout/interval ids.
*/
reset () {
this._ports = [];
this._motors = [];
this._sensors = {
@ -835,10 +844,6 @@ class Boost {
previousColor: BoostColor.NONE
};
if (this._ble) {
this._ble.disconnect();
}
if (this._pingDeviceId) {
window.clearInterval(this._pingDeviceId);
this._pingDeviceId = null;

View file

@ -9,9 +9,9 @@ class BLE extends JSONRPC {
* @param {string} extensionId - the id of the extension using this socket.
* @param {object} peripheralOptions - the list of options for peripheral discovery.
* @param {object} connectCallback - a callback for connection.
* @param {object} disconnectCallback - a callback for disconnection.
* @param {object} resetCallback - a callback for resetting extension state.
*/
constructor (runtime, extensionId, peripheralOptions, connectCallback, disconnectCallback = null) {
constructor (runtime, extensionId, peripheralOptions, connectCallback, resetCallback = null) {
super();
this._socket = runtime.getScratchLinkSocket('BLE');
@ -26,7 +26,7 @@ class BLE extends JSONRPC {
this._connectCallback = connectCallback;
this._connected = false;
this._characteristicDidChangeCallback = null;
this._disconnectCallback = disconnectCallback;
this._resetCallback = resetCallback;
this._discoverTimeoutID = null;
this._extensionId = extensionId;
this._peripheralOptions = peripheralOptions;
@ -199,18 +199,17 @@ class BLE extends JSONRPC {
* - being powered down
*
* Disconnect the socket, and if the extension using this socket has a
* disconnect callback, call it. Finally, emit an error to the runtime.
* reset callback, call it. Finally, emit an error to the runtime.
*/
handleDisconnectError (/* e */) {
// log.error(`BLE error: ${JSON.stringify(e)}`);
if (!this._connected) return;
// TODO: Fix branching by splitting up cleanup/disconnect in extension
if (this._disconnectCallback) {
this._disconnectCallback(); // must call disconnect()
} else {
this.disconnect();
if (this._resetCallback) {
this._resetCallback();
}
this._runtime.emit(this._runtime.constructor.PERIPHERAL_CONNECTION_LOST_ERROR, {