Пример #1
0
        // used only for testing
        public PlanePos GetLocation()
        {
            PlanePos plane = new PlanePos();
            Random   rnd   = new Random();

            plane.lattitude  = rnd.NextDouble() * 90;
            plane.longtitude = rnd.NextDouble() * 180;
            return(plane);
        }
Пример #2
0
        public PlanePos GetPlanePosition(string ip, int port)
        {
            // the string we need to send to the simulator to get the values
            string   logtitude = "get /position/longitude-deg" + Environment.NewLine;
            string   latitude  = "get /position/latitude-deg" + Environment.NewLine;
            string   rudder    = "get /controls/flight/rudder" + Environment.NewLine;
            string   throttle  = "get /controls/engines/current-engine/throttle" + Environment.NewLine;
            PlanePos result    = new PlanePos();

            try
            {
                IPEndPoint ep     = new IPEndPoint(IPAddress.Parse(ip), port);
                TcpClient  client = new TcpClient();
                client.Connect(ep);
                using (NetworkStream stream = client.GetStream())
                    using (BinaryWriter writer = new BinaryWriter(stream))
                        using (StreamReader reader = new StreamReader(stream))
                        {
                            // read the values we need from the simulator
                            string answer;
                            writer.Write(Encoding.ASCII.GetBytes(logtitude));
                            // make sure the data gets to the server right now.
                            writer.Flush();
                            answer            = reader.ReadLine();
                            result.longtitude = getNum(answer);
                            writer.Write(Encoding.ASCII.GetBytes(latitude));
                            // make sure the data gets to the server right now.
                            writer.Flush();
                            answer           = reader.ReadLine();
                            result.lattitude = getNum(answer);
                            writer.Write(Encoding.ASCII.GetBytes(rudder));
                            writer.Flush();
                            answer        = reader.ReadLine();
                            result.rudder = getNum(answer);
                            writer.Write(Encoding.ASCII.GetBytes(throttle));
                            writer.Flush();
                            answer          = reader.ReadLine();
                            result.throttle = getNum(throttle);
                        }
                // close the connection.
                client.Close();
            }
            catch (Exception)
            {
                result.lattitude  = 0;
                result.longtitude = 0;
            }
            // return the plane position with the parameters
            return(result);
        }