Creado apartir del commit b888de6c92056de294456f581a56e25e734d4ab7 de develop
This commit is contained in:
@@ -0,0 +1,74 @@
|
||||
using System.Net;
|
||||
using Serilog;
|
||||
|
||||
namespace adas_core.module.ProxyDevices.Devices;
|
||||
|
||||
public class HttpDevice : IProxyDevice
|
||||
{
|
||||
private string _domain = "";
|
||||
|
||||
private bool _isInitialized;
|
||||
private string _name = "";
|
||||
private string _password = "";
|
||||
private Uri _uri = null!;
|
||||
private string _username = "";
|
||||
|
||||
public void Initialize(Dictionary<string, object?> deviceParameters)
|
||||
{
|
||||
if (_isInitialized) throw new InvalidOperationException("Device already initialized");
|
||||
if (!deviceParameters.TryGetValue("url", out var urlobj) || urlobj is not string url ||
|
||||
string.IsNullOrEmpty(url.Trim()) || !Uri.TryCreate(url, UriKind.Absolute, out var uri))
|
||||
throw new ArgumentNullException(nameof(deviceParameters),
|
||||
"Device parameter 'url' is required and must be a valid ur");
|
||||
_uri = uri;
|
||||
if (deviceParameters.TryGetValue("name", out var name)) _name = name?.ToString() ?? "";
|
||||
if (deviceParameters.TryGetValue("username", out var username)) _username = username?.ToString() ?? "";
|
||||
if (deviceParameters.TryGetValue("password", out var password)) _password = password?.ToString() ?? "";
|
||||
if (deviceParameters.TryGetValue("domain", out var domain)) _domain = domain?.ToString() ?? "";
|
||||
_isInitialized = true;
|
||||
}
|
||||
|
||||
public async Task<HttpResponseMessage> Process()
|
||||
{
|
||||
try
|
||||
{
|
||||
var client = GetHttpClient();
|
||||
return await client.GetAsync(_uri);
|
||||
}
|
||||
catch (TaskCanceledException ex)
|
||||
{
|
||||
Log.Error("Error getting Http Client get async. Exception:{ex}", ex.ToString());
|
||||
throw new TimeoutException("La solicitud fue cancelada debido al tiempo de espera.", ex);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<HttpResponseMessage> Stream()
|
||||
{
|
||||
try
|
||||
{
|
||||
var client = GetHttpClient();
|
||||
var response = await client.GetAsync(_uri, HttpCompletionOption.ResponseHeadersRead);
|
||||
return response;
|
||||
}
|
||||
catch (TaskCanceledException ex)
|
||||
{
|
||||
Log.Error("Exception getting response. Ex: {ex}", ex.ToString());
|
||||
throw new TimeoutException("La solicitud fue cancelada debido al tiempo de espera.", ex);
|
||||
}
|
||||
}
|
||||
|
||||
private HttpClient GetHttpClient()
|
||||
{
|
||||
if (!_isInitialized) throw new InvalidOperationException("Device not initialized");
|
||||
// get content via http with digest auth
|
||||
var clientHandler = new HttpClientHandler();
|
||||
if (!string.IsNullOrEmpty(_username))
|
||||
clientHandler.Credentials = new NetworkCredential(_username, _password, _domain);
|
||||
return new HttpClient(clientHandler);
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return $"[HttpDevice] ({_name} {_uri} {_username} {_password} {_domain})";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
namespace adas_core.module.ProxyDevices.Devices;
|
||||
|
||||
public interface IProxyDevice
|
||||
{
|
||||
void Initialize(Dictionary<string, object?> deviceParameters);
|
||||
Task<HttpResponseMessage> Process();
|
||||
Task<HttpResponseMessage> Stream();
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
namespace adas_core.module.ProxyDevices;
|
||||
|
||||
public class ProxyDeviceSettings : List<ProxyDevice>
|
||||
{
|
||||
}
|
||||
|
||||
public class ProxyDevice
|
||||
{
|
||||
public string Id { get; set; } = null!;
|
||||
public string Type { get; set; } = null!;
|
||||
public bool Enabled { get; set; } = true;
|
||||
public Dictionary<string, object?> Parameters { get; set; } = new();
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace adas_core.module.ProxyDevices.Services;
|
||||
|
||||
public interface IProxyDeviceService
|
||||
{
|
||||
Task<HttpResponseMessage> Process(string deviceId);
|
||||
Task<HttpResponseMessage> Stream(string deviceId);
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
using System.Net;
|
||||
using adas_core.module.ProxyDevices.Devices;
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
namespace adas_core.module.ProxyDevices.Services;
|
||||
|
||||
public class ProxyDeviceService(IOptions<ProxyDeviceSettings> settings) : IProxyDeviceService
|
||||
{
|
||||
private readonly Dictionary<string, IProxyDevice?> _cache = new();
|
||||
|
||||
public async Task<HttpResponseMessage> Process(string deviceId)
|
||||
{
|
||||
try
|
||||
{
|
||||
var device = GetDevice(deviceId);
|
||||
if (device == null)
|
||||
return new HttpResponseMessage(HttpStatusCode.NotFound)
|
||||
{ ReasonPhrase = $"Proxy device not found. deviceId:{deviceId}" };
|
||||
|
||||
return await device.Process();
|
||||
}
|
||||
catch (HttpRequestException e)
|
||||
{
|
||||
return new HttpResponseMessage(e.StatusCode ?? HttpStatusCode.NotFound) { ReasonPhrase = e.Message };
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<HttpResponseMessage> Stream(string deviceId)
|
||||
{
|
||||
try
|
||||
{
|
||||
var device = GetDevice(deviceId);
|
||||
if (device == null)
|
||||
return new HttpResponseMessage(HttpStatusCode.NotFound)
|
||||
{ ReasonPhrase = $"Proxy device not found. deviceId:{deviceId}" };
|
||||
|
||||
return await device.Stream();
|
||||
}
|
||||
catch (HttpRequestException e)
|
||||
{
|
||||
return new HttpResponseMessage(e.StatusCode ?? HttpStatusCode.NotFound) { ReasonPhrase = e.Message };
|
||||
}
|
||||
}
|
||||
|
||||
private IProxyDevice? GetDevice(string deviceId)
|
||||
{
|
||||
var device = settings.Value.FirstOrDefault(d => d.Id == deviceId);
|
||||
if (device is not { Enabled: true })
|
||||
throw new HttpRequestException("Device not found or not enabled", null, HttpStatusCode.NotFound);
|
||||
IProxyDevice? deviceInstance = null;
|
||||
lock (_cache)
|
||||
{
|
||||
if (_cache.TryGetValue(deviceId, out var value))
|
||||
{
|
||||
deviceInstance = value;
|
||||
}
|
||||
else
|
||||
{
|
||||
var deviceType = Type.GetType("adas_core.module.ProxyDevices.Devices." + device.Type + "Device") ??
|
||||
throw new HttpRequestException("Device not implemented", null,
|
||||
HttpStatusCode.NotImplemented);
|
||||
var instance = Activator.CreateInstance(deviceType);
|
||||
if (instance is IProxyDevice proxyDevice)
|
||||
{
|
||||
deviceInstance = proxyDevice;
|
||||
deviceInstance.Initialize(device.Parameters);
|
||||
}
|
||||
|
||||
_cache.Add(deviceId, deviceInstance);
|
||||
}
|
||||
}
|
||||
|
||||
return deviceInstance;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<RootNamespace>adas_core.module.ProxyDevices</RootNamespace>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Compile Remove="Controllers\**" />
|
||||
<EmbeddedResource Remove="Controllers\**" />
|
||||
<None Remove="Controllers\**" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="AuditLogs" Version="1.0.59" />
|
||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="10.0.8" />
|
||||
<PackageReference Include="Microsoft.Extensions.Hosting" Version="10.0.8" />
|
||||
<PackageReference Include="Serilog" Version="4.3.1" />
|
||||
<PackageReference Include="SharpCompress" Version="0.49.0" />
|
||||
<PackageReference Include="Snappier" Version="1.3.1" />
|
||||
</ItemGroup>
|
||||
|
||||
|
||||
</Project>
|
||||
Reference in New Issue
Block a user