mirror of
https://github.com/meshcore-dev/meshcore.js.git
synced 2026-04-20 22:13:49 +00:00
get channels
This commit is contained in:
parent
19cbbb9206
commit
9e6f42f0bc
4 changed files with 107 additions and 2 deletions
|
|
@ -231,6 +231,13 @@ class Connection extends EventEmitter {
|
|||
await this.sendToRadioFrame(data.toBytes());
|
||||
}
|
||||
|
||||
async sendCommandGetChannel(channelIdx) {
|
||||
const data = new BufferWriter();
|
||||
data.writeByte(Constants.CommandCodes.GetChannel);
|
||||
data.writeByte(channelIdx);
|
||||
await this.sendToRadioFrame(data.toBytes());
|
||||
}
|
||||
|
||||
onFrameReceived(frame) {
|
||||
|
||||
// emit received frame
|
||||
|
|
@ -271,6 +278,8 @@ class Connection extends EventEmitter {
|
|||
this.onPrivateKeyResponse(bufferReader);
|
||||
} else if(responseCode === Constants.ResponseCodes.Disabled){
|
||||
this.onDisabledResponse(bufferReader);
|
||||
} else if(responseCode === Constants.ResponseCodes.ChannelInfo){
|
||||
this.onChannelInfoResponse(bufferReader);
|
||||
} else if(responseCode === Constants.PushCodes.Advert){
|
||||
this.onAdvertPush(bufferReader);
|
||||
} else if(responseCode === Constants.PushCodes.PathUpdated){
|
||||
|
|
@ -430,6 +439,23 @@ class Connection extends EventEmitter {
|
|||
});
|
||||
}
|
||||
|
||||
onChannelInfoResponse(bufferReader) {
|
||||
|
||||
const idx = bufferReader.readUInt8();
|
||||
const remainingBytesLength = bufferReader.getRemainingBytesCount();
|
||||
|
||||
// 128-bit keys
|
||||
if(remainingBytesLength === 16){
|
||||
this.emit(Constants.ResponseCodes.ChannelInfo, {
|
||||
channelIdx: idx,
|
||||
secret: bufferReader.readBytes(remainingBytesLength),
|
||||
});
|
||||
} else {
|
||||
console.log(`ChannelInfo has unexpected key length: ${remainingBytesLength}`);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
onSelfInfoResponse(bufferReader) {
|
||||
this.emit(Constants.ResponseCodes.SelfInfo, {
|
||||
type: bufferReader.readByte(),
|
||||
|
|
@ -1504,6 +1530,62 @@ class Connection extends EventEmitter {
|
|||
});
|
||||
}
|
||||
|
||||
getChannel(channelIdx) {
|
||||
return new Promise(async (resolve, reject) => {
|
||||
try {
|
||||
|
||||
// resolve promise when we receive channel info response
|
||||
const onChannelInfoResponse = (response) => {
|
||||
this.off(Constants.ResponseCodes.ChannelInfo, onChannelInfoResponse);
|
||||
this.off(Constants.ResponseCodes.Err, onErr);
|
||||
resolve(response);
|
||||
}
|
||||
|
||||
// reject promise when we receive err
|
||||
const onErr = () => {
|
||||
this.off(Constants.ResponseCodes.ChannelInfo, onChannelInfoResponse);
|
||||
this.off(Constants.ResponseCodes.Err, onErr);
|
||||
reject();
|
||||
}
|
||||
|
||||
// listen for events
|
||||
this.once(Constants.ResponseCodes.ChannelInfo, onChannelInfoResponse);
|
||||
this.once(Constants.ResponseCodes.Err, onErr);
|
||||
|
||||
// get channel
|
||||
await this.sendCommandGetChannel(channelIdx);
|
||||
|
||||
} catch(e) {
|
||||
reject(e);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
getChannels() {
|
||||
return new Promise(async (resolve, reject) => {
|
||||
|
||||
// add channels we receive to a list
|
||||
var channelIdx = 0;
|
||||
const channels = [];
|
||||
while(true){
|
||||
|
||||
// try to get next channel
|
||||
try {
|
||||
const channel = await this.getChannel(channelIdx);
|
||||
channels.push(channel);
|
||||
} catch(e){
|
||||
break;
|
||||
}
|
||||
|
||||
channelIdx++;
|
||||
|
||||
}
|
||||
|
||||
return resolve(channels);
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export default Connection;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue