add ability to reboot device

This commit is contained in:
liamcottle 2025-02-16 16:57:08 +13:00
parent 77803c5fca
commit 8125faaccf
3 changed files with 46 additions and 0 deletions

View file

@ -98,6 +98,9 @@
<button @click="exportContact(null)" class="border border-gray-500 px-2 bg-gray-100 hover:bg-gray-200 rounded">
ExportContact
</button>
<button @click="reboot" class="border border-gray-500 px-2 bg-gray-100 hover:bg-gray-200 rounded">
Reboot
</button>
</div>
</div>
@ -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('');
},

View file

@ -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;

View file

@ -30,6 +30,7 @@ class Constants {
ShareContact: 16,
ExportContact: 17,
ImportContact: 18,
Reboot: 19,
}
static ResponseCodes = {