1
0
Fork 0
mirror of https://github.com/hawkeye-stan/msfs-popout-panel-manager.git synced 2024-11-24 14:50:10 +00:00
msfs-popout-panel-manager/Orchestration/FlightSimOrchestrator.cs

441 lines
16 KiB
C#
Raw Normal View History

2023-07-12 22:41:31 +00:00
using MSFSPopoutPanelManager.DomainModel.Profile;
using MSFSPopoutPanelManager.DomainModel.SimConnect;
using MSFSPopoutPanelManager.Shared;
2022-07-23 19:23:32 +00:00
using MSFSPopoutPanelManager.SimConnectAgent;
2023-07-12 22:41:31 +00:00
using MSFSPopoutPanelManager.WindowsAgent;
2022-07-23 19:23:32 +00:00
using System;
2023-07-12 22:41:31 +00:00
using System.Collections.Generic;
using System.Threading;
2022-07-23 19:23:32 +00:00
namespace MSFSPopoutPanelManager.Orchestration
{
public class FlightSimOrchestrator : ObservableObject
{
2023-07-12 22:41:31 +00:00
private const int MSFS_GAME_EXIT_DETECTION_INTERVAL = 3000;
private System.Timers.Timer _msfsGameExitDetectionTimer;
2022-07-23 19:23:32 +00:00
private SimConnectProvider _simConnectProvider;
2023-07-12 22:41:31 +00:00
private ProfileData _profileData;
private AppSettingData _appSettingData;
private FlightSimData _flightSimData;
private bool _isTurnedOnPower;
private bool _isTurnedOnAvionics;
public FlightSimOrchestrator(ProfileData profileData, AppSettingData appSettingData, FlightSimData flightSimData)
2022-07-23 19:23:32 +00:00
{
2023-07-12 22:41:31 +00:00
_profileData = profileData;
_appSettingData = appSettingData;
_flightSimData = flightSimData;
2022-07-23 19:23:32 +00:00
_simConnectProvider = new SimConnectProvider();
}
2023-07-12 22:41:31 +00:00
internal PanelPopOutOrchestrator PanelPopOutOrchestrator { get; set; }
2022-07-23 19:23:32 +00:00
2023-07-12 22:41:31 +00:00
internal PanelConfigurationOrchestrator PanelConfigurationOrchestrator { get; set; }
2022-07-23 19:23:32 +00:00
internal SimConnectProvider SimConnectProvider { get { return _simConnectProvider; } }
2023-07-12 22:41:31 +00:00
public event EventHandler OnSimulatorExited;
2022-07-23 19:23:32 +00:00
public void StartSimConnectServer()
{
2023-07-12 22:41:31 +00:00
if (_simConnectProvider == null)
_simConnectProvider = new SimConnectProvider();
2022-07-23 19:23:32 +00:00
_simConnectProvider.OnConnected += (sender, e) =>
{
2023-08-05 03:40:29 +00:00
_flightSimData.IsSimConnectActive = true;
2023-07-12 22:41:31 +00:00
_flightSimData.IsSimulatorStarted = true;
WindowProcessManager.GetSimulatorProcess(); // refresh simulator process
DetectMsfsExit();
2022-07-23 19:23:32 +00:00
};
2023-07-12 22:41:31 +00:00
2022-07-23 19:23:32 +00:00
_simConnectProvider.OnDisconnected += (sender, e) =>
{
2023-08-05 03:40:29 +00:00
_flightSimData.IsSimConnectActive = false;
2023-07-12 22:41:31 +00:00
WindowProcessManager.GetSimulatorProcess(); // refresh simulator process
_flightSimData.Reset();
2022-07-23 19:23:32 +00:00
};
2023-07-12 22:41:31 +00:00
2023-08-05 03:40:29 +00:00
_simConnectProvider.OnException += (sender, e) =>
{
_flightSimData.IsSimConnectActive = false;
};
2023-07-12 22:41:31 +00:00
_simConnectProvider.OnSimConnectDataRequiredRefreshed += (sender, e) =>
2022-07-23 19:23:32 +00:00
{
2023-07-12 22:41:31 +00:00
var electricalMasterBattery = Convert.ToBoolean(e.Find(d => d.PropertyName == SimDataDefinitions.PropName.ElectricalMasterBattery).Value);
if (electricalMasterBattery != _flightSimData.ElectricalMasterBatteryStatus)
_flightSimData.ElectricalMasterBatteryStatus = electricalMasterBattery;
2022-08-05 03:43:51 +00:00
2023-07-12 22:41:31 +00:00
var avionicsMasterSwitch = Convert.ToBoolean(e.Find(d => d.PropertyName == SimDataDefinitions.PropName.AvionicsMasterSwitch).Value);
if (avionicsMasterSwitch != _flightSimData.AvionicsMasterSwitchStatus)
_flightSimData.AvionicsMasterSwitchStatus = avionicsMasterSwitch;
2022-08-05 03:43:51 +00:00
2023-07-12 22:41:31 +00:00
var trackIR = Convert.ToBoolean(e.Find(d => d.PropertyName == SimDataDefinitions.PropName.TrackIREnable).Value);
if (trackIR != _flightSimData.TrackIRStatus)
_flightSimData.TrackIRStatus = trackIR;
2023-07-30 21:30:47 +00:00
var cockpitCameraZoom = Convert.ToInt32(e.Find(d => d.PropertyName == SimDataDefinitions.PropName.CockpitCameraZoom).Value);
if (cockpitCameraZoom != _flightSimData.CockpitCameraZoom)
_flightSimData.CockpitCameraZoom = cockpitCameraZoom;
2023-08-14 04:35:14 +00:00
var cameraViewTypeAndIndex1 = Convert.ToInt32(e.Find(d => d.PropertyName == SimDataDefinitions.PropName.CameraViewTypeAndIndex1).Value);
if (cameraViewTypeAndIndex1 != _flightSimData.CameraViewTypeAndIndex1)
_flightSimData.CameraViewTypeAndIndex1 = cameraViewTypeAndIndex1;
2023-07-12 22:41:31 +00:00
};
2022-07-23 19:23:32 +00:00
2023-07-12 22:41:31 +00:00
_simConnectProvider.OnSimConnectDataHudBarRefreshed += (sender, e) =>
{
if (_profileData.ActiveProfile.ProfileSetting.HudBarConfig.IsEnabled)
MapHudBarSimConnectData(e);
};
_simConnectProvider.OnActiveAircraftChanged += (sender, e) =>
{
var aircraftName = String.IsNullOrEmpty(e) ? null : e;
if (_flightSimData.AircraftName != aircraftName)
2022-07-23 19:23:32 +00:00
{
2023-07-12 22:41:31 +00:00
_flightSimData.AircraftName = aircraftName;
_profileData.RefreshProfile();
2022-07-23 19:23:32 +00:00
}
};
2023-07-12 22:41:31 +00:00
2022-08-19 16:03:12 +00:00
_simConnectProvider.OnFlightStarted += HandleOnFlightStarted;
2022-07-23 19:23:32 +00:00
_simConnectProvider.OnFlightStopped += HandleOnFlightStopped;
2023-07-12 22:41:31 +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);
2023-07-12 22:41:31 +00:00
_simConnectProvider = null;
2022-07-23 19:23:32 +00:00
}
2023-08-16 03:41:14 +00:00
public void TurnOnTrackIR()
2022-07-23 19:23:32 +00:00
{
2023-07-12 22:41:31 +00:00
if (_simConnectProvider == null)
return;
if (!_appSettingData.ApplicationSetting.TrackIRSetting.AutoDisableTrackIR)
return;
2023-08-16 03:41:14 +00:00
WorkflowStepWithMessage.Execute("Turning on TrackIR", () =>
2023-07-12 22:41:31 +00:00
{
2023-08-16 03:41:14 +00:00
int count = 0;
do
{
_simConnectProvider.TurnOnTrackIR();
Thread.Sleep(500);
count++;
}
while (!_flightSimData.TrackIRStatus && count < 5);
2023-07-12 22:41:31 +00:00
2023-08-16 03:41:14 +00:00
return _flightSimData.TrackIRStatus;
});
2022-07-23 19:23:32 +00:00
}
2023-08-16 03:41:14 +00:00
public void TurnOffTrackIR()
2022-07-23 19:23:32 +00:00
{
2023-07-12 22:41:31 +00:00
if (_simConnectProvider == null)
return;
if (!_appSettingData.ApplicationSetting.TrackIRSetting.AutoDisableTrackIR)
return;
2023-08-16 03:41:14 +00:00
WorkflowStepWithMessage.Execute("Turning off TrackIR", () =>
2023-07-12 22:41:31 +00:00
{
2023-08-16 03:41:14 +00:00
int count = 0;
do
{
_simConnectProvider.TurnOffTrackIR();
Thread.Sleep(500);
count++;
}
while (_flightSimData.TrackIRStatus && count < 5);
2023-07-12 22:41:31 +00:00
2023-08-16 03:41:14 +00:00
return !_flightSimData.TrackIRStatus;
});
2022-07-23 19:23:32 +00:00
}
public void TurnOnPower()
{
2023-07-12 22:41:31 +00:00
if (_simConnectProvider == null)
return;
if (_profileData.ActiveProfile == null || _flightSimData.ElectricalMasterBatteryStatus)
return;
_isTurnedOnPower = true;
2023-08-16 03:41:14 +00:00
WorkflowStepWithMessage.Execute("Turning on battery", () =>
2023-07-12 22:41:31 +00:00
{
2023-08-16 03:41:14 +00:00
int count = 0;
do
{
_simConnectProvider.TurnOnPower(_profileData.ActiveProfile.ProfileSetting.PowerOnRequiredForColdStart);
Thread.Sleep(500);
count++;
}
while (!_flightSimData.ElectricalMasterBatteryStatus && count < 10);
2023-07-12 22:41:31 +00:00
2023-08-16 03:41:14 +00:00
return _flightSimData.ElectricalMasterBatteryStatus;
});
2022-07-23 19:23:32 +00:00
}
2023-07-12 22:41:31 +00:00
public void TurnOffPower()
2022-07-23 19:23:32 +00:00
{
2023-07-12 22:41:31 +00:00
if (_simConnectProvider == null)
return;
if (_profileData.ActiveProfile == null || !_isTurnedOnPower)
return;
2023-08-16 03:41:14 +00:00
WorkflowStepWithMessage.Execute("Turning off battery", () =>
2023-07-12 22:41:31 +00:00
{
2023-08-16 03:41:14 +00:00
int count = 0;
do
{
_simConnectProvider.TurnOffPower(_profileData.ActiveProfile.ProfileSetting.PowerOnRequiredForColdStart);
Thread.Sleep(500);
count++;
}
while (_flightSimData.ElectricalMasterBatteryStatus && count < 10);
2023-07-12 22:41:31 +00:00
2023-08-16 03:41:14 +00:00
return !_flightSimData.ElectricalMasterBatteryStatus;
});
2023-07-12 22:41:31 +00:00
_isTurnedOnPower = false;
2022-07-23 19:23:32 +00:00
}
2023-07-12 22:41:31 +00:00
public void TurnOnAvionics()
2022-07-23 19:23:32 +00:00
{
2023-07-12 22:41:31 +00:00
if (_simConnectProvider == null)
return;
if (_profileData.ActiveProfile == null || _flightSimData.AvionicsMasterSwitchStatus)
return;
_isTurnedOnAvionics = true;
2023-08-16 03:41:14 +00:00
WorkflowStepWithMessage.Execute("Turning on avionics", () =>
2023-07-12 22:41:31 +00:00
{
2023-08-16 03:41:14 +00:00
int count = 0;
do
{
_simConnectProvider.TurnOnAvionics(_profileData.ActiveProfile.ProfileSetting.PowerOnRequiredForColdStart);
Thread.Sleep(500);
count++;
}
while (!_flightSimData.AvionicsMasterSwitchStatus && count < 10);
2023-07-12 22:41:31 +00:00
2023-08-16 03:41:14 +00:00
return _flightSimData.AvionicsMasterSwitchStatus;
});
2022-07-23 19:23:32 +00:00
}
public void TurnOffAvionics()
{
2023-07-12 22:41:31 +00:00
if (_simConnectProvider == null)
return;
if (_profileData.ActiveProfile == null || !_isTurnedOnAvionics)
return;
2023-08-16 03:41:14 +00:00
WorkflowStepWithMessage.Execute("Turning off avionics", () =>
2023-07-12 22:41:31 +00:00
{
2023-08-16 03:41:14 +00:00
int count = 0;
do
{
_simConnectProvider.TurnOffAvionics(_profileData.ActiveProfile.ProfileSetting.PowerOnRequiredForColdStart);
Thread.Sleep(500);
count++;
}
while (_flightSimData.AvionicsMasterSwitchStatus && count < 10);
2023-07-12 22:41:31 +00:00
2023-08-16 03:41:14 +00:00
return !_flightSimData.AvionicsMasterSwitchStatus;
});
2023-07-12 22:41:31 +00:00
_isTurnedOnAvionics = false;
2022-07-23 19:23:32 +00:00
}
2023-07-12 22:41:31 +00:00
public void TurnOnActivePause()
2022-07-23 19:23:32 +00:00
{
2023-07-12 22:41:31 +00:00
if (_simConnectProvider == null)
return;
2022-07-23 19:23:32 +00:00
2023-07-12 22:41:31 +00:00
if (!_appSettingData.ApplicationSetting.PopOutSetting.AutoActivePause)
return;
2023-08-16 03:41:14 +00:00
WorkflowStepWithMessage.Execute("Turning on active pause", () =>
{
_simConnectProvider.TurnOnActivePause();
});
2023-07-12 22:41:31 +00:00
}
public void TurnOffActivePause()
{
if (_simConnectProvider == null)
return;
if (!_appSettingData.ApplicationSetting.PopOutSetting.AutoActivePause)
return;
2023-08-16 03:41:14 +00:00
WorkflowStepWithMessage.Execute("Turning off active pause", () =>
{
_simConnectProvider.TurnOffActivePause();
});
2023-07-12 22:41:31 +00:00
}
public void IncreaseSimRate()
{
if (_simConnectProvider == null)
return;
if (_flightSimData.HudBarData.SimRate == 16)
return;
_simConnectProvider.IncreaseSimRate();
}
public void DecreaseSimRate()
{
if (_simConnectProvider == null)
return;
if (_flightSimData.HudBarData.SimRate == 0.25)
return;
_simConnectProvider.DecreaseSimRate();
}
2023-07-30 21:30:47 +00:00
public void SetCockpitCameraZoomLevel(int zoomLevel)
{
_simConnectProvider.SetCockpitCameraZoomLevel(zoomLevel);
}
2023-08-16 03:41:14 +00:00
public void ResetCameraView()
{
_simConnectProvider.SetCameraRequestAction(1);
}
2023-07-12 22:41:31 +00:00
public void SetHudBarConfig()
{
if (_simConnectProvider == null)
return;
var hudBarType = _profileData.ActiveProfile.ProfileSetting.HudBarConfig.HudBarType;
switch (hudBarType)
{
case HudBarType.PMDG_737:
_flightSimData.HudBarData = new HudBarData737();
break;
case HudBarType.Generic_Aircraft:
default:
_flightSimData.HudBarData = new HudBarDataGeneric();
break;
}
_simConnectProvider.SetHudBarConfig(hudBarType);
}
public void StopHudBar()
{
_simConnectProvider.StopHudBar();
}
private void HandleOnFlightStarted(object sender, EventArgs e)
{
if (_appSettingData.ApplicationSetting.AutoPopOutSetting.IsEnabled)
PanelPopOutOrchestrator.AutoPopOut();
2022-07-23 19:23:32 +00:00
}
private void HandleOnFlightStopped(object sender, EventArgs e)
{
2023-07-12 22:41:31 +00:00
_profileData.ResetActiveProfile();
PanelConfigurationOrchestrator.EndConfiguration();
PanelConfigurationOrchestrator.EndTouchHook();
WindowActionManager.CloseAllPopOuts();
if (_flightSimData.HudBarData != null)
_flightSimData.HudBarData.Clear();
}
private void DetectMsfsExit()
{
_msfsGameExitDetectionTimer = new System.Timers.Timer();
_msfsGameExitDetectionTimer.Interval = MSFS_GAME_EXIT_DETECTION_INTERVAL;
_msfsGameExitDetectionTimer.Enabled = true;
_msfsGameExitDetectionTimer.Elapsed += (source, e) =>
{
WindowProcessManager.GetSimulatorProcess();
if (WindowProcessManager.SimulatorProcess == null)
{
if (_appSettingData.ApplicationSetting.GeneralSetting.AutoClose)
OnSimulatorExited?.Invoke(this, null);
else
{
_flightSimData.Reset();
_simConnectProvider.StopAndReconnect();
}
}
};
}
private void MapHudBarSimConnectData(List<SimDataItem> simData)
{
double newValue;
if (CompareSimConnectData(simData, SimDataDefinitions.PropName.ElevatorTrim, _flightSimData.HudBarData.ElevatorTrim, out newValue))
_flightSimData.HudBarData.ElevatorTrim = newValue;
if (CompareSimConnectData(simData, SimDataDefinitions.PropName.AileronTrim, _flightSimData.HudBarData.AileronTrim, out newValue))
_flightSimData.HudBarData.AileronTrim = newValue;
if (CompareSimConnectData(simData, SimDataDefinitions.PropName.RudderTrim, _flightSimData.HudBarData.RudderTrim, out newValue))
_flightSimData.HudBarData.RudderTrim = newValue;
if (CompareSimConnectData(simData, SimDataDefinitions.PropName.ParkingBrake, _flightSimData.HudBarData.ParkingBrake, out newValue))
_flightSimData.HudBarData.ParkingBrake = newValue;
if (CompareSimConnectData(simData, SimDataDefinitions.PropName.Flap, _flightSimData.HudBarData.Flap, out newValue))
_flightSimData.HudBarData.Flap = newValue;
if (CompareSimConnectData(simData, SimDataDefinitions.PropName.GearLeft, _flightSimData.HudBarData.GearLeft, out newValue))
_flightSimData.HudBarData.GearLeft = newValue;
if (CompareSimConnectData(simData, SimDataDefinitions.PropName.GearCenter, _flightSimData.HudBarData.GearCenter, out newValue))
_flightSimData.HudBarData.GearCenter = newValue;
if (CompareSimConnectData(simData, SimDataDefinitions.PropName.GearRight, _flightSimData.HudBarData.GearRight, out newValue))
_flightSimData.HudBarData.GearRight = newValue;
if (CompareSimConnectData(simData, SimDataDefinitions.PropName.SimRate, _flightSimData.HudBarData.SimRate, out newValue))
_flightSimData.HudBarData.SimRate = newValue;
}
private bool CompareSimConnectData(List<SimDataItem> simData, string propName, double source, out double newValue)
{
var propData = simData.Find(d => d.PropertyName == propName);
if (propData == null)
{
newValue = 0;
return false;
}
var value = Convert.ToDouble(simData.Find(d => d.PropertyName == propName).Value);
if (value != source)
{
newValue = value;
return true;
}
newValue = 0;
return false;
2022-07-23 19:23:32 +00:00
}
}
}