From fbe966903dd031e93150141ed0e8a758f778c524 Mon Sep 17 00:00:00 2001 From: Determinant Date: Fri, 16 Aug 2024 15:08:45 -0700 Subject: finish --- xplane.mjs | 100 +++++++++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 68 insertions(+), 32 deletions(-) (limited to 'xplane.mjs') diff --git a/xplane.mjs b/xplane.mjs index 688f85f..5f94625 100644 --- a/xplane.mjs +++ b/xplane.mjs @@ -1,36 +1,72 @@ import dgram from "node:dgram"; -const xplaneAddr = "localhost"; -const xplanePort = 49000; -const socket = dgram.createSocket("udp4"); +export class XPlane { + constructor(xplaneAddr = "localhost", xplanePort = 49000) { + this.socket = dgram.createSocket("udp4"); + this.subscribed = []; + this.xplaneAddr = xplaneAddr; + this.xplanePort = xplanePort; + this.socket.on("message", (msg, rinfo) => { + if (msg.subarray(0, 5).toString() != "RREF,") { + console.info("dropping unrelated message"); + return; + } + let num = (msg.length - 5) / 8; + for (let i = 0; i < num; i++) { + const idx = msg.readInt32LE(5 + i * 8); + if (idx < 0) { + console.info(`sender index ${idx} should be >= 0`); + return; + } + if (idx >= this.subscribed.length) { + console.info(`sender index ${idx} > subscribed.length`); + return; + } + const v = msg.readFloatLE(9 + i * 8); + //console.info(`${this.subscribed[idx].ref} = ${v}`); + this.subscribed[idx].handler(v); + } + }); + this.socket.bind(0); + } -socket.on("message", (msg, rinfo) => { - console.log(msg, rinfo); -}); + async subscribeDataRef(dataRef, freq, handler) { + const idx = this.subscribed.length; + if (handler) { + this.subscribed.push({ ref: dataRef, handler }); + } + let buffer = Buffer.alloc(4 + 1 + 4 * 2 + 400); + let off = buffer.write("RREF"); + off = buffer.writeUInt8(0, off); // null terminated + off = buffer.writeInt32LE(freq, off); // xint frequency + off = buffer.writeInt32LE(idx, off); // xint sender index + off += buffer.write(dataRef, off); // char[400] dataref + off = buffer.writeUInt8(0, off); // null terminated + console.info(`x-plane subscribed[${idx}] => ${dataRef}`); + await this.socket.send( + buffer, + 0, + buffer.length, + this.xplanePort, + this.xplaneAddr, + ); + } + //subscribeDataRef("sim/flightmodel/position/indicated_airspeed"); -socket.bind(10080); - -export const subscribeDataRef = async (dataRef) => { - //const dataRef = "sim/flightmodel/position/indicated_airspeed"; - let buffer = Buffer.alloc(4 + 1 + 4 * 2 + 400); - let off = buffer.write("RREF"); - off = buffer.writeUInt8(0, off); // null terminated - off = buffer.writeInt32LE(1, off); // xint frequency - off = buffer.writeInt32LE(0, off); // xint client - off += buffer.write(dataRef, off); // char[400] dataref - off = buffer.writeUInt8(0, off); // null terminated - console.info(`x-plane dataref: ${dataRef}`); - await socket.send(buffer, 0, buffer.length, xplanePort, xplaneAddr); -}; - -export const sendCommand = async (cmd) => { - let buffer = Buffer.alloc(4 + 1 + cmd.length + 1); - let off = buffer.write("CMND"); - off = buffer.writeUInt8(0, off); // null terminated - off += buffer.write(cmd, off); // command - off = buffer.writeUInt8(0, off); // null terminated - console.info(`x-plane cmd: ${cmd}`); - await socket.send(buffer, 0, buffer.length, xplanePort, xplaneAddr); -}; - -//sendCommand("sim/GPS/g1000n1_hdg_down"); + async sendCommand(cmd) { + let buffer = Buffer.alloc(4 + 1 + cmd.length + 1); + let off = buffer.write("CMND"); + off = buffer.writeUInt8(0, off); // null terminated + off += buffer.write(cmd, off); // command + off = buffer.writeUInt8(0, off); // null terminated + console.info(`x-plane cmd: ${cmd}`); + await this.socket.send( + buffer, + 0, + buffer.length, + this.xplanePort, + this.xplaneAddr, + ); + } + //sendCommand("sim/GPS/g1000n1_hdg_down"); +} -- cgit v1.2.3-70-g09d2