|
| 1 | +//!/usr/bin/env node |
| 2 | + |
| 3 | +const WebSocket = require('ws'); |
| 4 | + |
| 5 | +global.jspack = new (require('./MAVLink/local_modules/jspack/jspack.js')).default; |
| 6 | + |
| 7 | +const mavlib = require('./MAVLink/mavlink.js'); // Use local MAVLink definition |
| 8 | + |
| 9 | +if (process.argv.length < 3) { |
| 10 | + console.error("Usage: node ws_mavlink.js <WebSocket URL>"); |
| 11 | + process.exit(1); |
| 12 | +} |
| 13 | + |
| 14 | +const url = process.argv[2]; |
| 15 | +const ws = new WebSocket(url); |
| 16 | +ws.binaryType = "arraybuffer"; |
| 17 | + |
| 18 | +// Create a MAVLink v2 parser |
| 19 | +parser = new MAVLink20Processor() |
| 20 | + |
| 21 | +let signing_passphrase = null; |
| 22 | +if (process.argv.length > 3) { |
| 23 | + signing_passphrase = process.argv[3]; |
| 24 | +} |
| 25 | + |
| 26 | +const crypto = require('crypto'); |
| 27 | + |
| 28 | +function passphrase_to_key(passphrase) { |
| 29 | + return crypto.createHash('sha256').update(Buffer.from(passphrase, 'ascii')).digest(); |
| 30 | +} |
| 31 | + |
| 32 | +if (signing_passphrase) { |
| 33 | + parser.signing.secret_key = passphrase_to_key(signing_passphrase); |
| 34 | + parser.signing.sign_outgoing = true; |
| 35 | +} |
| 36 | + |
| 37 | +let heartbeat_interval; |
| 38 | + |
| 39 | +ws.on('open', () => { |
| 40 | + console.log(`Connected to ${url}`); |
| 41 | + |
| 42 | + heartbeat_interval = setInterval(() => { |
| 43 | + try { |
| 44 | + const msg = new mavlib.mavlink20.messages.heartbeat( |
| 45 | + 6, // MAV_TYPE_GCS |
| 46 | + 8, // MAV_AUTOPILOT_INVALID |
| 47 | + 0, // base_mode |
| 48 | + 0, // custom_mode |
| 49 | + 4 // MAV_STATE_ACTIVE |
| 50 | + ); |
| 51 | + const pkt = msg.pack(parser); |
| 52 | + ws.send(pkt); |
| 53 | + console.log("Sent HEARTBEAT"); |
| 54 | + } catch (e) { |
| 55 | + console.error("Error sending HEARTBEAT:", e.message); |
| 56 | + console.error(e.stack); |
| 57 | + } |
| 58 | + }, 1000); |
| 59 | +}); |
| 60 | + |
| 61 | +function mav_pretty(msg) { |
| 62 | + // console.log(JSON.stringify(msg, null, 2)); |
| 63 | + |
| 64 | + if (!msg || !msg._name || !msg.fieldnames) { |
| 65 | + return "<invalid MAVLink message>"; |
| 66 | + } |
| 67 | + |
| 68 | + const name = msg._name; |
| 69 | + const fields = msg.fieldnames |
| 70 | + .map(fn => { |
| 71 | + let val = msg[fn]; |
| 72 | + if (typeof val === "string") { |
| 73 | + val = `"${val}"`; |
| 74 | + } else if (Array.isArray(val)) { |
| 75 | + val = "[" + val.join(", ") + "]"; |
| 76 | + } |
| 77 | + return `${fn}=${val}`; |
| 78 | + }) |
| 79 | + .join(", "); |
| 80 | + |
| 81 | + return `${name} { ${fields} }`; |
| 82 | +} |
| 83 | + |
| 84 | + |
| 85 | +ws.on('message', (data) => { |
| 86 | + const buf = (data instanceof ArrayBuffer) ? Buffer.from(data) : |
| 87 | + (Buffer.isBuffer(data) ? data : Buffer.from(data.buffer || data)); |
| 88 | + |
| 89 | + console.log(`Received ${buf.length} bytes: [${buf.slice(0, 10).toString('hex')}...]`); |
| 90 | + |
| 91 | + for (const b of buf) { |
| 92 | + try { |
| 93 | + const msg = parser.parseChar(b); |
| 94 | + if (msg) { |
| 95 | + console.log(`MAVLink message ID: ${msg._id}`); |
| 96 | + console.log(mav_pretty(msg)); |
| 97 | + } |
| 98 | + } catch (e) { |
| 99 | + console.warn(`Parser error on byte 0x${byte.toString(16)}: ${e.message}`); |
| 100 | + } |
| 101 | + } |
| 102 | +}); |
| 103 | + |
| 104 | +ws.on('close', () => { |
| 105 | + console.log("WebSocket closed"); |
| 106 | + clearInterval(heartbeat_interval); |
| 107 | +}); |
| 108 | + |
| 109 | +ws.on('error', (err) => { |
| 110 | + console.error("WebSocket error:", err.message); |
| 111 | +}); |
0 commit comments