Skip to content
Draft
10,106 changes: 10,106 additions & 0 deletions generated/simvars.ts

Large diffs are not rendered by default.

6,857 changes: 1,310 additions & 5,547 deletions package-lock.json

Large diffs are not rendered by default.

28 changes: 15 additions & 13 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,31 +39,33 @@
"@types/debug": "^4.1.7",
"@types/ini": "^1.3.30",
"@types/jest": "^29.2.4",
"@typescript-eslint/eslint-plugin": "^5.26.0",
"@typescript-eslint/parser": "^5.26.0",
"eslint": "^8.16.0",
"@typescript-eslint/eslint-plugin": "^6.20.0",
"@typescript-eslint/parser": "^6.20.0",
"eslint": "^8.56.0",
"eslint-config-airbnb-base": "^15.0.0",
"eslint-config-airbnb-typescript": "^17.0.0",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-import": "^2.26.0",
"eslint-plugin-prettier": "^4.0.0",
"eslint-plugin-simple-import-sort": "^7.0.0",
"eslint-config-airbnb-typescript": "^17.1.0",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-import": "^2.29.1",
"eslint-plugin-prettier": "^5.1.3",
"eslint-plugin-simple-import-sort": "^10.0.0",
"eslint-plugin-tsdoc": "^0.2.17",
"husky": "^8.0.1",
"jest": "^29.3.1",
"lint-staged": "^12.4.2",
"prettier": "^2.6.2",
"pretty-quick": "^3.1.0",
"prettier": "^3.2.5",
"pretty-quick": "^4.0.0",
"ts-jest": "^29.0.3",
"typedoc": "^0.25.4",
"typescript": "^4.9.4"
"typedoc": "^0.25.7",
"typescript": "^5.3.3"
},
"dependencies": {
"@types/node": "*",
"bytebuffer": "^5.0.1",
"debug": "^4.3.4",
"ini": "^2.0.0",
"regedit": "^5.1.1"
"regedit": "^5.1.1",
"cheerio": "^1.0.0-rc.12",
"outdent": "^0.8.0"
},
"husky": {
"hooks": {
Expand Down
69 changes: 69 additions & 0 deletions samples/typescript/apiHelper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { ApiHelper } from '../../dist/src/apiHelper';
import { open, Protocol, SimConnectDataType } from '../../dist/src';

open('API-helper example', Protocol.KittyHawk)
.then(async ({ recvOpen, handle }) => {
console.log('Yay, connected!', recvOpen);
await doStuff(new ApiHelper(handle));
})
.catch(e => {
console.log('Unhandled error', e);
});

async function doStuff(apiHelper: ApiHelper) {
const { systemEvents, simulationVariables, facilities } = apiHelper;

/** Subscribe to a system event */
systemEvents.addEventListener('Pause', data => {
console.log(data === 0 ? 'UnPaused' : 'Paused');
});

/** Get a set of simulation variables once */
const aircraftTitle = await simulationVariables.get('TITLE');
const atcMdel = await simulationVariables.get('ATC_MODEL');
const fuelOnBoard = await simulationVariables.get('FUEL_TOTAL_QUANTITY');
const fuelOnBoardKgs = await simulationVariables.get({
name: 'FUEL_TOTAL_QUANTITY',
units: 'kilograms',
dataType: SimConnectDataType.FLOAT64,
});

console.log(
`Current aircraft is '${aircraftTitle}' (${atcMdel}). It has ${fuelOnBoard} gallons (${fuelOnBoardKgs} kgs) of fuel on board`
);

/** Get simulation variables whenever they change */
simulationVariables.monitor(
['AIRSPEED_INDICATED', 'STRUCT_LATLONALT'],
(err, data) => {
if (err) {
console.log(err);
} else if (data) {
console.log('Airspeed:', data.AIRSPEED_INDICATED);
console.log('Altitude:', data.STRUCT_LATLONALT.altitude);
}
},
{ onlyOnChange: true }
);

/** Set throttles to 50% */
simulationVariables.set(['GENERAL ENG THROTTLE LEVER POSITION:1', 50, 'Percent'], err =>
console.log(err)
);

/**
* The property names and corresponding data types are defined here:
* https://docs.flightsimulator.com/html/Programming_Tools/SimConnect/API_Reference/Facilities/SimConnect_AddToFacilityDefinition.htm
*/
const airportInfo = await facilities.getAirport('ENKJ', {
ICAO: SimConnectDataType.STRING8,
NAME: SimConnectDataType.STRING32,
RUNWAY: {
// TODO: fix return type. This should be a list in the returned type definition
PRIMARY_NUMBER: SimConnectDataType.INT32,
HEADING: SimConnectDataType.FLOAT32,
LENGTH: SimConnectDataType.FLOAT32,
},
});
console.log('Got airport', airportInfo);
}
36 changes: 36 additions & 0 deletions src/ApiHelper/BaseHelper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { SimConnectConnection } from '../SimConnectConnection';
import { SimConnectException } from '../enums/SimConnectException';

export class BaseHelper {
protected readonly _handle: SimConnectConnection;

private _exceptionHandlers: { [sendId: number]: (ex: SimConnectException) => void } = {};

constructor(handle: SimConnectConnection) {
this._handle = handle;

this._handle.on('exception', recvException => {
if (recvException.sendId in this._exceptionHandlers) {
const handler = this._exceptionHandlers[recvException.sendId];
handler(recvException.exception);
}
});
}

/**
* Should be called immediately after sending a simconnect packet
* @param sendId the ID of the newly sent packet
* @param handler function to be called in case of an exception
* @private
*/
protected _checkForException(
sendId: number,
handler: (exception: SimConnectException) => void
) {
this._exceptionHandlers[sendId] = handler;
setTimeout(() => {
// Give SimConnect server some time to throw the exception, then remove the listener
delete this._exceptionHandlers[sendId];
}, 1000);
}
}
Loading