From 76512cdbab2d0489ec4a316dd8167216f2fcc1ee Mon Sep 17 00:00:00 2001 From: EvenAR Date: Mon, 13 Nov 2023 20:36:36 +0100 Subject: [PATCH] Implement SimConnect_EnumerateControllers Issue #87 --- src/RawBuffer.ts | 8 ++++++ src/SimConnectConnection.ts | 28 +++++++++++++++---- src/SimConnectPacketBuilder.ts | 5 ++++ src/SimConnectSocket.ts | 1 + src/datastructures/ControllerItem.ts | 22 +++++++++++++++ .../FacilityAirport.ts | 0 .../FacilityMinimal.ts | 0 .../FacilityNDB.ts | 0 .../FacilityVOR.ts | 0 .../FacilityWaypoint.ts | 0 .../JetwayData.ts | 0 src/datastructures/VersionBaseType.ts | 18 ++++++++++++ src/dto/index.ts | 1 + src/index.ts | 8 +++--- src/recv/RecvAirportList.ts | 2 +- src/recv/RecvControllersList.ts | 16 +++++++++++ src/recv/RecvFacilityMinimalList.ts | 2 +- src/recv/RecvJetwayData.ts | 2 +- src/recv/RecvNDBList.ts | 2 +- src/recv/RecvVORList.ts | 2 +- src/recv/RecvWaypointList.ts | 2 +- 21 files changed, 103 insertions(+), 16 deletions(-) create mode 100644 src/datastructures/ControllerItem.ts rename src/{facility => datastructures}/FacilityAirport.ts (100%) rename src/{facility => datastructures}/FacilityMinimal.ts (100%) rename src/{facility => datastructures}/FacilityNDB.ts (100%) rename src/{facility => datastructures}/FacilityVOR.ts (100%) rename src/{facility => datastructures}/FacilityWaypoint.ts (100%) rename src/{facility => datastructures}/JetwayData.ts (100%) create mode 100644 src/datastructures/VersionBaseType.ts create mode 100644 src/recv/RecvControllersList.ts diff --git a/src/RawBuffer.ts b/src/RawBuffer.ts index 26a6db6..1fee1a4 100644 --- a/src/RawBuffer.ts +++ b/src/RawBuffer.ts @@ -41,6 +41,10 @@ class RawBuffer { return bytes.toBuffer(); } + readInt16(): number { + return this.buffer.readInt16(); + } + readInt32(): number { return this.buffer.readInt32(); } @@ -49,6 +53,10 @@ class RawBuffer { return this.buffer.readUint32(); } + writeInt16(value: number, offset?: number) { + this.buffer.writeInt16(value, offset); + } + /** @deprecated use readInt32() instead */ readInt = this.readInt32; diff --git a/src/SimConnectConnection.ts b/src/SimConnectConnection.ts index e6ad13e..7d1534b 100644 --- a/src/SimConnectConnection.ts +++ b/src/SimConnectConnection.ts @@ -16,7 +16,6 @@ import { EventFlag } from './flags/EventFlag'; import { DataSetFlag } from './flags/DataSetFlag'; import { ClientDataRequestFlag } from './flags/ClientDataRequestFlag'; import { SimConnectConstants } from './SimConnectConstants'; -import Timeout = NodeJS.Timeout; import { SimConnectPacketBuilder } from './SimConnectPacketBuilder'; import { RecvAirportList, @@ -25,12 +24,17 @@ import { RecvCustomAction, RecvEvent, RecvEventAddRemove, + RecvEventEx1, RecvEventFilename, RecvEventFrame, RecvEventRaceEnd, RecvEventRaceLap, RecvEventWeatherMode, RecvException, + RecvFacilityData, + RecvFacilityDataEnd, + RecvFacilityMinimalList, + RecvJetwayData, RecvNDBList, RecvOpen, RecvReservedKey, @@ -39,11 +43,6 @@ import { RecvVORList, RecvWaypointList, RecvWeatherObservation, - RecvFacilityData, - RecvFacilityDataEnd, - RecvFacilityMinimalList, - RecvEventEx1, - RecvJetwayData, } from './recv'; import { ClientDataDefinitionId, @@ -55,6 +54,8 @@ import { NotificationGroupId, ObjectId, } from './Types'; +import Timeout = NodeJS.Timeout; +import { RecvControllersList } from './recv/RecvControllersList'; type OpenPacketData = { major: number; @@ -129,6 +130,7 @@ interface SimConnectRecvEvents { facilityDataEnd: (recvFacilityDataEnd: RecvFacilityDataEnd) => void; facilityMinimalList: (recvFacilityMinimalList: RecvFacilityMinimalList) => void; jetwayData: (recvJetwayData: RecvJetwayData) => void; + controllersList: (recvControllersList: RecvControllersList) => void; } type ConnectionOptions = @@ -1462,6 +1464,10 @@ class SimConnectConnection extends EventEmitter { ); } + /** + * + * @returns sendId of packet (can be used to identify packet when exception event occurs) + */ requestJetwayData(airportIcao: string, parkingIndices?: number[]): number { if (this._ourProtocol < Protocol.KittyHawk) throw Error(SimConnectError.BadVersion); @@ -1480,6 +1486,13 @@ class SimConnectConnection extends EventEmitter { return this._buildAndSend(packet); } + enumerateControllers(): number { + if (this._ourProtocol < Protocol.KittyHawk) throw Error(SimConnectError.BadVersion); + + const packet = this._beginPacket(0x4c); + return this._buildAndSend(packet); + } + close() { if (this._openTimeout !== null) { clearTimeout(this._openTimeout); @@ -1608,6 +1621,9 @@ class SimConnectConnection extends EventEmitter { case RecvID.ID_JETWAY_DATA: this.emit('jetwayData', new RecvJetwayData(data)); break; + case RecvID.ID_CONTROLLERS_LIST: + this.emit('controllersList', new RecvControllersList(data)); + break; } } diff --git a/src/SimConnectPacketBuilder.ts b/src/SimConnectPacketBuilder.ts index 7aa78d8..45c07e5 100644 --- a/src/SimConnectPacketBuilder.ts +++ b/src/SimConnectPacketBuilder.ts @@ -39,6 +39,11 @@ export class SimConnectPacketBuilder { return this; } + putInt16(value: number, offset?: number) { + this.packetContent.writeInt16(value, offset); + return this; + } + putInt32(value: number, offset?: number) { this.packetContent.writeInt32(value, offset); return this; diff --git a/src/SimConnectSocket.ts b/src/SimConnectSocket.ts index 8ce08ce..a4b5b19 100644 --- a/src/SimConnectSocket.ts +++ b/src/SimConnectSocket.ts @@ -39,6 +39,7 @@ enum RecvID { ID_FACILITY_DATA_END, ID_FACILITY_MINIMAL_LIST, ID_JETWAY_DATA, + ID_CONTROLLERS_LIST, } interface SimConnectMessage { diff --git a/src/datastructures/ControllerItem.ts b/src/datastructures/ControllerItem.ts new file mode 100644 index 0000000..a200570 --- /dev/null +++ b/src/datastructures/ControllerItem.ts @@ -0,0 +1,22 @@ +import { VersionBaseType } from '../dto'; +import { RawBuffer } from '../RawBuffer'; + +export class ControllerItem { + deviceName: string; + + deviceId: number; + + productId: number; + + compositeId: number; + + hardwareVersion: VersionBaseType; + + constructor(data: RawBuffer) { + this.deviceName = data.readString256(); + this.deviceId = data.readInt32(); + this.productId = data.readInt32(); + this.compositeId = data.readInt32(); + this.hardwareVersion = new VersionBaseType(data); + } +} diff --git a/src/facility/FacilityAirport.ts b/src/datastructures/FacilityAirport.ts similarity index 100% rename from src/facility/FacilityAirport.ts rename to src/datastructures/FacilityAirport.ts diff --git a/src/facility/FacilityMinimal.ts b/src/datastructures/FacilityMinimal.ts similarity index 100% rename from src/facility/FacilityMinimal.ts rename to src/datastructures/FacilityMinimal.ts diff --git a/src/facility/FacilityNDB.ts b/src/datastructures/FacilityNDB.ts similarity index 100% rename from src/facility/FacilityNDB.ts rename to src/datastructures/FacilityNDB.ts diff --git a/src/facility/FacilityVOR.ts b/src/datastructures/FacilityVOR.ts similarity index 100% rename from src/facility/FacilityVOR.ts rename to src/datastructures/FacilityVOR.ts diff --git a/src/facility/FacilityWaypoint.ts b/src/datastructures/FacilityWaypoint.ts similarity index 100% rename from src/facility/FacilityWaypoint.ts rename to src/datastructures/FacilityWaypoint.ts diff --git a/src/facility/JetwayData.ts b/src/datastructures/JetwayData.ts similarity index 100% rename from src/facility/JetwayData.ts rename to src/datastructures/JetwayData.ts diff --git a/src/datastructures/VersionBaseType.ts b/src/datastructures/VersionBaseType.ts new file mode 100644 index 0000000..eed4723 --- /dev/null +++ b/src/datastructures/VersionBaseType.ts @@ -0,0 +1,18 @@ +import { RawBuffer } from '../RawBuffer'; + +export class VersionBaseType { + major: number; + + minor: number; + + revision: number; + + build: number; + + constructor(data: RawBuffer) { + this.major = data.readInt16(); + this.minor = data.readInt16(); + this.revision = data.readInt16(); + this.build = data.readInt16(); + } +} diff --git a/src/dto/index.ts b/src/dto/index.ts index 47822ac..134a9e0 100644 --- a/src/dto/index.ts +++ b/src/dto/index.ts @@ -7,3 +7,4 @@ export * from './XYZ'; export * from './bufferHelpers'; export * from './icao'; export * from './PBH'; +export * from '../datastructures/VersionBaseType'; diff --git a/src/index.ts b/src/index.ts index 96036f6..c297393 100644 --- a/src/index.ts +++ b/src/index.ts @@ -20,10 +20,10 @@ export * from './flags/EventFlag'; export * from './flags/DataRequestFlag'; export * from './flags/DataSetFlag'; export * from './flags/ClientDataRequestFlag'; -export * from './facility/FacilityAirport'; -export * from './facility/FacilityNDB'; -export * from './facility/FacilityVOR'; -export * from './facility/FacilityWaypoint'; +export * from './datastructures/FacilityAirport'; +export * from './datastructures/FacilityNDB'; +export * from './datastructures/FacilityVOR'; +export * from './datastructures/FacilityWaypoint'; export * from './Types'; export * from './recv'; diff --git a/src/recv/RecvAirportList.ts b/src/recv/RecvAirportList.ts index 2de6bb2..a450142 100644 --- a/src/recv/RecvAirportList.ts +++ b/src/recv/RecvAirportList.ts @@ -1,5 +1,5 @@ import { RawBuffer } from '../RawBuffer'; -import { FacilityAirport } from '../facility/FacilityAirport'; +import { FacilityAirport } from '../datastructures/FacilityAirport'; import { RecvFacilitiesList } from './RecvFacilitiesList'; export class RecvAirportList extends RecvFacilitiesList { diff --git a/src/recv/RecvControllersList.ts b/src/recv/RecvControllersList.ts new file mode 100644 index 0000000..18e8dca --- /dev/null +++ b/src/recv/RecvControllersList.ts @@ -0,0 +1,16 @@ +import { RawBuffer } from '../RawBuffer'; +import { RecvListTemplate } from './RecvListTemplate'; +import { ControllerItem } from '../datastructures/ControllerItem'; + +export class RecvControllersList extends RecvListTemplate { + controllers: ControllerItem[] = []; + + constructor(data: RawBuffer) { + super(data); + + this.controllers = []; + for (let i = 0; i < this.arraySize; i++) { + this.controllers.push(new ControllerItem(data)); + } + } +} diff --git a/src/recv/RecvFacilityMinimalList.ts b/src/recv/RecvFacilityMinimalList.ts index 7573232..ef2fa89 100644 --- a/src/recv/RecvFacilityMinimalList.ts +++ b/src/recv/RecvFacilityMinimalList.ts @@ -1,4 +1,4 @@ -import { FacilityMinimal } from '../facility/FacilityMinimal'; +import { FacilityMinimal } from '../datastructures/FacilityMinimal'; import { RawBuffer } from '../RawBuffer'; import { RecvFacilitiesList } from './RecvFacilitiesList'; diff --git a/src/recv/RecvJetwayData.ts b/src/recv/RecvJetwayData.ts index 7b05604..c7aacef 100644 --- a/src/recv/RecvJetwayData.ts +++ b/src/recv/RecvJetwayData.ts @@ -1,5 +1,5 @@ import { RecvListTemplate } from './RecvListTemplate'; -import { JetwayData } from '../facility/JetwayData'; +import { JetwayData } from '../datastructures/JetwayData'; import { RawBuffer } from '../RawBuffer'; export class RecvJetwayData extends RecvListTemplate { diff --git a/src/recv/RecvNDBList.ts b/src/recv/RecvNDBList.ts index ca525eb..064b28d 100644 --- a/src/recv/RecvNDBList.ts +++ b/src/recv/RecvNDBList.ts @@ -1,5 +1,5 @@ import { RawBuffer } from '../RawBuffer'; -import { FacilityNDB } from '../facility/FacilityNDB'; +import { FacilityNDB } from '../datastructures/FacilityNDB'; import { RecvFacilitiesList } from './RecvFacilitiesList'; export class RecvNDBList extends RecvFacilitiesList { diff --git a/src/recv/RecvVORList.ts b/src/recv/RecvVORList.ts index 4826643..2a0c986 100644 --- a/src/recv/RecvVORList.ts +++ b/src/recv/RecvVORList.ts @@ -1,5 +1,5 @@ import { RawBuffer } from '../RawBuffer'; -import { FacilityVOR } from '../facility/FacilityVOR'; +import { FacilityVOR } from '../datastructures/FacilityVOR'; import { RecvFacilitiesList } from './RecvFacilitiesList'; export class RecvVORList extends RecvFacilitiesList { diff --git a/src/recv/RecvWaypointList.ts b/src/recv/RecvWaypointList.ts index a390db6..bfd2d3d 100644 --- a/src/recv/RecvWaypointList.ts +++ b/src/recv/RecvWaypointList.ts @@ -1,5 +1,5 @@ import { RawBuffer } from '../RawBuffer'; -import { FacilityWaypoint } from '../facility/FacilityWaypoint'; +import { FacilityWaypoint } from '../datastructures/FacilityWaypoint'; import { RecvFacilitiesList } from './RecvFacilitiesList'; export class RecvWaypointList extends RecvFacilitiesList {