scratch-vm/src/io/btSession.js

37 lines
963 B
JavaScript
Raw Normal View History

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 JSONRPCWebSocket = require('../util/jsonrpc');
const ScratchLinkWebSocket = 'ws://localhost:20110/scratch/bt';
class BTSession extends JSONRPCWebSocket {
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
constructor () {
super(new WebSocket(ScratchLinkWebSocket));
}
requestDevice (options) {
return this.sendRemoteRequest('discover', options);
}
connectDevice (options) {
return this.sendRemoteRequest('connect', options);
}
sendMessage (options) {
return this.sendRemoteRequest('send', options);
}
didReceiveCall (method /* , params */) {
// TODO: Add peripheral 'undiscover' handling
switch (method) {
case 'didDiscoverPeripheral':
// TODO: do something on peripheral discovered
break;
case 'didReceiveMessage':
// TODO: do something on received message
break;
default:
return 'nah';
}
}
}
module.exports = BTSession;