2023-07-12 22:41:31 +00:00
|
|
|
|
using MSFSPopoutPanelManager.DomainModel.Profile;
|
|
|
|
|
using System;
|
2022-07-23 19:23:32 +00:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.Threading;
|
2023-07-30 21:30:47 +00:00
|
|
|
|
using static MSFSPopoutPanelManager.SimConnectAgent.SimDataDefinitions;
|
2022-07-23 19:23:32 +00:00
|
|
|
|
|
|
|
|
|
namespace MSFSPopoutPanelManager.SimConnectAgent
|
|
|
|
|
{
|
|
|
|
|
public class SimConnectProvider
|
|
|
|
|
{
|
|
|
|
|
private const int MSFS_DATA_REFRESH_TIMEOUT = 500;
|
2023-07-12 22:41:31 +00:00
|
|
|
|
private const int MSFS_HUDBAR_DATA_REFRESH_TIMEOUT = 100;
|
2022-07-23 19:23:32 +00:00
|
|
|
|
|
|
|
|
|
private SimConnector _simConnector;
|
|
|
|
|
|
|
|
|
|
private bool _isHandlingCriticalError;
|
2023-07-12 22:41:31 +00:00
|
|
|
|
private List<SimDataItem> _requiedSimData;
|
2022-07-23 19:23:32 +00:00
|
|
|
|
|
2023-07-12 22:41:31 +00:00
|
|
|
|
private System.Timers.Timer _requiredRequestDataTimer;
|
|
|
|
|
private System.Timers.Timer _hudBarRequestDataTimer;
|
2022-07-23 19:23:32 +00:00
|
|
|
|
private bool _isPowerOnForPopOut;
|
|
|
|
|
private bool _isAvionicsOnForPopOut;
|
|
|
|
|
private bool _isTrackIRManaged;
|
2023-07-12 22:41:31 +00:00
|
|
|
|
private bool _isHudBarDataActive;
|
|
|
|
|
private HudBarType _activeHudBarType;
|
2022-07-23 19:23:32 +00:00
|
|
|
|
|
|
|
|
|
public event EventHandler OnConnected;
|
|
|
|
|
public event EventHandler OnDisconnected;
|
2022-08-19 16:03:12 +00:00
|
|
|
|
public event EventHandler<bool> OnIsInCockpitChanged;
|
2022-07-23 19:23:32 +00:00
|
|
|
|
public event EventHandler OnFlightStarted;
|
|
|
|
|
public event EventHandler OnFlightStopped;
|
2023-07-12 22:41:31 +00:00
|
|
|
|
public event EventHandler<List<SimDataItem>> OnSimConnectDataRequiredRefreshed;
|
|
|
|
|
public event EventHandler<List<SimDataItem>> OnSimConnectDataHudBarRefreshed;
|
|
|
|
|
public event EventHandler<string> OnActiveAircraftChanged;
|
2022-07-23 19:23:32 +00:00
|
|
|
|
|
|
|
|
|
public SimConnectProvider()
|
|
|
|
|
{
|
|
|
|
|
_simConnector = new SimConnector();
|
|
|
|
|
_simConnector.OnConnected += HandleSimConnected;
|
|
|
|
|
_simConnector.OnDisconnected += HandleSimDisonnected;
|
|
|
|
|
_simConnector.OnException += HandleSimException;
|
|
|
|
|
_simConnector.OnReceiveSystemEvent += HandleReceiveSystemEvent;
|
2023-07-12 22:41:31 +00:00
|
|
|
|
_simConnector.OnReceivedRequiredData += HandleRequiredDataReceived;
|
|
|
|
|
_simConnector.OnReceivedHudBarData += HandleHudBarDataReceived;
|
|
|
|
|
_simConnector.OnActiveAircraftChanged += (sender, e) => OnActiveAircraftChanged?.Invoke(this, e);
|
2022-07-23 19:23:32 +00:00
|
|
|
|
|
|
|
|
|
_isHandlingCriticalError = false;
|
2023-07-12 22:41:31 +00:00
|
|
|
|
|
|
|
|
|
_isHudBarDataActive = false;
|
|
|
|
|
_activeHudBarType = HudBarType.None;
|
2022-07-23 19:23:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Start()
|
|
|
|
|
{
|
2023-07-12 22:41:31 +00:00
|
|
|
|
_simConnector.Stop();
|
|
|
|
|
Thread.Sleep(2000); // wait for everything to stop
|
2022-07-23 19:23:32 +00:00
|
|
|
|
_simConnector.Start();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Stop(bool appExit)
|
|
|
|
|
{
|
|
|
|
|
_simConnector.Stop();
|
|
|
|
|
|
|
|
|
|
if (!appExit)
|
|
|
|
|
OnDisconnected?.Invoke(this, null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void StopAndReconnect()
|
|
|
|
|
{
|
|
|
|
|
_simConnector.Stop();
|
|
|
|
|
Thread.Sleep(2000); // wait for everything to stop
|
|
|
|
|
_simConnector.Restart();
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-12 22:41:31 +00:00
|
|
|
|
public void SetHudBarConfig(HudBarType hudBarType)
|
|
|
|
|
{
|
|
|
|
|
if (_hudBarRequestDataTimer.Enabled && _activeHudBarType == hudBarType)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
_activeHudBarType = hudBarType;
|
|
|
|
|
_isHudBarDataActive = true;
|
|
|
|
|
|
|
|
|
|
// shut down data request and wait for the last request to be completed
|
|
|
|
|
_hudBarRequestDataTimer.Stop();
|
|
|
|
|
Thread.Sleep(MSFS_HUDBAR_DATA_REFRESH_TIMEOUT);
|
|
|
|
|
|
|
|
|
|
switch (hudBarType)
|
|
|
|
|
{
|
|
|
|
|
case HudBarType.Generic_Aircraft:
|
|
|
|
|
_simConnector.SetSimConnectHudBarDataDefinition(SimDataDefinitionType.GenericHudBar);
|
|
|
|
|
_hudBarRequestDataTimer.Start();
|
|
|
|
|
break;
|
|
|
|
|
case HudBarType.PMDG_737:
|
|
|
|
|
_simConnector.SetSimConnectHudBarDataDefinition(SimDataDefinitionType.PMDG737HudBar);
|
|
|
|
|
_hudBarRequestDataTimer.Start();
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
_simConnector.SetSimConnectHudBarDataDefinition(SimDataDefinitionType.NoHudBar);
|
|
|
|
|
_hudBarRequestDataTimer.Stop();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void StopHudBar()
|
|
|
|
|
{
|
|
|
|
|
_hudBarRequestDataTimer.Stop();
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-23 19:23:32 +00:00
|
|
|
|
public void TurnOnPower(bool isRequiredForColdStart)
|
|
|
|
|
{
|
2023-07-12 22:41:31 +00:00
|
|
|
|
if (!isRequiredForColdStart || _requiedSimData == null)
|
2022-07-23 19:23:32 +00:00
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
// Wait for _simData.AtcOnParkingSpot to refresh
|
|
|
|
|
Thread.Sleep(MSFS_DATA_REFRESH_TIMEOUT + 500);
|
|
|
|
|
|
2023-07-12 22:41:31 +00:00
|
|
|
|
var planeInParkingSpot = Convert.ToBoolean(_requiedSimData.Find(d => d.PropertyName == SimDataDefinitions.PropName.PlaneInParkingSpot).Value);
|
2022-07-23 19:23:32 +00:00
|
|
|
|
|
2023-07-12 22:41:31 +00:00
|
|
|
|
if (isRequiredForColdStart && planeInParkingSpot)
|
2022-07-23 19:23:32 +00:00
|
|
|
|
{
|
2023-07-12 22:41:31 +00:00
|
|
|
|
Debug.WriteLine("Turn On Battery Power...");
|
2022-07-23 19:23:32 +00:00
|
|
|
|
|
|
|
|
|
_isPowerOnForPopOut = true;
|
2023-07-12 22:41:31 +00:00
|
|
|
|
_simConnector.TransmitActionEvent(ActionEvent.MASTER_BATTERY_SET, 1);
|
2022-07-23 19:23:32 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-12 22:41:31 +00:00
|
|
|
|
public void TurnOffPower(bool isRequiredForColdStart)
|
2022-07-23 19:23:32 +00:00
|
|
|
|
{
|
2023-07-12 22:41:31 +00:00
|
|
|
|
if (!isRequiredForColdStart || _requiedSimData == null)
|
2022-07-23 19:23:32 +00:00
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (_isPowerOnForPopOut)
|
|
|
|
|
{
|
2023-07-12 22:41:31 +00:00
|
|
|
|
Debug.WriteLine("Turn Off Battery Power...");
|
2022-07-23 19:23:32 +00:00
|
|
|
|
|
2023-07-12 22:41:31 +00:00
|
|
|
|
_simConnector.TransmitActionEvent(ActionEvent.MASTER_BATTERY_SET, 0);
|
2022-07-23 19:23:32 +00:00
|
|
|
|
_isPowerOnForPopOut = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void TurnOnAvionics(bool isRequiredForColdStart)
|
|
|
|
|
{
|
2023-07-12 22:41:31 +00:00
|
|
|
|
if (!isRequiredForColdStart || _requiedSimData == null)
|
2022-07-23 19:23:32 +00:00
|
|
|
|
return;
|
|
|
|
|
|
2023-07-12 22:41:31 +00:00
|
|
|
|
var planeInParkingSpot = Convert.ToBoolean(_requiedSimData.Find(d => d.PropertyName == SimDataDefinitions.PropName.PlaneInParkingSpot).Value);
|
2022-07-23 19:23:32 +00:00
|
|
|
|
|
2023-07-12 22:41:31 +00:00
|
|
|
|
Debug.WriteLine("Turn On Avionics...");
|
2022-07-23 19:23:32 +00:00
|
|
|
|
|
2023-07-12 22:41:31 +00:00
|
|
|
|
if (isRequiredForColdStart && planeInParkingSpot)
|
2022-07-23 19:23:32 +00:00
|
|
|
|
{
|
|
|
|
|
_isAvionicsOnForPopOut = true;
|
2023-07-12 22:41:31 +00:00
|
|
|
|
_simConnector.TransmitActionEvent(ActionEvent.AVIONICS_MASTER_SET, 1);
|
2022-07-23 19:23:32 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void TurnOffAvionics(bool isRequiredForColdStart)
|
|
|
|
|
{
|
2023-07-12 22:41:31 +00:00
|
|
|
|
if (!isRequiredForColdStart || _requiedSimData == null)
|
2022-07-23 19:23:32 +00:00
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (_isAvionicsOnForPopOut)
|
|
|
|
|
{
|
2023-07-12 22:41:31 +00:00
|
|
|
|
Debug.WriteLine("Turn Off Avionics...");
|
2022-07-23 19:23:32 +00:00
|
|
|
|
|
2023-07-12 22:41:31 +00:00
|
|
|
|
_simConnector.TransmitActionEvent(ActionEvent.AVIONICS_MASTER_SET, 0);
|
2022-07-23 19:23:32 +00:00
|
|
|
|
_isAvionicsOnForPopOut = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-12 22:41:31 +00:00
|
|
|
|
public void TurnOnTrackIR()
|
|
|
|
|
{
|
|
|
|
|
if (_requiedSimData != null)
|
|
|
|
|
{
|
|
|
|
|
var trackIREnable = Convert.ToBoolean(_requiedSimData.Find(d => d.PropertyName == SimDataDefinitions.PropName.TrackIREnable).Value);
|
|
|
|
|
|
|
|
|
|
if (_isTrackIRManaged)
|
|
|
|
|
{
|
|
|
|
|
Debug.WriteLine("Turn On TrackIR...");
|
|
|
|
|
SetTrackIREnable(true);
|
|
|
|
|
_isTrackIRManaged = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-23 19:23:32 +00:00
|
|
|
|
public void TurnOffTrackIR()
|
|
|
|
|
{
|
2023-07-12 22:41:31 +00:00
|
|
|
|
if (_requiedSimData != null)
|
2022-07-23 19:23:32 +00:00
|
|
|
|
{
|
2023-07-12 22:41:31 +00:00
|
|
|
|
var trackIREnable = Convert.ToBoolean(_requiedSimData.Find(d => d.PropertyName == SimDataDefinitions.PropName.TrackIREnable).Value);
|
2022-07-23 19:23:32 +00:00
|
|
|
|
|
|
|
|
|
if (trackIREnable)
|
|
|
|
|
{
|
2023-07-12 22:41:31 +00:00
|
|
|
|
Debug.WriteLine("Turn Off TrackIR...");
|
2022-07-23 19:23:32 +00:00
|
|
|
|
|
|
|
|
|
SetTrackIREnable(false);
|
|
|
|
|
_isTrackIRManaged = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Thread.Sleep(200);
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
Debug.WriteLine($"Acitve Pause On...");
|
2022-07-23 19:23:32 +00:00
|
|
|
|
|
2023-07-12 22:41:31 +00:00
|
|
|
|
_simConnector.TransmitActionEvent(ActionEvent.PAUSE_SET, 1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void TurnOffActivePause()
|
|
|
|
|
{
|
|
|
|
|
Debug.WriteLine($"Acitve Pause Off...");
|
2022-07-23 19:23:32 +00:00
|
|
|
|
|
2023-07-12 22:41:31 +00:00
|
|
|
|
_simConnector.TransmitActionEvent(ActionEvent.PAUSE_SET, 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void IncreaseSimRate()
|
|
|
|
|
{
|
|
|
|
|
_simConnector.TransmitActionEvent(ActionEvent.SIM_RATE_INCR, 1);
|
|
|
|
|
Thread.Sleep(200);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void DecreaseSimRate()
|
|
|
|
|
{
|
|
|
|
|
_simConnector.TransmitActionEvent(ActionEvent.SIM_RATE_DECR, 1);
|
2022-07-23 19:23:32 +00:00
|
|
|
|
Thread.Sleep(200);
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-30 21:30:47 +00:00
|
|
|
|
public void SetCockpitCameraZoomLevel(int zoomLevel)
|
|
|
|
|
{
|
|
|
|
|
_simConnector.SetDataObject(WriteableVariableName.CockpitCameraZoom, Convert.ToDouble(zoomLevel));
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-23 19:23:32 +00:00
|
|
|
|
private void SetTrackIREnable(bool enable)
|
|
|
|
|
{
|
2023-07-30 21:30:47 +00:00
|
|
|
|
_simConnector.SetDataObject(WriteableVariableName.TrackIREnable, enable ? Convert.ToDouble(1) : Convert.ToDouble(0));
|
2022-07-23 19:23:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void HandleSimConnected(object source, EventArgs e)
|
|
|
|
|
{
|
2023-07-12 22:41:31 +00:00
|
|
|
|
// Setup required data request timer
|
|
|
|
|
_requiredRequestDataTimer = new System.Timers.Timer();
|
|
|
|
|
_requiredRequestDataTimer.Interval = MSFS_DATA_REFRESH_TIMEOUT;
|
|
|
|
|
_requiredRequestDataTimer.Start();
|
|
|
|
|
_requiredRequestDataTimer.Elapsed += (sender, e) => { try { _simConnector.RequestRequiredData(); } catch { } };
|
|
|
|
|
_requiredRequestDataTimer.Elapsed += (sender, e) => { try { _simConnector.ReceiveMessage(); } catch { } };
|
|
|
|
|
|
|
|
|
|
// Setup hudbar data request timer
|
|
|
|
|
_hudBarRequestDataTimer = new System.Timers.Timer();
|
|
|
|
|
_hudBarRequestDataTimer.Interval = MSFS_HUDBAR_DATA_REFRESH_TIMEOUT;
|
|
|
|
|
_hudBarRequestDataTimer.Stop();
|
|
|
|
|
_hudBarRequestDataTimer.Elapsed += (sender, e) => { try { _simConnector.RequestHudBarData(); } catch { } }; ;
|
|
|
|
|
|
|
|
|
|
if (_isHudBarDataActive)
|
|
|
|
|
SetHudBarConfig(_activeHudBarType);
|
2022-08-13 06:14:49 +00:00
|
|
|
|
|
|
|
|
|
OnConnected?.Invoke(this, null);
|
2022-07-23 19:23:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void HandleSimDisonnected(object source, EventArgs e)
|
|
|
|
|
{
|
2023-07-12 22:41:31 +00:00
|
|
|
|
_requiredRequestDataTimer.Stop();
|
|
|
|
|
_hudBarRequestDataTimer.Stop();
|
2022-07-23 19:23:32 +00:00
|
|
|
|
OnDisconnected?.Invoke(this, null);
|
|
|
|
|
StopAndReconnect();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void HandleSimException(object source, string e)
|
|
|
|
|
{
|
2023-07-12 22:41:31 +00:00
|
|
|
|
_requiredRequestDataTimer.Stop();
|
|
|
|
|
_hudBarRequestDataTimer.Stop();
|
|
|
|
|
|
2022-07-23 19:23:32 +00:00
|
|
|
|
if (!_isHandlingCriticalError)
|
|
|
|
|
{
|
|
|
|
|
_isHandlingCriticalError = true; // Prevent restarting to occur in parallel
|
|
|
|
|
StopAndReconnect();
|
|
|
|
|
_isHandlingCriticalError = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-12 22:41:31 +00:00
|
|
|
|
private void HandleRequiredDataReceived(object sender, List<SimDataItem> e)
|
2022-07-23 19:23:32 +00:00
|
|
|
|
{
|
2023-07-12 22:41:31 +00:00
|
|
|
|
_requiedSimData = e;
|
|
|
|
|
DetectFlightStartedOrStopped(e);
|
|
|
|
|
OnSimConnectDataRequiredRefreshed?.Invoke(this, e);
|
2022-07-23 19:23:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-07-12 22:41:31 +00:00
|
|
|
|
private void HandleHudBarDataReceived(object sender, List<SimDataItem> e)
|
2022-07-23 19:23:32 +00:00
|
|
|
|
{
|
2023-07-12 22:41:31 +00:00
|
|
|
|
OnSimConnectDataHudBarRefreshed?.Invoke(this, e);
|
2022-07-23 19:23:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private const int CAMERA_STATE_COCKPIT = 2;
|
|
|
|
|
private const int CAMERA_STATE_LOAD_SCREEN = 11;
|
|
|
|
|
private const int CAMERA_STATE_HOME_SCREEN = 15;
|
2022-07-23 20:04:06 +00:00
|
|
|
|
private int _currentCameraState = -1;
|
2022-07-23 19:23:32 +00:00
|
|
|
|
|
2023-07-12 22:41:31 +00:00
|
|
|
|
private void DetectFlightStartedOrStopped(List<SimDataItem> simData)
|
2022-07-23 19:23:32 +00:00
|
|
|
|
{
|
|
|
|
|
// Determine is flight started or ended
|
2023-07-12 22:41:31 +00:00
|
|
|
|
var cameraState = Convert.ToInt32(simData.Find(d => d.PropertyName == SimDataDefinitions.PropName.CameraState).Value);
|
2022-07-23 19:23:32 +00:00
|
|
|
|
|
|
|
|
|
if (_currentCameraState == cameraState)
|
|
|
|
|
return;
|
|
|
|
|
|
2022-08-19 16:03:12 +00:00
|
|
|
|
if (cameraState == CAMERA_STATE_COCKPIT)
|
|
|
|
|
OnIsInCockpitChanged?.Invoke(this, true);
|
|
|
|
|
|
2022-07-23 20:04:06 +00:00
|
|
|
|
switch (_currentCameraState)
|
2022-07-23 19:23:32 +00:00
|
|
|
|
{
|
2022-07-23 20:04:06 +00:00
|
|
|
|
case CAMERA_STATE_HOME_SCREEN:
|
|
|
|
|
case CAMERA_STATE_LOAD_SCREEN:
|
|
|
|
|
if (cameraState == CAMERA_STATE_COCKPIT)
|
|
|
|
|
{
|
|
|
|
|
_currentCameraState = cameraState;
|
|
|
|
|
OnFlightStarted?.Invoke(this, null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
case CAMERA_STATE_COCKPIT:
|
2022-08-19 16:03:12 +00:00
|
|
|
|
if (cameraState == CAMERA_STATE_LOAD_SCREEN || cameraState == CAMERA_STATE_HOME_SCREEN)
|
2022-07-23 20:04:06 +00:00
|
|
|
|
{
|
|
|
|
|
_currentCameraState = cameraState;
|
|
|
|
|
OnFlightStopped?.Invoke(this, null);
|
2022-08-19 16:03:12 +00:00
|
|
|
|
OnIsInCockpitChanged?.Invoke(this, false);
|
2023-07-12 22:41:31 +00:00
|
|
|
|
|
|
|
|
|
_isHudBarDataActive = false;
|
|
|
|
|
_hudBarRequestDataTimer.Stop();
|
2022-07-23 20:04:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
break;
|
2022-07-23 19:23:32 +00:00
|
|
|
|
}
|
2022-07-23 20:04:06 +00:00
|
|
|
|
|
2022-08-19 16:03:12 +00:00
|
|
|
|
if (cameraState == CAMERA_STATE_COCKPIT || cameraState == CAMERA_STATE_HOME_SCREEN || cameraState == CAMERA_STATE_LOAD_SCREEN)
|
2022-07-23 20:04:06 +00:00
|
|
|
|
_currentCameraState = cameraState;
|
2022-07-23 19:23:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-07-12 22:41:31 +00:00
|
|
|
|
private void HandleReceiveSystemEvent(object sender, SimConnectEvent e)
|
2022-07-23 19:23:32 +00:00
|
|
|
|
{
|
|
|
|
|
// TBD
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|