Passing custom alert messages with PERIPHERAL_ERROR.

This commit is contained in:
Evelyn Eastmond 2018-09-14 16:26:08 -04:00
parent e2c6c7667b
commit 96327c66f3
6 changed files with 17 additions and 9 deletions

View file

@ -548,7 +548,7 @@ class EV3 {
* Called by the runtime when user wants to scan for an EV3 peripheral.
*/
scan () {
this._bt = new BT(this._runtime, {
this._bt = new BT(this._runtime, 'EV3', {
majorDeviceClass: 8,
minorDeviceClass: 1
}, this._onConnect, this._onMessage);

View file

@ -200,7 +200,7 @@ class MicroBit {
* Called by the runtime when user wants to scan for a peripheral.
*/
scan () {
this._ble = new BLE(this._runtime, {
this._ble = new BLE(this._runtime, 'micro:bit', {
filters: [
{services: [BLEUUID.service]}
]

View file

@ -549,7 +549,7 @@ class WeDo2 {
* Called by the runtime when user wants to scan for a WeDo 2.0 peripheral.
*/
scan () {
this._ble = new BLE(this._runtime, {
this._ble = new BLE(this._runtime, 'WeDo 2', {
filters: [{
services: [BLEService.DEVICE_SERVICE]
}],

View file

@ -8,10 +8,11 @@ class BLE extends JSONRPCWebSocket {
* A BLE peripheral socket object. It handles connecting, over web sockets, to
* BLE peripherals, and reading and writing data to them.
* @param {Runtime} runtime - the Runtime for sending/receiving GUI update events.
* @param {string} peripheralType - the type of peripheral.
* @param {object} peripheralOptions - the list of options for peripheral discovery.
* @param {object} connectCallback - a callback for connection.
*/
constructor (runtime, peripheralOptions, connectCallback) {
constructor (runtime, peripheralType, peripheralOptions, connectCallback) {
const ws = new WebSocket(ScratchLinkWebSocket);
super(ws);
@ -25,6 +26,7 @@ class BLE extends JSONRPCWebSocket {
this._connected = false;
this._characteristicDidChangeCallback = null;
this._peripheralOptions = peripheralOptions;
this._peripheralType = peripheralType;
this._discoverTimeoutID = null;
this._runtime = runtime;
}
@ -172,7 +174,9 @@ class BLE extends JSONRPCWebSocket {
_sendError (/* e */) {
this.disconnect();
// log.error(`BLE error: ${JSON.stringify(e)}`);
this._runtime.emit(this._runtime.constructor.PERIPHERAL_ERROR);
this._runtime.emit(this._runtime.constructor.PERIPHERAL_ERROR, {
message: `Scratch lost connection to ${this._peripheralType}.`
});
}
_sendDiscoverTimeout () {

View file

@ -8,11 +8,12 @@ class BT extends JSONRPCWebSocket {
* A BT peripheral socket object. It handles connecting, over web sockets, to
* BT peripherals, and reading and writing data to them.
* @param {Runtime} runtime - the Runtime for sending/receiving GUI update events.
* @param {string} peripheralType - the type of peripheral.
* @param {object} peripheralOptions - the list of options for peripheral discovery.
* @param {object} connectCallback - a callback for connection.
* @param {object} messageCallback - a callback for message sending.
*/
constructor (runtime, peripheralOptions, connectCallback, messageCallback) {
constructor (runtime, peripheralType, peripheralOptions, connectCallback, messageCallback) {
const ws = new WebSocket(ScratchLinkWebSocket);
super(ws);
@ -26,6 +27,7 @@ class BT extends JSONRPCWebSocket {
this._connected = false;
this._characteristicDidChangeCallback = null;
this._peripheralOptions = peripheralOptions;
this._peripheralType = peripheralType;
this._discoverTimeoutID = null;
this._messageCallback = messageCallback;
this._runtime = runtime;
@ -115,7 +117,9 @@ class BT extends JSONRPCWebSocket {
_sendError (/* e */) {
this.disconnect();
// log.error(`BT error: ${JSON.stringify(e)}`);
this._runtime.emit(this._runtime.constructor.PERIPHERAL_ERROR);
this._runtime.emit(this._runtime.constructor.PERIPHERAL_ERROR, {
message: `Scratch lost connection to ${this._peripheralType}.`
});
}
_sendDiscoverTimeout () {

View file

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