private async Task GetWeatherForecast(DepartureQuery departureQuery) { var departureDate = departureQuery.DepartureDate; forecast = null; try { var weatherPrediction = await darkSky.GetForecast(departureQuery.OriginAirportLat, departureQuery.OriginAirportLong, new DarkSkyService.OptionalParameters { ExtendHourly = true, DataBlocksToExclude = new List <ExclusionBlock> { ExclusionBlock.Flags, ExclusionBlock.Alerts, ExclusionBlock.Minutely } }); if (weatherPrediction.Response.Hourly.Data != null && weatherPrediction.Response.Hourly.Data.Count > 0) { var timeZone = DateTimeZoneProviders.Tzdb[weatherPrediction.Response.TimeZone]; var zonedDepartureDate = LocalDateTime.FromDateTime(departureDate) .InZoneLeniently(timeZone); forecast = (from f in weatherPrediction.Response.Hourly.Data where f.DateTime == zonedDepartureDate.ToDateTimeOffset() select new ForecastResult() { WindSpeed = f.WindSpeed ?? 0, Precipitation = f.PrecipIntensity ?? 0, Pressure = f.Pressure ?? 0, ForecastIconUrl = GetImagePathFromIcon(f.Icon), Condition = f.Summary }).FirstOrDefault(); } } catch (Exception ex) { System.Diagnostics.Trace.TraceError("Failed retrieving weather forecast: " + ex.ToString()); } }
private async Task GetWeatherForecast(DepartureQuery departureQuery) { DateTime departureDate = departureQuery.DepartureDate; string fullWeatherURI = string.Format(BASE_WEATHER_URI, weatherApiKey, 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()); } }
private async Task PredictDelays(DepartureQuery query, ForecastResult forecast) { if (String.IsNullOrEmpty(mlApiKey)) { return; } string fullMLUri = string.Format(BASE_ML_URI, !String.IsNullOrWhiteSpace(mlServiceLocation) ? mlServiceLocation : DEFAULT_ML_SERVICE_LOCATION, mlWorkspaceId, mlServiceId); 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", mlApiKey); 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")) { this.prediction.ExpectDelays = true; this.prediction.Confidence = double.Parse(confidence); } else if (prediction.Equals("0")) { this.prediction.ExpectDelays = false; this.prediction.Confidence = double.Parse(confidence); } else { this.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()); throw; } }
private async Task PredictDelays(DepartureQuery query, ForecastResult forecast) { if (string.IsNullOrEmpty(mlUrl)) { return; } var departureDate = DateTime.Parse(txtDepartureDate.Text); prediction = new DelayPrediction(); try { using (var client = new HttpClient()) { var predictionRequest = new PredictionRequest { OriginAirportCode = query.OriginAirportCode, Month = query.DepartureDate.Month, DayofMonth = query.DepartureDate.Day, CRSDepHour = query.DepartureDate.Hour, DayOfWeek = query.DepartureDayOfWeek, Carrier = query.Carrier, DestAirportCode = query.DestAirportCode, WindSpeed = forecast.WindSpeed, SeaLevelPressure = forecast.Pressure, HourlyPrecip = forecast.Precipitation }; client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", mlApiKey); client.BaseAddress = new Uri(mlUrl); var response = await client.PostAsJsonAsync("", predictionRequest).ConfigureAwait(false); if (response.IsSuccessStatusCode) { var result = await response.Content.ReadAsStringAsync(); var token = JToken.Parse(result); var parsedResult = JsonConvert.DeserializeObject <PredictionResult>((string)token); if (parsedResult.prediction == 1) { this.prediction.ExpectDelays = true; this.prediction.Confidence = parsedResult.probability; } else if (parsedResult.prediction == 0) { this.prediction.ExpectDelays = false; this.prediction.Confidence = parsedResult.probability; } else { this.prediction = null; } } else { prediction = null; Trace.Write($"The request failed with status code: {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()); var responseContent = await response.Content.ReadAsStringAsync(); Trace.Write(responseContent); } } } catch (Exception ex) { prediction = null; System.Diagnostics.Trace.TraceError("Failed retrieving delay prediction: " + ex.ToString()); throw; } }