-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #38 from davewalker5/BSR-90-Flight-Details-Lookup
BSR-90 Add flight details to the active flight dialog
- Loading branch information
Showing
26 changed files
with
335 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
public enum ApiEndpointType | ||
{ | ||
Airlines, | ||
Aircraft | ||
Aircraft, | ||
ActiveFlights | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
src/BaseStationReader.Entities/Interfaces/IActiveFlightApi.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
using BaseStationReader.Entities.Tracking; | ||
|
||
namespace BaseStationReader.Entities.Interfaces | ||
{ | ||
public interface IActiveFlightApi | ||
{ | ||
Task<Dictionary<ApiProperty, string>?> LookupFlightByAircraft(string address); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
using System.ComponentModel.DataAnnotations; | ||
using System.Diagnostics.CodeAnalysis; | ||
|
||
namespace BaseStationReader.Entities.Lookup | ||
{ | ||
[ExcludeFromCodeCoverage] | ||
public class FlightDetails | ||
{ | ||
[Required] | ||
public string Address { get; set; } = ""; | ||
public string? DepartureAirportIATA { get; set; } | ||
public string? DepartureAirportICAO { get; set; } | ||
public string? DestinationAirportIATA { get; set; } | ||
public string? DestinationAirportICAO { get; set; } | ||
public string? FlightNumberIATA { get; set; } | ||
public string? FlightNumberICAO { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
src/BaseStationReader.Logic/Api/AirLabs/AirLabsActiveFlightApi.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
using BaseStationReader.Entities.Interfaces; | ||
using BaseStationReader.Entities.Logging; | ||
using BaseStationReader.Entities.Tracking; | ||
|
||
namespace BaseStationReader.Logic.Api.AirLabs | ||
{ | ||
public class AirLabsActiveFlightApi : ExternalApiBase, IActiveFlightApi | ||
{ | ||
private readonly string _baseAddress; | ||
|
||
public AirLabsActiveFlightApi(ITrackerLogger logger, ITrackerHttpClient client, string url, string key) : base(logger, client) | ||
{ | ||
_baseAddress = $"{url}?api_key={key}"; | ||
} | ||
|
||
/// <summary> | ||
/// Lookup an active flight's details using the aircraft's ICAO 24-bit address | ||
/// </summary> | ||
/// <param name="address"></param> | ||
/// <returns></returns> | ||
public async Task<Dictionary<ApiProperty, string>?> LookupFlightByAircraft(string address) | ||
{ | ||
Logger.LogMessage(Severity.Info, $"Looking up active flight for aircraft with address {address}"); | ||
var properties = await MakeApiRequest($"&hex={address}"); | ||
return properties; | ||
} | ||
|
||
/// <summary> | ||
/// Make a request to the specified URL | ||
/// </summary> | ||
/// <param name="parameters"></param> | ||
/// <returns></returns> | ||
private async Task<Dictionary<ApiProperty, string>?> MakeApiRequest(string parameters) | ||
{ | ||
Dictionary<ApiProperty, string>? properties = null; | ||
|
||
// Make a request for the data from the API | ||
var url = $"{_baseAddress}{parameters}"; | ||
var node = await SendRequest(url); | ||
|
||
if (node != null) | ||
{ | ||
try | ||
{ | ||
// Extract the response element from the JSON DOM | ||
var apiResponse = node!["response"]![0]; | ||
|
||
// Extract the values into a dictionary | ||
properties = new() | ||
{ | ||
{ ApiProperty.DepartureAirportIATA, apiResponse!["dep_iata"]?.GetValue<string?>() ?? "" }, | ||
{ ApiProperty.DepartureAirportICAO, apiResponse!["dep_icao"]?.GetValue<string?>() ?? "" }, | ||
{ ApiProperty.DestinationAirportIATA, apiResponse!["arr_iata"]?.GetValue<string>() ?? "" }, | ||
{ ApiProperty.DestinationAirportICAO, apiResponse!["arr_icao"]?.GetValue<string>() ?? "" }, | ||
{ ApiProperty.FlightIATA, apiResponse!["flight_iata"]?.GetValue<string>() ?? "" }, | ||
{ ApiProperty.FlightICAO, apiResponse!["flight_icao"]?.GetValue<string>() ?? "" } | ||
}; | ||
|
||
// Log the properties dictionary | ||
LogProperties(properties!); | ||
} | ||
catch (Exception ex) | ||
{ | ||
var message = $"Error processing response: {ex.Message}"; | ||
Logger.LogMessage(Severity.Error, message); | ||
Logger.LogException(ex); | ||
properties = null; | ||
} | ||
} | ||
|
||
return properties; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
using BaseStationReader.Entities.Interfaces; | ||
using BaseStationReader.Entities.Tracking; | ||
using BaseStationReader.Logic.Api.AirLabs; | ||
using BaseStationReader.Tests.Mocks; | ||
|
||
namespace BaseStationReader.Tests | ||
{ | ||
[TestClass] | ||
public class AirLabsActiveFlightApiTest | ||
{ | ||
private const string AircraftAddress = "4CAC23"; | ||
private const string Response = "{\"response\": [{\"hex\": \"4CAC23\",\"reg_number\": \"EI-HGL\",\"flag\": \"IE\",\"lat\": 40.733487,\"lng\": -0.049688,\"alt\": 10683,\"dir\": 192.1,\"speed\": 822,\"v_speed\": -5.5,\"squawk\": \"2074\",\"flight_number\": \"4N\",\"flight_icao\": \"RYR4N\",\"flight_iata\": \"FR9073\",\"dep_icao\": \"EGCC\",\"dep_iata\": \"MAN\",\"arr_icao\": \"LEAL\",\"arr_iata\": \"ALC\",\"airline_icao\": \"RYR\",\"airline_iata\": \"FR\",\"aircraft_icao\": \"B38M\",\"updated\": 1695907120,\"status\": \"en-route\"}]}"; | ||
|
||
private MockTrackerHttpClient? _client = null; | ||
private IActiveFlightApi? _api = null; | ||
|
||
[TestInitialize] | ||
public void Initialise() | ||
{ | ||
var logger = new MockFileLogger(); | ||
_client = new MockTrackerHttpClient(); | ||
_api = new AirLabsActiveFlightApi(logger, _client, "", ""); | ||
} | ||
|
||
[TestMethod] | ||
public void GetActiveFlightTest() | ||
{ | ||
_client!.AddResponse(Response); | ||
var properties = Task.Run(() => _api!.LookupFlightByAircraft(AircraftAddress)).Result; | ||
|
||
Assert.IsNotNull(properties); | ||
Assert.AreEqual(6, properties.Count); | ||
Assert.AreEqual("MAN", properties[ApiProperty.DepartureAirportIATA]); | ||
Assert.AreEqual("EGCC", properties[ApiProperty.DepartureAirportICAO]); | ||
Assert.AreEqual("ALC", properties[ApiProperty.DestinationAirportIATA]); | ||
Assert.AreEqual("LEAL", properties[ApiProperty.DestinationAirportICAO]); | ||
Assert.AreEqual("FR9073", properties[ApiProperty.FlightIATA]); | ||
Assert.AreEqual("RYR4N", properties[ApiProperty.FlightICAO]); | ||
} | ||
|
||
[TestMethod] | ||
public void InvalidJsonResponseTest() | ||
{ | ||
_client!.AddResponse("{}"); | ||
var properties = Task.Run(() => _api!.LookupFlightByAircraft(AircraftAddress)).Result; | ||
|
||
Assert.IsNull(properties); | ||
} | ||
|
||
[TestMethod] | ||
public void ClientExceptionTest() | ||
{ | ||
_client!.AddResponse(null); | ||
var properties = Task.Run(() => _api!.LookupFlightByAircraft(AircraftAddress)).Result; | ||
|
||
Assert.IsNull(properties); | ||
} | ||
} | ||
} |
Oops, something went wrong.