rama creada apartir de master en j
This commit is contained in:
@@ -18,6 +18,9 @@ using Patient = adas_core.Domain.Models.MongoModels.Patient;
|
||||
|
||||
namespace adas_core.Application.Services;
|
||||
|
||||
/// <summary>
|
||||
/// Provides functionality for recording operations as defined by the <see cref="IRecordingService"/> contract.
|
||||
/// </summary>
|
||||
public class RecordingService : IRecordingService
|
||||
{
|
||||
private readonly IAuthService _authService;
|
||||
@@ -69,6 +72,14 @@ public class RecordingService : IRecordingService
|
||||
private string ErrorRecordingQueueName { get; }
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Sends a cancel recording request to the recording API for the specified patient and point of care.
|
||||
/// Throws an exception if the recording API URL is not configured or a valid authentication token cannot be obtained.
|
||||
/// </summary>
|
||||
/// <param name="patient">The patient whose recording in progress should be cancelled.</param>
|
||||
/// <param name="poc">The point of care associated with the recording.</param>
|
||||
/// <returns>A task that resolves to <c>true</c> if the cancel request succeeded; otherwise, <c>false</c>.</returns>
|
||||
/// <exception cref="Exception">Thrown when the recording API URL is not defined or the authentication token is null or empty.</exception>
|
||||
public async Task<bool> SendCancelRecordingToRecordingApi(Patient patient, PointOfCare poc)
|
||||
{
|
||||
var token = await _authService.GetToken();
|
||||
@@ -80,9 +91,9 @@ public class RecordingService : IRecordingService
|
||||
var request =
|
||||
new HttpRequestMessage(HttpMethod.Post,
|
||||
$"{_url}/videos/delete-video-in-progress") //las fechas no se mandan
|
||||
{
|
||||
Content = new StringContent(body, Encoding.UTF8)
|
||||
};
|
||||
{
|
||||
Content = new StringContent(body, Encoding.UTF8)
|
||||
};
|
||||
request.Headers.TryAddWithoutValidation("Authorization", $"Bearer {token}");
|
||||
request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
|
||||
var response = await _httpClient.SendAsync(request);
|
||||
@@ -92,13 +103,28 @@ public class RecordingService : IRecordingService
|
||||
return response.IsSuccessStatusCode;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sends the manual recording data to the processing queue, forwarding the recording's start and stop times along with the patient and point of care information.
|
||||
/// </summary>
|
||||
/// <param name="patient">The patient associated with the manual recording.</param>
|
||||
/// <param name="poc">The point of care where the recording was performed.</param>
|
||||
/// <param name="manualRecording">The manual recording whose start and stop times are forwarded to the queue.</param>
|
||||
/// <param name="start">A flag indicating whether the recording is being started or stopped.</param>
|
||||
public async Task SendRecordingData(Patient patient, PointOfCare poc, ManualRecording manualRecording,
|
||||
bool start)
|
||||
bool start)
|
||||
{
|
||||
await SendRecordingDataToQueue(patient, poc, manualRecording.Recording?.StartRecordingTime,
|
||||
manualRecording.Recording?.StopRecordingTime, null, null, AlarmEnum.Severity.None, null, start);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sends automatic recording data for the specified patient and point of care to the recording data queue.
|
||||
/// Validates that the alarm name can be parsed to <see cref="AlarmEnum.Name"/>; returns <c>false</c> if the name is invalid or if an exception occurs during the send operation.
|
||||
/// </summary>
|
||||
/// <param name="patient">The patient associated with the automatic recording.</param>
|
||||
/// <param name="poc">The point of care where the recording was captured.</param>
|
||||
/// <param name="automaticRecording">The automatic recording payload, including alarm name, start/stop/event timestamps, severity, and description.</param>
|
||||
/// <returns>A task that resolves to <c>true</c> if the recording data was queued successfully; otherwise, <c>false</c> when the alarm name cannot be parsed or the send operation fails.</returns>
|
||||
public async Task<bool> SendAutoRecordingData(Patient patient, PointOfCare poc, RecordingData automaticRecording)
|
||||
{
|
||||
try
|
||||
@@ -120,9 +146,26 @@ public class RecordingService : IRecordingService
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sends recording data for a patient to the recording message queue, broadcasting the recording after a successful send.
|
||||
/// Validates the patient number when the start-without-patient-number feature is disabled, ensures the point of care is in use,
|
||||
/// defaults a missing alarm description to "UNKNOWN", and retries the send on transient HTTP connection failures up to the configured maximum,
|
||||
/// routing the last failure to the error recording queue.
|
||||
/// </summary>
|
||||
/// <param name="patient">The patient whose recording is being sent; its patient number is validated when required.</param>
|
||||
/// <param name="poc">The point of care (box) that must be in use to allow the recording to be sent.</param>
|
||||
/// <param name="date">Optional recording date used to build the recording payload.</param>
|
||||
/// <param name="endDate">Optional end date used to build the recording payload.</param>
|
||||
/// <param name="eventDate">Optional event date used to build the recording payload.</param>
|
||||
/// <param name="alarmName">Optional alarm name to associate with the recording.</param>
|
||||
/// <param name="severity">The alarm severity for the recording.</param>
|
||||
/// <param name="alarmDescription">Optional alarm description; when null or empty it is replaced with "UNKNOWN".</param>
|
||||
/// <param name="start">Flag indicating whether the recording is a start event; defaults to true.</param>
|
||||
/// <param name="type">The alarm type for the recording; defaults to <see cref="AlarmEnum.Type.Manual"/>.</param>
|
||||
/// <exception cref="Exception">Thrown when the point of care status is not <see cref="StatusEnum.PointOfCare.InUse"/>, including the serialized point of care in the message.</exception>
|
||||
public async Task SendRecordingDataToQueue(Patient patient, PointOfCare poc, DateTime? date, DateTime? endDate,
|
||||
DateTime? eventDate, AlarmEnum.Name? alarmName, AlarmEnum.Severity severity, string? alarmDescription,
|
||||
bool start = true, AlarmEnum.Type type = AlarmEnum.Type.Manual)
|
||||
DateTime? eventDate, AlarmEnum.Name? alarmName, AlarmEnum.Severity severity, string? alarmDescription,
|
||||
bool start = true, AlarmEnum.Type type = AlarmEnum.Type.Manual)
|
||||
{
|
||||
if (!_startRecordingWithoutPatientNumber && string.IsNullOrEmpty(patient.PatientNumber))
|
||||
{
|
||||
@@ -178,6 +221,12 @@ public class RecordingService : IRecordingService
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Asynchronously retrieves the list of in-progress recordings for the specified room from the recording API.
|
||||
/// Performs token-based authentication, retries transient HTTP request failures up to the configured maximum, and falls back to an empty list or <c>null</c> when the API is unreachable, the room has no recordings, or retries are exhausted.
|
||||
/// </summary>
|
||||
/// <param name="roomId">The identifier of the room whose recordings should be fetched.</param>
|
||||
/// <returns>A task that resolves to a list of <see cref="RecordingData"/> for the room, an empty list when the request fails or the room has no recordings, or <c>null</c> when the recording API URL or authentication token is not configured or when the maximum number of retries is reached.</returns>
|
||||
public async Task<List<RecordingData>?> GetRecordings(int roomId)
|
||||
{
|
||||
var retryCount = 1;
|
||||
@@ -262,6 +311,11 @@ public class RecordingService : IRecordingService
|
||||
return [];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sends the recording data from the given API request to the Recording API with authentication, retrying on failure up to the configured maximum and routing unsuccessful requests to the error queue. Clears the access token on 401 responses, maps the returned video data into a recording broadcast message on success, and falls back to the error queue when retries are exhausted or a connection error occurs.
|
||||
/// </summary>
|
||||
/// <param name="apiRequest">The API request whose recording data and patient information will be sent to the Recording API.</param>
|
||||
/// <exception cref="Exception">Rethrown via Task.FromException when a non-HTTP error occurs while sending the recording request.</exception>
|
||||
public async Task SaveRequestAsync(ApiRequest apiRequest)
|
||||
{
|
||||
var retryCount = 1;
|
||||
@@ -346,16 +400,37 @@ public class RecordingService : IRecordingService
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Saves the specified API request. The method is not yet implemented.
|
||||
/// </summary>
|
||||
/// <param name="apiRequest">The API request to save.</param>
|
||||
/// <exception cref="NotImplementedException">Always thrown because the method has not been implemented.</exception>
|
||||
public Task SaveRequest(ApiRequest apiRequest)
|
||||
{
|
||||
//return Task.CompletedTask;
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Generates a <see cref="RecordingData"/> object for the specified patient at the given point of care, incorporating alarm metadata and timing details.
|
||||
/// Returns <c>null</c> and logs an error when the patient number is missing, the room identifier is unavailable, or the room identifier cannot be parsed as an integer.
|
||||
/// The stop recording time is derived from <paramref name="endDate"/> or, when not provided, calculated from <paramref name="minToExpired"/>; alarm names are translated to their description via <see cref="EnumUtils.GetDescription"/>.
|
||||
/// </summary>
|
||||
/// <param name="patient">The patient whose recording data is being generated; used to populate patient demographics and validate the patient number.</param>
|
||||
/// <param name="poc">The point of care providing the room identifier for the recording.</param>
|
||||
/// <param name="startDate">The optional start time of the recording.</param>
|
||||
/// <param name="endDate">The optional stop time of the recording; when omitted, the stop time is derived from <paramref name="minToExpired"/>.</param>
|
||||
/// <param name="eventTime">The optional time at which the triggering event occurred.</param>
|
||||
/// <param name="minToExpired">The optional number of minutes added to the current UTC time to compute the stop recording time when <paramref name="endDate"/> is not supplied.</param>
|
||||
/// <param name="alarmName">The optional alarm name whose description is resolved via the enum utility; stored as a string when present.</param>
|
||||
/// <param name="severity">The severity associated with the alarm.</param>
|
||||
/// <param name="alarmDescription">A textual description of the alarm.</param>
|
||||
/// <param name="alarm">The alarm type, defaulting to <see cref="AlarmEnum.Type.Manual"/> when not specified.</param>
|
||||
/// <returns>A configured <see cref="RecordingData"/> instance, or <c>null</c> if any required identifier is invalid.</returns>
|
||||
private RecordingData? GenerateRecordingData(Patient patient, PointOfCare poc, DateTime? startDate,
|
||||
DateTime? endDate, DateTime? eventTime, int? minToExpired, AlarmEnum.Name? alarmName,
|
||||
AlarmEnum.Severity severity,
|
||||
string alarmDescription, AlarmEnum.Type alarm = AlarmEnum.Type.Manual)
|
||||
DateTime? endDate, DateTime? eventTime, int? minToExpired, AlarmEnum.Name? alarmName,
|
||||
AlarmEnum.Severity severity,
|
||||
string alarmDescription, AlarmEnum.Type alarm = AlarmEnum.Type.Manual)
|
||||
{
|
||||
if (patient.PatientNumber == null)
|
||||
{
|
||||
@@ -406,6 +481,12 @@ public class RecordingService : IRecordingService
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Sends a recording status broadcast to WebSocket subscribers associated with the patient's bed and unit.
|
||||
/// Skips processing when the recording list is empty, logs an error and returns when the patient number is missing,
|
||||
/// and silently returns when the patient cannot be resolved.
|
||||
/// </summary>
|
||||
/// <param name="recording">The list of recording entries to broadcast; the first item is used to resolve the patient and room context.</param>
|
||||
private async Task SendRecordingBroadcast(List<RecordingData> recording)
|
||||
{
|
||||
if (!recording.IsNullOrEmpty())
|
||||
@@ -433,6 +514,11 @@ public class RecordingService : IRecordingService
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Encodes the specified plain text to a Base64 string using UTF-8 encoding.
|
||||
/// </summary>
|
||||
/// <param name="plainText">The text to encode.</param>
|
||||
/// <returns>The Base64 encoded representation of the input text.</returns>
|
||||
protected static string Base64Encode(string plainText)
|
||||
{
|
||||
var plainTextBytes = Encoding.UTF8.GetBytes(plainText);
|
||||
|
||||
Reference in New Issue
Block a user