mirror of
https://github.com/meshcore-dev/meshcore.js.git
synced 2026-04-20 22:13:49 +00:00
add helper function for getting device time
This commit is contained in:
parent
e2d2ab7cf8
commit
6e4696679e
2 changed files with 33 additions and 1 deletions
|
|
@ -250,7 +250,8 @@
|
|||
console.log(this.contacts);
|
||||
},
|
||||
async sendCommandGetDeviceTime() {
|
||||
await this.connection.sendCommandGetDeviceTime();
|
||||
const deviceTime = await this.connection.getDeviceTime();
|
||||
console.log(deviceTime);
|
||||
},
|
||||
async sendCommandSetDeviceTime() {
|
||||
const timestamp = Math.floor(Date.now() / 1000);
|
||||
|
|
|
|||
|
|
@ -664,6 +664,37 @@ class Connection extends EventEmitter {
|
|||
});
|
||||
}
|
||||
|
||||
getDeviceTime() {
|
||||
return new Promise(async (resolve, reject) => {
|
||||
try {
|
||||
|
||||
// resolve promise when we receive sent response
|
||||
const onCurrTime = (response) => {
|
||||
this.off(Constants.ResponseCodes.CurrTime, onCurrTime);
|
||||
this.off(Constants.ResponseCodes.Err, onErr);
|
||||
resolve(response);
|
||||
}
|
||||
|
||||
// reject promise when we receive err
|
||||
const onErr = () => {
|
||||
this.off(Constants.ResponseCodes.CurrTime, onCurrTime);
|
||||
this.off(Constants.ResponseCodes.Err, onErr);
|
||||
reject();
|
||||
}
|
||||
|
||||
// listen for events
|
||||
this.once(Constants.ResponseCodes.CurrTime, onCurrTime);
|
||||
this.once(Constants.ResponseCodes.Err, onErr);
|
||||
|
||||
// get device time
|
||||
await this.sendCommandGetDeviceTime();
|
||||
|
||||
} catch(e) {
|
||||
reject(e);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export default Connection;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue