msfs-simconnect-api-wrapper
Advanced tools
Comparing version 1.1.10 to 1.2.0
@@ -13,6 +13,8 @@ import { | ||
// direct export for users: | ||
export { SystemEvents } from "./system-events/index.js"; | ||
import { SystemEvents as SysEvents } from "./system-events/index.js"; | ||
const AIRPORTS_EVENT = SysEvents.AIRPORTS; | ||
const codeSafe = (string) => string.replaceAll(` `, `_`); | ||
export const SystemEvents = SysEvents; | ||
/** | ||
@@ -43,2 +45,3 @@ * API: | ||
opts.onRetry ??= () => {}; | ||
let handle; | ||
try { | ||
@@ -52,2 +55,3 @@ const { handle } = await open(this.appName, Protocol.KittyHawk); | ||
handle.on("exception", (e) => console.error(e)); | ||
this.addAirportHandling(handle); | ||
} catch (err) { | ||
@@ -86,2 +90,25 @@ if (opts.retries) { | ||
addAirportHandling(handle) { | ||
const SIMCONNECT_FACILITY_LIST_TYPE_AIRPORT = 0; | ||
const IN_RANGE = this.nextId(); | ||
const OUT_OF_RANGE = this.nextId(); | ||
this.airportData = {}; | ||
handle.subscribeToFacilitiesEx1( | ||
SIMCONNECT_FACILITY_LIST_TYPE_AIRPORT, | ||
IN_RANGE, | ||
OUT_OF_RANGE | ||
); | ||
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); | ||
} | ||
}); | ||
}); | ||
} | ||
/** | ||
@@ -99,13 +126,31 @@ * Add an event listener. This returns a function that acts | ||
const { handle } = this; | ||
// special case handling | ||
if (eventName === AIRPORTS_EVENT.name) { | ||
return this.__subscribeToAirports(eventHandler); | ||
} | ||
const eventID = this.nextId(); | ||
handle.subscribeToSystemEvent(eventID, eventName); | ||
// console.log(`registering for "${eventName}" using id ${eventID}`); | ||
this.eventListeners.push({ | ||
id: eventID, | ||
eventName, | ||
eventHandler, | ||
}); | ||
this.eventListeners.push({ id: eventID, eventName, eventHandler }); | ||
return () => this.off(eventID, eventName); | ||
} | ||
// Special case handler for getting "airports nearby" data. | ||
__subscribeToAirports(eventHandler, eventName = AIRPORTS_EVENT.name) { | ||
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); | ||
} | ||
/** | ||
@@ -119,5 +164,8 @@ * Remove an event listener. | ||
const { handle } = this; | ||
handle.unsubscribeFromSystemEvent(eventID); | ||
// If this was a negative number, it's an internal id rathe than a true event ID. | ||
if (eventID > 0) handle.unsubscribeFromSystemEvent(eventID); | ||
// console.log(`unregistering from "${eventName}" with id ${eventID}`); | ||
const pos = this.eventListeners.findIndex((e) => e.eventName === eventName); | ||
const pos = this.eventListeners.findIndex( | ||
(e) => e.id === eventID && e.eventName === eventName | ||
); | ||
// console.log(`removing listener in position ${pos}`) | ||
@@ -124,0 +172,0 @@ this.eventListeners.splice(pos, 1); |
{ | ||
"name": "msfs-simconnect-api-wrapper", | ||
"version": "1.1.10", | ||
"version": "1.2.0", | ||
"description": "A convenient SimConnect API for playing with Microsoft Flight Simulator 2020", | ||
@@ -5,0 +5,0 @@ "main": "msfs-api.js", |
@@ -7,4 +7,2 @@ # msfs-simconnect-api-wrapper | ||
This is still a work in progress, although it does wrap all simvars and system events at the moment. | ||
## Installation and use | ||
@@ -59,12 +57,12 @@ | ||
- `connect(opts?)`, sets up a connection to MSFS, see above for an explanation of `opts`. If let unspecified, no retries will be attempted. | ||
- `on(evtDefinition, handler)`, starts listening for a specific simconnect event with a specific handler. Returns a corresponding arg-less `off()` function to clean up the listener. See the "System events" section below for details on the event definition. | ||
- `off(evtDefinition, handler)`, stop listening for a specific simconnect event with a specific handler. You'll typically not need to call this, just use the function that `on` returns. See the "System events" section below for details on the event definition. | ||
- `get(...propNames)`, accepts a list of simvars (with spaces or underscores) and async-returns a key/value pair object with each simvar as key (with spaces replaced by underscores). | ||
- `schedule(handler, interval, ...propNames)`, sets up a periodic call to `handler` every `interval` milliseconds with the result of `get(...propNames)`. Returns an arg-less `off()` to end the scheduled call. | ||
- `set(propName, value)`, accepts a single simvar and the value its should be set to. This will throw "SimVar ... is not settable" when attempting to set the value for a read-only variable. | ||
- `trigger(triggerName, value?)`, triggers a simconnect event, with optional value. | ||
#### `connect(opts?)` | ||
#### System events (used for on/off handling): | ||
Sets up a connection to MSFS, see above for an explanation of `opts`. If let unspecified, no retries will be attempted. | ||
#### `on(evtDefinition, handler)` | ||
Starts listening for a specific simconnect event with a specific handler. Returns a corresponding arg-less `off()` function to clean up the listener. See the "System events" section below for details on the event definition. | ||
##### System events (used for on/off handling): | ||
All event names in https://docs.flightsimulator.com/html/Programming_Tools/SimConnect/API_Reference/Events_And_Data/SimConnect_SubscribeToSystemEvent.htm are supported as constants on the `SystemEvents` object, importable alongside MSFS_API: | ||
@@ -90,5 +88,31 @@ | ||
##### Special Events | ||
There is currently a single "not-official" event 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. | ||
#### `off(evtDefinition, handler)` | ||
Stop listening for a specific simconnect event with a specific handler. You'll typically not need to call this function directly, as you can just use the function that `on` returns. See the "System events" section above for more details on the event definition. | ||
#### `get(...propNames)` | ||
Accepts a list of simvars (with spaces or underscores) and async-returns a key/value pair object with each simvar as key (with spaces replaced by underscores). | ||
#### `schedule(handler, interval, ...propNames)` | ||
Sets up a periodic call to `handler` every `interval` milliseconds with the result of `get(...propNames)`. Returns an arg-less `off()` to end the scheduled call. | ||
#### `set(propName, value)` | ||
Accepts a single simvar and the value its should be set to. This will throw "SimVar ... is not settable" when attempting to set the value for a read-only variable. | ||
#### `trigger(triggerName, value?)` | ||
Triggers a simconnect event, with optional value. | ||
### Supported Simvars: | ||
All simvars are supported, barring several simvars with data types for which I need to figure out how to actually deference then, such as LatLonAlt structs, or the (super rare) bool/string combination. | ||
All simvars are supported, barring several simvars with data types for which I need to figure out how to actually deference then, such as LatLonAlt structs, or the (super rare) bool/string combination, as well a any simvar that is officially deprecated, or marked as "legacy, do not use these going forward". If you get an error about an unknown Simvar, look up that variable on the [SimConnect variables list](https://docs.flightsimulator.com/html/Programming_Tools/SimVars/Simulation_Variables.htm) and see if it's either deprecated, or part of a collection that is considered legacy. | ||
@@ -115,4 +139,6 @@ - [x] Camera Variables (_not verified_) | ||
SimEvents are resolved by key name, so as long as you use a valid keyname, you can trigger it. See https://docs.flightsimulator.com/html/Programming_Tools/Event_IDs/Event_IDs.htm for the full list (there are... a lot). | ||
SimEvents are resolved by key name, so as long as you use a valid key name, you can trigger it. | ||
See https://docs.flightsimulator.com/html/Programming_Tools/Event_IDs/Event_IDs.htm for the full list (there are... a lot). | ||
## Helping out | ||
@@ -122,8 +148,1 @@ | ||
Even if you just want to help verify a few variables, that's a few variables fewer that I'll need to run through =) | ||
<!-- | ||
simvar regex: | ||
from: \s+([A-Z ]+(:index)?)\s\s+(.*)\s\s+(.*) | ||
to: "$1": {\ndesc: `$3`,\nunits: `$4`,\n}, | ||
--> |
@@ -104,2 +104,8 @@ // 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.` | ||
} | ||
}; |
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
234022
6766
144