diff --git a/index.html b/index.html index f6260e8..dd92845 100644 --- a/index.html +++ b/index.html @@ -65,28 +65,28 @@ Advert (Flood) - + SetAdvertName - + SetAdvertLatLon AddUpdateContact - + SyncNextMessage - + GetDeviceTime - + SetDeviceTime - + SetRadioParams - + SetTxPower @@ -245,19 +245,19 @@ async sendFloodAdvert() { await this.connection.sendFloodAdvert(); }, - async sendCommandGetContacts() { + async getContacts() { this.contacts = await this.connection.getContacts(); console.log(this.contacts); }, - async sendCommandGetDeviceTime() { + async getDeviceTime() { const deviceTime = await this.connection.getDeviceTime(); console.log(deviceTime); }, - async sendCommandSetDeviceTime() { + async setDeviceTime() { const timestamp = Math.floor(Date.now() / 1000); await this.connection.setDeviceTime(timestamp); }, - async sendCommandSetTxPower() { + async setTxPower() { // ask user for tx power const txPowerString = prompt("Please enter TX power in dBm"); @@ -270,14 +270,14 @@ await this.connection.setTxPower(txPower); }, - async sendCommandSetRadioParams() { + async setRadioParams() { const radioFreq = 917375; const radioBw = 250000; const radioSf = 7; const radioCr = 5; await this.connection.setRadioParams(radioFreq, radioBw, radioSf, radioCr); }, - async sendCommandSetAdvertName() { + async setAdvertName() { // ask user for name const name = prompt("Please enter name"); @@ -286,10 +286,10 @@ } // set name - await this.connection.sendCommandSetAdvertName(name); + await this.connection.setAdvertName(name); }, - async sendCommandSetAdvertLatLon() { + async setAdvertLatLon() { const lat = Math.floor(-38.661727955271765 * 1000000); const lon = Math.floor(178.0236810462527 * 1000000); console.log(lat, lon); @@ -307,7 +307,7 @@ const advLon = 0; await this.connection.sendCommandAddUpdateContact(publicKey, type, flags, outPathLen, outPath, advName, lastAdvert, advLat, advLon); }, - async sendCommandSyncNextMessage() { + async syncNextMessage() { const message = await this.connection.syncNextMessage(); console.log("syncNextMessage", message); }, @@ -389,7 +389,7 @@ contact.outPath = outPath; // update contact - await this.connection.sendCommandAddUpdateContact(contact.publicKey, contact.type, contact.flags, contact.outPathLen, contact.outPath, contact.advName, contact.lastAdvert, contact.advLat, contact.advLon); + await this.connection.addOrUpdateContact(contact.publicKey, contact.type, contact.flags, contact.outPathLen, contact.outPath, contact.advName, contact.lastAdvert, contact.advLat, contact.advLon); }, bytesToHex(uint8Array) { diff --git a/src/connection/connection.js b/src/connection/connection.js index 46aaabf..6e781bf 100644 --- a/src/connection/connection.js +++ b/src/connection/connection.js @@ -850,6 +850,37 @@ class Connection extends EventEmitter { }); } + addOrUpdateContact(publicKey, type, flags, outPathLen, outPath, advName, lastAdvert, advLat, advLon) { + return new Promise(async (resolve, reject) => { + try { + + // resolve promise when we receive ok + const onOk = () => { + this.off(Constants.ResponseCodes.Ok, onOk); + this.off(Constants.ResponseCodes.Err, onErr); + resolve(); + } + + // 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); + + // add or update contact + await this.sendCommandAddUpdateContact(publicKey, type, flags, outPathLen, outPath, advName, lastAdvert, advLat, advLon); + + } catch(e) { + reject(e); + } + }); + } + resetPath(pubKey) { return new Promise(async (resolve, reject) => { try {