rama creada apartir de master en j
This commit is contained in:
@@ -1,7 +1,21 @@
|
||||
namespace adas_core.module.ProxyDevices.Services;
|
||||
|
||||
/// <summary>
|
||||
/// Interface for the Proxy Device Service, which handles processing and streaming of data from proxy devices.
|
||||
/// </summary>
|
||||
public interface IProxyDeviceService
|
||||
{
|
||||
/// <summary>
|
||||
/// Processes data from a proxy device identified by the given device ID. This method is responsible for handling
|
||||
/// </summary>
|
||||
/// <param name="deviceId">The unique identifier of the proxy device to be processed.</param>
|
||||
/// <returns>A task that represents the asynchronous operation. The task result contains the HttpResponseMessage returned by the device's Process method.</returns>
|
||||
Task<HttpResponseMessage> Process(string deviceId);
|
||||
|
||||
/// <summary>
|
||||
/// Streams data from a proxy device identified by the given device ID. This method is responsible for handling
|
||||
/// </summary>
|
||||
/// <param name="deviceId">The unique identifier of the proxy device to be streamed.</param>
|
||||
/// <returns>A task that represents the asynchronous operation. The task result contains the HttpResponseMessage returned by the device's Stream method.</returns>
|
||||
Task<HttpResponseMessage> Stream(string deviceId);
|
||||
}
|
||||
@@ -4,10 +4,25 @@ using Microsoft.Extensions.Options;
|
||||
|
||||
namespace adas_core.module.ProxyDevices.Services;
|
||||
|
||||
/// <summary>
|
||||
/// ProxyDeviceService is responsible for managing proxy devices, processing requests, and streaming data from the devices.
|
||||
/// It uses a cache to store device instances for efficient retrieval and ensures that only enabled devices are processed.
|
||||
/// The service handles HTTP requests and returns appropriate responses based on the device's availability and functionality.
|
||||
/// </summary>
|
||||
/// <param name="settings">The settings for the proxy devices, including their configuration and parameters.</param>
|
||||
public class ProxyDeviceService(IOptions<ProxyDeviceSettings> settings) : IProxyDeviceService
|
||||
{
|
||||
/// <summary>
|
||||
/// _cache is a dictionary that stores instances of IProxyDevice, keyed by their deviceId.
|
||||
/// </summary>
|
||||
private readonly Dictionary<string, IProxyDevice?> _cache = new();
|
||||
|
||||
/// <summary>
|
||||
/// Process method takes a deviceId as input, retrieves the corresponding device from the cache or creates a new instance if it doesn't exist, and then calls the Process method of the device.
|
||||
/// It returns an HttpResponseMessage based on the outcome of the operation, handling any exceptions that may occur during the process.
|
||||
/// </summary>
|
||||
/// <param name="deviceId">The unique identifier of the proxy device to be processed.</param>
|
||||
/// <returns>A task that represents the asynchronous operation. The task result contains the HttpResponseMessage returned by the device's Process method.</returns>
|
||||
public async Task<HttpResponseMessage> Process(string deviceId)
|
||||
{
|
||||
try
|
||||
@@ -25,6 +40,11 @@ public class ProxyDeviceService(IOptions<ProxyDeviceSettings> settings) : IProxy
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Stream method takes a deviceId as input, retrieves the corresponding device from the cache or creates a new instance if it doesn't exist, and then calls the Stream method of the device.
|
||||
/// </summary>
|
||||
/// <param name="deviceId">The unique identifier of the proxy device to be streamed.</param>
|
||||
/// <returns>A task that represents the asynchronous operation. The task result contains the HttpResponseMessage returned by the device's Stream method.</returns>
|
||||
public async Task<HttpResponseMessage> Stream(string deviceId)
|
||||
{
|
||||
try
|
||||
@@ -42,6 +62,12 @@ public class ProxyDeviceService(IOptions<ProxyDeviceSettings> settings) : IProxy
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Retrieves the proxy device instance corresponding to the given deviceId. If the device is not found or not enabled, an HttpRequestException is thrown.
|
||||
/// </summary>
|
||||
/// <param name="deviceId">The unique identifier of the proxy device to be retrieved.</param>
|
||||
/// <returns>The proxy device instance corresponding to the given deviceId.</returns>
|
||||
/// <exception cref="HttpRequestException">Thrown when the device is not found or not enabled.</exception>
|
||||
private IProxyDevice? GetDevice(string deviceId)
|
||||
{
|
||||
var device = settings.Value.FirstOrDefault(d => d.Id == deviceId);
|
||||
|
||||
Reference in New Issue
Block a user