rama creada apartir de master en j

This commit is contained in:
jrojas
2026-06-26 10:29:23 +02:00
parent 319fd3dfb0
commit c1517fda87
2810 changed files with 1927392 additions and 25392 deletions
@@ -3,17 +3,53 @@ using adas_core.Domain.Utils;
namespace adas_core.module.LightBeacons.Devices;
/// <summary>
/// Abstract class representing a light beacon device. This class defines the basic structure and behavior of a light beacon, including methods for controlling the beacon's colors and generating alerts based on patient observations.
/// The specific implementation of these methods will depend on the type of light beacon being used and the requirements of the application.
/// </summary>
/// <param name="options">A dictionary containing configuration options for the light beacon. This can include settings such as the beacon's IP address, port number, color configurations, and other relevant parameters.</param>
public abstract class LightBeacon(EquatableDictionary<string, object> options)
{
/// <summary>
/// A dictionary containing configuration options for the light beacon. This can include settings such as the beacon's IP address, port number, color configurations, and other relevant parameters.
/// </summary>
protected readonly EquatableDictionary<string, object> Options = options;
/// <summary>
/// Turns the light beacon blue. This method should be implemented to control the beacon's hardware or software interface to change its color to blue.
/// </summary>
/// <returns>A task representing the asynchronous operation.</returns>
public abstract Task BlueCode();
/// <summary>
/// Turns the light beacon red. This method should be implemented to control the beacon's hardware or software interface to change its color to red.
/// </summary>
/// <returns>A task representing the asynchronous operation.</returns>
public abstract Task RedCode();
/// <summary>
/// Turns the light beacon yellow. This method should be implemented to control the beacon's hardware or software interface to change its color to yellow.
/// </summary>
/// <returns>A task representing the asynchronous operation.</returns>
public abstract Task YellowCode();
/// <summary>
/// Turns off the light beacon. This method should be implemented to control the beacon's hardware or software interface to turn off the light, regardless of its current color.
/// </summary>
/// <returns></returns>
public abstract Task PowerOffLed();
/// <summary>
/// Generates a color alert based on the provided patient observation. This method should analyze the patient observation and determine the appropriate color alert to display on the light beacon.
/// The specific logic for determining the color alert will depend on the criteria defined in the patient observation, such as thresholds for vital signs or other relevant parameters.
/// </summary>
/// <param name="obs">The patient observation used to determine the appropriate color alert.</param>
/// <returns>A task representing the asynchronous operation.</returns>
public abstract Task GenerateColorAlert(PatientObservation obs);
/// <summary>
/// Retrieves the current color of the light beacon. This method should be implemented to query the beacon's hardware or software interface to determine its current color state and return it as a value from the LightBeaconColor enumeration.
/// </summary>
/// <returns>A task representing the asynchronous operation, with a result of the current color of the light beacon.</returns>
public abstract Task<Domain.Enums.LightBeaconColor> GetBeaconColor();
}
@@ -2,20 +2,55 @@
namespace adas_core.module.LightBeacons.Devices;
/// <summary>
/// Abstract class for LightBeacon devices. Each specific LightBeacon model should inherit from this class and implement the abstract methods for controlling the LED states (CodeBlue, CodeRed, CodeYellow, PowerOffLed).
/// </summary>
/// <param name="host">The host address of the LightBeacon device. This is typically the IP address or hostname used to connect to the device for controlling its LED states.</param>
/// <param name="password">The password used for authenticating with the LightBeacon device. This is required to establish a connection and control the device's LED states securely.</param>
public abstract class LightBeaconAbstract(string host, string password)
{
/// <summary>
/// The host address of the LightBeacon device. This is typically the IP address or hostname used to connect to the device for controlling its LED states.
/// </summary>
public string Host = host;
/// <summary>
/// The password used for authenticating with the LightBeacon device. This is required to establish a connection and control the device's LED states securely.
/// </summary>
public string Password = password;
/// <summary>
/// Abstract method to set the LightBeacon device to Code Blue state. This method should be implemented by each specific LightBeacon model to control the LED state accordingly.
/// </summary>
/// <param name="entry">An object parameter that can be used to pass additional information or context needed for setting the LED state.</param>
public abstract void CodeBlue(object entry);
/// <summary>
/// Abstract method to set the LightBeacon device to Code Red state. This method should be implemented by each specific LightBeacon model to control the LED state accordingly.
/// </summary>
/// <param name="entry">An object parameter that can be used to pass additional information or context needed for setting the LED state.</param>
public abstract void CodeRed(object entry);
/// <summary>
/// Abstract method to set the LightBeacon device to Code Yellow state. This method should be implemented by each specific LightBeacon model to control the LED state accordingly.
/// </summary>
/// <param name="entry">An object parameter that can be used to pass additional information or context needed for setting the LED state.</param>
public abstract void CodeYellow(object entry);
/// <summary>
/// Abstract method to power off the LED of the LightBeacon device. This method should be implemented by each specific LightBeacon model to control the LED state accordingly.
/// </summary>
/// <param name="entry">An object parameter that can be used to pass additional information or context needed for setting the LED state.</param>
public abstract void PowerOffLed(object entry);
/// <summary>
/// Static factory method to create an instance of a specific LightBeacon device based on the provided driver name, host, and password.
/// This method uses reflection to dynamically instantiate the appropriate LightBeacon model class that inherits from LightBeaconAbstract.
/// </summary>
/// <param name="driver">The name of the driver class for the specific LightBeacon model.</param>
/// <param name="host">The host address of the LightBeacon device.</param>
/// <param name="password">The password for authenticating with the LightBeacon device.</param>
/// <returns>An instance of the specific LightBeacon device, or null if the driver class is not found or cannot be instantiated.</returns>
public static LightBeaconAbstract? GetBeaconDevice(string driver, string host, string password)
{
var modelType = Type.GetType($"adas_core.Drivers.LightBeacon.{driver}");
@@ -6,15 +6,47 @@ using Serilog;
namespace adas_core.module.LightBeacons.Devices;
/// <summary>
/// This class represents a light beacon device that can be controlled via HTTP requests. It retrieves the current color of the beacon by parsing HTML pages and sends commands to change the beacon's color.
/// The class includes error handling and retry logic for network operations, and it can be configured to emulate the beacon for testing purposes.
/// </summary>
public class Turktbens2LightBeacon : LightBeacon
{
/// <summary>
/// The _client field is an instance of HttpClient that is used to send HTTP requests to the beacon's web interface. It is initialized with a timeout value specified in the options, and it is disposed of after use to free up resources.
/// </summary>
private readonly HttpClient _client;
/// <summary>
/// The _code field is a constant integer that represents a base code used in the construction of the HTTP request body when sending commands to the beacon.
/// It is combined with specific port and color information to form the complete command sent to the beacon's web interface.
/// </summary>
private readonly int _code = 75;
/// <summary>
/// The _emulate field is a boolean that indicates whether the beacon should operate in emulation mode.
/// When set to true, the GetBeaconColor method will return Off without attempting to access the web interface, allowing for testing and development without requiring a physical beacon device.
/// </summary>
private readonly bool _emulate;
/// <summary>
/// The _logger field is an instance of ILogger from the Serilog library, used for logging information, warnings, and errors throughout the class.
/// </summary>
private readonly ILogger _logger;
/// <summary>
/// The _password field is a string that holds the password used for authentication when sending commands to the beacon's web interface.
/// </summary>
private readonly string _password = "password";
/// <summary>
/// The _port field is an integer that specifies which port of the beacon is being controlled. The beacon has multiple ports, and this field determines which one is targeted for color changes and status retrieval.
/// </summary>
private readonly int _port = 1;
/// <summary>
/// The _portMappings dictionary maps each port number to the corresponding indices of the checkbox inputs in the HTML page that represent the output states for that port.
/// </summary>
private readonly Dictionary<int, int[]> _portMappings = new()
{
{ 1, [0, 1] },
@@ -23,6 +55,9 @@ public class Turktbens2LightBeacon : LightBeacon
{ 4, [6, 7] }
};
/// <summary>
/// The _ports dictionary maps each port number to an array of strings that represent the specific parameters used in the HTTP request body for controlling the beacon's color.
/// </summary>
private readonly Dictionary<int, string[]> _ports = new()
{
{ 1, ["|1|1|1|2", "|12|0|1|0", "|1|0|1|2"] },
@@ -31,10 +66,21 @@ public class Turktbens2LightBeacon : LightBeacon
{ 4, ["|1|7|1|2", "|12|3|1|0", "|1|6|1|2"] }
};
/// <summary>
/// The _timeout field is an integer that specifies the timeout duration in seconds for HTTP requests made to the beacon's web interface. If a request takes longer than this duration, it will be aborted and an error will be logged.
/// </summary>
private readonly int _timeout = 30;
/// <summary>
/// The _url field is a string that holds the base URL of the beacon's web interface. This URL is used to construct the full endpoints for retrieving the beacon's status and sending commands to change its color.
/// </summary>
private readonly string _url;
/// <summary>
/// The constructor of the Turktbens2LightBeacon class initializes a new instance of the class with the specified options.
/// </summary>
/// <param name="options">A dictionary containing the configuration options for the beacon.</param>
/// <exception cref="ArgumentException">Thrown when required options are missing or invalid.</exception>
public Turktbens2LightBeacon(EquatableDictionary<string, object> options) : base(options)
{
_logger = Log.ForContext<Turktbens2LightBeacon>();
@@ -61,7 +107,7 @@ public class Turktbens2LightBeacon : LightBeacon
/// bits, Vaux html changes if logged/not logged
/// returns OFF when some error appears or try to parse the bits to BalizaColors enum
/// </summary>
/// <returns></returns>
/// <returns>The current color of the beacon as a LightBeaconColor enum value.</returns>
public override async Task<LightBeaconColor> GetBeaconColor()
{
if (_emulate)
@@ -142,6 +188,12 @@ public class Turktbens2LightBeacon : LightBeacon
}
}
/// <summary>
/// The BlueCode method sends a command to the beacon's web interface to change the beacon's color to blue.
/// It constructs the appropriate HTTP request body using the predefined code and port mappings, and it includes error handling to log any exceptions that occur during the process.
/// If an error occurs, it logs the error message and rethrows the exception to be handled by the caller.
/// </summary>
/// <returns>A task representing the asynchronous operation.</returns>
public override async Task BlueCode()
{
try
@@ -155,6 +207,10 @@ public class Turktbens2LightBeacon : LightBeacon
}
}
/// <summary>
/// The RedCode method sends a command to the beacon's web interface to change the beacon's color to red.
/// </summary>
/// <returns>A task representing the asynchronous operation.</returns>
public override async Task RedCode()
{
try
@@ -168,6 +224,10 @@ public class Turktbens2LightBeacon : LightBeacon
}
}
/// <summary>
/// The YellowCode method sends a command to the beacon's web interface to change the beacon's color to yellow.
/// </summary>
/// <returns>A task representing the asynchronous operation.</returns>
public override async Task YellowCode()
{
try
@@ -181,6 +241,10 @@ public class Turktbens2LightBeacon : LightBeacon
}
}
/// <summary>
/// The GreenCode method sends a command to the beacon's web interface to change the beacon's color to green.
/// </summary>
/// <returns>A task representing the asynchronous operation.</returns>
public override async Task PowerOffLed()
{
try
@@ -194,12 +258,22 @@ public class Turktbens2LightBeacon : LightBeacon
}
}
/// <summary>
/// The GenerateColorAlert method is not implemented in this class. It is intended to generate a color alert based on the provided PatientObservation, but the specific implementation details are not defined in this class and will need to be implemented in a subclass or by the caller.
/// </summary>
/// <param name="obs">The patient observation based on which the color alert should be generated.</param>
/// <returns>A task representing the asynchronous operation.</returns>
/// <exception cref="NotImplementedException"></exception>
public override Task GenerateColorAlert(PatientObservation obs)
{
throw new NotImplementedException();
}
/// <summary>
/// The Send method is a private helper method that constructs and sends an HTTP request to the beacon's web interface to change the beacon's color based on the provided ColorsCodes enum value.
/// </summary>
/// <param name="colorCodes">The color code to be sent to the beacon.</param>
/// <returns>A task representing the asynchronous operation.</returns>
private async Task Send(ColorsCodes colorCodes)
{
var clr = ((int)colorCodes).ToString().PadLeft(3, '0');
@@ -224,7 +298,14 @@ public class Turktbens2LightBeacon : LightBeacon
}
}
/// <summary>
/// The SendMessage method is a private helper method that sends an HTTP POST request to the beacon's web interface with the specified body parameters.
/// It includes retry logic to handle transient network errors, attempting to resend the request up to a maximum number of retries with exponential backoff between attempts.
/// If the request fails after all retry attempts, it logs an error message and rethrows the exception.
/// </summary>
/// <param name="body">The body parameters to be sent in the HTTP POST request.</param>
/// <returns>A task representing the asynchronous operation.</returns>
/// <exception cref="InvalidOperationException"></exception>
protected async Task SendMessage(Dictionary<string, string> body)
{
if (string.IsNullOrEmpty(_url)) throw new InvalidOperationException("URL is not set.");
@@ -265,6 +346,11 @@ public class Turktbens2LightBeacon : LightBeacon
}
}
/// <summary>
/// The ColorsCodes enum defines the binary codes corresponding to each color that the beacon can display.
/// Each color is represented by a three-bit binary code, where each bit corresponds to a specific output state of the beacon.
/// The enum values are used in the Send method to construct the appropriate command for changing the beacon's color.
/// </summary>
private enum ColorsCodes
{
Off = 000,