Esempio n. 1
0
        private static void Run(Options commandLineOptions)
        {
            _levelSwitch.MinimumLevel = commandLineOptions.LogLevel;

            try
            {
                _fsConnect = new FsConnect();

                try
                {
                    if (string.IsNullOrEmpty(commandLineOptions.Hostname))
                    {
                        Console.WriteLine($"Connecting to Flight Simulator using index {commandLineOptions.ConfigIndex}");
                        _fsConnect.Connect("FsConnectTestConsole", commandLineOptions.ConfigIndex);
                    }
                    else
                    {
                        Console.WriteLine($"Connecting to Flight Simulator on {commandLineOptions.Hostname}:{commandLineOptions.Port}");
                        _fsConnect.Connect("FsConnectTestConsole", commandLineOptions.Hostname, commandLineOptions.Port, SimConnectProtocol.Ipv4);
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine("An error occurred while connection to Microsoft Flight Simulator: " + e.Message);
                    return;
                }

                //
                // Register event handlers
                //
                _fsConnect.ConnectionChanged += OnFsConnectOnConnectionChanged;

                //
                // Wait for connection to Flight Simulator before using API
                //

                bool receivedEvent = _connectedResetEvent.WaitOne(2000);

                if (receivedEvent == false)
                {
                    Console.WriteLine("Could not connect to Flight Simulator. Timed out waiting for connection");
                    return;
                }

                //
                // Post connection initialization
                //

                Console.WriteLine("Initializing data definitions");
                InitializeDataDefinitions(_fsConnect);

                _fsConnect.SetText("Test Console connected", 2);

                //
                // Show menu
                //

                MainMenu mainMenu = new MainMenu(_fsConnect);
                mainMenu.Run();

                //
                // Tear down
                //

                if (_fsConnect.Connected)
                {
                    _fsConnect.Disconnect();
                }

                _fsConnect.Dispose();
                _fsConnect = null;

                Console.WriteLine("Done");
            }
            catch (Exception e)
            {
                Console.WriteLine("An error occurred: " + e);
            }
        }
Esempio n. 2
0
        private static void Run(Options commandLineOptions)
        {
            try
            {
                _fsConnect = new FsConnect();

                try
                {
                    if (string.IsNullOrEmpty(commandLineOptions.Hostname))
                    {
                        Console.WriteLine($"Connecting to Flight Simulator using index {commandLineOptions.ConfigIndex}");
                        _fsConnect.Connect("FsConnectTestConsole", commandLineOptions.ConfigIndex);
                    }
                    else
                    {
                        Console.WriteLine($"Connecting to Flight Simulator on {commandLineOptions.Hostname}:{commandLineOptions.Port}");
                        _fsConnect.Connect("FsConnectTestConsole", commandLineOptions.Hostname, commandLineOptions.Port, SimConnectProtocol.Ipv4);
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                    return;
                }

                _fsConnect.FsDataReceived += HandleReceivedFsData;

                Console.WriteLine("Initializing data definitions");
                InitializeDataDefinitions(_fsConnect);

                _keyHandlers.Add(ConsoleKey.P, PollFlightSimulator);
                _keyHandlers.Add(ConsoleKey.W, MoveForward);
                _keyHandlers.Add(ConsoleKey.S, MoveBackward);
                _keyHandlers.Add(ConsoleKey.A, MoveLeft);
                _keyHandlers.Add(ConsoleKey.D, MoveRight);
                _keyHandlers.Add(ConsoleKey.Q, RotateLeft);
                _keyHandlers.Add(ConsoleKey.E, RotateRight);
                _keyHandlers.Add(ConsoleKey.R, IncreaseAltitude);
                _keyHandlers.Add(ConsoleKey.F, DecreaseAltitude);

                Console.WriteLine("Press any key to request data from Flight Simulator or ESC to quit.");
                Console.WriteLine("Press WASD keys to move, Q and E to rotate, R and F to change altitude.");
                ConsoleKeyInfo cki = Console.ReadKey(true);

                _fsConnect.SetText("Test Console connected", 2);

                _fsConnect.RequestData(Requests.PlaneInfo);

                while (cki.Key != ConsoleKey.Escape)
                {
                    if (_keyHandlers.ContainsKey(cki.Key))
                    {
                        _keyHandlers[cki.Key].Invoke();
                    }
                    else
                    {
                        PollFlightSimulator();
                    }

                    cki = Console.ReadKey(true);
                }

                Console.ReadKey(true);

                Console.WriteLine("Disconnecting from Flight Simulator");
                _fsConnect.SetText("Test Console disconnecting", 1);
                _fsConnect.Disconnect();
                _fsConnect.Dispose();
                _fsConnect = null;

                Console.WriteLine("Done");
            }
            catch (Exception e)
            {
                Console.WriteLine("An error occurred: " + e);
            }
        }