Adding interval to check for battery level.

This commit is contained in:
Evelyn Eastmond 2018-11-16 12:52:54 -05:00
parent b57b6c0a84
commit a177b4eeb7

View file

@ -40,11 +40,18 @@ const BLEService = {
*/
const BLECharacteristic = {
ATTACHED_IO: '00001527-1212-efde-1523-785feabcd123',
LOW_VOLTAGE_ALERT: '00001528-1212-efde-1523-785feabcd123',
INPUT_VALUES: '00001560-1212-efde-1523-785feabcd123',
INPUT_COMMAND: '00001563-1212-efde-1523-785feabcd123',
OUTPUT_COMMAND: '00001565-1212-efde-1523-785feabcd123'
};
/**
* A time interval to wait (in milliseconds) in between battery check calls.
* @type {number}
*/
const BLEBatteryCheckInterval = 5000;
/**
* A time interval to wait (in milliseconds) while a block that sends a BLE message is running.
* @type {number}
@ -421,8 +428,16 @@ class WeDo2 {
*/
this._rateLimiter = new RateLimiter(BLESendRateMax);
/**
* An interval id for the battery check interval.
* @type {number}
* @private
*/
this._batteryLevelIntervalId = null;
this._onConnect = this._onConnect.bind(this);
this._onMessage = this._onMessage.bind(this);
this._checkBatteryLevel = this._checkBatteryLevel.bind(this);
}
/**
@ -606,6 +621,12 @@ class WeDo2 {
if (this._ble) {
this._ble.disconnect();
}
if (this._batteryLevelIntervalId) {
console.log('clearing batterylevelintervalid');
window.clearInterval(this._batteryLevelIntervalId);
this._batteryLevelIntervalId = null;
}
}
/**
@ -711,6 +732,7 @@ class WeDo2 {
BLECharacteristic.ATTACHED_IO,
this._onMessage
);
this._batteryLevelIntervalId = window.setInterval(this._checkBatteryLevel, BLEBatteryCheckInterval);
}
/**
@ -757,6 +779,20 @@ class WeDo2 {
}
}
/**
* Check the battery level on the WeDo 2.0. If the WeDo 2.0 has disconnected
* for some reason, the BLE socket will get an error back and automatically
* close the socket.
*/
_checkBatteryLevel () {
this._ble.read(
BLEService.DEVICE_SERVICE,
BLECharacteristic.LOW_VOLTAGE_ALERT,
false,
this._onMessage // will ignore result
);
}
/**
* Register a new sensor or motor connected at a port. Store the type of
* sensor or motor internally, and then register for notifications on input