61 lines
2.0 KiB
JavaScript
61 lines
2.0 KiB
JavaScript
import { Transform } from 'stream';
|
|
import { Mesh, Channel, Config, ModuleConfig } from '@meshtastic/protobufs';
|
|
import { fromBinary, create } from '@bufbuild/protobuf';
|
|
|
|
export class MeshtasticStream extends Transform {
|
|
constructor(options) {
|
|
super({ readableObjectMode: true, ...options });
|
|
}
|
|
_transform(chunk, encoding, callback) {
|
|
const dataPacket = fromBinary(Mesh.FromRadioSchema, chunk);
|
|
let schema = null;
|
|
switch(dataPacket.payloadVariant.case) {
|
|
case 'packet':
|
|
schema = Mesh.MeshPacketSchema;
|
|
break;
|
|
case 'nodeInfo':
|
|
schema = Mesh.NodeInfoSchema;
|
|
break;
|
|
case 'myInfo':
|
|
schema = Mesh.MyNodeInfoSchema;
|
|
break;
|
|
case 'deviceuiConfig':
|
|
// can't find, come back to this
|
|
break;
|
|
case 'config':
|
|
// todo: config has another payloadVariant within it
|
|
schema = Config.ConfigSchema
|
|
break;
|
|
case 'moduleConfig':
|
|
// todo: config has another payloadVariant within it
|
|
schema = ModuleConfig.ModuleConfigSchema
|
|
break;
|
|
case 'fileInfo':
|
|
schema = Mesh.FileInfoSchema
|
|
break;
|
|
case 'channel':
|
|
schema = Channel.ChannelSchema
|
|
break;
|
|
case 'metadata':
|
|
schema = Mesh.DeviceMetadataSchema
|
|
break;
|
|
case 'configCompleteId':
|
|
// done sending init data
|
|
break;
|
|
}
|
|
if (schema !== null) {
|
|
this.push(create(schema, dataPacket.payloadVariant.value));
|
|
}
|
|
callback();
|
|
}
|
|
_flush(callback) {
|
|
try {
|
|
if (this._buffer) {
|
|
this.push(this._buffer.trim());
|
|
}
|
|
callback();
|
|
} catch (err) {
|
|
callback(err);
|
|
}
|
|
}
|
|
} |