コード例 #1
0
 public void start()
 {
     new Thread(delegate()
     {
         while (!stop)
         {
             //push the message to the server.
             pushMassege();
             while (queue.Count > 0)
             {
                 str = queue.Dequeue();
                 telnetClient.write(str);
                 var task = Task.Run(() => (value = telnetClient.read()));
                 //check for timeout of 10 seconds.
                 if (task.Wait(TimeSpan.FromSeconds(10)))
                 {
                     property = checkProperty(str);
                     initializeProperty(property, value);
                 }
                 //shoe the right message of timeout.
                 else
                 {
                     ErrorMessage = "Theres no information from the\nserver for over 10 seconds.";
                 }
             }
             Thread.Sleep(250); //read the data in 4Hz
         }
     }).Start();
 }
コード例 #2
0
        /**
         * start func - where everything is happen!
         * reading from the server, set theam in there properties.
         * if the server is crushed or disconnected put the plan in Ben Gurion Air Port
         * and notify that.
         */
        public void start()
        {
            new Thread(delegate() {
                while (!stop)
                {
                    try
                    {
                        mutex.WaitOne();
                        String data = "";
                        // reading from the server.
                        telnetClient.write("get /instrumentation/attitude-indicator/internal-roll-deg\r\n " +
                                           "get /instrumentation/attitude-indicator/internal-pitch-deg\r\n " +
                                           "get /instrumentation/gps/indicated-vertical-speed\r\n" +
                                           "get /instrumentation/gps/indicated-ground-speed-kt\r\n" +
                                           "get /instrumentation/airspeed-indicator/indicated-speed-kt\r\n" +
                                           "get /instrumentation/heading-indicator/indicated-heading-deg\r\n" +
                                           "get /instrumentation/gps/indicated-altitude-ft\r\n" +
                                           "get /instrumentation/altimeter/indicated-altitude-ft\r\n" +
                                           "get /position/latitude-deg\r\n" +
                                           "get /position/longitude-deg\r\n" +
                                           "get /controls/flight/aileron\r\n" +
                                           "get /controls/engines/current-engine/throttle\r\n" +
                                           "get /controls/flight/elevator\r\n" +
                                           "get /controls/flight/rudder\r\n");
                        // reading 4 time in a secoend.
                        Thread.Sleep(250);
                        data = telnetClient.read();
                        mutex.ReleaseMutex();
                        setEverything(data);
                        Debug.WriteLine(longtitude + " , " + Latitude);
                        // check if the plan is in earth
                        if ((longtitude <= 180 && longtitude >= -180) && (latitude <= 90 && latitude >= -90))
                        {
                            Location = latitude + "," + longtitude;
                        }
                        else
                        {
                            ErrorString = "go back!! this is the end of the world!";
                        }

                        // read the data in 4Hz
                    }
                    // notify there is timeout
                    catch (IOException e)
                    {
                        ErrorString = "timeout! lets wait some more!";
                    }
                    // if the server crushed or disconnected.
                    catch (Exception e)
                    {
                        ErrorString = "cannot use the server!";
                        mutex.WaitOne();
                        latitude   = 32.005;
                        longtitude = 34.887;
                        Location   = latitude + "," + longtitude;
                        mutex.ReleaseMutex();
                    }
                }
            }).Start();
        }
コード例 #3
0
        /// <summary>
        /// Start Method, running a thread which gets the relevant data from server every 0.25 seconds.
        /// </summary>
        public void start()
        {
            this.srartThread = new Thread(delegate()
            {
                Stopwatch stopwatch = new Stopwatch();

                while (true)
                {
                    try
                    {
                        // Get all dashboard values from server (only the first 5 characters).
                        IndicatedHeadingDeg          = Truncate(telnetClient.read("get /instrumentation/heading-indicator/indicated-heading-deg\n"), 5);
                        IndicatedVerticalSpeed       = Truncate(telnetClient.read("get /instrumentation/gps/indicated-vertical-speed\n"), 5);
                        IndicatedGroundSpeedKt       = Truncate(telnetClient.read("get /instrumentation/gps/indicated-ground-speed-kt\n"), 5);
                        IndicatedSpeedKt             = Truncate(telnetClient.read("get /instrumentation/airspeed-indicator/indicated-speed-kt\n"), 5);
                        IndicatedAltitudeFt          = Truncate(telnetClient.read("get /instrumentation/gps/indicated-altitude-ft\n"), 5);
                        InternalRollDeg              = Truncate(telnetClient.read("get /instrumentation/attitude-indicator/internal-roll-deg\n"), 5);
                        InternalPitchDeg             = Truncate(telnetClient.read("get /instrumentation/attitude-indicator/internal-pitch-deg\n"), 5);
                        AltimeterIndicatedAltitudeFt = Truncate(telnetClient.read("get /instrumentation/altimeter/indicated-altitude-ft\n"), 5);
                        // Get the airplane location values (for replacing correctly the pushpin on the map).
                        double longtitude = double.Parse(this.telnetClient.read("get /position/longitude-deg\n"));
                        double latitude   = double.Parse(this.telnetClient.read("get /position/latitude-deg\n"));
                        Location          = new Location(latitude, longtitude);
                        // Sleeping - so the server won't be overloaded.
                        Thread.Sleep(250);

                        // If Slowness error message apppears (stopwatch turned on)
                        // show the slowness message for 5 seconds and then remove it.
                        if (stopwatch.ElapsedMilliseconds > 5000)
                        {
                            Slowness = "";
                            stopwatch.Reset();
                        }
                    }
                    // If an IO exception is catched (means slowness / disconnection occured):
                    catch (IOException io)
                    {
                        // Display the relevant error message for each case.
                        if (this.telnetClient.isConnected() == true)
                        {
                            Slowness = "Slowness was detected";
                            stopwatch.Start();
                        }
                        else
                        {
                            Slowness = "Server is down";
                        }
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine("\n\n\n\n\n*************Thread got an exeption:**************\n");
                        Console.WriteLine(e.Message + "\n\n\n\n\n");
                    }
                }
            });
            srartThread.Start();
        }
コード例 #4
0
        /// <summary>
        ///  Validate the data we send to the Simulator
        /// </summary>
        private Result ValidateData(Command command)
        {
            //Get data from simulator
            double aileron  = Double.Parse(_tcp.read("get /controls/flight/aileron"));
            double rudder   = Double.Parse(_tcp.read("get /controls/flight/rudder"));
            double elevator = Double.Parse(_tcp.read("get /controls/flight/elevator"));
            double throttle = Double.Parse(_tcp.read("get /controls/engines/current-engine/throttle"));

            if (aileron != Convert.ToDouble(String.Format("{0:0.##}", command.aileron)) ||
                rudder != Convert.ToDouble(String.Format("{0:0.##}", command.rudder)) ||
                elevator != Convert.ToDouble(String.Format("{0:0.##}", command.elevator)) ||
                throttle != Convert.ToDouble(String.Format("{0:0.##}", command.throttle)))
            {
                return(Result.NotOk);
            }
            else
            {
                return(Result.Ok);
            }
        }
コード例 #5
0
        private string getValuesFromServer(string ip, int port, List <string> vars)
        {
            IModel model = MyModel.Instance;

            model.connectClient(ip, port);
            ICollection <string> elemetsWithGet = addGetAndNewLineToStrings(vars);

            ITelnetClient c       = model.getClient();
            string        strings = c.read(elemetsWithGet);

            model.disconnectClient();
            return(strings);
        }
コード例 #6
0
        public string checkSet(ITelnetClient myTelnetClient, string property, double value)
        {
            ITelnetClient telnetClient = myTelnetClient;
            string        get          = null;

            if (property.Equals("throttle"))
            {
                get = "get /controls/engines/current-engine/" + property + "\r\n";
            }
            else
            {
                get = "get /controls/flight/" + property + "\r\n";
            }
            telnetClient.write(get);

            string retValue = telnetClient.read();

            Console.WriteLine(retValue);

            string s;
            double newRet = Convert.ToDouble(retValue);

            if (retValue.ToString().Length > 7)
            {
                s      = retValue.ToString().Substring(0, 7);
                newRet = Convert.ToDouble(s);
            }

            double newVal = value;

            if (value.ToString().Length > 7)
            {
                s      = value.ToString().Substring(0, 7);
                newVal = Convert.ToDouble(s);
            }


            if (newRet == newVal)
            {
                return("Ok");
            }
            else
            {
                return("NotOk");
            }
        }
コード例 #7
0
        public ActionResult save(string ip, int port, int tempo, int duration, string fileName)
        {
            ActionResult action = getArgsAndDisplay(ip, port, tempo);

            ICollection <string> elemetsWithGet = addGetAndNewLineToStrings(elements.Values);
            IModel        model = MyModel.Instance;
            ITelnetClient c     = model.getClient();

            lines = new string[2];

            Task taskA = new Task(() => {
                //string str = "start";
                try
                {
                    bool running = ExecuteWithTimeLimit(TimeSpan.FromSeconds(duration), () =>
                    {
                        do
                        {
                            model.connectClient(ip, port);
                            string strings = c.read(elemetsWithGet);
                            model.disconnectClient();
                            string properties = getValuesAsString(strings);
                            lines[0]          = properties;
                            writeToFile(fileName, properties);
                            System.Threading.Thread.Sleep(1000 * tempo);
                        } while (true);
                    });
                    lineIndex = -1;
                    //     str = "success";
                }
                catch {
                    //str = "fail";
                }
            });

            // Start the task.
            taskA.Start();

            isMultiLine = false;
            return(action);
        }
コード例 #8
0
        public void Start()
        {
            //getting the dashboard information
            new Thread(delegate()
            {
                Console.WriteLine("entered new thread");

                if (Thread.CurrentThread.Name == null)
                {
                    Thread.CurrentThread.Name = "RequestPropsThread";
                }

                while (!stop)
                {
                    try
                    {
                        mut.WaitOne();

                        telnetClient.write("get /instrumentation/heading-indicator/indicated-heading-deg\n");
                        Degree = Math.Round(double.Parse(telnetClient.read()), 4);

                        telnetClient.write("get /instrumentation/gps/indicated-vertical-speed\n");
                        VerticalSpeed = Math.Round(double.Parse(telnetClient.read()), 4);

                        telnetClient.write("get /instrumentation/gps/indicated-ground-speed-kt\n");
                        GroundSpeed = Math.Round(double.Parse(telnetClient.read()), 4);

                        telnetClient.write("get /instrumentation/airspeed-indicator/indicated-speed-kt\n");
                        AirSpeed = Math.Round(double.Parse(telnetClient.read()), 4);

                        telnetClient.write("get /instrumentation/gps/indicated-altitude-ft\n");
                        GpsAltitude = Math.Round(double.Parse(telnetClient.read()), 4);

                        telnetClient.write("get /instrumentation/attitude-indicator/internal-roll-deg\n");
                        RollDegree = Math.Round(double.Parse(telnetClient.read()), 4);

                        telnetClient.write("get /instrumentation/attitude-indicator/internal-pitch-deg\n");
                        PitchDegree = Math.Round(double.Parse(telnetClient.read()), 4);

                        telnetClient.write("get /instrumentation/altimeter/indicated-altitude-ft\n");
                        AltimeterAltitude = Math.Round(double.Parse(telnetClient.read()), 4);

                        for (int i = 0; i < 5; i++)
                        {
                            if (!stop)
                            {
                                telnetClient.write("get /position/longitude-deg\n");
                                Longitude = Math.Round(double.Parse(telnetClient.read()), 4);
                            }
                            if (!stop)
                            {
                                telnetClient.write("get /position/latitude-deg\n");
                                Latitude = Math.Round(double.Parse(telnetClient.read()), 4);
                            }
                            Thread.Sleep(50);
                        }
                    }catch (IOException e)
                    {
                        Got_IO_Error();
                        Thread.Sleep(5000);

                        Err_Server_IO = Visibility.Collapsed;
                        disconnect();
                        Err_visiblity_Not_Connected = Visibility.Visible;
                    }
                    catch (FormatException e)
                    {
                        Got_Format_Error();
                    }



                    mut.ReleaseMutex();
                }
            }).Start();
        }
コード例 #9
0
ファイル: FlightModel.cs プロジェクト: liad2508/FlightGearApp
        public void startFromServer()
        {
            // Using a seperate thread for reading the values from the server.
            new Thread(delegate()
            {
                // Using try & catch to deal with a case of crashing.
                try
                {
                    // A main loop of reading the values from the server.
                    while (!stop)
                    {
                        MuTexLock.WaitOne();
                        string s;

                        this.telnetClient.write("get /instrumentation/airspeed-indicator/indicated-speed-kt\r\n");
                        s = telnetClient.read();
                        if (s.Equals("Time Out 10s"))
                        {
                            this.timeOut = s;
                        }
                        else
                        {
                            this.timeOut  = "";
                            this.airSpeed = s;
                        }


                        this.telnetClient.write("get /instrumentation/heading-indicator/indicated-heading-deg\r\n");
                        s = telnetClient.read();
                        if (s.Equals("Time Out 10s"))
                        {
                            this.timeOut = s;
                        }
                        else
                        {
                            this.timeOut        = "";
                            this.heading_Degree = s;
                        }


                        this.telnetClient.write("get /instrumentation/gps/indicated-altitude-ft\r\n");
                        s = telnetClient.read();
                        if (s.Equals("Time Out 10s"))
                        {
                            this.timeOut = s;
                        }
                        else
                        {
                            this.timeOut  = "";
                            this.altitude = s;
                        }


                        this.telnetClient.write("get /instrumentation/gps/indicated-ground-speed-kt\r\n");
                        s = telnetClient.read();
                        if (s.Equals("Time Out 10s"))
                        {
                            this.timeOut = s;
                        }
                        else
                        {
                            this.timeOut      = "";
                            this.ground_Speed = s;
                        }


                        this.telnetClient.write("get /instrumentation/altimeter/indicated-altitude-ft\r\n");
                        s = telnetClient.read();
                        if (s.Equals("Time Out 10s"))
                        {
                            this.timeOut = s;
                        }
                        else
                        {
                            this.timeOut   = "";
                            this.altimeter = s;
                        }


                        this.telnetClient.write("get /instrumentation/gps/indicated-vertical-speed\r\n");
                        s = telnetClient.read();
                        if (s.Equals("Time Out 10s"))
                        {
                            this.timeOut = s;
                        }
                        else
                        {
                            this.timeOut        = "";
                            this.vertical_Speed = s;
                        }



                        this.telnetClient.write("get /instrumentation/attitude-indicator/internal-pitch-deg\r\n");
                        s = telnetClient.read();
                        if (s.Equals("Time Out 10s"))
                        {
                            this.timeOut = s;
                        }
                        else
                        {
                            this.timeOut = "";
                            this.pitch   = s;
                        }


                        this.telnetClient.write("get /instrumentation/attitude-indicator/internal-roll-deg\r\n");
                        s = telnetClient.read();
                        if (s.Equals("Time Out 10s"))
                        {
                            this.timeOut = s;
                        }
                        else
                        {
                            this.timeOut = "";
                            this.roll    = s;
                        }


                        string t;
                        this.telnetClient.write("get /position/latitude-deg\r\n");
                        s = (telnetClient.read()).ToString();
                        this.telnetClient.write("get /position/longitude-deg\r\n");
                        t = (telnetClient.read()).ToString();

                        try
                        {
                            if ((!s.Equals("Time Out 10s")) && (!t.Equals("Time Out 10s")))
                            {
                                lat = Double.Parse(s);
                                lon = Double.Parse(t);
                            }
                        } catch (Exception e)
                        {
                            e.ToString();
                            this.timeOut = s;
                        }


                        MuTexLock.ReleaseMutex();
                        Thread.Sleep(250);
                    }
                    // In a case of exception the user will be asked if he would like to connect again.
                } catch (Exception e)
                {
                    con = false;
                    string messageBoxText   = "Do you want to connect again?";
                    string caption          = "Error Connection";
                    MessageBoxButton button = MessageBoxButton.YesNo;
                    MessageBoxImage icon    = MessageBoxImage.Warning;
                    // Display message box
                    MessageBoxResult result = MessageBox.Show(messageBoxText, caption, button, icon);

                    switch (result)
                    {
                    // The user chose Yes.
                    case MessageBoxResult.Yes:
                        dis = "error";
                        break;

                    // The user chose No.
                    case MessageBoxResult.No:
                        Environment.Exit(0);
                        break;
                    }
                }
            }).Start();
        }
コード例 #10
0
        /*
         * The method set the all values by the command values it gets and by using the telnet
         * client, and if there is any kind of prolbem in the command sending, the method return 1.
         * for evrey single value that we need to set, we send to the server "set" command and
         * then "get" command, reed the feedback from the server and check if the values are equals
         */
        public int SetAllValues(Command command)
        {
            try
            {
                if (telnetClientHandler.isConnected())
                {
                    string tmp = "";

                    telnetClientHandler.write("set /controls/flight/aileron " +
                                              String.Format("{0:0.##}", command.Aileron) + "\r\n");
                    telnetClientHandler.write("get /controls/flight/aileron" + "\r\n");
                    tmp = telnetClientHandler.read();
                    if (tryToConvert(tmp) == true)
                    {
                        if (command.Aileron != double.Parse(tmp))
                        {
                            return(1); //notOk
                        }
                    }

                    telnetClientHandler.write("set /controls/engines/current-engine/throttle " +
                                              String.Format("{0:0.##}", command.Throttle) + "\r\n");
                    telnetClientHandler.write("get /controls/engines/current-engine/throttle" +
                                              "\r\n");
                    tmp = this.telnetClientHandler.read();
                    if (tryToConvert(tmp) == true)
                    {
                        if (command.Throttle != double.Parse(tmp))
                        {
                            return(1); //notOk
                        }
                    }
                    telnetClientHandler.write("set /controls/flight/elevator " +
                                              String.Format("{0:0.##}", command.Elevator) + "\r\n");
                    telnetClientHandler.write("get /controls/flight/elevator" +
                                              "\r\n");
                    tmp = this.telnetClientHandler.read();
                    if (tryToConvert(tmp) == true)
                    {
                        if (command.Elevator != double.Parse(tmp))
                        {
                            return(1); //notOk
                        }
                    }
                    telnetClientHandler.write("set /controls/flight/rudder " +
                                              String.Format("{0:0.##}", command.Rudder) + "\r\n");
                    telnetClientHandler.write("get /controls/flight/rudder" + "\r\n");
                    tmp = this.telnetClientHandler.read();
                    if (tryToConvert(tmp) == true)
                    {
                        if (command.Rudder != double.Parse(tmp))
                        {
                            return(1); //notOk
                        }
                    }
                }
                return(0); //Ok
            }
            catch
            {
                throw new Exception("Could not read / write"); //notOk
            }
        }
コード例 #11
0
        public void start()
        {
            new Thread(delegate() {
                String message = String.Empty;

                while (!stop)
                {
                    // Read all values from server.
                    // If the value is not valid - print a message. else, convert it to double.
                    try
                    {
                        message = telnetClient.read("get /instrumentation/heading-indicator/indicated-heading-deg\n");
                        if (message.Contains("ERR"))
                        {
                            Error = "Heading value is err";
                            Thread.Sleep(250);
                        }
                        else
                        {
                            Heading = Double.Parse(message);
                        }

                        message = telnetClient.read("get /instrumentation/gps/indicated-vertical-speed\n");
                        if (message.Contains("ERR"))
                        {
                            Error = "Vertical Speed value is err";
                            Thread.Sleep(250);
                        }
                        else
                        {
                            VerticalSpeed = Double.Parse(message);
                        }

                        message = telnetClient.read("get /instrumentation/gps/indicated-ground-speed-kt\n");
                        if (message.Contains("ERR"))
                        {
                            Error = "Ground Speed value is err";
                            Thread.Sleep(250);
                        }
                        else
                        {
                            GroundSpeed = Double.Parse(message);
                        }

                        message = telnetClient.read("get /instrumentation/airspeed-indicator/indicated-speed-kt\n");
                        if (message.Contains("ERR"))
                        {
                            Error = "Air Speed value is err";
                            Thread.Sleep(250);
                        }
                        else
                        {
                            AirSpeed = Double.Parse(message);
                        }

                        message = telnetClient.read("get /instrumentation/gps/indicated-altitude-ft\n");
                        if (message.Contains("ERR"))
                        {
                            Error = "Altitude value is err";
                            Thread.Sleep(250);
                        }
                        else
                        {
                            Altitude = Double.Parse(message);
                        }

                        message = telnetClient.read("get /instrumentation/attitude-indicator/internal-roll-deg\n");
                        if (message.Contains("ERR"))
                        {
                            Error = "Roll value is err";
                            Thread.Sleep(250);
                        }
                        else
                        {
                            Roll = Double.Parse(message);
                        }

                        message = telnetClient.read("get /instrumentation/attitude-indicator/internal-pitch-deg\n");
                        if (message.Contains("ERR"))
                        {
                            Error = "Pitch value is err";
                            Thread.Sleep(250);
                        }
                        else
                        {
                            Pitch = Double.Parse(message);
                        }

                        message = telnetClient.read("get /instrumentation/altimeter/indicated-altitude-ft\n");
                        if (message.Contains("ERR"))
                        {
                            Error = "Altimeter value is err";
                            Thread.Sleep(250);
                        }
                        else
                        {
                            Altimeter = Double.Parse(message);
                        }

                        message = telnetClient.read("get /position/latitude-deg\n");
                        if (message.Contains("ERR"))
                        {
                            Error = "Latitude value is err";
                            Thread.Sleep(250);
                        }
                        else
                        {
                            latitude = Double.Parse(message);
                            // If value is bigger then the limit - make the latitude be equal to limit.
                            if (latitude >= 90)
                            {
                                latitude = 90;
                                Error    = "Plane has gotten to limit";
                            }
                            // If value is smaller then the limit - make the latitude be equal to limit.
                            else if (latitude <= -90)
                            {
                                latitude = -90;
                                Error    = "Plane has gotten to limit";
                            }
                        }

                        message = telnetClient.read("get /position/longitude-deg\n");
                        if (message.Contains("ERR"))
                        {
                            Error = "Longitude value is err";
                            Thread.Sleep(250);
                        }
                        else
                        {
                            longitude = Double.Parse(message);
                            // If value is bigger then the limit - make the longitude be equal to limit.
                            if (longitude >= 180)
                            {
                                longitude = 180;
                                Error     = "Plane has gotten to limit";
                            }
                            // If value is smaller then the limit - make the longitude be equal to limit.
                            else if (longitude <= -180)
                            {
                                longitude = -180;
                                Error     = "Plane has gotten to limit";
                            }
                        }
                        Location = Convert.ToString(latitude + "," + longitude);

                        // Read the data in 4Hz.
                        Thread.Sleep(250);
                    }
                    catch (SocketException)
                    {
                        stop  = true;
                        Error = "Reading timeout";
                    }
                    catch (FormatException f)
                    {
                        // Connection to server lost.
                        if (message == "READFAILURE")
                        {
                            Error = "Connection to server lost \nclick disconnect and try again";
                            stop  = true;
                        }
                        // Server did not sent values for more then 10 seconds.
                        else if (message == "TIMEOUT")
                        {
                            Error = "Read timeout";
                            stop  = true;
                        }
                        else if (message == "GENERALFAILURE")
                        {
                            Error = "Socket failure occured";
                            stop  = true;
                        }
                        // Value got from server is not a number or "err", so print "Bad format" to the error board.
                        else if (f.ToString().Contains("not in a correct format"))
                        {
                            Error = "Bad format";
                        }
                    }
                    catch (Exception)
                    {
                        Error = "General failure occured";
                        stop  = true;
                    }
                }
            }).Start();
        }
コード例 #12
0
ファイル: Model.cs プロジェクト: valdman40/FilghtSim
        public void Start()
        {
            new Thread(delegate()
            {
                while (IsConnected)
                {
                    try
                    {
                        mutex.WaitOne();

                        // Roll
                        try  {
                            Client.write("get /instrumentation/attitude-indicator/internal-roll-deg\r\n");
                            Roll = Convert.ToDouble(Client.read());
                        }
                        catch (System.IO.IOException Err) { ErrorString = "Timeout exception"; Console.WriteLine(Err); }
                        catch (Exception e1)  { ErrorString = "Roll"; Console.WriteLine(e1); }

                        // Pitch
                        try {
                            Client.write("get /instrumentation/attitude-indicator/internal-pitch-deg\r\n");
                            Pitch = Convert.ToDouble(Client.read());
                        }
                        catch (System.IO.IOException Err) { ErrorString = "Timeout exception"; Console.WriteLine(Err); }
                        catch (Exception e1)  { ErrorString = "Pitch"; Console.WriteLine(e1); }

                        // VerticalSpeed
                        try
                        {
                            Client.write("get /instrumentation/gps/indicated-vertical-speed\r\n");
                            VerticalSpeed = Convert.ToDouble(Client.read());
                        }
                        catch (System.IO.IOException Err) { ErrorString = "Timeout exception"; Console.WriteLine(Err); }
                        catch (Exception e1) { ErrorString = "VerticalSpeed"; Console.WriteLine(e1); }

                        // GroundSpeed
                        try {
                            Client.write("get /instrumentation/gps/indicated-ground-speed-kt\r\n");
                            GroundSpeed = Convert.ToDouble(Client.read());
                        }
                        catch (System.IO.IOException Err) { ErrorString = "Timeout exception"; Console.WriteLine(Err); }
                        catch (Exception e1)  { ErrorString = "GroundSpeed"; Console.WriteLine(e1); }

                        // AirSpeed
                        try {
                            Client.write("get /instrumentation/airspeed-indicator/indicated-speed-kt\r\n");
                            AirSpeed = Convert.ToDouble(Client.read());
                        }
                        catch (System.IO.IOException Err) { ErrorString = "Timeout exception"; Console.WriteLine(Err); }
                        catch (Exception e1)  { ErrorString = "AirSpeed"; Console.WriteLine(e1); }

                        // Heading
                        try {
                            Client.write("get /instrumentation/heading-indicator/indicated-heading-deg\r\n");
                            Heading = Convert.ToDouble(Client.read());
                        }
                        catch (System.IO.IOException Err) { ErrorString = "Timeout exception"; Console.WriteLine(Err); }
                        catch (Exception e1) { ErrorString = "Heading"; Console.WriteLine(e1); }

                        // Altitude
                        try  {
                            Client.write("get /instrumentation/gps/indicated-altitude-ft\r\n");
                            Altitude = Convert.ToDouble(Client.read());
                        }
                        catch (System.IO.IOException Err) { ErrorString = "Timeout exception"; Console.WriteLine(Err); }
                        catch (Exception e1)  { ErrorString = "Altitude"; Console.WriteLine(e1); }

                        // Altimeter
                        try{
                            Client.write("get /instrumentation/altimeter/indicated-altitude-ft\r\n");
                            Altimeter = Convert.ToDouble(Client.read());
                        }
                        catch (System.IO.IOException Err) { ErrorString = "Timeout exception"; Console.WriteLine(Err); }
                        catch (Exception e1)  { ErrorString = "Altimeter"; Console.WriteLine(e1); }

                        // Latitude
                        try   {
                            Client.write("get /position/latitude-deg\r\n");
                            Latitude = Convert.ToDouble(Client.read());
                        }
                        catch (System.IO.IOException Err) { ErrorString = "Timeout exception"; Console.WriteLine(Err); }
                        catch (Exception e1) { ErrorString = "Latitude"; Console.WriteLine(e1); }

                        // Longitude
                        try  {
                            Client.write("get /position/longitude-deg\r\n");
                            Longitude = Convert.ToDouble(Client.read());
                        }
                        catch (System.IO.IOException Err) { ErrorString = "Timeout exception"; Console.WriteLine(Err); }
                        catch (Exception e1) { ErrorString = "Longitude"; Console.WriteLine(e1); }

                        // Location
                        if ((longitude <= 180 && longitude >= -180) && (latitude <= 90 && latitude >= -90))
                        {
                            Location = latitude + "," + longitude;
                        }
                        else
                        {
                            ErrorString = "plain is out of bounds";
                        }
                        mutex.ReleaseMutex();
                        Thread.Sleep(250);
                    }
                    catch (System.IO.IOException Err)
                    {
                        ErrorString = "Timeout exception";
                        Console.WriteLine(Err);
                    }
                }
            }).Start();
        }
コード例 #13
0
 public string read()
 {
     return(telnetClient.read());
 }
コード例 #14
0
        public void start()
        {
            new Thread(delegate ()
            {
                Location lastKnownCoordinates = new Location(0,0);
                List<String> getMessages = new List<String>();
                getMessages.Add("get /instrumentation/heading-indicator/indicated-heading-deg\r\n");
                getMessages.Add("get /instrumentation/gps/indicated-vertical-speed\r\n");
                getMessages.Add("get /instrumentation/gps/indicated-ground-speed-kt\r\n");
                getMessages.Add("get /instrumentation/airspeed-indicator/indicated-speed-kt\r\n");
                getMessages.Add("get /instrumentation/gps/indicated-altitude-ft\r\n");
                getMessages.Add("get /instrumentation/attitude-indicator/internal-roll-deg\r\n");
                getMessages.Add("get /instrumentation/attitude-indicator/internal-pitch-deg\r\n");
                getMessages.Add("get /instrumentation/altimeter/indicated-altitude-ft\r\n");
                getMessages.Add("get /position/latitude-deg\r\n");
                getMessages.Add("get /position/longitude-deg\r\n");

                while (!m_stop)
                {
                    bool everythingsFine = true;
                    //for dashbord
                    for (int i = 0; i < 10; i++)
                    {
                        try
                        {
                            m_telnetClient.write(getMessages[i]);
                        }
                        catch (Exception)
                        {
                            //cant write get message, server closed
                            ErrorMessage = connectionError;
                            m_stop = true;
                            break;
                        }
                        try
                        {
                            switch (i)
                            {
                                case 0:
                                    HeadingDeg = Double.Parse(m_telnetClient.read());
                                    break;
                                case 1:
                                    VerticalSpeed = Double.Parse(m_telnetClient.read());
                                    break;
                                case 2:
                                    GroundSpeed = Double.Parse(m_telnetClient.read());
                                    break;
                                case 3:
                                    Airspeed = Double.Parse(m_telnetClient.read());
                                    break;
                                case 4:
                                    Alttitude = Double.Parse(m_telnetClient.read());
                                    break;
                                case 5:
                                    RollDeg = Double.Parse(m_telnetClient.read());
                                    break;
                                case 6:
                                    PitchDeg = Double.Parse(m_telnetClient.read());
                                    break;
                                case 7:
                                    Altimeter = Double.Parse(m_telnetClient.read());
                                    break;
                                case 8:
                                    Latitude = Double.Parse(m_telnetClient.read());
                                    if (Latitude >= 90)
                                    {
                                        Latitude = 90;
                                        everythingsFine = false;
                                        ErrorMessage = "Latitude out of range";
                                    }
                                    else if (Latitude <= -90)
                                    {
                                        Latitude = 90;
                                        everythingsFine = false;
                                        ErrorMessage = "Latitude out of range";
                                    }
                                    break;
                                case 9:
                                    Longitude = Double.Parse(m_telnetClient.read());
                                    if (Longitude >= 180)
                                    {
                                        Longitude = 180;
                                        everythingsFine = false;
                                        ErrorMessage = "Longitude out of range";
                                    }
                                    else if (Longitude <= -180)
                                    {
                                        Longitude = -180;
                                        everythingsFine = false;
                                        ErrorMessage = "Longitude out of range";
                                    }
                                    break;
                                default:
                                    ErrorMessage = "shit pavel im sorry dont ruin my grade, something unexcepted happened :(";
                                    break;
                            }
                        }
                        catch (FormatException)
                        {
                            //incorrect values
                            switch (i)
                            {
                                case 0:
                                    ErrorMessage = "HeadingDeg value is incorrect\n";
                                    break;
                                case 1:
                                    ErrorMessage = "verticalSpeed value is incorrect\n";
                                    break;
                                case 2:
                                    ErrorMessage = "GroundSpeed value is incorrect\n";
                                    break;
                                case 3:
                                    ErrorMessage = "Airspeed value is incorrect\n";
                                    break;
                                case 4:
                                    ErrorMessage = "Alttitude value is incorrect\n";
                                    break;
                                case 5:
                                    ErrorMessage = "RollDeg value is incorrect\n";
                                    break;
                                case 6:
                                    ErrorMessage = "PitchDeg value is incorrect\n";
                                    break;
                                case 7:
                                    ErrorMessage = "Altimeter value is incorrect\n";
                                    break;
                                case 8:
                                    ErrorMessage = "Latitude value is incorrect\n";
                                    everythingsFine = false;
                                    break;
                                case 9:
                                    ErrorMessage = "Longitude value is incorrect\n";
                                    everythingsFine = false;
                                    break;
                                default:
                                    ErrorMessage = "shit\n";
                                    break;
                            }
                        }
                        catch (Exception)
                        {
                            // cant read values, server closed.
                            ErrorMessage = connectionError;
                            m_stop = true;
                            break;
                        }
                    }
                    if (everythingsFine)
                    {
                        lastKnownCoordinates = Cordinates;
                        //Cordinates = Longitude.ToString() + "," + Latitude.ToString();
                        Cordinates = new Location(Latitude, Longitude);
                    }
                    else
                    {
                        Cordinates = lastKnownCoordinates;
                    }

                    while (commandsForServer.Count() > 0)
                    {
                        {
                            try
                            {
                            m_telnetClient.write(commandsForServer.Peek());
                            m_telnetClient.read();
                            }
                            catch (Exception)
                            {
                                ErrorMessage = connectionError;
                                m_stop = true;
                                break;
                            }
                        }
                        commandsForServer.Dequeue();
                    }
                    Thread.Sleep(250);                    
                }
                Console.WriteLine("finished get and set thread");
            }).Start();
        }
コード例 #15
0
        public void start()
        {
            new Thread(delegate()
            {
                while (!stopRunning)
                {
                    try
                    {
                        string tempStr;
                        try
                        {
                            telnetClient.write("get /instrumentation/heading-indicator/indicated-heading-deg\n"); //1
                                                                                                                  //HeadingDeg = Double.Parse(telnetClient.read());
                            tempStr = telnetClient.read();
                            //Console.WriteLine("\nheading value="+ telnetClient.read());
                            HeadingDeg = Double.Parse(tempStr);

                            //Console.WriteLine("1, " + a);
                        }
                        catch
                        {
                            StrException = "error - can't update the current value of heading";
                            //Console.WriteLine("heading, " + telnetClient.read());
                            errorCounter++;
                        }

                        try
                        {
                            telnetClient.write("get /instrumentation/gps/indicated-vertical-speed\n");//2
                            tempStr          = telnetClient.read();
                            GpsVerticalSpeed = Double.Parse(tempStr);
                            //errorCounter = 0;
                        }
                        catch
                        {
                            StrException = "error - can't update the current value of vertical speed";
                            errorCounter++;
                        }

                        try
                        {
                            telnetClient.write("get /instrumentation/gps/indicated-ground-speed-kt\n");//3
                            tempStr        = telnetClient.read();
                            GpsGroundSpeed = Double.Parse(tempStr);
                            //errorCounter = 0;
                        }
                        catch
                        {
                            StrException = "error - can't update the current value of GpsGroundSpeed";
                            //Console.WriteLine("GpsGroundSpeed, " + telnetClient.read());
                            errorCounter++;
                        }

                        try
                        {
                            telnetClient.write("get /instrumentation/airspeed-indicator/indicated-speed-kt\n");//4
                            tempStr  = telnetClient.read();
                            AirSpeed = Double.Parse(tempStr);
                        }
                        catch
                        {
                            StrException = "error - can't update the current value of AirSpeed";
                            //Console.WriteLine("AirSpeed, " + telnetClient.read());
                            errorCounter++;
                        }

                        try
                        {
                            telnetClient.write("get /instrumentation/gps/indicated-altitude-ft\n");//5
                            tempStr     = telnetClient.read();
                            GpsAltitude = Double.Parse(tempStr);
                        }
                        catch
                        {
                            StrException = "error - can't update the current value of GpsAltitude";
                            //Console.WriteLine("GpsAltitude, " + telnetClient.read());
                            errorCounter++;
                        }

                        try
                        {
                            telnetClient.write("get /instrumentation/attitude-indicator/internal-roll-deg\n");//6
                            tempStr         = telnetClient.read();
                            InternalRollDeg = Double.Parse(tempStr);
                        }
                        catch
                        {
                            StrException = "error - can't update the current value of InternalRollDeg";
                            //Console.WriteLine("InternalRollDeg, " + telnetClient.read());
                            errorCounter++;
                        }

                        try
                        {
                            telnetClient.write("get /instrumentation/attitude-indicator/internal-pitch-deg\n");//7
                            tempStr          = telnetClient.read();
                            InternalPitchDeg = Double.Parse(tempStr);
                        }
                        catch
                        {
                            StrException = "error - can't update the current value of InternalPitchDeg";
                        }

                        try
                        {
                            telnetClient.write("get /instrumentation/altimeter/indicated-altitude-ft\n");//8
                            tempStr           = telnetClient.read();
                            AltimeterAltitude = Double.Parse(tempStr);
                        }
                        catch
                        {
                            StrException = "error - can't update the current value of AltimeterAltitude";
                        }

                        // update Latitude
                        try
                        {
                            telnetClient.write("get /position/latitude-deg\n"); // x value of the pin. (x,y)
                            tempStr      = telnetClient.read();
                            tempLatitude = Double.Parse(tempStr);
                        }
                        catch
                        {
                            StrException = "error - can't update the current value of Latitude";
                        }
                        if ((tempLatitude >= -90) && (tempLatitude <= 90))
                        {
                            Latitude = tempLatitude;
                            //Console.WriteLine("latitude = " + Latitude);
                        }
                        else
                        {
                            StrException = "altitude is not in the range";
                        }

                        // update Longtitude
                        try
                        {
                            telnetClient.write("get /position/longitude-deg\n"); // y value of the pin. (x,y)
                            tempStr       = telnetClient.read();
                            tempLongitude = Double.Parse(tempStr);
                        }
                        catch
                        {
                            StrException = "error - can't update the current value of Longtitude";
                        }
                        if ((tempLongitude >= -180) && (tempLongitude <= 180))
                        {
                            Longtitude = tempLongitude;
                        }
                        else
                        {
                            StrException = "longitude is not in the range";
                        }

                        Location = new Location(latitude, longitude);
                        // reads 5 times per second.
                        Thread.Sleep(200);
                    }
                    catch (System.IO.IOException e)
                    {
                        if (!stopRunning)
                        {
                            this.disconnect();
                            this.StrException = "problem with connecting the server\nplease try to connect again\n";
                            break;
                        }
                        else
                        {
                            break;
                        }
                    }
                    //Console.WriteLine("counter is: " + this.errorCounter);

                    //if(this.errorCounter==10)
                    //{
                    //    //this.telnetClient.disconnect();
                    //    this.disconnect();
                    //    this.StrException = "problem with connecting the server\nplease try to connect again\n";
                    //}
                }
            }).Start();
        }
コード例 #16
0
        public void SendCommand(Command command)
        {
            string messageFromSimulator;

            telnetClient.write("set /controls/flight/aileron " + command.Aileron.ToString() + "\r\n");
            try
            {
                telnetClient.write("get /controls/flight/aileron\r\n");
                try
                {
                    messageFromSimulator = telnetClient.read();
                    if (messageFromSimulator.Contains("ERR") || Double.Parse(messageFromSimulator) != command.Aileron)
                    {
                    }
                }
                catch
                {
                }
            }
            catch
            {
            }
            telnetClient.write("set /controls/engines/current-engine/throttle " + command.Throttle.ToString() + "\r\n");
            try
            {
                telnetClient.write("get /controls/engines/current-engine/throttle\r\n");
                try
                {
                    messageFromSimulator = telnetClient.read();
                    if (messageFromSimulator.Contains("ERR") || Double.Parse(messageFromSimulator) != command.Throttle)
                    {
                    }
                }
                catch
                {
                }
            }
            catch
            {
            }
            telnetClient.write("set /controls/flight/rudder " + command.Rudder.ToString() + "\r\n");
            try
            {
                telnetClient.write("get /controls/flight/rudder\r\n");
                try
                {
                    messageFromSimulator = telnetClient.read();
                    if (messageFromSimulator.Contains("ERR") || Double.Parse(messageFromSimulator) != command.Rudder)
                    {
                    }
                }
                catch
                {
                }
            }
            catch
            {
            }
            telnetClient.write("set /controls/flight/elevator " + command.Elevator.ToString() + "\r\n");
            try
            {
                telnetClient.write("get /controls/flight/elevator\r\n");
                try
                {
                    messageFromSimulator = telnetClient.read();
                    if (messageFromSimulator.Contains("ERR") || Double.Parse(messageFromSimulator) != command.Elevator)
                    {
                    }
                }
                catch
                {
                }
            }
            catch
            {
            }
        }