2018-06-25 13:42:48 -04:00
|
|
|
const JSONRPCWebSocket = require('../util/jsonrpc-web-socket');
|
|
|
|
const log = require('../util/log');
|
2018-07-17 15:40:47 -04:00
|
|
|
const ScratchLinkWebSocket = 'wss://device-manager.scratch.mit.edu:20110/scratch/bt';
|
2018-06-18 14:56:51 -04:00
|
|
|
|
2018-06-20 13:59:23 -04:00
|
|
|
class BTSession extends JSONRPCWebSocket {
|
2018-06-25 13:42:48 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
2018-06-25 13:42:48 -04:00
|
|
|
*/
|
2018-06-26 15:07:08 -04:00
|
|
|
constructor (runtime, deviceOptions, connectCallback, messageCallback) {
|
2018-06-25 13:42:48 -04:00
|
|
|
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;
|
2018-06-26 15:07:08 -04:00
|
|
|
this._messageCallback = messageCallback;
|
2018-06-25 13:42:48 -04:00
|
|
|
this._runtime = runtime;
|
2018-06-27 14:21:11 -04:00
|
|
|
|
|
|
|
this._connected = false;
|
2018-06-18 14:56:51 -04:00
|
|
|
}
|
|
|
|
|
2018-06-25 13:42:48 -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?
|
2018-06-18 14:56:51 -04:00
|
|
|
}
|
|
|
|
|
2018-06-25 13:42:48 -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(() => {
|
2018-06-27 14:21:11 -04:00
|
|
|
this._connected = true;
|
2018-07-10 15:57:25 -04:00
|
|
|
this._runtime.emit(this._runtime.constructor.PERIPHERAL_CONNECTED);
|
2018-06-25 13:42:48 -04:00
|
|
|
this._connectCallback();
|
|
|
|
})
|
|
|
|
.catch(e => {
|
|
|
|
this._sendError(e);
|
|
|
|
});
|
2018-06-18 14:56:51 -04:00
|
|
|
}
|
|
|
|
|
2018-06-27 14:21:11 -04:00
|
|
|
/**
|
|
|
|
* Close the websocket.
|
|
|
|
*/
|
|
|
|
disconnectSession () {
|
|
|
|
this._ws.close();
|
|
|
|
this._connected = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return {bool} whether the peripheral is connected.
|
|
|
|
*/
|
|
|
|
getPeripheralIsConnected () {
|
|
|
|
return this._connected;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-06-18 14:56:51 -04:00
|
|
|
sendMessage (options) {
|
|
|
|
return this.sendRemoteRequest('send', options);
|
|
|
|
}
|
|
|
|
|
2018-06-25 13:42:48 -04:00
|
|
|
/**
|
|
|
|
* 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) {
|
2018-06-18 14:56:51 -04:00
|
|
|
// TODO: Add peripheral 'undiscover' handling
|
|
|
|
switch (method) {
|
|
|
|
case 'didDiscoverPeripheral':
|
2018-06-25 18:24:23 -04:00
|
|
|
this._availablePeripherals[params.peripheralId] = params;
|
2018-06-25 13:42:48 -04:00
|
|
|
this._runtime.emit(
|
|
|
|
this._runtime.constructor.PERIPHERAL_LIST_UPDATE,
|
|
|
|
this._availablePeripherals
|
2018-06-25 18:24:23 -04:00
|
|
|
);
|
2018-06-25 13:42:48 -04:00
|
|
|
// TODO: cancel a discover timeout if one is active
|
2018-06-18 14:56:51 -04:00
|
|
|
break;
|
|
|
|
case 'didReceiveMessage':
|
2018-06-26 15:07:08 -04:00
|
|
|
this._messageCallback(params); // TODO: refine?
|
2018-06-18 14:56:51 -04:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return 'nah';
|
|
|
|
}
|
|
|
|
}
|
2018-06-25 13:42:48 -04:00
|
|
|
|
|
|
|
_sendError (e) {
|
2018-07-02 10:35:50 -04:00
|
|
|
log.error(`BTSession error: ${JSON.stringify(e)}`);
|
2018-06-25 13:42:48 -04:00
|
|
|
this._runtime.emit(this._runtime.constructor.PERIPHERAL_ERROR);
|
|
|
|
}
|
2018-06-18 14:56:51 -04:00
|
|
|
}
|
|
|
|
|
2018-06-20 13:59:23 -04:00
|
|
|
module.exports = BTSession;
|