mirror of
https://github.com/meshcore-dev/meshcore.js.git
synced 2026-04-20 22:13:49 +00:00
add ability to reboot device
This commit is contained in:
parent
77803c5fca
commit
8125faaccf
3 changed files with 46 additions and 0 deletions
11
index.html
11
index.html
|
|
@ -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('');
|
||||
},
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -30,6 +30,7 @@ class Constants {
|
|||
ShareContact: 16,
|
||||
ExportContact: 17,
|
||||
ImportContact: 18,
|
||||
Reboot: 19,
|
||||
}
|
||||
|
||||
static ResponseCodes = {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue