From 687cb12ae751d02a731503560621eead6e7284e4 Mon Sep 17 00:00:00 2001 From: Adam Kapos Date: Sun, 14 Feb 2021 23:14:52 +0200 Subject: [PATCH] Get aircraft data from SimConnect --- FlightStreamDeck.Logics/IFlightConnector.cs | 18 +++++++ .../SimConnectFlightConnector.cs | 52 +++++++++++++++++++ FlightStreamDeck.SimConnectFSX/Structs.cs | 1 - 3 files changed, 70 insertions(+), 1 deletion(-) diff --git a/FlightStreamDeck.Logics/IFlightConnector.cs b/FlightStreamDeck.Logics/IFlightConnector.cs index b878999..6c0443f 100644 --- a/FlightStreamDeck.Logics/IFlightConnector.cs +++ b/FlightStreamDeck.Logics/IFlightConnector.cs @@ -6,6 +6,7 @@ namespace FlightStreamDeck.Logics { public interface IFlightConnector { + event EventHandler AircraftDataUpdated; event EventHandler AircraftStatusUpdated; event EventHandler GenericValuesUpdated; void ApOff(); @@ -48,6 +49,16 @@ public interface IFlightConnector void RegisterSimValues(params (TOGGLE_VALUE variables, string unit)[] simValues); void DeRegisterSimValues(params (TOGGLE_VALUE variables, string unit)[] simValues); } + public class AircraftDataUpdatedEventArgs : EventArgs + { + public AircraftDataUpdatedEventArgs(AircraftData aircraftData) + { + AircraftData = aircraftData; + } + + public AircraftData AircraftData { get; } + } + public class AircraftStatusUpdatedEventArgs : EventArgs { @@ -69,6 +80,13 @@ public ToggleValueUpdatedEventArgs(Dictionary<(TOGGLE_VALUE variable, string uni public Dictionary<(TOGGLE_VALUE variable, string unit), double> GenericValueStatus { get; } } + public class AircraftData + { + public string Type { get; set; } + public string Model { get; set; } + public string Title { get; set; } + } + public class AircraftStatus { public string Callsign { get; set; } diff --git a/FlightStreamDeck.SimConnectFSX/SimConnectFlightConnector.cs b/FlightStreamDeck.SimConnectFSX/SimConnectFlightConnector.cs index f034f93..adfb7a0 100644 --- a/FlightStreamDeck.SimConnectFSX/SimConnectFlightConnector.cs +++ b/FlightStreamDeck.SimConnectFSX/SimConnectFlightConnector.cs @@ -13,6 +13,7 @@ namespace FlightStreamDeck.SimConnectFSX { public class SimConnectFlightConnector : IFlightConnector { + public event EventHandler AircraftDataUpdated; public event EventHandler AircraftStatusUpdated; public event EventHandler GenericValuesUpdated; @@ -97,6 +98,7 @@ public void Initialize(IntPtr Handle) simconnect.OnRecvSimobjectDataBytype += Simconnect_OnRecvSimobjectDataBytypeAsync; simconnect.OnRecvSystemState += Simconnect_OnRecvSystemState; + RegisterAircraftDataDefinition(); RegisterFlightStatusDefinition(); simconnect.MapClientEventToSimEvent(EVENTS.AUTOPILOT_ON, "AUTOPILOT_ON"); @@ -311,6 +313,32 @@ public void CloseConnection() } } + private void RegisterAircraftDataDefinition() + { + simconnect.AddToDataDefinition(DEFINITIONS.AircraftData, + "ATC TYPE", + null, + SIMCONNECT_DATATYPE.STRING32, + 0.0f, + SimConnect.SIMCONNECT_UNUSED); + simconnect.AddToDataDefinition(DEFINITIONS.AircraftData, + "ATC MODEL", + null, + SIMCONNECT_DATATYPE.STRING32, + 0.0f, + SimConnect.SIMCONNECT_UNUSED); + simconnect.AddToDataDefinition(DEFINITIONS.AircraftData, + "TITLE", + null, + SIMCONNECT_DATATYPE.STRING256, + 0.0f, + SimConnect.SIMCONNECT_UNUSED); + + // IMPORTANT: register it with the simconnect managed wrapper marshaller + // if you skip this step, you will only receive a uint in the .dwData field. + simconnect.RegisterDataDefineStruct(DEFINITIONS.AircraftData); + } + private void RegisterFlightStatusDefinition() { simconnect.AddToDataDefinition(DEFINITIONS.FlightStatus, @@ -593,6 +621,29 @@ private void Simconnect_OnRecvSimobjectDataBytypeAsync(SimConnect sender, SIMCON // Must be general SimObject information switch (data.dwRequestID) { + case (uint)DATA_REQUESTS.AIRCRAFT_DATA: + { + var aircraftData = data.dwData[0] as AircraftDataStruct?; + + if (aircraftData.HasValue) + { + logger.LogTrace("Get Aircraft data"); + AircraftDataUpdated?.Invoke(this, new AircraftDataUpdatedEventArgs( + new AircraftData + { + Type = aircraftData.Value.Type, + Model = aircraftData.Value.Model, + Title = aircraftData.Value.Title, + })); + } + else + { + // Cast failed + logger.LogError($"Cannot cast to {nameof(AircraftDataStruct)}!"); + } + } + break; + case (uint)DATA_REQUESTS.FLIGHT_STATUS: { var flightStatus = data.dwData[0] as FlightStatusStruct?; @@ -715,6 +766,7 @@ void Simconnect_OnRecvOpen(SimConnect sender, SIMCONNECT_RECV_OPEN data) try { cts?.Token.ThrowIfCancellationRequested(); + simconnect?.RequestDataOnSimObjectType(DATA_REQUESTS.AIRCRAFT_DATA, DEFINITIONS.AircraftData, 0, SIMCONNECT_SIMOBJECT_TYPE.USER); simconnect?.RequestDataOnSimObjectType(DATA_REQUESTS.FLIGHT_STATUS, DEFINITIONS.FlightStatus, 0, SIMCONNECT_SIMOBJECT_TYPE.USER); if (genericValues.Count > 0 && isGenericValueRegistered) diff --git a/FlightStreamDeck.SimConnectFSX/Structs.cs b/FlightStreamDeck.SimConnectFSX/Structs.cs index 29099ad..8614e56 100644 --- a/FlightStreamDeck.SimConnectFSX/Structs.cs +++ b/FlightStreamDeck.SimConnectFSX/Structs.cs @@ -68,7 +68,6 @@ struct AircraftDataStruct public string Model; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)] public string Title; - public double EstimatedCruiseSpeed; } [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)]