From 54a3d00d7e1f8f23949a14d6d8bd95daee4c46cf Mon Sep 17 00:00:00 2001 From: liamcottle Date: Sat, 15 Feb 2025 22:26:58 +1300 Subject: [PATCH] add helper function for importing contact --- index.html | 6 +++--- src/connection/connection.js | 31 +++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/index.html b/index.html index 3c08bd6..8bbde55 100644 --- a/index.html +++ b/index.html @@ -267,7 +267,7 @@ // update tx power const txPower = parseInt(txPowerString); - await this.connection.sendCommandSetTxPower(txPower); + await this.connection.setTxPower(txPower); }, async sendCommandSetRadioParams() { @@ -275,7 +275,7 @@ const radioBw = 250000; const radioSf = 7; const radioCr = 5; - await this.connection.sendCommandSetRadioParams(radioFreq, radioBw, radioSf, radioCr); + await this.connection.setRadioParams(radioFreq, radioBw, radioSf, radioCr); }, async sendCommandSetAdvertName() { @@ -365,7 +365,7 @@ await this.connection.sendCommandExportContact(contact?.publicKey); }, async importContact() { - await this.connection.sendCommandImportContact(this.hexToBytes("120070b78b64782bffb918da2d6432204a149bd232dd66373415b5f7ba24733ba2efe843b0674dc86210127e259f417a24150af38953dd2eb597a6a75a5c0a86adea7e81dbfc7023e4cc12bfe1b628aeaf76303f456545e555ae6554344b8391c2f07e9b010381506f636b6574204e6f64652031")); + await this.connection.importContact(this.hexToBytes("120070b78b64782bffb918da2d6432204a149bd232dd66373415b5f7ba24733ba2efe843b0674dc86210127e259f417a24150af38953dd2eb597a6a75a5c0a86adea7e81dbfc7023e4cc12bfe1b628aeaf76303f456545e555ae6554344b8391c2f07e9b010381506f636b6574204e6f64652031")); }, async setPath(contact) { diff --git a/src/connection/connection.js b/src/connection/connection.js index 8a73b41..18a7c94 100644 --- a/src/connection/connection.js +++ b/src/connection/connection.js @@ -726,6 +726,37 @@ class Connection extends EventEmitter { }); } + importContact(advertPacketBytes) { + 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); + + // import contact + await this.sendCommandImportContact(advertPacketBytes); + + } catch(e) { + reject(e); + } + }); + } + } export default Connection;