Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get aircraft data from SimConnect #180

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions FlightStreamDeck.Logics/IFlightConnector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ namespace FlightStreamDeck.Logics
{
public interface IFlightConnector
{
event EventHandler<AircraftDataUpdatedEventArgs> AircraftDataUpdated;
event EventHandler<AircraftStatusUpdatedEventArgs> AircraftStatusUpdated;
event EventHandler<ToggleValueUpdatedEventArgs> GenericValuesUpdated;
void ApOff();
Expand Down Expand Up @@ -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
{
Expand All @@ -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; }
Expand Down
52 changes: 52 additions & 0 deletions FlightStreamDeck.SimConnectFSX/SimConnectFlightConnector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ namespace FlightStreamDeck.SimConnectFSX
{
public class SimConnectFlightConnector : IFlightConnector
{
public event EventHandler<AircraftDataUpdatedEventArgs> AircraftDataUpdated;
public event EventHandler<AircraftStatusUpdatedEventArgs> AircraftStatusUpdated;
public event EventHandler<ToggleValueUpdatedEventArgs> GenericValuesUpdated;

Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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<AircraftDataStruct>(DEFINITIONS.AircraftData);
}

private void RegisterFlightStatusDefinition()
{
simconnect.AddToDataDefinition(DEFINITIONS.FlightStatus,
Expand Down Expand Up @@ -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?;
Expand Down Expand Up @@ -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)
Expand Down
1 change: 0 additions & 1 deletion FlightStreamDeck.SimConnectFSX/Structs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down