public void RegisterGenericValues(bool wasEmpty)
        {
            if (simconnect == null)
            {
                return;
            }

            if (!wasEmpty)
            {
                logger.LogInformation("Clearing Data definition");
                simconnect.ClearDataDefinition(DEFINITIONS.GenericData);
            }

            foreach (TOGGLE_VALUE simValue in genericValues)
            {
                string value = simValue.ToString().Replace("__", ":").Replace("_", " ");
                logger.LogInformation("RegisterValue {1} {2}", simValue, value);

                simconnect?.AddToDataDefinition(
                    DEFINITIONS.GenericData,
                    value,
                    eventLib.GetUnit(simValue),
                    SIMCONNECT_DATATYPE.FLOAT64,
                    0.0f,
                    SimConnect.SIMCONNECT_UNUSED
                    );
            }

            simconnect?.RegisterDataDefineStruct <GenericValuesStruct>(DEFINITIONS.GenericData);
        }
        void setPayloadStation(SimConnect connection, int station, int weight)
        {
            payloadStruct payload = new payloadStruct(weight);

            connection.ClearDataDefinition(DATA_DEF.AIRCRAFT_PAYLOAD);
            connection.AddToDataDefinition(DATA_DEF.AIRCRAFT_PAYLOAD, String.Format("Payload station weight:{0}", station), "kg", SIMCONNECT_DATATYPE.INT32, 0.0f, SimConnect.SIMCONNECT_UNUSED);
            connection.SetDataOnSimObject(DATA_DEF.AIRCRAFT_PAYLOAD, SimConnect.SIMCONNECT_OBJECT_ID_USER, SIMCONNECT_DATA_SET_FLAG.DEFAULT, payload);
        }
Example #3
0
        private void UnregisterToSimConnect(SimVarRequest simvarRequest)
        {
            if (_simConnect == null)
            {
                return;
            }

            _simConnect.ClearDataDefinition((eDummy)simvarRequest.Def);
        }
        private void RefreshSimVars()
        {
            try
            {
                if (!_isConnected)
                {
                    Connect();
                }

                m_oSimConnect?.ClearDataDefinition(SimVarsEnum.TITLE);
                m_oSimConnect?.ClearDataDefinition(SimVarsEnum.TOTAL_PAYLOAD);
                RegisterSimVars();

                RequestDataOnSim();
                ReceiveSimConnectMessage();
            }
            catch
            {
                AddLogMessage($"Simconnect fail", LogMessageTypeEnum.Error);
                Disconnect();
            }
        }
Example #5
0
 public void Disconnect()
 {
     logger.LogInformation("Cleaning up SimConnect client...");
     // Destroy client
     if (api != null)
     {
         // Try cleaning up before disposing client
         try {
             api.ClearDataDefinition(DATA_DEFINITON_ID.UserPosition);
             logger.LogDebug("Cleaned up client data definitions.");
         }
         catch {
             logger.LogWarning("Unable to clean up data definitions. Skipping to client disposal.");
         }
         // Dispose client
         api.Dispose();
         api = null;
     }
     logger.LogInformation("SimConnect client cleaned up.");
 }
Example #6
0
        private void RegisterGenericValues()
        {
            if (simconnect == null)
            {
                return;
            }

            CancellationTokenSource cts;

            lock (lockGeneric)
            {
                ctsGeneric?.Cancel();
                cts = ctsGeneric = new CancellationTokenSource();
            }

            Task.Run(async() =>
            {
                try
                {
                    await smGeneric.WaitAsync();

                    await Task.Delay(500, cts.Token);
                    cts.Token.ThrowIfCancellationRequested();

                    if (simconnect == null)
                    {
                        return;
                    }

                    if (isGenericValueRegistered)
                    {
                        logger.LogInformation("Clearing Data definition");
                        simconnect.ClearDataDefinition(DEFINITIONS.GenericData);
                        isGenericValueRegistered = false;
                    }

                    if (genericValues.Count == 0)
                    {
                        logger.LogInformation("Registration is not needed.");
                    }
                    else
                    {
                        var log = "Registering generic data structure:";

                        foreach (TOGGLE_VALUE simValue in genericValues)
                        {
                            string value = simValue.ToString().Replace("__", ":").Replace("_", " ");
                            log         += string.Format("\n- {0} {1}", simValue, value);

                            simconnect.AddToDataDefinition(
                                DEFINITIONS.GenericData,
                                value,
                                eventLib.GetUnit(simValue),
                                SIMCONNECT_DATATYPE.FLOAT64,
                                0.0f,
                                SimConnect.SIMCONNECT_UNUSED
                                );
                        }

                        logger.LogInformation(log);

                        simconnect.RegisterDataDefineStruct <GenericValuesStruct>(DEFINITIONS.GenericData);

                        isGenericValueRegistered = true;
                    }
                }
                catch (TaskCanceledException)
                {
                    logger.LogDebug("Registration is cancelled.");
                }
                finally
                {
                    smGeneric.Release();
                }
            });
        }
Example #7
0
        // methods

        public bool SetUp()         // Set up all the SimConnect related data definitions and event handlers
        {
            if (isSetUp)
            {
                return(true);                     // already set up
            }
            // try connect

            try
            {
                simConnect = new SimConnect("Managed Data Request", Program.Handle, WM_USER_SIMCONNECT, null, 0);
            }
            catch (COMException x)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.Write("\nSimConnectManager: ");
                Console.ResetColor();
                //Console.Write($"{x} \n"); // it is pointless printing this as 99% of the time this is just the SC service not running
                Console.Write($"Connection setup FAILED - Please Start MSFS2020 \n");
                return(false);
            }

            // set up data struct definition & registration

            try
            {
                #region  @MOD struct definition & registration - SimDataAircraft
                // ! you must keep the ORDER of these definitions identical in both the struct and the definition list

                simConnect.ClearDataDefinition(DataStructName.SimDataAircraft);


                simConnect.AddToDataDefinition(DataStructName.SimDataAircraft, "TITLE", null, SIMCONNECT_DATATYPE.STRING256, 0.0f, SimConnect.SIMCONNECT_UNUSED);

                simConnect.AddToDataDefinition(DataStructName.SimDataAircraft, "ATC FLIGHT NUMBER", null, SIMCONNECT_DATATYPE.STRING256, 0.0f, SimConnect.SIMCONNECT_UNUSED);
                simConnect.AddToDataDefinition(DataStructName.SimDataAircraft, "ATC ID", null, SIMCONNECT_DATATYPE.STRING256, 0.0f, SimConnect.SIMCONNECT_UNUSED);
                simConnect.AddToDataDefinition(DataStructName.SimDataAircraft, "ATC TYPE", null, SIMCONNECT_DATATYPE.STRING256, 0.0f, SimConnect.SIMCONNECT_UNUSED);
                simConnect.AddToDataDefinition(DataStructName.SimDataAircraft, "ATC MODEL", null, SIMCONNECT_DATATYPE.STRING256, 0.0f, SimConnect.SIMCONNECT_UNUSED);

                simConnect.AddToDataDefinition(DataStructName.SimDataAircraft, "SIM ON GROUND", "bool", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED);
                simConnect.AddToDataDefinition(DataStructName.SimDataAircraft, "GPS POSITION LAT", "degrees", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED);
                simConnect.AddToDataDefinition(DataStructName.SimDataAircraft, "GPS POSITION LON", "degrees", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED);
                simConnect.AddToDataDefinition(DataStructName.SimDataAircraft, "MAGNETIC COMPASS", "degrees", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED);

                simConnect.AddToDataDefinition(DataStructName.SimDataAircraft, "PLANE ALTITUDE", "feet", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED);
                simConnect.AddToDataDefinition(DataStructName.SimDataAircraft, "GROUND ALTITUDE", "meters", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED);

                simConnect.AddToDataDefinition(DataStructName.SimDataAircraft, "PLANE BANK DEGREES", "radians", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED);
                simConnect.AddToDataDefinition(DataStructName.SimDataAircraft, "PLANE PITCH DEGREES", "radians", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED);

                simConnect.AddToDataDefinition(DataStructName.SimDataAircraft, "AIRSPEED TRUE", "knots", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED);
                simConnect.AddToDataDefinition(DataStructName.SimDataAircraft, "AIRSPEED INDICATED", "knots", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED);
                simConnect.AddToDataDefinition(DataStructName.SimDataAircraft, "AIRSPEED MACH", "mach", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED);
                simConnect.AddToDataDefinition(DataStructName.SimDataAircraft, "GROUND VELOCITY", "knots", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED);

                simConnect.RegisterDataDefineStruct <SimDataAircraft>(DataStructName.SimDataAircraft);                // register the struct definition
                #endregion

                // ... define more - copy and paste the above region
            }
            catch (COMException x)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.Write("\nSimConnectManager: ");
                Console.ResetColor();
                Console.Write($"Data definition setup FAILED: {x} \n");
                return(false);
            }

            // bind events

            simConnect.OnRecvOpen += OnReceiveEventOpen;
            simConnect.OnRecvQuit += OnReceiveEventClosed;
            simConnect.OnRecvSimobjectDataBytype += OnReceiveSimData;
            simConnect.OnRecvException           += OnReceiveEventException;

            Console.ForegroundColor = ConsoleColor.Green;
            Console.Write("\nSimConnectManager: ");
            Console.ResetColor();
            Console.Write($"Setup SUCCESS \n");

            Program.DispatchToWPFApplication(() => Program.mainWindow.ReportSimConnectConnected());             // dispatch the status change to the WPF thread

            isSetUp = true;
            return(isSetUp);
        }