mirror of
https://github.com/meshcore-dev/meshcore.js.git
synced 2026-04-20 22:13:49 +00:00
add helper function for adding or updating contact
This commit is contained in:
parent
3040bb54a9
commit
2aeb37ffa8
2 changed files with 48 additions and 17 deletions
34
index.html
34
index.html
|
|
@ -65,28 +65,28 @@
|
||||||
<button @click="sendFloodAdvert" class="border border-gray-500 px-2 bg-gray-100 hover:bg-gray-200 rounded">
|
<button @click="sendFloodAdvert" class="border border-gray-500 px-2 bg-gray-100 hover:bg-gray-200 rounded">
|
||||||
Advert (Flood)
|
Advert (Flood)
|
||||||
</button>
|
</button>
|
||||||
<button @click="sendCommandSetAdvertName" class="border border-gray-500 px-2 bg-gray-100 hover:bg-gray-200 rounded">
|
<button @click="setAdvertName" class="border border-gray-500 px-2 bg-gray-100 hover:bg-gray-200 rounded">
|
||||||
SetAdvertName
|
SetAdvertName
|
||||||
</button>
|
</button>
|
||||||
<button @click="sendCommandSetAdvertLatLon" class="border border-gray-500 px-2 bg-gray-100 hover:bg-gray-200 rounded">
|
<button @click="setAdvertLatLon" class="border border-gray-500 px-2 bg-gray-100 hover:bg-gray-200 rounded">
|
||||||
SetAdvertLatLon
|
SetAdvertLatLon
|
||||||
</button>
|
</button>
|
||||||
<button @click="sendCommandAddUpdateContact" class="border border-gray-500 px-2 bg-gray-100 hover:bg-gray-200 rounded">
|
<button @click="sendCommandAddUpdateContact" class="border border-gray-500 px-2 bg-gray-100 hover:bg-gray-200 rounded">
|
||||||
AddUpdateContact
|
AddUpdateContact
|
||||||
</button>
|
</button>
|
||||||
<button @click="sendCommandSyncNextMessage" class="border border-gray-500 px-2 bg-gray-100 hover:bg-gray-200 rounded">
|
<button @click="syncNextMessage" class="border border-gray-500 px-2 bg-gray-100 hover:bg-gray-200 rounded">
|
||||||
SyncNextMessage
|
SyncNextMessage
|
||||||
</button>
|
</button>
|
||||||
<button @click="sendCommandGetDeviceTime" class="border border-gray-500 px-2 bg-gray-100 hover:bg-gray-200 rounded">
|
<button @click="getDeviceTime" class="border border-gray-500 px-2 bg-gray-100 hover:bg-gray-200 rounded">
|
||||||
GetDeviceTime
|
GetDeviceTime
|
||||||
</button>
|
</button>
|
||||||
<button @click="sendCommandSetDeviceTime" class="border border-gray-500 px-2 bg-gray-100 hover:bg-gray-200 rounded">
|
<button @click="setDeviceTime" class="border border-gray-500 px-2 bg-gray-100 hover:bg-gray-200 rounded">
|
||||||
SetDeviceTime
|
SetDeviceTime
|
||||||
</button>
|
</button>
|
||||||
<button @click="sendCommandSetRadioParams" class="border border-gray-500 px-2 bg-gray-100 hover:bg-gray-200 rounded">
|
<button @click="setRadioParams" class="border border-gray-500 px-2 bg-gray-100 hover:bg-gray-200 rounded">
|
||||||
SetRadioParams
|
SetRadioParams
|
||||||
</button>
|
</button>
|
||||||
<button @click="sendCommandSetTxPower" class="border border-gray-500 px-2 bg-gray-100 hover:bg-gray-200 rounded">
|
<button @click="setTxPower" class="border border-gray-500 px-2 bg-gray-100 hover:bg-gray-200 rounded">
|
||||||
SetTxPower
|
SetTxPower
|
||||||
</button>
|
</button>
|
||||||
<button @click="sendChannelTextMessage" class="border border-gray-500 px-2 bg-gray-100 hover:bg-gray-200 rounded">
|
<button @click="sendChannelTextMessage" class="border border-gray-500 px-2 bg-gray-100 hover:bg-gray-200 rounded">
|
||||||
|
|
@ -245,19 +245,19 @@
|
||||||
async sendFloodAdvert() {
|
async sendFloodAdvert() {
|
||||||
await this.connection.sendFloodAdvert();
|
await this.connection.sendFloodAdvert();
|
||||||
},
|
},
|
||||||
async sendCommandGetContacts() {
|
async getContacts() {
|
||||||
this.contacts = await this.connection.getContacts();
|
this.contacts = await this.connection.getContacts();
|
||||||
console.log(this.contacts);
|
console.log(this.contacts);
|
||||||
},
|
},
|
||||||
async sendCommandGetDeviceTime() {
|
async getDeviceTime() {
|
||||||
const deviceTime = await this.connection.getDeviceTime();
|
const deviceTime = await this.connection.getDeviceTime();
|
||||||
console.log(deviceTime);
|
console.log(deviceTime);
|
||||||
},
|
},
|
||||||
async sendCommandSetDeviceTime() {
|
async setDeviceTime() {
|
||||||
const timestamp = Math.floor(Date.now() / 1000);
|
const timestamp = Math.floor(Date.now() / 1000);
|
||||||
await this.connection.setDeviceTime(timestamp);
|
await this.connection.setDeviceTime(timestamp);
|
||||||
},
|
},
|
||||||
async sendCommandSetTxPower() {
|
async setTxPower() {
|
||||||
|
|
||||||
// ask user for tx power
|
// ask user for tx power
|
||||||
const txPowerString = prompt("Please enter TX power in dBm");
|
const txPowerString = prompt("Please enter TX power in dBm");
|
||||||
|
|
@ -270,14 +270,14 @@
|
||||||
await this.connection.setTxPower(txPower);
|
await this.connection.setTxPower(txPower);
|
||||||
|
|
||||||
},
|
},
|
||||||
async sendCommandSetRadioParams() {
|
async setRadioParams() {
|
||||||
const radioFreq = 917375;
|
const radioFreq = 917375;
|
||||||
const radioBw = 250000;
|
const radioBw = 250000;
|
||||||
const radioSf = 7;
|
const radioSf = 7;
|
||||||
const radioCr = 5;
|
const radioCr = 5;
|
||||||
await this.connection.setRadioParams(radioFreq, radioBw, radioSf, radioCr);
|
await this.connection.setRadioParams(radioFreq, radioBw, radioSf, radioCr);
|
||||||
},
|
},
|
||||||
async sendCommandSetAdvertName() {
|
async setAdvertName() {
|
||||||
|
|
||||||
// ask user for name
|
// ask user for name
|
||||||
const name = prompt("Please enter name");
|
const name = prompt("Please enter name");
|
||||||
|
|
@ -286,10 +286,10 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
// set name
|
// set name
|
||||||
await this.connection.sendCommandSetAdvertName(name);
|
await this.connection.setAdvertName(name);
|
||||||
|
|
||||||
},
|
},
|
||||||
async sendCommandSetAdvertLatLon() {
|
async setAdvertLatLon() {
|
||||||
const lat = Math.floor(-38.661727955271765 * 1000000);
|
const lat = Math.floor(-38.661727955271765 * 1000000);
|
||||||
const lon = Math.floor(178.0236810462527 * 1000000);
|
const lon = Math.floor(178.0236810462527 * 1000000);
|
||||||
console.log(lat, lon);
|
console.log(lat, lon);
|
||||||
|
|
@ -307,7 +307,7 @@
|
||||||
const advLon = 0;
|
const advLon = 0;
|
||||||
await this.connection.sendCommandAddUpdateContact(publicKey, type, flags, outPathLen, outPath, advName, lastAdvert, advLat, advLon);
|
await this.connection.sendCommandAddUpdateContact(publicKey, type, flags, outPathLen, outPath, advName, lastAdvert, advLat, advLon);
|
||||||
},
|
},
|
||||||
async sendCommandSyncNextMessage() {
|
async syncNextMessage() {
|
||||||
const message = await this.connection.syncNextMessage();
|
const message = await this.connection.syncNextMessage();
|
||||||
console.log("syncNextMessage", message);
|
console.log("syncNextMessage", message);
|
||||||
},
|
},
|
||||||
|
|
@ -389,7 +389,7 @@
|
||||||
contact.outPath = outPath;
|
contact.outPath = outPath;
|
||||||
|
|
||||||
// update contact
|
// 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) {
|
bytesToHex(uint8Array) {
|
||||||
|
|
|
||||||
|
|
@ -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) {
|
resetPath(pubKey) {
|
||||||
return new Promise(async (resolve, reject) => {
|
return new Promise(async (resolve, reject) => {
|
||||||
try {
|
try {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue