scratch-vm/src/io/btSession.js

116 lines
3.8 KiB
JavaScript
Raw Normal View History

const JSONRPCWebSocket = require('../util/jsonrpc-web-socket');
const log = require('../util/log');
MicroBit extension, Scratch Link first draft. (#1230) * First microbit gui tests * Fixed JSONRPC inheritance. Renamed ScratchBLE/ScratchBT files. Removed ScratchBT test code from Microbit extension. Renamed addLine to log. * Fixed log comments. Removed addLine from Microbit. * Adding auto-connect to Microbit at extension loading. Adding hack for displayText block to Scratch-Link. * Resolved merge conflicts and brought in latest microbit extension example code. * Updated microbit write tests for displayText and displaySymbol blocks. Some linting. * Some linting and adding of BLE Characteristic consts. * Linting fixes. * Moving micro:bit device connection code all to the MicroBit class, decoupling Scratch3MicroBitBlocks from connection code. * Removing old disconenct handlers from MicroBit class. Moved service into new BLEUUID data structure. * Renamed _write to _send. Moved all BLE encoding concerns to the _send method. * Using the util log. Some linting. * Added _read method to MicroBit class. Renamed _send to _write. * Some linting and formatting comments. * First pass at peripheral chooser pattern for ScratchBLE. * Testing characteristicDidChange events, and some changes to ScratchBLE on ready events. * Refactoring work on PeripheralChooser and ScratchBLE. * Some variable renaming and method signature stubs. * Peripheral chooser method signatures. * Moved base64 encoding/decoding to util. Some method signature formatting. * Adding test stubs for new util and io classes. * Adding test stub for MicroBit extension. * Clean up for PR. * Clean up for PR. * Final cleanup for PR. * Removed logging to console. * Adding 'btoa' and 'atob' node modules and using them in Base64Util.
2018-06-18 14:56:51 -04:00
const ScratchLinkWebSocket = 'ws://localhost:20110/scratch/bt';
class BTSession extends JSONRPCWebSocket {
/**
* A BT device session object. It handles connecting, over web sockets, to
* BT devices, and reading and writing data to them.
* @param {Runtime} runtime - the Runtime for sending/receiving GUI update events.
* @param {object} deviceOptions - the list of options for device discovery.
* @param {object} connectCallback - a callback for connection.
2018-06-27 22:28:33 -04:00
* @param {object} messageCallback - a callback for message sending.
*/
constructor (runtime, deviceOptions, connectCallback, messageCallback) {
const ws = new WebSocket(ScratchLinkWebSocket);
super(ws);
this._ws = ws;
this._ws.onopen = this.requestDevice.bind(this); // only call request device after socket opens
this._ws.onerror = this._sendError.bind(this, 'ws onerror');
this._ws.onclose = this._sendError.bind(this, 'ws onclose');
this._availablePeripherals = {};
this._connectCallback = connectCallback;
this._characteristicDidChangeCallback = null;
this._deviceOptions = deviceOptions;
this._messageCallback = messageCallback;
this._runtime = runtime;
this._connected = false;
MicroBit extension, Scratch Link first draft. (#1230) * First microbit gui tests * Fixed JSONRPC inheritance. Renamed ScratchBLE/ScratchBT files. Removed ScratchBT test code from Microbit extension. Renamed addLine to log. * Fixed log comments. Removed addLine from Microbit. * Adding auto-connect to Microbit at extension loading. Adding hack for displayText block to Scratch-Link. * Resolved merge conflicts and brought in latest microbit extension example code. * Updated microbit write tests for displayText and displaySymbol blocks. Some linting. * Some linting and adding of BLE Characteristic consts. * Linting fixes. * Moving micro:bit device connection code all to the MicroBit class, decoupling Scratch3MicroBitBlocks from connection code. * Removing old disconenct handlers from MicroBit class. Moved service into new BLEUUID data structure. * Renamed _write to _send. Moved all BLE encoding concerns to the _send method. * Using the util log. Some linting. * Added _read method to MicroBit class. Renamed _send to _write. * Some linting and formatting comments. * First pass at peripheral chooser pattern for ScratchBLE. * Testing characteristicDidChange events, and some changes to ScratchBLE on ready events. * Refactoring work on PeripheralChooser and ScratchBLE. * Some variable renaming and method signature stubs. * Peripheral chooser method signatures. * Moved base64 encoding/decoding to util. Some method signature formatting. * Adding test stubs for new util and io classes. * Adding test stub for MicroBit extension. * Clean up for PR. * Clean up for PR. * Final cleanup for PR. * Removed logging to console. * Adding 'btoa' and 'atob' node modules and using them in Base64Util.
2018-06-18 14:56:51 -04:00
}
/**
* Request connection to the device.
* If the web socket is not yet open, request when the socket promise resolves.
*/
requestDevice () {
if (this._ws.readyState === 1) { // is this needed since it's only called on ws.onopen?
// TODO: start a 'discover' timeout
this.sendRemoteRequest('discover', this._deviceOptions)
.catch(e => this._sendError(e)); // never reached?
}
// TODO: else?
MicroBit extension, Scratch Link first draft. (#1230) * First microbit gui tests * Fixed JSONRPC inheritance. Renamed ScratchBLE/ScratchBT files. Removed ScratchBT test code from Microbit extension. Renamed addLine to log. * Fixed log comments. Removed addLine from Microbit. * Adding auto-connect to Microbit at extension loading. Adding hack for displayText block to Scratch-Link. * Resolved merge conflicts and brought in latest microbit extension example code. * Updated microbit write tests for displayText and displaySymbol blocks. Some linting. * Some linting and adding of BLE Characteristic consts. * Linting fixes. * Moving micro:bit device connection code all to the MicroBit class, decoupling Scratch3MicroBitBlocks from connection code. * Removing old disconenct handlers from MicroBit class. Moved service into new BLEUUID data structure. * Renamed _write to _send. Moved all BLE encoding concerns to the _send method. * Using the util log. Some linting. * Added _read method to MicroBit class. Renamed _send to _write. * Some linting and formatting comments. * First pass at peripheral chooser pattern for ScratchBLE. * Testing characteristicDidChange events, and some changes to ScratchBLE on ready events. * Refactoring work on PeripheralChooser and ScratchBLE. * Some variable renaming and method signature stubs. * Peripheral chooser method signatures. * Moved base64 encoding/decoding to util. Some method signature formatting. * Adding test stubs for new util and io classes. * Adding test stub for MicroBit extension. * Clean up for PR. * Clean up for PR. * Final cleanup for PR. * Removed logging to console. * Adding 'btoa' and 'atob' node modules and using them in Base64Util.
2018-06-18 14:56:51 -04:00
}
/**
* Try connecting to the input peripheral id, and then call the connect
* callback if connection is successful.
* @param {number} id - the id of the peripheral to connect to
*/
connectDevice (id) {
this.sendRemoteRequest('connect', {peripheralId: id})
.then(() => {
this._connected = true;
this._runtime.emit(this._runtime.constructor.PERIPHERAL_CONNECTED);
this._connectCallback();
})
.catch(e => {
this._sendError(e);
});
MicroBit extension, Scratch Link first draft. (#1230) * First microbit gui tests * Fixed JSONRPC inheritance. Renamed ScratchBLE/ScratchBT files. Removed ScratchBT test code from Microbit extension. Renamed addLine to log. * Fixed log comments. Removed addLine from Microbit. * Adding auto-connect to Microbit at extension loading. Adding hack for displayText block to Scratch-Link. * Resolved merge conflicts and brought in latest microbit extension example code. * Updated microbit write tests for displayText and displaySymbol blocks. Some linting. * Some linting and adding of BLE Characteristic consts. * Linting fixes. * Moving micro:bit device connection code all to the MicroBit class, decoupling Scratch3MicroBitBlocks from connection code. * Removing old disconenct handlers from MicroBit class. Moved service into new BLEUUID data structure. * Renamed _write to _send. Moved all BLE encoding concerns to the _send method. * Using the util log. Some linting. * Added _read method to MicroBit class. Renamed _send to _write. * Some linting and formatting comments. * First pass at peripheral chooser pattern for ScratchBLE. * Testing characteristicDidChange events, and some changes to ScratchBLE on ready events. * Refactoring work on PeripheralChooser and ScratchBLE. * Some variable renaming and method signature stubs. * Peripheral chooser method signatures. * Moved base64 encoding/decoding to util. Some method signature formatting. * Adding test stubs for new util and io classes. * Adding test stub for MicroBit extension. * Clean up for PR. * Clean up for PR. * Final cleanup for PR. * Removed logging to console. * Adding 'btoa' and 'atob' node modules and using them in Base64Util.
2018-06-18 14:56:51 -04:00
}
/**
* Close the websocket.
*/
disconnectSession () {
this._ws.close();
this._connected = false;
}
/**
* @return {bool} whether the peripheral is connected.
*/
getPeripheralIsConnected () {
return this._connected;
}
MicroBit extension, Scratch Link first draft. (#1230) * First microbit gui tests * Fixed JSONRPC inheritance. Renamed ScratchBLE/ScratchBT files. Removed ScratchBT test code from Microbit extension. Renamed addLine to log. * Fixed log comments. Removed addLine from Microbit. * Adding auto-connect to Microbit at extension loading. Adding hack for displayText block to Scratch-Link. * Resolved merge conflicts and brought in latest microbit extension example code. * Updated microbit write tests for displayText and displaySymbol blocks. Some linting. * Some linting and adding of BLE Characteristic consts. * Linting fixes. * Moving micro:bit device connection code all to the MicroBit class, decoupling Scratch3MicroBitBlocks from connection code. * Removing old disconenct handlers from MicroBit class. Moved service into new BLEUUID data structure. * Renamed _write to _send. Moved all BLE encoding concerns to the _send method. * Using the util log. Some linting. * Added _read method to MicroBit class. Renamed _send to _write. * Some linting and formatting comments. * First pass at peripheral chooser pattern for ScratchBLE. * Testing characteristicDidChange events, and some changes to ScratchBLE on ready events. * Refactoring work on PeripheralChooser and ScratchBLE. * Some variable renaming and method signature stubs. * Peripheral chooser method signatures. * Moved base64 encoding/decoding to util. Some method signature formatting. * Adding test stubs for new util and io classes. * Adding test stub for MicroBit extension. * Clean up for PR. * Clean up for PR. * Final cleanup for PR. * Removed logging to console. * Adding 'btoa' and 'atob' node modules and using them in Base64Util.
2018-06-18 14:56:51 -04:00
sendMessage (options) {
return this.sendRemoteRequest('send', options);
}
/**
* Handle a received call from the socket.
* @param {string} method - a received method label.
* @param {object} params - a received list of parameters.
* @return {object} - optional return value.
*/
didReceiveCall (method, params) {
MicroBit extension, Scratch Link first draft. (#1230) * First microbit gui tests * Fixed JSONRPC inheritance. Renamed ScratchBLE/ScratchBT files. Removed ScratchBT test code from Microbit extension. Renamed addLine to log. * Fixed log comments. Removed addLine from Microbit. * Adding auto-connect to Microbit at extension loading. Adding hack for displayText block to Scratch-Link. * Resolved merge conflicts and brought in latest microbit extension example code. * Updated microbit write tests for displayText and displaySymbol blocks. Some linting. * Some linting and adding of BLE Characteristic consts. * Linting fixes. * Moving micro:bit device connection code all to the MicroBit class, decoupling Scratch3MicroBitBlocks from connection code. * Removing old disconenct handlers from MicroBit class. Moved service into new BLEUUID data structure. * Renamed _write to _send. Moved all BLE encoding concerns to the _send method. * Using the util log. Some linting. * Added _read method to MicroBit class. Renamed _send to _write. * Some linting and formatting comments. * First pass at peripheral chooser pattern for ScratchBLE. * Testing characteristicDidChange events, and some changes to ScratchBLE on ready events. * Refactoring work on PeripheralChooser and ScratchBLE. * Some variable renaming and method signature stubs. * Peripheral chooser method signatures. * Moved base64 encoding/decoding to util. Some method signature formatting. * Adding test stubs for new util and io classes. * Adding test stub for MicroBit extension. * Clean up for PR. * Clean up for PR. * Final cleanup for PR. * Removed logging to console. * Adding 'btoa' and 'atob' node modules and using them in Base64Util.
2018-06-18 14:56:51 -04:00
// TODO: Add peripheral 'undiscover' handling
switch (method) {
case 'didDiscoverPeripheral':
this._availablePeripherals[params.peripheralId] = params;
this._runtime.emit(
this._runtime.constructor.PERIPHERAL_LIST_UPDATE,
this._availablePeripherals
);
// TODO: cancel a discover timeout if one is active
MicroBit extension, Scratch Link first draft. (#1230) * First microbit gui tests * Fixed JSONRPC inheritance. Renamed ScratchBLE/ScratchBT files. Removed ScratchBT test code from Microbit extension. Renamed addLine to log. * Fixed log comments. Removed addLine from Microbit. * Adding auto-connect to Microbit at extension loading. Adding hack for displayText block to Scratch-Link. * Resolved merge conflicts and brought in latest microbit extension example code. * Updated microbit write tests for displayText and displaySymbol blocks. Some linting. * Some linting and adding of BLE Characteristic consts. * Linting fixes. * Moving micro:bit device connection code all to the MicroBit class, decoupling Scratch3MicroBitBlocks from connection code. * Removing old disconenct handlers from MicroBit class. Moved service into new BLEUUID data structure. * Renamed _write to _send. Moved all BLE encoding concerns to the _send method. * Using the util log. Some linting. * Added _read method to MicroBit class. Renamed _send to _write. * Some linting and formatting comments. * First pass at peripheral chooser pattern for ScratchBLE. * Testing characteristicDidChange events, and some changes to ScratchBLE on ready events. * Refactoring work on PeripheralChooser and ScratchBLE. * Some variable renaming and method signature stubs. * Peripheral chooser method signatures. * Moved base64 encoding/decoding to util. Some method signature formatting. * Adding test stubs for new util and io classes. * Adding test stub for MicroBit extension. * Clean up for PR. * Clean up for PR. * Final cleanup for PR. * Removed logging to console. * Adding 'btoa' and 'atob' node modules and using them in Base64Util.
2018-06-18 14:56:51 -04:00
break;
case 'didReceiveMessage':
this._messageCallback(params); // TODO: refine?
MicroBit extension, Scratch Link first draft. (#1230) * First microbit gui tests * Fixed JSONRPC inheritance. Renamed ScratchBLE/ScratchBT files. Removed ScratchBT test code from Microbit extension. Renamed addLine to log. * Fixed log comments. Removed addLine from Microbit. * Adding auto-connect to Microbit at extension loading. Adding hack for displayText block to Scratch-Link. * Resolved merge conflicts and brought in latest microbit extension example code. * Updated microbit write tests for displayText and displaySymbol blocks. Some linting. * Some linting and adding of BLE Characteristic consts. * Linting fixes. * Moving micro:bit device connection code all to the MicroBit class, decoupling Scratch3MicroBitBlocks from connection code. * Removing old disconenct handlers from MicroBit class. Moved service into new BLEUUID data structure. * Renamed _write to _send. Moved all BLE encoding concerns to the _send method. * Using the util log. Some linting. * Added _read method to MicroBit class. Renamed _send to _write. * Some linting and formatting comments. * First pass at peripheral chooser pattern for ScratchBLE. * Testing characteristicDidChange events, and some changes to ScratchBLE on ready events. * Refactoring work on PeripheralChooser and ScratchBLE. * Some variable renaming and method signature stubs. * Peripheral chooser method signatures. * Moved base64 encoding/decoding to util. Some method signature formatting. * Adding test stubs for new util and io classes. * Adding test stub for MicroBit extension. * Clean up for PR. * Clean up for PR. * Final cleanup for PR. * Removed logging to console. * Adding 'btoa' and 'atob' node modules and using them in Base64Util.
2018-06-18 14:56:51 -04:00
break;
default:
return 'nah';
}
}
_sendError (e) {
2018-07-02 10:35:50 -04:00
log.error(`BTSession error: ${JSON.stringify(e)}`);
this._runtime.emit(this._runtime.constructor.PERIPHERAL_ERROR);
}
MicroBit extension, Scratch Link first draft. (#1230) * First microbit gui tests * Fixed JSONRPC inheritance. Renamed ScratchBLE/ScratchBT files. Removed ScratchBT test code from Microbit extension. Renamed addLine to log. * Fixed log comments. Removed addLine from Microbit. * Adding auto-connect to Microbit at extension loading. Adding hack for displayText block to Scratch-Link. * Resolved merge conflicts and brought in latest microbit extension example code. * Updated microbit write tests for displayText and displaySymbol blocks. Some linting. * Some linting and adding of BLE Characteristic consts. * Linting fixes. * Moving micro:bit device connection code all to the MicroBit class, decoupling Scratch3MicroBitBlocks from connection code. * Removing old disconenct handlers from MicroBit class. Moved service into new BLEUUID data structure. * Renamed _write to _send. Moved all BLE encoding concerns to the _send method. * Using the util log. Some linting. * Added _read method to MicroBit class. Renamed _send to _write. * Some linting and formatting comments. * First pass at peripheral chooser pattern for ScratchBLE. * Testing characteristicDidChange events, and some changes to ScratchBLE on ready events. * Refactoring work on PeripheralChooser and ScratchBLE. * Some variable renaming and method signature stubs. * Peripheral chooser method signatures. * Moved base64 encoding/decoding to util. Some method signature formatting. * Adding test stubs for new util and io classes. * Adding test stub for MicroBit extension. * Clean up for PR. * Clean up for PR. * Final cleanup for PR. * Removed logging to console. * Adding 'btoa' and 'atob' node modules and using them in Base64Util.
2018-06-18 14:56:51 -04:00
}
module.exports = BTSession;