// Get route information from id. if valid we fire the delegate. (on main thread.)
        public async void RequestFlightRoute(long id)
        {
            HttpRequestMessage getRequest = new HttpRequestMessage(HttpMethod.Get, $@"https://api.flightplandatabase.com/plan/{id}");

            try
            {
                HttpResponseMessage getResponse = await httpClient.SendAsync(getRequest);

                if (!getResponse.IsSuccessStatusCode)
                {
                    Console.Error.WriteLine("RequestFlightRoute() received a bad status code.");
                    return;
                }

                string routeMessage = await getResponse.Content.ReadAsStringAsync();

                FlightPlanData data = JsonConvert.DeserializeObject <FlightPlanData>(routeMessage);
                data.notes = data.notes.Remove(data.notes.IndexOf("\n\nOptions:"));

                string flightPlanName = data.fromICAO + " - " + data.toICAO;

                Application.Current?.Dispatcher.Invoke(new Action(() =>
                {
                    FlightRouteReadyEventArgs args = new FlightRouteReadyEventArgs(flightPlanName, data);
                    FlightRouteReadyDelegate?.Invoke(this, args);
                }));
            }
            catch
            {
                MessageBox.Show("Cannot download data. Check your internet connection.", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
        // How it works: Build information and create a route. After the route-request is sent,
        // we fetch the route with the given id.
        public async void RequestFlightRoute(string fromAirportIcaoCode, string toAirportIcaoCode)
        {
            var req = new HttpRequestMessage(HttpMethod.Post, @"https://api.flightplandatabase.com/auto/generate");

            // Build post Content
            var dict = new Dictionary <string, string>();

            dict.Add("fromICAO", fromAirportIcaoCode);
            dict.Add("toICAO", toAirportIcaoCode);
            dict.Add("fromProc", "Auto");
            dict.Add("toProc", "Auto");
            dict.Add("useNAT", "true");
            dict.Add("useAWYLO", "true");
            dict.Add("usePACOT", "true");
            dict.Add("useAWYHI", "true");

            req.Content = new FormUrlEncodedContent(dict);

            // Set credentials.
            string user = "******";
            string pwd  = "";

            String encoded = System.Convert.ToBase64String(System.Text.Encoding.GetEncoding("ISO-8859-1").GetBytes(user + ":" + pwd));

            req.Headers.Add("Authorization", "Basic " + encoded);

            // Request
            try
            {
                HttpResponseMessage responseMessage = await httpClient.SendAsync(req);

                if (!responseMessage.IsSuccessStatusCode)
                {
                    Console.Error.WriteLine("RequestFlightRoute() received a bad status code.");
                    return;
                }

                string response = await responseMessage.Content.ReadAsStringAsync();

                Console.WriteLine("FlightPlan Created");

                // Parse request
                FlightPlanData data = JsonConvert.DeserializeObject <FlightPlanData>(response);

                RequestFlightRoute(data.id);
            }
            catch
            {
                MessageBox.Show("Cannot download data. Check your internet connection.", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
Esempio n. 3
0
 public FlightRouteReadyEventArgs(string name, FlightPlanData data) : base(name)
 {
     FlightPlan = data;
 }