Testing adding message/extensionId passing into specific, custom alerts.

This commit is contained in:
Evelyn Eastmond 2018-09-21 09:44:48 -04:00
parent 96327c66f3
commit ad3328197e
4 changed files with 22 additions and 10 deletions

View file

@ -73,6 +73,8 @@ class MicroBit {
this._ble = null; this._ble = null;
this._runtime.registerPeripheralExtension(extensionId, this); this._runtime.registerPeripheralExtension(extensionId, this);
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>}
@ -132,6 +134,15 @@ class MicroBit {
this.disconnect = this.disconnect.bind(this); this.disconnect = this.disconnect.bind(this);
this._onConnect = this._onConnect.bind(this); this._onConnect = this._onConnect.bind(this);
this._onMessage = this._onMessage.bind(this); this._onMessage = this._onMessage.bind(this);
this._runtime.emit(this._runtime.constructor.PERIPHERAL_ERROR, {
message: `Scratch lost connection to peripheral.`,
extensionId: this._extensionId
});
this._runtime.emit(this._runtime.constructor.PERIPHERAL_ERROR, {
message: `Scratch lost connection to peripheral.`,
extensionId: 2
});
} }
/** /**
@ -200,7 +211,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, 'micro:bit', { this._ble = new BLE(this._runtime, this._extensionId, {
filters: [ filters: [
{services: [BLEUUID.service]} {services: [BLEUUID.service]}
] ]

View file

@ -8,11 +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} peripheralType - the type of peripheral. * @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, peripheralType, peripheralOptions, connectCallback) { constructor (runtime, extensionId, peripheralOptions, connectCallback) {
const ws = new WebSocket(ScratchLinkWebSocket); const ws = new WebSocket(ScratchLinkWebSocket);
super(ws); super(ws);
@ -25,8 +25,8 @@ 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._peripheralType = peripheralType;
this._discoverTimeoutID = null; this._discoverTimeoutID = null;
this._runtime = runtime; this._runtime = runtime;
} }
@ -175,7 +175,8 @@ class BLE extends JSONRPCWebSocket {
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 ${this._peripheralType}.` message: `Scratch lost connection to ${this._peripheralType}.`,
extensionId: this._extensionId
}); });
} }

View file

@ -8,12 +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} peripheralType - the type of peripheral. * @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, peripheralType, peripheralOptions, connectCallback, messageCallback) { constructor (runtime, extensionId, peripheralOptions, connectCallback, messageCallback) {
const ws = new WebSocket(ScratchLinkWebSocket); const ws = new WebSocket(ScratchLinkWebSocket);
super(ws); super(ws);
@ -26,8 +26,8 @@ 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._peripheralType = peripheralType;
this._discoverTimeoutID = null; this._discoverTimeoutID = null;
this._messageCallback = messageCallback; this._messageCallback = messageCallback;
this._runtime = runtime; this._runtime = runtime;

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, message => this.runtime.on(Runtime.PERIPHERAL_ERROR, data =>
this.emit(Runtime.PERIPHERAL_ERROR, message) 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)