1
0
Fork 0
mirror of https://github.com/hawkeye-stan/msfs-popout-panel-manager.git synced 2024-11-22 05:40:11 +00:00
msfs-popout-panel-manager/Orchestration/FlightSimOrchestrator.cs

128 lines
4.7 KiB
C#
Raw Normal View History

2022-07-23 19:23:32 +00:00
using MSFSPopoutPanelManager.Shared;
using MSFSPopoutPanelManager.SimConnectAgent;
using System;
namespace MSFSPopoutPanelManager.Orchestration
{
public class FlightSimOrchestrator : ObservableObject
{
private SimConnectProvider _simConnectProvider;
public FlightSimOrchestrator()
{
_simConnectProvider = new SimConnectProvider();
}
internal ProfileData ProfileData { get; set; }
internal AppSettingData AppSettingData { get; set; }
internal FlightSimData FlightSimData { get; set; }
internal SimConnectProvider SimConnectProvider { get { return _simConnectProvider; } }
public event EventHandler OnSimulatorStarted;
public event EventHandler OnSimulatorStopped;
public event EventHandler OnFlightStarted;
public event EventHandler OnFlightStopped;
public event EventHandler OnFlightStartedForAutoPopOut;
public void StartSimConnectServer()
{
_simConnectProvider.OnConnected += (sender, e) =>
{
FlightSimData.IsSimulatorStarted = true;
OnSimulatorStarted?.Invoke(this, null);
};
_simConnectProvider.OnDisconnected += (sender, e) =>
{
FlightSimData.IsSimulatorStarted = false;
FlightSimData.ClearData();
OnSimulatorStopped?.Invoke(this, null);
};
_simConnectProvider.OnSimConnectDataRefreshed += (sender, e) =>
{
2022-08-01 23:21:42 +00:00
var aircraftName = Convert.ToString(e.Find(d => d.PropName == "AircraftName").Value);
aircraftName = String.IsNullOrEmpty(aircraftName) ? null : aircraftName;
2022-07-23 19:23:32 +00:00
var electricalMasterBattery = Convert.ToBoolean(e.Find(d => d.PropName == "ElectricalMasterBattery").Value);
2022-08-05 03:43:51 +00:00
var liveryName = Convert.ToString(e.Find(d => d.PropName == "Title").Value);
if (electricalMasterBattery != FlightSimData.ElectricalMasterBatteryStatus)
FlightSimData.ElectricalMasterBatteryStatus = electricalMasterBattery;
if (liveryName != FlightSimData.CurrentMsfsLiveryTitle)
{
FlightSimData.CurrentMsfsLiveryTitle = liveryName;
ProfileData.MigrateLiveryToAircraftBinding(liveryName, aircraftName);
}
2022-07-23 19:23:32 +00:00
2022-08-01 23:21:42 +00:00
// Automatic switching of active profile when SimConnect active aircraft change
if (FlightSimData.CurrentMsfsAircraft != aircraftName)
2022-07-23 19:23:32 +00:00
{
2022-08-01 23:21:42 +00:00
FlightSimData.CurrentMsfsAircraft = aircraftName;
2022-08-13 06:14:49 +00:00
ProfileData.AutoSwitchProfile();
2022-07-23 19:23:32 +00:00
}
};
2022-08-19 16:03:12 +00:00
_simConnectProvider.OnFlightStarted += HandleOnFlightStarted;
2022-07-23 19:23:32 +00:00
_simConnectProvider.OnFlightStopped += HandleOnFlightStopped;
2022-08-19 16:03:12 +00:00
_simConnectProvider.OnIsInCockpitChanged += (sender, e) => FlightSimData.IsInCockpit = e;
2022-07-23 19:23:32 +00:00
_simConnectProvider.Start();
}
public void EndSimConnectServer(bool appExit)
{
_simConnectProvider.Stop(appExit);
}
public void TurnOnTrackIR()
{
if (AppSettingData.AppSetting.AutoDisableTrackIR)
_simConnectProvider.TurnOnTrackIR();
}
public void TurnOffTrackIR()
{
if (AppSettingData.AppSetting.AutoDisableTrackIR)
_simConnectProvider.TurnOffTrackIR();
}
public void TurnOnPower()
{
if (ProfileData.ActiveProfile != null)
_simConnectProvider.TurnOnPower(ProfileData.ActiveProfile.PowerOnRequiredForColdStart);
}
public void TurnOnAvionics()
{
if (ProfileData.ActiveProfile != null)
_simConnectProvider.TurnOnAvionics(ProfileData.ActiveProfile.PowerOnRequiredForColdStart);
}
public void TurnOffPower()
{
if (ProfileData.ActiveProfile != null)
_simConnectProvider.TurnOffpower(ProfileData.ActiveProfile.PowerOnRequiredForColdStart);
}
public void TurnOffAvionics()
{
if (ProfileData.ActiveProfile != null)
_simConnectProvider.TurnOffAvionics(ProfileData.ActiveProfile.PowerOnRequiredForColdStart);
}
2022-08-19 16:03:12 +00:00
private void HandleOnFlightStarted(object sender, EventArgs e)
2022-07-23 19:23:32 +00:00
{
2022-08-19 16:03:12 +00:00
OnFlightStarted?.Invoke(this, null);
2022-07-23 19:23:32 +00:00
2022-08-19 16:03:12 +00:00
if (AppSettingData.AppSetting.AutoPopOutPanels)
OnFlightStartedForAutoPopOut?.Invoke(this, null);
2022-07-23 19:23:32 +00:00
}
private void HandleOnFlightStopped(object sender, EventArgs e)
{
OnFlightStopped?.Invoke(this, null);
}
}
}