mirror of
https://github.com/meshcore-dev/meshcore.js.git
synced 2026-04-20 22:13:49 +00:00
add device query command
This commit is contained in:
parent
939ca22918
commit
6dc6285cda
2 changed files with 52 additions and 0 deletions
|
|
@ -185,6 +185,13 @@ class Connection extends EventEmitter {
|
|||
await this.sendToRadioFrame(data.toBytes());
|
||||
}
|
||||
|
||||
async sendCommandDeviceQuery(appTargetVer) {
|
||||
const data = new BufferWriter();
|
||||
data.writeByte(Constants.CommandCodes.DeviceQuery);
|
||||
data.writeByte(appTargetVer); // e.g: 1
|
||||
await this.sendToRadioFrame(data.toBytes());
|
||||
}
|
||||
|
||||
onFrameReceived(frame) {
|
||||
|
||||
// emit received frame
|
||||
|
|
@ -219,6 +226,8 @@ class Connection extends EventEmitter {
|
|||
this.onExportContactResponse(bufferReader);
|
||||
} else if(responseCode === Constants.ResponseCodes.BatteryVoltage){
|
||||
this.onBatteryVoltageResponse(bufferReader);
|
||||
} else if(responseCode === Constants.ResponseCodes.DeviceInfo){
|
||||
this.onDeviceInfoResponse(bufferReader);
|
||||
} else if(responseCode === Constants.PushCodes.Advert){
|
||||
this.onAdvertPush(bufferReader);
|
||||
} else if(responseCode === Constants.PushCodes.PathUpdated){
|
||||
|
|
@ -317,6 +326,15 @@ class Connection extends EventEmitter {
|
|||
});
|
||||
}
|
||||
|
||||
onDeviceInfoResponse(bufferReader) {
|
||||
this.emit(Constants.ResponseCodes.DeviceInfo, {
|
||||
firmwareVer: bufferReader.readInt8(),
|
||||
reserved: bufferReader.readBytes(6), // reserved
|
||||
firmware_build_date: bufferReader.readCString(12), // eg. "19 Feb 2025"
|
||||
manufacturerModel: bufferReader.readString(), // remainder of frame
|
||||
});
|
||||
}
|
||||
|
||||
onSelfInfoResponse(bufferReader) {
|
||||
this.emit(Constants.ResponseCodes.SelfInfo, {
|
||||
type: bufferReader.readByte(),
|
||||
|
|
@ -1018,6 +1036,37 @@ class Connection extends EventEmitter {
|
|||
});
|
||||
}
|
||||
|
||||
deviceQuery(appTargetVer) {
|
||||
return new Promise(async (resolve, reject) => {
|
||||
try {
|
||||
|
||||
// resolve promise when we receive device info
|
||||
const onDeviceInfo = (response) => {
|
||||
this.off(Constants.ResponseCodes.DeviceInfo, onDeviceInfo);
|
||||
this.off(Constants.ResponseCodes.Err, onErr);
|
||||
resolve(response);
|
||||
}
|
||||
|
||||
// reject promise when we receive err
|
||||
const onErr = () => {
|
||||
this.off(Constants.ResponseCodes.DeviceInfo, onDeviceInfo);
|
||||
this.off(Constants.ResponseCodes.Err, onErr);
|
||||
reject();
|
||||
}
|
||||
|
||||
// listen for events
|
||||
this.once(Constants.ResponseCodes.DeviceInfo, onDeviceInfo);
|
||||
this.once(Constants.ResponseCodes.Err, onErr);
|
||||
|
||||
// query device
|
||||
await this.sendCommandDeviceQuery(appTargetVer);
|
||||
|
||||
} catch(e) {
|
||||
reject(e);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export default Connection;
|
||||
|
|
|
|||
|
|
@ -32,6 +32,8 @@ class Constants {
|
|||
ImportContact: 18,
|
||||
Reboot: 19,
|
||||
GetBatteryVoltage: 20,
|
||||
SetTuningParams: 21, // todo
|
||||
DeviceQuery: 22,
|
||||
}
|
||||
|
||||
static ResponseCodes = {
|
||||
|
|
@ -48,6 +50,7 @@ class Constants {
|
|||
NoMoreMessages: 10,
|
||||
ExportContact: 11,
|
||||
BatteryVoltage: 12,
|
||||
DeviceInfo: 13,
|
||||
}
|
||||
|
||||
static PushCodes = {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue