From 8125faaccfcc111d669d6ce0fe4f03e3278e421b Mon Sep 17 00:00:00 2001 From: liamcottle Date: Sun, 16 Feb 2025 16:57:08 +1300 Subject: [PATCH] add ability to reboot device --- index.html | 11 +++++++++++ src/connection/connection.js | 34 ++++++++++++++++++++++++++++++++++ src/constants.js | 1 + 3 files changed, 46 insertions(+) diff --git a/index.html b/index.html index a6a2013..3dea23a 100644 --- a/index.html +++ b/index.html @@ -98,6 +98,9 @@ + @@ -382,6 +385,14 @@ 0xd2, ]); }, + async reboot() { + try { + await this.connection.reboot(); + alert("Device is rebooting!"); + } catch(e) { + alert("Failed to reboot!"); + } + }, 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 a34650f..1c6e74d 100644 --- a/src/connection/connection.js +++ b/src/connection/connection.js @@ -172,6 +172,12 @@ class Connection extends EventEmitter { await this.sendToRadioFrame(data.toBytes()); } + async sendCommandReboot() { + const data = new BufferWriter(); + data.writeByte(Constants.CommandCodes.Reboot); + await this.sendToRadioFrame(data.toBytes()); + } + onFrameReceived(frame) { // emit received frame @@ -938,6 +944,34 @@ class Connection extends EventEmitter { }); } + reboot() { + return new Promise(async (resolve, reject) => { + try { + + // reject promise when we receive err + const onErr = () => { + this.off(Constants.ResponseCodes.Err, onErr); + reject(); + } + + // assume device rebooted after a short delay + setTimeout(() => { + this.off(Constants.ResponseCodes.Err, onErr); + resolve(); + }, 1000); + + // listen for events + this.once(Constants.ResponseCodes.Err, onErr); + + // reboot + await this.sendCommandReboot(); + + } catch(e) { + reject(e); + } + }); + } + } export default Connection; diff --git a/src/constants.js b/src/constants.js index 783c657..9fd2ce9 100644 --- a/src/constants.js +++ b/src/constants.js @@ -30,6 +30,7 @@ class Constants { ShareContact: 16, ExportContact: 17, ImportContact: 18, + Reboot: 19, } static ResponseCodes = {