diff --git a/index.html b/index.html index 3dea23a..30019e4 100644 --- a/index.html +++ b/index.html @@ -101,6 +101,9 @@ + @@ -393,6 +396,14 @@ alert("Failed to reboot!"); } }, + async getBatteryVoltage() { + try { + const response = await this.connection.getBatteryVoltage(); + alert(`Battery Voltage: ${response.batteryMilliVolts}mV`); + } catch(e) { + alert("Failed to get battery voltage!"); + } + }, bytesToHex(uint8Array) { return Array.from(uint8Array).map(byte => byte.toString(16).padStart(2, '0')).join(''); }, diff --git a/src/connection/connection.js b/src/connection/connection.js index 1c6e74d..a50e680 100644 --- a/src/connection/connection.js +++ b/src/connection/connection.js @@ -178,6 +178,12 @@ class Connection extends EventEmitter { await this.sendToRadioFrame(data.toBytes()); } + async sendCommandGetBatteryVoltage() { + const data = new BufferWriter(); + data.writeByte(Constants.CommandCodes.GetBatteryVoltage); + await this.sendToRadioFrame(data.toBytes()); + } + onFrameReceived(frame) { // emit received frame @@ -210,6 +216,8 @@ class Connection extends EventEmitter { this.onSentResponse(bufferReader); } else if(responseCode === Constants.ResponseCodes.ExportContact){ this.onExportContactResponse(bufferReader); + } else if(responseCode === Constants.ResponseCodes.BatteryVoltage){ + this.onBatteryVoltageResponse(bufferReader); } else if(responseCode === Constants.PushCodes.Advert){ this.onAdvertPush(bufferReader); } else if(responseCode === Constants.PushCodes.PathUpdated){ @@ -302,6 +310,12 @@ class Connection extends EventEmitter { }); } + onBatteryVoltageResponse(bufferReader) { + this.emit(Constants.ResponseCodes.BatteryVoltage, { + batteryMilliVolts: bufferReader.readUInt16LE(), + }); + } + onSelfInfoResponse(bufferReader) { this.emit(Constants.ResponseCodes.SelfInfo, { type: bufferReader.readByte(), @@ -972,6 +986,37 @@ class Connection extends EventEmitter { }); } + getBatteryVoltage() { + return new Promise(async (resolve, reject) => { + try { + + // resolve promise when we receive battery voltage + const onBatteryVoltage = (response) => { + this.off(Constants.ResponseCodes.BatteryVoltage, onBatteryVoltage); + this.off(Constants.ResponseCodes.Err, onErr); + resolve(response); + } + + // reject promise when we receive err + const onErr = () => { + this.off(Constants.ResponseCodes.BatteryVoltage, onBatteryVoltage); + this.off(Constants.ResponseCodes.Err, onErr); + reject(); + } + + // listen for events + this.once(Constants.ResponseCodes.BatteryVoltage, onBatteryVoltage); + this.once(Constants.ResponseCodes.Err, onErr); + + // get battery voltage + await this.sendCommandGetBatteryVoltage(); + + } catch(e) { + reject(e); + } + }); + } + } export default Connection; diff --git a/src/constants.js b/src/constants.js index 9fd2ce9..02fb0e7 100644 --- a/src/constants.js +++ b/src/constants.js @@ -31,6 +31,7 @@ class Constants { ExportContact: 17, ImportContact: 18, Reboot: 19, + GetBatteryVoltage: 20, } static ResponseCodes = { @@ -46,6 +47,7 @@ class Constants { CurrTime: 9, NoMoreMessages: 10, ExportContact: 11, + BatteryVoltage: 12, } static PushCodes = {