private BoundingBox GetBoundingBoxFromRequest(StateVectorsRequestDto request) { if (request == null) { return(null); } if (request.LaMin.HasValue && request.LoMin.HasValue && request.LaMax.HasValue && request.LoMax.HasValue) { return(new BoundingBox { LaMin = request.LaMin.Value, LoMin = request.LoMin.Value, LaMax = request.LaMax.Value, LoMax = request.LoMax.Value }); } return(null); }
public async Task Get([FromQuery] StateVectorsRequestDto request, CancellationToken cancellationToken) { // Prepare some data for the OpenSkyClient request: Credentials credentials = GetCredentials(); BoundingBox boundingBox = GetBoundingBoxFromRequest(request); TimeSpan refreshInterval = GetRefreshInterval(); Response.Headers.Add("Content-Type", "text/event-stream"); Response.Headers.Add("Cache-Control", "no-cache"); while (!cancellationToken.IsCancellationRequested) { try { // Get the data for the given Request: var data = await GetDataAsync(request.Time, request.Icao24, boundingBox, credentials, cancellationToken); if (data == null) { logger.LogInformation("No Data received. See Error Logs for details. Skipping Event ..."); continue; } // Serialize as a Json String: var dataAsJson = JsonSerializer.Serialize(data); // Send the data as JSON over the wire: await Response.WriteAsync($"data: {dataAsJson}\r\r", cancellationToken); await Response.Body.FlushAsync(cancellationToken); } catch (Exception e) { logger.LogError(e, "Requesting Data failed"); } await Task.Delay(refreshInterval); } }