msfs-simconnect-api-wrapper
Advanced tools
Comparing version 1.2.1 to 1.3.0
113
msfs-api.js
@@ -12,7 +12,10 @@ import { | ||
// direct export for users: | ||
// Special import for working with airport data | ||
import { SystemEvents as SysEvents } from "./system-events/index.js"; | ||
const AIRPORTS_EVENT = SysEvents.AIRPORTS; | ||
const SIMCONNECT_FACILITY_LIST_TYPE_AIRPORT = 0; | ||
const { AIRPORTS_IN_RANGE, AIRPORTS_OUT_OF_RANGE } = SysEvents; | ||
const codeSafe = (string) => string.replaceAll(` `, `_`); | ||
// direct export for downstream users: | ||
export const SystemEvents = SysEvents; | ||
@@ -29,4 +32,5 @@ | ||
export class MSFS_API { | ||
constructor(appName = "MSFS API") { | ||
constructor(appName = "MSFS API", AirportDB) { | ||
this.appName = appName; | ||
this.AirportDB = AirportDB; | ||
@@ -89,21 +93,39 @@ // set up a listener list for simconnect event handling: | ||
addAirportHandling(handle) { | ||
const SIMCONNECT_FACILITY_LIST_TYPE_AIRPORT = 0; | ||
// TODO: - [x] get(`NEARBY_AIRPORTS`) to get the current list of nearby airports. | ||
// - [ ] get(`ALL_AIRPORTS`) to get the list of all airports in the sim. | ||
// - [x] on(`AIRPORTS_IN_RANGE`) to subscribe to "new airports in range" events. | ||
// - [x] on(`AIRPORTS_OUT_OF_RANGE`) to subscribe to "airports dropping out of range" events. | ||
// - [x] document these four special cases | ||
const TYPE = SIMCONNECT_FACILITY_LIST_TYPE_AIRPORT; | ||
const IN_RANGE = this.nextId(); | ||
const OUT_OF_RANGE = this.nextId(); | ||
this.airportData = []; | ||
handle.subscribeToFacilitiesEx1(TYPE, IN_RANGE, OUT_OF_RANGE); | ||
handle.subscribeToFacilitiesEx1( | ||
SIMCONNECT_FACILITY_LIST_TYPE_AIRPORT, | ||
IN_RANGE, | ||
OUT_OF_RANGE | ||
); | ||
handle.on("airportList", (data) => { | ||
const { requestID: id } = data; | ||
handle.on("airportList", (data) => { | ||
this.airportData = data.aiports; // TODO: FIX THIS WHEN UPDATING NODE-SIMCONNECT | ||
this.eventListeners.forEach(({ eventName, eventHandler }) => { | ||
if (eventName === AIRPORTS_EVENT.name) { | ||
eventHandler(...this.airportData); | ||
} | ||
}); | ||
// work around the node-simconnect typo, if it still exists | ||
if (data.aiports) { | ||
data.airports ??= data.aiports; | ||
delete data.aiports; | ||
} | ||
// Are there new airports? | ||
if (id === IN_RANGE) { | ||
this.eventListeners.forEach(({ eventName, eventHandler }) => { | ||
if (eventName === AIRPORTS_IN_RANGE.name) { | ||
eventHandler(data.airports); | ||
} | ||
}); | ||
} | ||
// Are there old airports? | ||
else if (id === OUT_OF_RANGE) { | ||
this.eventListeners.forEach(({ eventName, eventHandler }) => { | ||
if (eventName === AIRPORTS_OUT_OF_RANGE.name) { | ||
eventHandler(data.airports); | ||
} | ||
}); | ||
} | ||
}); | ||
@@ -122,2 +144,8 @@ } | ||
on(eventDefinition, eventHandler) { | ||
if (!eventDefinition) { | ||
console.error(`on() called without an event definition`); | ||
console.trace(); | ||
return; | ||
} | ||
const { name: eventName } = eventDefinition; | ||
@@ -127,4 +155,6 @@ const { handle } = this; | ||
// special case handling | ||
if (eventName === AIRPORTS_EVENT.name) { | ||
return this.__subscribeToAirports(eventHandler); | ||
if ( | ||
[AIRPORTS_IN_RANGE.name, AIRPORTS_OUT_OF_RANGE.name].includes(eventName) | ||
) { | ||
return this.__subscribeToAirports(eventName, eventHandler); | ||
} | ||
@@ -140,14 +170,6 @@ | ||
// Special case handler for getting "airports nearby" data. | ||
__subscribeToAirports(eventHandler, eventName = AIRPORTS_EVENT.name) { | ||
__subscribeToAirports(eventName, eventHandler) { | ||
const listenerId = -this.nextId(); | ||
const bundle = { id: listenerId, eventName, eventHandler }; | ||
this.eventListeners.push(bundle); | ||
// As this is not a standard event, we need to trigger the event | ||
// handler manually | ||
setTimeout(() => { | ||
// with a safety in case code immedately called off()... | ||
if (this.eventListeners.indexOf(bundle) > -1) { | ||
eventHandler(...this.airportData); | ||
} | ||
}, 100); | ||
return () => this.off(listenerId, eventName); | ||
@@ -272,2 +294,37 @@ } | ||
/** | ||
* Get a special value. Currently supported values: | ||
* - [x] NEARBY_AIRPORTS, the list of airports around the plane inside the sim's "reality bubble". | ||
* - [ ] ALL_AIRPORTS, the list of literally every airport known to the sim. | ||
*/ | ||
getSpecial(propName) { | ||
propName.replaceAll(` `, `_`); | ||
if (propName === `NEARBY_AIRPORTS`) { | ||
return new Promise((resolve) => { | ||
const getID = this.nextId(); | ||
const handler = (data) => { | ||
if (data.requestID === getID) { | ||
handle.off("airportList", handler); | ||
this.releaseId(getID); | ||
resolve({ NEARBY_AIRPORTS: data.airports ?? data.aiports}); | ||
} | ||
}; | ||
const { handle } = this; | ||
handle.on("airportList", handler); | ||
handle.requestFacilitiesListEx1( | ||
SIMCONNECT_FACILITY_LIST_TYPE_AIRPORT, | ||
getID | ||
); | ||
}); | ||
} | ||
if (propName === `ALL_AIRPORTS`) { | ||
console.warn(`getSpecial(ALL_AIRPORTS) is not currently supported.`); | ||
if (!this.AirportDB) return []; | ||
return new Promise((resolve) => { | ||
// TODO: load sqlite db from this.AirportDB and convert to an array of {icao, latitude, longitude, elevation } | ||
resolve([]); | ||
}); | ||
} | ||
} | ||
/** | ||
* Set a simconnect variable. | ||
@@ -274,0 +331,0 @@ * |
{ | ||
"name": "msfs-simconnect-api-wrapper", | ||
"version": "1.2.1", | ||
"version": "1.3.0", | ||
"description": "A convenient SimConnect API for playing with Microsoft Flight Simulator 2020", | ||
@@ -5,0 +5,0 @@ "main": "msfs-api.js", |
@@ -88,5 +88,6 @@ # msfs-simconnect-api-wrapper | ||
There is currently a single "not-official" event that can be listened to: | ||
There are currently two "unofficial" events that can be listened to: | ||
- `AIRPORTS`, registers a listener for which airports are nearby (or rather, "in the current Sim bubble", which is all world tiles currently loaded and active in the sim), which triggers every time an airport comes into range, _as well as_ when airports go out of range. | ||
- `AIRPORTS_IN_RANGE`, registers a listener for notifications about airports coming into range of our airplane (or rather, coming into range of "the current Sim bubble", which is all world tiles currently loaded and active in the sim). | ||
- `AIRPORTS_OUT_OF_RANGE`, registers a listener for notifications about airports dropping out of range of our airplane (with the same note as above). | ||
@@ -101,2 +102,8 @@ #### `off(evtDefinition, handler)` | ||
#### `getSpecial(propName)` | ||
A special get function for getting individual values that secretly require a whole bunch of complex SimConnect code. There is currently only one such value available: | ||
- `NEARBY_AIRPORTS`, which yields the list of airports that are currently in range of our airplane (or rather, in range of "the current Sim bubble", which is all world tiles currently loaded and active in the sim). | ||
#### `schedule(handler, interval, ...propNames)` | ||
@@ -103,0 +110,0 @@ |
@@ -106,6 +106,10 @@ // See https://docs.flightsimulator.com/html/Programming_Tools/SimConnect/API_Reference/Events_And_Data/SimConnect_SubscribeToSystemEvent.htm | ||
// CUSTOM LIST, NOT PART OF "REAL" SIMCONNECT | ||
AIRPORTS: { | ||
name: `Airports`, | ||
desc: `Use this event with the on() function to register for airports coming into range and dropping out of range of the sim's reality bubble.` | ||
} | ||
AIRPORTS_IN_RANGE: { | ||
name: `Airports in range`, | ||
desc: `Use this event with the on() function to get notified about airports coming into range of our airplane (by being loaded into the sim's "reality bubble").` | ||
}, | ||
AIRPORTS_OUT_OF_RANGE: { | ||
name: `Airports out of range`, | ||
desc: `Use this event with the on() function to get notified about airports dropping out of range of our airplane (by being loaded into the sim's "reality bubble").` | ||
}, | ||
}; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
236913
6821
151