예제 #1
0
        public void SetUp()
        {
            AutoResetEvent resetEvent = new AutoResetEvent(false);

            _fsConnect = new FsConnect();
            _fsConnect.ConnectionChanged += (sender, b) =>
            {
                if (b)
                {
                    resetEvent.Set();
                }
            };
            _fsConnect.FsError += (sender, args) =>
            {
                Assert.Fail($"MSFS Error: {args.ExceptionDescription}");
            };

            _fsConnect.Connect("AutopilotManagerIntegrationTest", 0);

            bool res = resetEvent.WaitOne(2000);

            if (!res)
            {
                Assert.Fail("Not connected to MSFS within timeout");
            }

            _manager = new AutopilotManager(_fsConnect);
            _manager.Initialize();
        }
예제 #2
0
        static void Main(string[] args)
        {
            run = true;

            fs = new FsConnect();
            fs.Connect("FlightEconOnline");
            fs.ConnectionChanged += Fs_ConnectionChanged;
            fs.FsDataReceived    += Fs_FsDataReceived;

            List <SimProperty> definition = new List <SimProperty>();

            // Consult the SDK for valid sim variable names, units and whether they can be written to.
            definition.Add(new SimProperty("Title", null, SIMCONNECT_DATATYPE.STRING256));
            definition.Add(new SimProperty("Plane Latitude", "degrees", SIMCONNECT_DATATYPE.FLOAT64));
            definition.Add(new SimProperty("Plane Longitude", "degrees", SIMCONNECT_DATATYPE.FLOAT64));

            // Can also use predefined enums for sim variables and units (incomplete)
            definition.Add(new SimProperty(FsSimVar.PlaneAltitude, FsUnit.Feet, SIMCONNECT_DATATYPE.FLOAT64));
            definition.Add(new SimProperty(FsSimVar.PlaneHeadingDegreesTrue, FsUnit.Degrees, SIMCONNECT_DATATYPE.FLOAT64));
            definition.Add(new SimProperty(FsSimVar.AirspeedIndicated, FsUnit.Knots, SIMCONNECT_DATATYPE.FLOAT64));
            definition.Add(new SimProperty(FsSimVar.GpsGroundSpeed, FsUnit.Knots, SIMCONNECT_DATATYPE.FLOAT64));

            fs.RegisterDataDefinition <PlaneInfoResponse>(Requests.PlaneInfo, definition);


            while (run)
            {
                // Request data
                fs.RequestData(Requests.PlaneInfo);

                // Sleep for a sec
                Thread.Sleep(1000);
            }
        }
예제 #3
0
 private void backgroundConnector_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
 {
     if (!fsConnect.Connected)
     {
         try
         {
             fsConnect.Connect("TestApp", "localhost", 500);
             fsConnect.RegisterDataDefinition <PlaneInfoResponse>(Requests.PlaneInfo, definition);
         }
         catch { } // ignore
     }
 }
        public void SetTime_SetsTimeInMSFS()
        {
            // Arrange
            AutoResetEvent resetEvent = new AutoResetEvent(false);
            int            errorCount = 0;

            FsConnect fsConnect = new FsConnect();

            fsConnect.ConnectionChanged += (sender, b) =>
            {
                if (b)
                {
                    resetEvent.Set();
                }
            };
            fsConnect.FsError += (sender, args) =>
            {
                errorCount++;
                Console.WriteLine($"Error: {args.ExceptionDescription}");
            };

            fsConnect.Connect("WorldManagerIntegrationTests", 0);

            bool res = resetEvent.WaitOne(2000);

            if (!res)
            {
                Assert.Fail("Not connected to MSFS within timeout");
            }

            WorldManager worldManager = new WorldManager(fsConnect);

            // Act
            DateTime now   = new DateTime();
            DateTime night = new DateTime(now.Year, now.Month, now.Day, 3, 0, 0);

            fsConnect.SetText("Setting time to night", 1000);
            worldManager.SetTime(night);

            Thread.Sleep(3000);

            fsConnect.SetText("Setting time to morning", 1000);
            DateTime morning = new DateTime(now.Year, now.Month, now.Day, 9, 0, 0);

            worldManager.SetTime(morning);

            // Assert
            Assert.That(errorCount, Is.Zero);
        }
예제 #5
0
        public static void Main(string[] args)
        {
            string hostName = "localhost";
            uint   port     = 500;

            // Also supports "somehostname 1234"
            if (args.Length == 2)
            {
                hostName = args[0];
                port     = uint.Parse(args[1]);
            }

            FsConnect fsConnect = new FsConnect();

            // Specify where the SimConnect.cfg should be written to
            fsConnect.SimConnectFileLocation = SimConnectFileLocation.Local;

            // Creates a SimConnect.cfg and connect to Flight Simulator using this configuration.
            fsConnect.Connect("TestApp", hostName, port, SimConnectProtocol.Ipv4);

            // Other alternatives, use existing SimConfig.cfg and specify config index:
            // fsConnect.Connect(1);
            // or
            // fsConnect.Connect();

            fsConnect.FsDataReceived += HandleReceivedFsData;

            List <SimProperty> definition = new List <SimProperty>();

            // Consult the SDK for valid sim variable names, units and whether they can be written to.
            definition.Add(new SimProperty("Title", null, SIMCONNECT_DATATYPE.STRING256));
            definition.Add(new SimProperty("Plane Latitude", "degrees", SIMCONNECT_DATATYPE.FLOAT64));
            definition.Add(new SimProperty("Plane Longitude", "degrees", SIMCONNECT_DATATYPE.FLOAT64));

            // Can also use predefined enums for sim variables and units (incomplete)
            definition.Add(new SimProperty(FsSimVar.PlaneAltitude, FsUnit.Feet, SIMCONNECT_DATATYPE.FLOAT64));
            definition.Add(new SimProperty(FsSimVar.PlaneHeadingDegreesTrue, FsUnit.Degrees, SIMCONNECT_DATATYPE.FLOAT64));
            definition.Add(new SimProperty(FsSimVar.AirspeedTrue, FsUnit.MeterPerSecond, SIMCONNECT_DATATYPE.FLOAT64));
            definition.Add(new SimProperty(FsSimVar.AirspeedTrue, FsUnit.Knot, SIMCONNECT_DATATYPE.FLOAT64));

            fsConnect.RegisterDataDefinition <PlaneInfoResponse>(Requests.PlaneInfo, definition);

            fsConnect.RequestData(Requests.PlaneInfo);
            Console.ReadKey();
            fsConnect.Disconnect();
        }
        public void GetAircraftData()
        {
            // Arrange
            AutoResetEvent resetEvent = new AutoResetEvent(false);
            int            errorCount = 0;

            FsConnect fsConnect = new FsConnect();

            fsConnect.ConnectionChanged += (sender, b) =>
            {
                if (b)
                {
                    resetEvent.Set();
                }
            };
            fsConnect.FsError += (sender, args) =>
            {
                errorCount++;
                Console.WriteLine($"Error: {args.ExceptionDescription}");
            };

            fsConnect.Connect("AircraftManagersIntegrationTests", 0);

            bool res = resetEvent.WaitOne(2000);

            if (!res)
            {
                Assert.Fail("Not connected to MSFS within timeout");
            }

            var defId = fsConnect.RegisterDataDefinition <AircraftInfo>();

            AircraftManager <AircraftInfo> aircraftManager = new AircraftManager <AircraftInfo>(fsConnect, defId);

            // Act
            var info = aircraftManager.Get();

            // Assert
            Assert.That(errorCount, Is.Zero);
            Assert.That(info.Title, Is.Not.Empty);
            Assert.That(info.Latitude, Is.Not.EqualTo(0));
            Assert.That(info.Longitude, Is.Not.EqualTo(0));
        }
예제 #7
0
        public static void Main(string[] args)
        {
            string hostName = "localhost";
            uint   port     = 500;

            // Also supports "somehostname 1234"
            if (args.Length == 2)
            {
                hostName = args[0];
                port     = uint.Parse(args[1]);
            }

            FsConnect fsConnect = new FsConnect();

            // Specify where the SimConnect.cfg should be written to
            fsConnect.SimConnectFileLocation = SimConnectFileLocation.Local;

            // Creates a SimConnect.cfg and connect to Flight Simulator using this configuration.
            fsConnect.Connect("TestApp", hostName, port, SimConnectProtocol.Ipv4);

            // Other alternatives, use existing SimConfig.cfg and specify config index:
            // fsConnect.Connect(1);
            // or
            // fsConnect.Connect();

            fsConnect.FsDataReceived += HandleReceivedFsData;

            int planeInfoDefinitionId = fsConnect.RegisterDataDefinition <PlaneInfoResponse>();

            ConsoleKeyInfo cki;

            do
            {
                fsConnect.RequestData((int)Requests.PlaneInfoRequest, planeInfoDefinitionId);
                cki = Console.ReadKey();
            } while (cki.Key != ConsoleKey.Escape);

            fsConnect.Disconnect();
        }
예제 #8
0
파일: Program.cs 프로젝트: qery/FsConnect
        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);
            }
        }
예제 #9
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);
            }
        }
예제 #10
0
        public void TransmitClientEvent_HeadingBugDirectionSet_SetsHeadingBugDirection()
        {
            // Arrange
            AutoResetEvent resetEvent = new AutoResetEvent(false);
            int            errorCount = 0;

            FsConnect fsConnect = new FsConnect();

            fsConnect.ConnectionChanged += (sender, b) =>
            {
                if (b)
                {
                    resetEvent.Set();
                }
            };

            fsConnect.FsError += (sender, args) =>
            {
                errorCount++;
                Console.WriteLine($"Error: {args.ExceptionDescription}");
            };

            fsConnect.Connect("FsConnectIntegrationTest", 0);
            bool res = resetEvent.WaitOne(2000);

            if (!res)
            {
                Assert.Fail("Not connected to MSFS within timeout");
            }

            var            hbDef          = fsConnect.RegisterDataDefinition <HeadingBugTest>();
            HeadingBugTest headingBugData = default;

            // Act
            fsConnect.MapClientEventToSimEvent(TestEnums.GroupId, TestEnums.EventId, FsEventNameId.HeadingBugSet);
            fsConnect.SetNotificationGroupPriority(TestEnums.GroupId);
            uint headingValue = (uint)DateTime.Now.Second * 6;

            fsConnect.TransmitClientEvent(TestEnums.EventId, (uint)headingValue, TestEnums.GroupId);

            // Assert
            Assert.That(errorCount, Is.Zero, "MSFS returned errors. Check console output.");

            fsConnect.FsDataReceived += (sender, args) =>
            {
                var data = args.Data.FirstOrDefault();

                headingBugData = data is HeadingBugTest ? (HeadingBugTest)data : default;

                resetEvent.Set();
            };

            fsConnect.RequestData((int)TestEnums.RequestId, hbDef);
            res = resetEvent.WaitOne(2000);
            if (!res)
            {
                Assert.Fail("Data not returned from MSFS within timeout");
            }

            Assert.That(headingBugData, Is.Not.Null);
            Assert.That(headingBugData.HeadingBug, Is.EqualTo(headingValue));

            // Teardown
            fsConnect?.Disconnect();
        }