public static async Task Run(payload flightStatusQueue, TraceWriter log) { log.Info($"Starting to retrieve flight info for {flightStatusQueue.Text}."); string flightNumber = flightStatusQueue.Text.ToUpper(); string airlineIata = flightNumber.Substring(0, 2); //convert the airline's IATA code to ICAO var uri = "http://avcodes.co.uk/airlcoderes.asp"; HttpClient client = new HttpClient(); var values = new Dictionary <string, string> { { "status", "Y" }, { "iataairl", airlineIata } }; var content = new FormUrlEncodedContent(values); var response = await client.PostAsync(uri, content); var responseContent = response.Content; string result = null; using (var reader = new StreamReader(await responseContent.ReadAsStreamAsync())) { result = await reader.ReadToEndAsync(); } var regex = new Regex(@"ICAO Code:<br /> \D{3}"); var matches = regex.Matches(result); string icaoCode = $"{(matches[0].Value).Split(';')[1].Substring(0, 3)}"; log.Info(icaoCode); // get the flight number from the input string string flightno = flightNumber.Substring(2); Int32 today = (Int32)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds; Int32 tomorrow = (Int32)(DateTime.UtcNow.AddDays(1).Subtract(new DateTime(1970, 1, 1))).TotalSeconds; var uriAirlineFlightSchedules = $"https://flightxml.flightaware.com/json/FlightXML2/AirlineFlightSchedules?startDate={today}&endDate={tomorrow}&airline={icaoCode}&flightno={flightno}"; string airlineFlightSchedules = await GenericHelper.FlightAwareGet(uriAirlineFlightSchedules); log.Info(airlineFlightSchedules); JObject jsonAirlineFlightSchedules = JObject.Parse(airlineFlightSchedules); JArray airlineFlightSchedulesResults = jsonAirlineFlightSchedules["AirlineFlightSchedulesResult"] .SelectToken("data") .Value <JArray>(); JArray actualFlights = Flights.FilterFlights(airlineFlightSchedulesResults, $"{icaoCode}{flightno}"); foreach (JToken flight in actualFlights) { string flightident = $"{icaoCode}{flightno}@{flight.SelectToken("departuretime").Value<string>()}"; var uriFlightInfoEx = $"https://flightxml.flightaware.com/json/FlightXML2/FlightInfoEx?ident={flightident}&howMany=2"; string flightInfoExs = await GenericHelper.FlightAwareGet(uriFlightInfoEx); JObject jsonFlightInfoEx = JObject.Parse(flightInfoExs); JArray flightInfo = jsonFlightInfoEx["FlightInfoExResult"].SelectToken("flights").Value <JArray>(); string faFlightId = flightInfo[0].SelectToken("faFlightID").Value <string>(); string flightIdent = flight.SelectToken("ident").Value <string>(); string origin = flight.SelectToken("origin").Value <string>(); string destination = flight.SelectToken("destination").Value <string>(); string typeOfAircraft = flight.SelectToken("aircrafttype").Value <string>(); string route = null; ; if (flight.SelectToken("route").IsNullOrEmpty()) { route = "Route not provided"; } else { route = flight.SelectToken("route").Value <string>(); } var uriAirlineFlightInfo = $"https://flightxml.flightaware.com/json/FlightXML2/AirlineFlightInfo?faFlightID={faFlightId}"; string airlineFlightInfo = await GenericHelper.FlightAwareGet(uriAirlineFlightInfo); JObject jsonAirlineFlightInfo = JObject.Parse(airlineFlightInfo); JObject arrAirlineFlightInfo = jsonAirlineFlightInfo["AirlineFlightInfoResult"].Value <JObject>(); DateTime filedDepartureTime = GenericHelper.FromUnixTime(flightInfo[0].SelectToken("filed_departuretime").Value <long>()); DateTime estimatedArrivalTime = GenericHelper.FromUnixTime(flightInfo[0].SelectToken("estimatedarrivaltime").Value <long>()); string estimatedTimeEnroute = flightInfo[0].SelectToken("filed_ete").Value <string>(); TimeSpan estimatedTimeEnrouteTimeSpan = TimeSpan.Parse(estimatedTimeEnroute); DateTime estimatedDepartureTime = estimatedArrivalTime.Add(-estimatedTimeEnrouteTimeSpan); TimeSpan delay = estimatedDepartureTime - filedDepartureTime; var slackResponseUri = HttpUtility.UrlDecode(flightStatusQueue.Response_Url); //{GenericHelper.ConvertFromUtcToLocal(GenericHelper.FromUnixTime(flightInfo[0].SelectToken("filed_departuretime").Value<long>()))}" // if you haven't set your Azure Web App time zone var jsonPayload = new { text = $"*{flightStatusQueue.User_Name} here is your flight info for Flight # {icaoCode}{flightno} / {flightIdent}* \n" + $"From = {origin} / {flightInfo[0].SelectToken("originName").Value<string>()}\n" + $"To = {destination} / {flightInfo[0].SelectToken("destinationName").Value<string>()} \n" + $"Type of Aircraft = {typeOfAircraft} \n" + $"Filed Departure time = {GenericHelper.FromUnixTime(flightInfo[0].SelectToken("filed_departuretime").Value<long>())} \n" + $"Estimated Departure time = {estimatedDepartureTime} \n" + $"Estimated Arrival time = {GenericHelper.FromUnixTime(flightInfo[0].SelectToken("estimatedarrivaltime").Value<long>())} \n" + $"Current delay = {delay} \n" + $"Estimated Flight time = {estimatedTimeEnroute} \n" + $"Filed route = {route} \n" + $"Departure Terminal = {arrAirlineFlightInfo.SelectToken("terminal_orig").Value<string>()} \n" + $"Departure Gate = {arrAirlineFlightInfo.SelectToken("gate_orig").Value<string>()} " }; GenericHelper.SendMessageToSlack(slackResponseUri, jsonPayload); log.Info(jsonPayload.ToString()); } }
public static async Task Run(payload weatherQueue, TraceWriter log) { log.Info($"Starting to retrieve weather for {weatherQueue.Text}."); string airportCode = null; string weather = null; string airportInfo = null; switch (weatherQueue.Text.Length) { case 3: log.Info("This is an IATA code, need to get the ICAO code first..."); airportCode = (await ConvertIataToIcao(weatherQueue.Text)).ToUpper(); break; case 4: log.Info("This is an ICAO code, we can continue from here..."); airportCode = weatherQueue.Text.ToUpper(); break; default: log.Error("Not a valid airport code."); break; } var airportInfoResultUri = $"https://flightxml.flightaware.com/json/FlightXML2/AirportInfo?airportCode={airportCode}"; airportInfo = await GenericHelper.FlightAwareGet(airportInfoResultUri); JObject airResultJson = JObject.Parse(airportInfo); string airportName = airResultJson["AirportInfoResult"].SelectToken("name").Value <string>(); log.Info(airportName); var uri = $"https://flightxml.flightaware.com/json/FlightXML2/MetarEx?airport={airportCode}&howMany=1"; weather = await GenericHelper.FlightAwareGet(uri); JObject resultJson = JObject.Parse(weather); // get latest metar from results JToken metar = resultJson["MetarExResult"].SelectToken("metar").Value <JToken>()[0]; string airport = metar["airport"].ToString(); string windDirection = metar["wind_direction"].ToString(); string windSpeed = metar["wind_speed"].ToString(); string windGusts = metar["wind_speed_gust"].ToString(); string visibility = metar["raw_data"].ToString().Split()[3]; // find the air pressure in the raw data string pressure = null; // hPa regex var regexHp = new Regex(@"Q\d{4}"); var resultsHp = regexHp.Matches(metar["raw_data"].ToString()); if (resultsHp.Count == 0) { // apparently an american code, so need to get the inch HG value var regexHg = new Regex(@"A\d{4}"); var resultsHg = regexHg.Matches(metar["raw_data"].ToString()); pressure = $"{resultsHg[0].Value} inHg"; } else { pressure = $"{resultsHp[0].Value} hPa"; } string clouds = metar["cloud_friendly"].ToString(); string cloudsAltitude = metar["cloud_altitude"].ToString(); string cloudType = metar["cloud_type"].ToString(); string tempAir = metar["temp_air"].ToString(); log.Info(pressure); var slackResponseUri = HttpUtility.UrlDecode(weatherQueue.Response_Url); var jsonPayload = new { text = $"{weatherQueue.User_Name} here is your weather request for {airportName} / {airport} \n" + $"Wind = {windDirection} / {windSpeed} kts \n" + $"Wind Gusts = {windGusts} kts \n" + $"Visibility = {visibility} \n" + $"QNH = {pressure} \n" + $"Clouds = {clouds} \n" + $"Clouds altitude = {cloudsAltitude} ft {cloudType} \n" + $"Temperature = {tempAir} C \n" + $"Raw Report = {metar["raw_data"].ToString()}" }; GenericHelper.SendMessageToSlack(slackResponseUri, jsonPayload); }