Merge pull request #1607 from evhan55/multiple-alerts

Show customized alerts on hardware extension peripheral errors.
This commit is contained in:
Evelyn Eastmond 2018-09-25 14:44:29 -04:00 committed by GitHub
commit a676970359
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 34 additions and 9 deletions

View file

@ -408,6 +408,11 @@ class EV3 {
this._runtime = runtime; this._runtime = runtime;
this._runtime.on('PROJECT_STOP_ALL', this.stopAll.bind(this)); this._runtime.on('PROJECT_STOP_ALL', this.stopAll.bind(this));
/**
* The id of the extension this peripheral belongs to.
*/
this._extensionId = extensionId;
/** /**
* A list of the names of the sensors connected in ports 1,2,3,4. * A list of the names of the sensors connected in ports 1,2,3,4.
* @type {string[]} * @type {string[]}
@ -548,7 +553,7 @@ class EV3 {
* Called by the runtime when user wants to scan for an EV3 peripheral. * Called by the runtime when user wants to scan for an EV3 peripheral.
*/ */
scan () { scan () {
this._bt = new BT(this._runtime, { this._bt = new BT(this._runtime, this._extensionId, {
majorDeviceClass: 8, majorDeviceClass: 8,
minorDeviceClass: 1 minorDeviceClass: 1
}, this._onConnect, this._onMessage); }, this._onConnect, this._onMessage);

View file

@ -73,6 +73,11 @@ class MicroBit {
this._ble = null; this._ble = null;
this._runtime.registerPeripheralExtension(extensionId, this); this._runtime.registerPeripheralExtension(extensionId, this);
/**
* The id of the extension this peripheral belongs to.
*/
this._extensionId = extensionId;
/** /**
* The most recently received value for each sensor. * The most recently received value for each sensor.
* @type {Object.<string, number>} * @type {Object.<string, number>}
@ -200,7 +205,7 @@ class MicroBit {
* Called by the runtime when user wants to scan for a peripheral. * Called by the runtime when user wants to scan for a peripheral.
*/ */
scan () { scan () {
this._ble = new BLE(this._runtime, { this._ble = new BLE(this._runtime, this._extensionId, {
filters: [ filters: [
{services: [BLEUUID.service]} {services: [BLEUUID.service]}
] ]

View file

@ -357,6 +357,11 @@ class WeDo2 {
this._runtime = runtime; this._runtime = runtime;
this._runtime.on('PROJECT_STOP_ALL', this.stopAll.bind(this)); this._runtime.on('PROJECT_STOP_ALL', this.stopAll.bind(this));
/**
* The id of the extension this peripheral belongs to.
*/
this._extensionId = extensionId;
/** /**
* A list of the ids of the motors or sensors in ports 1 and 2. * A list of the ids of the motors or sensors in ports 1 and 2.
* @type {string[]} * @type {string[]}
@ -549,7 +554,7 @@ class WeDo2 {
* Called by the runtime when user wants to scan for a WeDo 2.0 peripheral. * Called by the runtime when user wants to scan for a WeDo 2.0 peripheral.
*/ */
scan () { scan () {
this._ble = new BLE(this._runtime, { this._ble = new BLE(this._runtime, this._extensionId, {
filters: [{ filters: [{
services: [BLEService.DEVICE_SERVICE] 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 * A BLE peripheral socket object. It handles connecting, over web sockets, to
* BLE peripherals, and reading and writing data to them. * BLE peripherals, and reading and writing data to them.
* @param {Runtime} runtime - the Runtime for sending/receiving GUI update events. * @param {Runtime} runtime - the Runtime for sending/receiving GUI update events.
* @param {string} extensionId - the id of the extension using this socket.
* @param {object} peripheralOptions - the list of options for peripheral discovery. * @param {object} peripheralOptions - the list of options for peripheral discovery.
* @param {object} connectCallback - a callback for connection. * @param {object} connectCallback - a callback for connection.
*/ */
constructor (runtime, peripheralOptions, connectCallback) { constructor (runtime, extensionId, peripheralOptions, connectCallback) {
const ws = new WebSocket(ScratchLinkWebSocket); const ws = new WebSocket(ScratchLinkWebSocket);
super(ws); super(ws);
@ -24,6 +25,7 @@ class BLE extends JSONRPCWebSocket {
this._connectCallback = connectCallback; this._connectCallback = connectCallback;
this._connected = false; this._connected = false;
this._characteristicDidChangeCallback = null; this._characteristicDidChangeCallback = null;
this._extensionId = extensionId;
this._peripheralOptions = peripheralOptions; this._peripheralOptions = peripheralOptions;
this._discoverTimeoutID = null; this._discoverTimeoutID = null;
this._runtime = runtime; this._runtime = runtime;
@ -172,7 +174,10 @@ class BLE extends JSONRPCWebSocket {
_sendError (/* e */) { _sendError (/* e */) {
this.disconnect(); this.disconnect();
// log.error(`BLE error: ${JSON.stringify(e)}`); // 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`,
extensionId: this._extensionId
});
} }
_sendDiscoverTimeout () { _sendDiscoverTimeout () {

View file

@ -8,11 +8,12 @@ class BT extends JSONRPCWebSocket {
* A BT peripheral socket object. It handles connecting, over web sockets, to * A BT peripheral socket object. It handles connecting, over web sockets, to
* BT peripherals, and reading and writing data to them. * BT peripherals, and reading and writing data to them.
* @param {Runtime} runtime - the Runtime for sending/receiving GUI update events. * @param {Runtime} runtime - the Runtime for sending/receiving GUI update events.
* @param {string} extensionId - the id of the extension using this socket.
* @param {object} peripheralOptions - the list of options for peripheral discovery. * @param {object} peripheralOptions - the list of options for peripheral discovery.
* @param {object} connectCallback - a callback for connection. * @param {object} connectCallback - a callback for connection.
* @param {object} messageCallback - a callback for message sending. * @param {object} messageCallback - a callback for message sending.
*/ */
constructor (runtime, peripheralOptions, connectCallback, messageCallback) { constructor (runtime, extensionId, peripheralOptions, connectCallback, messageCallback) {
const ws = new WebSocket(ScratchLinkWebSocket); const ws = new WebSocket(ScratchLinkWebSocket);
super(ws); super(ws);
@ -25,6 +26,7 @@ class BT extends JSONRPCWebSocket {
this._connectCallback = connectCallback; this._connectCallback = connectCallback;
this._connected = false; this._connected = false;
this._characteristicDidChangeCallback = null; this._characteristicDidChangeCallback = null;
this._extensionId = extensionId;
this._peripheralOptions = peripheralOptions; this._peripheralOptions = peripheralOptions;
this._discoverTimeoutID = null; this._discoverTimeoutID = null;
this._messageCallback = messageCallback; this._messageCallback = messageCallback;
@ -115,7 +117,10 @@ class BT extends JSONRPCWebSocket {
_sendError (/* e */) { _sendError (/* e */) {
this.disconnect(); this.disconnect();
// log.error(`BT error: ${JSON.stringify(e)}`); // 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`,
extensionId: this._extensionId
});
} }
_sendDiscoverTimeout () { _sendDiscoverTimeout () {

View file

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