public IActionResult ReceiveInstruction(string callSign, [FromBody] AtcInstruction instruction) { Requires.NotNull(instruction, "instruction"); Requires.NotNullOrWhiteSpace(callSign, nameof(callSign)); try { var airplane = EnsureAirplane(callSign); lock (airplane) { if (airplane.AirplaneState == null) { throw new InvalidOperationException("Cannot receive ATC instruction if the airplane location is unknown. The airplane needs to start the flight first."); } airplane.Instruction = instruction; } if (logger_.IsEnabled(LogLevel.Debug)) { logger_.LogDebug(LoggingEvents.InstructionReceived, "Airplane {CallSign} received {Instruction}", callSign, instruction.ToString()); } return(NoContent()); } catch (Exception e) { logger_.LogWarning(LoggingEvents.InstructionProcessingFailed, e, "Unexpected error ocurred when processing ATC instruction"); throw; } }
private async Task SendInstructionAsync(string callSign, AtcInstruction instruction) { Requires.NotNullOrWhiteSpace(callSign, nameof(callSign)); Requires.NotNull(instruction, nameof(instruction)); await AirplaneSvcOperationAsync(async() => { using (var client = GetAirplaneSvcClient()) using (var instructionJson = GetJsonContent(instruction)) { var response = await client.PutAsync($"clearance/{callSign}", instructionJson, shutdownTokenSource_.Token); if (!response.IsSuccessStatusCode) { var ex = new HttpRequestException($"Sending instruction to airplane {callSign} has failed"); ex.Data.Add("Instruction", instruction); ex.Data.Add("HttpResponse", response); throw ex; } } }, null, nameof(SendInstructionAsync)); }