add helper function for getting device time

This commit is contained in:
liamcottle 2025-02-15 22:18:39 +13:00
parent e2d2ab7cf8
commit 6e4696679e
2 changed files with 33 additions and 1 deletions

View file

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

View file

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