Resolves 1975: Vernier gdxfor extension should disconnect on sensor timeout.

This commit is contained in:
Evelyn Eastmond 2019-02-07 16:18:56 -05:00
parent 16ebcb82b2
commit b5eb54fc44

View file

@ -25,6 +25,18 @@ const BLEUUID = {
responseChar: 'b41e6675-a329-40e0-aa01-44d2f444babe' responseChar: 'b41e6675-a329-40e0-aa01-44d2f444babe'
}; };
/**
* A time interval to wait (in milliseconds) before reporting to the BLE socket
* that data has stopped coming from the peripheral.
*/
const BLETimeout = 4500;
/**
* A string to report to the BLE socket when the GdxFor has stopped receiving data.
* @type {string}
*/
const BLEDataStoppedError = 'Force and Acceleration extension stopped receiving data';
/** /**
* Sensor ID numbers for the GDX-FOR. * Sensor ID numbers for the GDX-FOR.
*/ */
@ -140,6 +152,13 @@ class GdxFor {
spinSpeedZ: 0 spinSpeedZ: 0
}; };
/**
* Interval ID for data reading timeout.
* @type {number}
* @private
*/
this._timeoutID = null;
this.disconnect = this.disconnect.bind(this); this.disconnect = this.disconnect.bind(this);
this._onConnect = this._onConnect.bind(this); this._onConnect = this._onConnect.bind(this);
} }
@ -160,7 +179,7 @@ class GdxFor {
optionalServices: [ optionalServices: [
BLEUUID.service BLEUUID.service
] ]
}, this._onConnect); }, this._onConnect, this.disconnect);
} }
/** /**
@ -178,6 +197,7 @@ class GdxFor {
* Disconnect from the GDX FOR. * Disconnect from the GDX FOR.
*/ */
disconnect () { disconnect () {
window.clearInterval(this._timeoutID);
this._sensors = { this._sensors = {
force: 0, force: 0,
accelerationX: 0, accelerationX: 0,
@ -228,6 +248,10 @@ class GdxFor {
this._onSensorValueChanged(s); this._onSensorValueChanged(s);
}); });
}); });
this._timeoutID = window.setInterval(
() => this._scratchLinkSocket.handleDisconnectError(BLEDataStoppedError),
BLETimeout
);
}); });
// Start device // Start device
@ -266,6 +290,12 @@ class GdxFor {
this._sensors.spinSpeedZ = this._spinSpeedFromGyro(sensor.value); this._sensors.spinSpeedZ = this._spinSpeedFromGyro(sensor.value);
break; break;
} }
// cancel disconnect timeout and start a new one
window.clearInterval(this._timeoutID);
this._timeoutID = window.setInterval(
() => this._scratchLinkSocket.handleDisconnectError(BLEDataStoppedError),
BLETimeout
);
} }
_spinSpeedFromGyro (val) { _spinSpeedFromGyro (val) {