Differentiate peripheral errors: request vs. disconnect (#1654)

* Beginning to add differentiation for hardware disconnect alerts.

* Set connected status after error is sent.
This commit is contained in:
Evelyn Eastmond 2018-10-17 15:48:07 -04:00 committed by Eric Rosenbaum
parent 899ce56214
commit 2564b24d71
4 changed files with 60 additions and 25 deletions

View file

@ -457,11 +457,19 @@ class Runtime extends EventEmitter {
}
/**
* Event name for reporting that a peripheral has encountered an error.
* Event name for reporting that a peripheral has encountered a request error.
* @const {string}
*/
static get PERIPHERAL_ERROR () {
return 'PERIPHERAL_ERROR';
static get PERIPHERAL_REQUEST_ERROR () {
return 'PERIPHERAL_REQUEST_ERROR';
}
/**
* Event name for reporting that a peripheral has encountered a disconnect error.
* @const {string}
*/
static get PERIPHERAL_DISCONNECT_ERROR () {
return 'PERIPHERAL_DISCONNECT_ERROR';
}
/**

View file

@ -18,8 +18,8 @@ class BLE extends JSONRPCWebSocket {
this._ws = ws;
this._ws.onopen = this.requestPeripheral.bind(this); // only call request peripheral after socket opens
this._ws.onerror = this._sendError.bind(this, 'ws onerror');
this._ws.onclose = this._sendError.bind(this, 'ws onclose');
this._ws.onerror = this._sendRequestError.bind(this, 'ws onerror');
this._ws.onclose = this._sendDisconnectError.bind(this, 'ws onclose');
this._availablePeripherals = {};
this._connectCallback = connectCallback;
@ -41,7 +41,7 @@ class BLE extends JSONRPCWebSocket {
this._discoverTimeoutID = window.setTimeout(this._sendDiscoverTimeout.bind(this), 15000);
this.sendRemoteRequest('discover', this._peripheralOptions)
.catch(e => {
this._sendError(e);
this._sendRequestError(e);
}); // never reached?
}
// TODO: else?
@ -60,7 +60,7 @@ class BLE extends JSONRPCWebSocket {
this._connectCallback();
})
.catch(e => {
this._sendError(e);
this._sendRequestError(e);
});
}
@ -69,7 +69,6 @@ class BLE extends JSONRPCWebSocket {
*/
disconnect () {
this._ws.close();
this._connected = false;
}
/**
@ -94,7 +93,7 @@ class BLE extends JSONRPCWebSocket {
this._characteristicDidChangeCallback = onCharacteristicChanged;
return this.sendRemoteRequest('startNotifications', params)
.catch(e => {
this._sendError(e);
this._sendDisconnectError(e);
});
}
@ -117,7 +116,7 @@ class BLE extends JSONRPCWebSocket {
this._characteristicDidChangeCallback = onCharacteristicChanged;
return this.sendRemoteRequest('read', params)
.catch(e => {
this._sendError(e);
this._sendDisconnectError(e);
});
}
@ -140,7 +139,7 @@ class BLE extends JSONRPCWebSocket {
}
return this.sendRemoteRequest('write', params)
.catch(e => {
this._sendError(e);
this._sendDisconnectError(e);
});
}
@ -171,10 +170,23 @@ class BLE extends JSONRPCWebSocket {
}
}
_sendError (/* e */) {
if (this._connected) this.disconnect();
_sendRequestError (/* e */) {
// log.error(`BLE error: ${JSON.stringify(e)}`);
this._runtime.emit(this._runtime.constructor.PERIPHERAL_ERROR, {
this._runtime.emit(this._runtime.constructor.PERIPHERAL_REQUEST_ERROR, {
message: `Scratch lost connection to`,
extensionId: this._extensionId
});
}
_sendDisconnectError (/* e */) {
// log.error(`BLE error: ${JSON.stringify(e)}`);
if (!this._connected) return;
this._connected = false;
this._runtime.emit(this._runtime.constructor.PERIPHERAL_DISCONNECT_ERROR, {
message: `Scratch lost connection to`,
extensionId: this._extensionId
});

View file

@ -19,8 +19,8 @@ class BT extends JSONRPCWebSocket {
this._ws = ws;
this._ws.onopen = this.requestPeripheral.bind(this); // only call request peripheral after socket opens
this._ws.onerror = this._sendError.bind(this, 'ws onerror');
this._ws.onclose = this._sendError.bind(this, 'ws onclose');
this._ws.onerror = this._sendDisconnectError.bind(this, 'ws onerror');
this._ws.onclose = this._sendDisconnectError.bind(this, 'ws onclose');
this._availablePeripherals = {};
this._connectCallback = connectCallback;
@ -42,7 +42,7 @@ class BT extends JSONRPCWebSocket {
this._availablePeripherals = {};
this._discoverTimeoutID = window.setTimeout(this._sendDiscoverTimeout.bind(this), 15000);
this.sendRemoteRequest('discover', this._peripheralOptions)
.catch(e => this._sendError(e)); // never reached?
.catch(e => this._sendRequestError(e)); // never reached?
}
// TODO: else?
}
@ -60,7 +60,7 @@ class BT extends JSONRPCWebSocket {
this._connectCallback();
})
.catch(e => {
this._sendError(e);
this._sendRequestError(e);
});
}
@ -69,7 +69,6 @@ class BT extends JSONRPCWebSocket {
*/
disconnect () {
this._ws.close();
this._connected = false;
}
/**
@ -82,7 +81,7 @@ class BT extends JSONRPCWebSocket {
sendMessage (options) {
return this.sendRemoteRequest('send', options)
.catch(e => {
this._sendError(e);
this._sendDisconnectError(e);
});
}
@ -114,10 +113,23 @@ class BT extends JSONRPCWebSocket {
}
}
_sendError (/* e */) {
if (this._connected) this.disconnect();
_sendRequestError (/* e */) {
// log.error(`BT error: ${JSON.stringify(e)}`);
this._runtime.emit(this._runtime.constructor.PERIPHERAL_ERROR, {
this._runtime.emit(this._runtime.constructor.PERIPHERAL_REQUEST_ERROR, {
message: `Scratch lost connection to`,
extensionId: this._extensionId
});
}
_sendDisconnectError (/* e */) {
// log.error(`BT error: ${JSON.stringify(e)}`);
if (!this._connected) return;
this._connected = false;
this._runtime.emit(this._runtime.constructor.PERIPHERAL_DISCONNECT_ERROR, {
message: `Scratch lost connection to`,
extensionId: this._extensionId
});

View file

@ -111,8 +111,11 @@ class VirtualMachine extends EventEmitter {
this.runtime.on(Runtime.PERIPHERAL_CONNECTED, () =>
this.emit(Runtime.PERIPHERAL_CONNECTED)
);
this.runtime.on(Runtime.PERIPHERAL_ERROR, data =>
this.emit(Runtime.PERIPHERAL_ERROR, data)
this.runtime.on(Runtime.PERIPHERAL_REQUEST_ERROR, () =>
this.emit(Runtime.PERIPHERAL_REQUEST_ERROR)
);
this.runtime.on(Runtime.PERIPHERAL_DISCONNECT_ERROR, data =>
this.emit(Runtime.PERIPHERAL_DISCONNECT_ERROR, data)
);
this.runtime.on(Runtime.PERIPHERAL_SCAN_TIMEOUT, () =>
this.emit(Runtime.PERIPHERAL_SCAN_TIMEOUT)