Skip to content

Commit

Permalink
updates to flight tracking
Browse files Browse the repository at this point in the history
  • Loading branch information
jschluchter committed May 1, 2024
1 parent 6808c74 commit eae777c
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 50 deletions.
159 changes: 111 additions & 48 deletions panynj-plugin/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
using Microsoft.OpenApi.Models;
using System;
using System.Net.Http;
using System.Threading.Tasks;
using Newtonsoft.Json;
using System.Collections.Generic;

var builder = WebApplication.CreateBuilder(args);
HttpClient client = new HttpClient();

builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(c =>
Expand Down Expand Up @@ -37,7 +43,7 @@

app.Logger.LogInformation("TSAWait endpoint called with airportCode: {airportCode} and wait time: {waitTime}", airportCode, waitTime);

return new { WaitTime = waitTime };
return new { WaitTime = waitTime };

})
.WithDescription("Calculates the TSA wait time for an airport when provided a 3 digit code as a string")
Expand All @@ -47,40 +53,71 @@
app.MapGet("/WalkTime/{airportCode}", (string airportCode) =>
{
int walkTime = WalkTime(airportCode);

app.Logger.LogInformation("Walk Time endpoint called with airportCode: {airportCode} and walk time: {walkTime}", airportCode, walkTime);
return new { WalkTime = walkTime };

return new { WalkTime = walkTime };

})
.WithDescription("Calculates the Average Walk Time to Gates from Terminal Security Checkpoint for an airport when provided a 3 digit code as a string")
.WithName("Walk Time to Gates")
.WithOpenApi();

// EXAMPLE FLIGHT REQUEST
// https://api.aviationstack.com/v1/flights?access_key=[YOUR_ACCESS_KEY]&limit=10&airline_iata=AA&flight_number=76
// Requests American Airlines Flight 76 - which is out of JFK en route to SFO
app.MapGet("/FlightStatus/{airline_iata}/{flight_number}", async (string flight_number, string airline_iata) =>
{

string access_key = "";
app.Logger.LogInformation("FlightStatus endpoint called with Flight Number: {0}{1} ", airline_iata, flight_number);
var outputString = "Error";
try
{
var urlString = string.Format("https://api.aviationstack.com/v1/flights?access_key={0}&airline_iata={1}&flight_number={2}&limit=3", access_key, airline_iata, flight_number);
HttpResponseMessage response = await client.GetAsync(urlString);
response.EnsureSuccessStatusCode();

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

dynamic apiResponse = JsonConvert.DeserializeObject(responseBody);

if (apiResponse.data.Count > 0)
{
dynamic firstFlight = apiResponse.data[0];
var propertyValue = firstFlight.propertyName;
Console.WriteLine($"First flight: {firstFlight}");
var departure_gate = firstFlight.departure.gate;
var departure_airport = firstFlight.departure.airport;
var ddtString = firstFlight.departure.estimated;
DateTime departure_dt = DateTime.Parse(ddtString.ToString()).ToLocalTime();
string formatted_departure = departure_dt.ToString("MM/dd/yyyy HH:mm:ss tt").ToString();

var arrival_gate = firstFlight.arrival.gate;
var arrival_airport = firstFlight.arrival.airport;
var adtString = firstFlight.arrival.estimated;
DateTime arrival_dt = DateTime.Parse(adtString.ToString()).ToLocalTime();
string formatted_arrival = arrival_dt.ToString("MM/dd/yyyy HH:mm:ss tt").ToString();

outputString = string.Format("{0} flight {1} is estimated to depart from gate {2} from {3} Airport at {4} and estimated to arrive at {5} Airport at {6}.", airline_iata, flight_number, departure_gate, departure_airport, formatted_departure, arrival_airport, formatted_arrival);
return outputString;
}
}
catch (HttpRequestException e)
{
Console.WriteLine("\nException Caught!");
outputString = string.Format("Message :{0} ", e);
}
return outputString;

app.MapGet("/FlightStatus/{airline}/{flightNumber}", (string airline, string flightNumber) =>
{
string flightStatus = FlightStatus(airline, flightNumber);
app.Logger.LogInformation("Flight Status: {airline}, {flightNumber}, {flightStatus}", airline, flightNumber, flightStatus);
return new { FlightStatus = flightStatus };

})
.WithDescription("Returns the status for a flight when provided the airline and flight number")
.WithName("Flight Status")
.WithDescription("Returns the Flight Status for flight when provided an airline code and flight number by string")
.WithName("Flight Status API")
.WithOpenApi();

// app.MapGet("/EstimatedArrivalTime/{airline}/{flightNumber}", (string airline, string flightNumber) =>
// {
// string estArrivalTime = "ETA not set";
// estArrivalTime = TravelerTimeToAirport(airline, flightNumber);

// app.Logger.LogInformation("Estimated Arrival Time: {airline}, {flightNumber}, {EstimatedArrivalTime}", airline, flightNumber, estArrivalTime);
// return new { EstimatedArrivalTime = estArrivalTime };

// })
// .WithDescription("Returns the Estimated Arrival Time for a traveler based on flight departure, TSA wait time and walk time to gate")
// .WithName("EstimatedArrivalTime")
// .WithOpenApi();


app.Run();

Expand Down Expand Up @@ -157,47 +194,26 @@ string TravelerTimeToAirport(string airline, string flightNumber)
if (flightNumberUpper == "61613")
{
//JetBlue 61613, New York, JFK to Los Angeles, LAX, Scheduled departure 9:27 am
ScheduledDepartureTime = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 9, 27, 0);
ScheduledDepartureTime = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 9, 27, 0);
airportCode = "JFK";
tsaWaitTime = TSAWait(airportCode);
walkTime = WalkTime(airportCode);

EstimatedTravelerTimeToAirport = ScheduledDepartureTime.AddMinutes(-(tsaWaitTime + walkTime));
estArrivalTime = EstimatedTravelerTimeToAirport.ToString("hh:mm tt");

}
break;
break;
default:
estArrivalTime = "Flight not found";
break;
}
}

app.Logger.LogInformation("TravelerTimeToAirport: {airportCode}, {tsaWaitTime}, {walkTime},{ScheduledDepartureTime},{EstimatedTravelerTimeToAirport}", airportCode, tsaWaitTime, walkTime,ScheduledDepartureTime,EstimatedTravelerTimeToAirport);
app.Logger.LogInformation("TravelerTimeToAirport: {airportCode}, {tsaWaitTime}, {walkTime},{ScheduledDepartureTime},{EstimatedTravelerTimeToAirport}", airportCode, tsaWaitTime, walkTime, ScheduledDepartureTime, EstimatedTravelerTimeToAirport);

return estArrivalTime;
}

string FlightStatus(string airline, string flightNumber)
{
string airlineUpper = airline.ToUpper();
string flightNumberUpper = flightNumber.ToUpper();
string flightStatus = "Flight status not set";

switch (airlineUpper)
{
case "JETBLUE":
if (flightNumberUpper == "61613")
{
flightStatus = "JetBlue 61613, New York, JFK to Los Angeles, LAX, Scheduled departure 9:30 am, Scheduled arrival 12:37 pm";
}
break;
default:
flightStatus = "Flight not found";
break;
}

return flightStatus;
}

static double CalcTimeOfDayFactor()
{
Expand Down Expand Up @@ -233,3 +249,50 @@ static double CalcTimeOfDayFactor()
return TimeOfDayFactor;
}

// string FlightStatus(string airline, string flightNumber)
// {
// string airlineUpper = airline.ToUpper();
// string flightNumberUpper = flightNumber.ToUpper();
// string flightStatus = "Flight status not set";

// switch (airlineUpper)
// {
// case "JETBLUE":
// if (flightNumberUpper == "61613")
// {
// flightStatus = "JetBlue 61613, New York, JFK to Los Angeles, LAX, Scheduled departure 9:30 am, Scheduled arrival 12:37 pm";
// }
// break;
// default:
// flightStatus = "Flight not found";
// break;
// }

// return flightStatus;
// }

// app.MapGet("/FlightStatus/{airline}/{flightNumber}", (string airline, string flightNumber) =>
// {
// string flightStatus = FlightStatus(airline, flightNumber);
// app.Logger.LogInformation("Flight Status: {airline}, {flightNumber}, {flightStatus}", airline, flightNumber, flightStatus);
// return new { FlightStatus = flightStatus };

// })
// .WithDescription("Returns the status for a flight when provided the airline and flight number")
// .WithName("Flight Status")
// .WithOpenApi();

// app.MapGet("/EstimatedArrivalTime/{airline}/{flightNumber}", (string airline, string flightNumber) =>
// {
// string estArrivalTime = "ETA not set";
// estArrivalTime = TravelerTimeToAirport(airline, flightNumber);

// app.Logger.LogInformation("Estimated Arrival Time: {airline}, {flightNumber}, {EstimatedArrivalTime}", airline, flightNumber, estArrivalTime);
// return new { EstimatedArrivalTime = estArrivalTime };

// })
// .WithDescription("Returns the Estimated Arrival Time for a traveler based on flight departure, TSA wait time and walk time to gate")
// .WithName("EstimatedArrivalTime")
// .WithOpenApi();


1 change: 1 addition & 0 deletions panynj-plugin/panynj-plugin.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.3" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
</ItemGroup>

Expand Down
4 changes: 2 additions & 2 deletions webapi/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@
// // "Key": ""
// },
{
"Name": "TSA Wait times",
"ManifestDomain": "http://localhost:5088"
"Name": "NYNJ Port Authority Services API",
"ManifestDomain": "http://localhost:5178"
}
// {
// "Name": "Miyagi calculator",
Expand Down

0 comments on commit eae777c

Please sign in to comment.