예제 #1
0
        protected void btnPredictDelays_Click(object sender, EventArgs e)
        {
            var departureDate = DateTime.Parse(txtDepartureDate.Text);

            departureDate.AddHours(double.Parse(txtDepartureHour.Text));

            var selectedAirport = ddlOriginAirportCode.SelectedItem;

            DepartureQuery query = new DepartureQuery()
            {
                DepartureDate               = departureDate,
                DepartureDayOfWeek          = ((int)departureDate.DayOfWeek) + 1, //Monday = 1
                Carrier                     = txtCarrier.Text,
                OriginAirportCode           = ddlOriginAirportCode.SelectedItem.Text,
                OriginAirportWundergroundID = ddlOriginAirportCode.SelectedItem.Value,
                DestAirportCode             = ddlDestAirportCode.SelectedItem.Text
            };


            GetWeatherForecast(query).Wait();

            if (_forecast == null)
            {
                throw new Exception("Forecast request did not succeed. Check Settings for Weather_APIKey.");
            }

            PredictDelays(query, _forecast).Wait();

            UpdateStatusDisplay(_prediction, _forecast);
        }
예제 #2
0
        private async Task GetWeatherForecast(DepartureQuery departureQuery)
        {
            DateTime departureDate  = departureQuery.DepartureDate;
            string   fullWeatherURI = string.Format(baseWeatherUri, settings.Weather_APIKey, departureQuery.OriginAirportWundergroundID);

            _forecast = null;

            try
            {
                using (var client = new HttpClient())
                {
                    HttpResponseMessage response = await client.GetAsync(fullWeatherURI).ConfigureAwait(false);

                    if (response.IsSuccessStatusCode)
                    {
                        string result = await response.Content.ReadAsStringAsync();

                        JObject jsonObj = JObject.Parse(result);

                        _forecast = (from f in jsonObj["hourly_forecast"]
                                     where f["FCTTIME"]["year"].Value <int>() == departureDate.Year &&
                                     f["FCTTIME"]["mon"].Value <int>() == departureDate.Month &&
                                     f["FCTTIME"]["mday"].Value <int>() == departureDate.Day &&
                                     f["FCTTIME"]["hour"].Value <int>() == departureDate.Hour
                                     select new ForecastResult()
                        {
                            WindSpeed = f["wspd"]["english"].Value <int>(),
                            Precipitation = f["qpf"]["english"].Value <double>(),
                            Pressure = f["mslp"]["english"].Value <double>(),
                            ForecastIconUrl = f["icon_url"].Value <string>(),
                            Condition = f["condition"].Value <string>()
                        }).FirstOrDefault();
                    }
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Trace.TraceError("Failed retrieving weather forecast: " + ex.ToString());
            }
        }
예제 #3
0
        private async Task PredictDelays(DepartureQuery query, ForecastResult forecast)
        {
            if (String.IsNullOrEmpty(settings.ML_APIKey))
            {
                return;
            }

            string fullMLUri     = string.Format(baseMLUri, settings.ML_RegionPrefix, settings.ML_WorkspaceID, settings.ML_ServiceID);
            var    departureDate = DateTime.Parse(txtDepartureDate.Text);

            _prediction = new DelayPrediction();

            try
            {
                using (var client = new HttpClient())
                {
                    var scoreRequest = new
                    {
                        Inputs = new Dictionary <string, StringTable>()
                        {
                            {
                                "input1",
                                new StringTable()
                                {
                                    ColumnNames = new string[] {
                                        "OriginAirportCode", "Month", "DayofMonth",
                                        "CRSDepHour", "DayOfWeek", "Carrier",
                                        "DestAirportCode", "WindSpeed", "SeaLevelPressure", "HourlyPrecip"
                                    },
                                    Values = new string[, ]
                                    {
                                        {
                                            query.OriginAirportCode, query.DepartureDate.Month.ToString(), query.DepartureDate.Day.ToString(),
                                            query.DepartureDate.Hour.ToString(), query.DepartureDayOfWeek.ToString(), query.Carrier,
                                            query.DestAirportCode,
                                            forecast.WindSpeed.ToString(),
                                            forecast.Pressure.ToString(),
                                            forecast.Precipitation.ToString()
                                        }
                                    }
                                }
                            },
                        },
                        GlobalParameters = new Dictionary <string, string>()
                        {
                        }
                    };

                    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", settings.ML_APIKey);
                    client.BaseAddress = new Uri(fullMLUri);
                    HttpResponseMessage response = await client.PostAsJsonAsync("", scoreRequest).ConfigureAwait(false);

                    if (response.IsSuccessStatusCode)
                    {
                        string result = await response.Content.ReadAsStringAsync();

                        JObject jsonObj = JObject.Parse(result);

                        string prediction = jsonObj["Results"]["output1"]["value"]["Values"][0][10].ToString();
                        string confidence = jsonObj["Results"]["output1"]["value"]["Values"][0][11].ToString();

                        if (prediction.Equals("1"))
                        {
                            _prediction.ExpectDelays = true;
                            _prediction.Confidence   = Double.Parse(confidence);
                        }
                        else if (prediction.Equals("0"))
                        {
                            _prediction.ExpectDelays = false;
                            _prediction.Confidence   = Double.Parse(confidence);
                        }
                        else
                        {
                            _prediction = null;
                        }
                    }
                    else
                    {
                        _prediction = null;

                        Trace.Write(string.Format("The request failed with status code: {0}", response.StatusCode));

                        // Print the headers - they include the request ID and the timestamp, which are useful for debugging the failure
                        Trace.Write(response.Headers.ToString());

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

                        Trace.Write(responseContent);
                    }
                }
            }
            catch (Exception ex)
            {
                _prediction = null;
                System.Diagnostics.Trace.TraceError("Failed retrieving delay prediction: " + ex.ToString());
            }
        }