107 lines
2.9 KiB
JavaScript
107 lines
2.9 KiB
JavaScript
import { Mesh, Mqtt, Portnums, Telemetry, Config, Channel } from '@meshtastic/protobufs';
|
|
import { fromBinary, toBinary, create } from '@bufbuild/protobuf';
|
|
import { LoraStream } from './LoraStream';
|
|
import { MeshtasticStream } from './MeshtasticStream';
|
|
import net from 'net';
|
|
|
|
const client = new net.Socket();
|
|
const IP = '192.168.10.117';
|
|
const PORT = 4403;
|
|
|
|
function sendHello() {
|
|
const data = create(Mesh.ToRadioSchema, {
|
|
payloadVariant: {
|
|
case: 'wantConfigId',
|
|
value: 1,
|
|
}
|
|
});
|
|
sendToDevice(toBinary(Mesh.ToRadioSchema, data));
|
|
}
|
|
|
|
function sendToDevice(data) {
|
|
const bufferLength = data.length;
|
|
const header = new Uint8Array([
|
|
0x94,
|
|
0xC3,
|
|
(bufferLength >> 8) & 0xFF,
|
|
bufferLength & 0xFF,
|
|
]);
|
|
data = new Uint8Array([...header, ...data]);
|
|
client.write(data);
|
|
}
|
|
|
|
client.connect(PORT, IP, function() {
|
|
console.log('Connected');
|
|
sendHello();
|
|
});
|
|
|
|
const meshtasticStream = new MeshtasticStream();
|
|
client.pipe(new LoraStream()).pipe(meshtasticStream);
|
|
meshtasticStream.on('data', (data) => {
|
|
parseMeshtastic(data['$typeName'], data);
|
|
})
|
|
|
|
function parseMeshtastic(typeName, data) {
|
|
switch(typeName) {
|
|
case Mesh.MeshPacketSchema.typeName:
|
|
onMeshPacket(data);
|
|
break;
|
|
case Mesh.NodeInfoSchema.typeName:
|
|
console.log('node info');
|
|
break;
|
|
}
|
|
}
|
|
|
|
function onMeshPacket(envelope) {
|
|
const payloadVariant = envelope.payloadVariant.case;
|
|
|
|
if (payloadVariant === 'encrypted') {
|
|
// attempt decryption
|
|
}
|
|
|
|
const dataPacket = envelope.payloadVariant.value;
|
|
const portNum = dataPacket.portnum;
|
|
|
|
if (!portNum) {
|
|
return;
|
|
}
|
|
|
|
let schema = null;
|
|
switch (portNum) {
|
|
case Portnums.PortNum.POSITION_APP:
|
|
schema = Mesh.PositionSchema;
|
|
break;
|
|
case Portnums.PortNum.TELEMETRY_APP:
|
|
schema = Telemetry.TelemetrySchema;
|
|
break;
|
|
case Portnums.PortNum.TEXT_MESSAGE_APP:
|
|
// no schema?
|
|
break;
|
|
case Portnums.PortNum.WAYPOINT_APP:
|
|
schema = Mesh.WaypointSchema;
|
|
break;
|
|
case Portnums.PortNum.TRACEROUTE_APP:
|
|
schema = Mesh.RouteDiscoverySchema;
|
|
break;
|
|
case Portnums.PortNum.NODEINFO_APP:
|
|
schema = Mesh.NodeInfoSchema;
|
|
break;
|
|
case Portnums.PortNum.NEIGHBORINFO_APP:
|
|
schema = Mesh.NeighborInfoSchema;
|
|
break;
|
|
case Portnums.PortNum.MAP_REPORT_APP:
|
|
schema = Mqtt.MapReportSchema;
|
|
break;
|
|
}
|
|
|
|
let decodedData = dataPacket.payload;
|
|
if (schema !== null) {
|
|
try {
|
|
decodedData = fromBinary(schema, decodedData);
|
|
console.log(decodedData);
|
|
} catch(e) {
|
|
// ignore errors, likely incomplete data
|
|
return;
|
|
}
|
|
}
|
|
} |