From 3628669dff65acb2c4c6df6d3f1fb593930acb24 Mon Sep 17 00:00:00 2001 From: Determinant Date: Mon, 26 Aug 2024 13:24:40 -0700 Subject: v0.0.3 (auto x-plane detection and upstream bug fixes) --- xplane.mjs | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 51 insertions(+), 8 deletions(-) (limited to 'xplane.mjs') diff --git a/xplane.mjs b/xplane.mjs index d105fd4..dd50b20 100644 --- a/xplane.mjs +++ b/xplane.mjs @@ -1,9 +1,31 @@ import dgram from "node:dgram"; export class XPlane { - constructor(xplaneAddr = "localhost", xplanePort = 49000) { + constructor( + xplaneAddr = "localhost", + xplanePort = 49000, + statusCheck = 5000, + statusTimeout = 1000, + ) { this.socket = dgram.createSocket("udp4"); this.subscribed = []; + this.lastReceived = null; + const xplane = this; + this.statusChecker = setTimeout(async function statusChecker() { + let active = false; + if (xplane.lastReceived) { + if (new Date() - xplane.lastReceived < statusTimeout) { + active = true; + } + } + if (!active) { + //console.info( + // `x-plane data not detected, will try again in ${(statusCheck / 1000).toFixed(2)} secs`, + //); + await xplane._subscribeAll(); + } + xplane.statusChecker = setTimeout(statusChecker, statusCheck); + }, statusCheck); this.xplaneAddr = xplaneAddr; this.xplanePort = xplanePort; this.socket.on("message", async (msg, rinfo) => { @@ -11,6 +33,7 @@ export class XPlane { console.info("dropping unrelated message"); return; } + this.lastReceived = new Date(); let num = (msg.length - 5) / 8; for (let i = 0; i < num; i++) { const idx = msg.readInt32LE(5 + i * 8); @@ -23,18 +46,15 @@ export class XPlane { return; } const v = msg.readFloatLE(9 + i * 8); - //console.info(`${this.subscribed[idx].ref} = ${v}`); + //console.info(`${this.subscribed[idx].dataRef} = ${v}`); await this.subscribed[idx].handler(v); } }); this.socket.bind(0); } - async subscribeDataRef(dataRef, freq, handler) { - const idx = this.subscribed.length; - if (handler) { - this.subscribed.push({ ref: dataRef, handler }); - } + async _subscribeDataRef(idx) { + const { dataRef, freq } = this.subscribed[idx]; let buffer = Buffer.alloc(4 + 1 + 4 * 2 + 400); let off = buffer.write("RREF"); off = buffer.writeUInt8(0, off); // null terminated @@ -42,7 +62,6 @@ export class XPlane { 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, @@ -51,6 +70,21 @@ export class XPlane { this.xplaneAddr, ); } + + async _subscribeAll() { + for (let i = 0; i < this.subscribed.length; i++) { + await this._subscribeDataRef(i); + } + } + + async subscribeDataRef(dataRef, freq, handler) { + const idx = this.subscribed.length; + if (handler) { + this.subscribed.push({ dataRef, handler, freq }); + } + console.info(`x-plane subscribed[${idx}] => ${dataRef}`); + this._subscribeDataRef(idx); + } //subscribeDataRef("sim/flightmodel/position/indicated_airspeed"); async sendCommand(cmd) { @@ -69,4 +103,13 @@ export class XPlane { ); } //sendCommand("sim/GPS/g1000n1_hdg_down"); + + async close() { + for (let i = 0; i < this.subscribed.length; i++) { + this.subscribed[i].freq = 0; + await this._subscribeDataRef(i); + } + this.subscribed = []; + clearTimeout(this.statusChecker); + } } -- cgit v1.2.3-70-g09d2