diff --git a/index.html b/index.html index 66c93dd..3c08bd6 100644 --- a/index.html +++ b/index.html @@ -255,7 +255,7 @@ }, async sendCommandSetDeviceTime() { const timestamp = Math.floor(Date.now() / 1000); - await this.connection.sendCommandSetDeviceTime(timestamp); + await this.connection.setDeviceTime(timestamp); }, async sendCommandSetTxPower() { diff --git a/src/connection/connection.js b/src/connection/connection.js index 636cf58..8a73b41 100644 --- a/src/connection/connection.js +++ b/src/connection/connection.js @@ -695,6 +695,37 @@ class Connection extends EventEmitter { }); } + setDeviceTime(epochSecs) { + return new Promise(async (resolve, reject) => { + try { + + // resolve promise when we receive ok + const onOk = (response) => { + this.off(Constants.ResponseCodes.Ok, onOk); + this.off(Constants.ResponseCodes.Err, onErr); + resolve(response); + } + + // reject promise when we receive err + const onErr = () => { + this.off(Constants.ResponseCodes.Ok, onOk); + this.off(Constants.ResponseCodes.Err, onErr); + reject(); + } + + // listen for events + this.once(Constants.ResponseCodes.Ok, onOk); + this.once(Constants.ResponseCodes.Err, onErr); + + // set device time + await this.sendCommandSetDeviceTime(epochSecs); + + } catch(e) { + reject(e); + } + }); + } + } export default Connection;