<commit_before>﻿namespace FsxWebApi.Infrastructure
{
    using System;
    using FsxConfig;
    using FsxConfig.Enums;
    using Interfaces;
    using Microsoft.FlightSimulator.SimConnect;
    using Model;

    public class FsxCommunicator
    {
        private readonly ILogger _logger;
        // SimConnect object
        private SimConnect _simConnect;
        private bool _receivedMessage = false;
        private PlaneData _planeData;

        public FsxCommunicator(ILogger logger)
        {
            _logger = logger;
        }

        public PlaneData GetLocation()
        {
            if (_simConnect == null)
                _simConnect = FsxFactory.GetSimConnectObject(this);

            if (_simConnect == null)
                return null;

            _simConnect.RequestDataOnSimObjectType(DataRequest.FromBrowser, Definition.Plane, 0, SIMCONNECT_SIMOBJECT_TYPE.USER);
            
            // ReceiveMessage must be called to trigger the events. 
            do
            {
                try
                {
                    _simConnect.ReceiveMessage();
                }
                catch (Exception)
                {
                    return null;
                }
            } while (!_receivedMessage);

            _receivedMessage = false;

            return _planeData;
        }

        public void CloseConnection()
        {
            CloseFsxConnection();
        }

        public void Fsx_ReceiveDataEventHandler(SimConnect sender, SIMCONNECT_RECV_SIMOBJECT_DATA_BYTYPE fsxData)
        {

            switch ((DataRequest)fsxData.dwRequestID)
            {
                case DataRequest.FromBrowser:
                    PlaneDataStruct userPlaneData = (PlaneDataStruct)fsxData.dwData[0];

                    Location location = new Location
                    {
                        Latitude = userPlaneData.Latitude,
                        Longitude = userPlaneData.Longitude,
                        Altitude = userPlaneData.Altitude
                    };

                    _planeData = new PlaneData
                    {
                        FuelLevel = userPlaneData.Fuel,
                        Speed = userPlaneData.Speed,
                        Location = location
                    };

                    break;
                default:
                    throw new ArgumentOutOfRangeException();
            }

            _receivedMessage = true;
        }


        // The case where the user closes FSX
        public void Fsx_UserClosedFsxEventHandler(SimConnect sender, SIMCONNECT_RECV data)
        {
            CloseFsxConnection();
        }

        public void Fsx_ExceptionEventHandler(SimConnect sender, SIMCONNECT_RECV_EXCEPTION data)
        {
            // handle exceptions (save to file/display message)
        }


        private void CloseFsxConnection()
        {
            if (_simConnect == null) return;

            _simConnect.Dispose();
            _simConnect = null;
        }
    }
}<commit_msg>Added msgs logging<commit_after>﻿namespace FsxWebApi.Infrastructure
{
    using System;
    using FsxConfig;
    using FsxConfig.Enums;
    using Interfaces;
    using Microsoft.FlightSimulator.SimConnect;
    using Model;

    public class FsxCommunicator
    {
        private readonly ILogger _logger;
        // SimConnect object
        private SimConnect _simConnect;
        private bool _receivedMessage = false;
        private PlaneData _planeData;

        public FsxCommunicator(ILogger logger)
        {
            _logger = logger;
        }

        public PlaneData GetLocation()
        {
            if (_simConnect == null)
                _simConnect = FsxFactory.GetSimConnectObject(this);

            if (_simConnect == null)
            {
                _logger.Log("Couldn't connect to the FSX.");
                return null;                
            }

            _simConnect.RequestDataOnSimObjectType(DataRequest.FromBrowser, Definition.Plane, 0, SIMCONNECT_SIMOBJECT_TYPE.USER);
            
            // ReceiveMessage must be called to trigger the events. 
            do
            {
                try
                {
                    _simConnect.ReceiveMessage();
                }
                catch (Exception)
                {
                    return null;
                }
            } while (!_receivedMessage);

            _receivedMessage = false;

            return _planeData;
        }

        public void Fsx_ReceiveDataEventHandler(SimConnect sender, SIMCONNECT_RECV_SIMOBJECT_DATA_BYTYPE fsxData)
        {

            switch ((DataRequest)fsxData.dwRequestID)
            {
                case DataRequest.FromBrowser:
                    PlaneDataStruct userPlaneData = (PlaneDataStruct)fsxData.dwData[0];

                    Location location = new Location
                    {
                        Latitude = userPlaneData.Latitude,
                        Longitude = userPlaneData.Longitude,
                        Altitude = userPlaneData.Altitude
                    };

                    _planeData = new PlaneData
                    {
                        FuelLevel = userPlaneData.Fuel,
                        Speed = userPlaneData.Speed,
                        Location = location
                    };

                    break;
                default:
                    throw new ArgumentOutOfRangeException();
            }

            _receivedMessage = true;
        }


        // The case where the user closes FSX
        public void Fsx_UserClosedFsxEventHandler(SimConnect sender, SIMCONNECT_RECV data)
        {
            CloseFsxConnection();
            _logger.Log("User has closed FSX.");
        }

        public void Fsx_ExceptionEventHandler(SimConnect sender, SIMCONNECT_RECV_EXCEPTION data)
        {
            // handle exceptions (save to file/display message)
            _logger.Log("Error during connection with FSX.");
        }


        private void CloseFsxConnection()
        {
            if (_simConnect == null) return;

            _simConnect.Dispose();
            _simConnect = null;
        }
    }
}
{'fraction_non_alphanumeric': 0.05938511326860842, 'fraction_numerical': 0.0006472491909385113, 'mean_word_length': 1.3484042553191489, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1786124', 'n_tokens_mistral': 1589, 'n_tokens_neox': 1436, 'n_words': 322}