diff --git a/BLL/Integracion_DGA/BusinessLogic.cs b/BLL/Integracion_DGA/BusinessLogic.cs index e33c379..ff4c145 100644 --- a/BLL/Integracion_DGA/BusinessLogic.cs +++ b/BLL/Integracion_DGA/BusinessLogic.cs @@ -3,8 +3,8 @@ using DAL; using Microsoft.Extensions.Configuration; using Shared.DTO; using Shared.DTO.Integracion_DGA; -using Shared.DTO.VariablesEntorno; using Shared.Helper; +using Shared.Utils.Variables_Entorno; using System; using System.Collections.Generic; using System.Linq; @@ -23,13 +23,15 @@ namespace BLL.Integracion_DGA private readonly JobsDgaVilosRepository _jobsVilos; private readonly JobsDgaSupFlujRepository _jobsSupFluj; private readonly FileLoggerHelper _fileLoggerHelper; + private readonly NexusApiUrl _nexusApi; - public BusinessLogic(IConfiguration configuration, ApiService apiService, JobsDgaRepository jobs, JobsDgaVilosRepository jobsVilos, JobsDgaSupFlujRepository jobsSupFluj) { + public BusinessLogic(IConfiguration configuration, ApiService apiService, JobsDgaRepository jobs, JobsDgaVilosRepository jobsVilos, JobsDgaSupFlujRepository jobsSupFluj,NexusApiUrl nexusApiUrl) { _configuration = configuration; _apiService = apiService; _jobs = jobs; _jobsVilos = jobsVilos; _jobsSupFluj = jobsSupFluj; + _nexusApi = nexusApiUrl; FileLoggerHelper.ConfigureLogger(_configuration); } @@ -45,11 +47,11 @@ namespace BLL.Integracion_DGA WriteLineAndLog($"Inicia Proceso DGA"); try { - string apiUrlBase = NexusApiUrl.ApiUrl; + string apiUrlBase = _nexusApi.ApiUrl; Dictionary headers = new Dictionary { - { "nexustoken", NexusApiUrl.ApiKey }, - { "nexusapiversion", NexusApiUrl.Version }, + { "nexustoken", _nexusApi.ApiKey }, + { "nexusapiversion", _nexusApi.Version }, { "accept", "application/json" } }; @@ -74,8 +76,8 @@ namespace BLL.Integracion_DGA } HistoricRequest historicRequest = new HistoricRequest(); - historicRequest.DataSource = NexusApiUrl.DataSource; - historicRequest.Resolution = NexusApiUrl.Resolution; + historicRequest.DataSource = _nexusApi.DataSource; + historicRequest.Resolution = _nexusApi.Resolution; historicRequest.Uids = listTagsID; historicRequest.StartTs = dateStart.ToUnixTimeSeconds(); historicRequest.EndTs = dateEnd.ToUnixTimeSeconds(); diff --git a/BLL/Recuperacion_DGA/EnvioDGA.cs b/BLL/Recuperacion_DGA/EnvioDGA.cs index 476d3cb..95c0272 100644 --- a/BLL/Recuperacion_DGA/EnvioDGA.cs +++ b/BLL/Recuperacion_DGA/EnvioDGA.cs @@ -17,48 +17,39 @@ namespace BLL.Recuperacion_DGA _registrarMedicion = registrarMedicion; } - public async TaskRegistrarMedicionesAsync() + public async Task> ObtenerMedicionesAsync() { var mediciones = await _dGAMedicionScadaRepository.ObtenerMedicionesAsync(); foreach (var medicion in mediciones) { - try + if (!string.IsNullOrEmpty(medicion.Code)) { - if (!string.IsNullOrEmpty(medicion.Code)) + var body = new MedicionSubterraneaRequest { - var body = new MedicionSubterraneaRequest + Autenticacion = new Autenticacion { - Autenticacion = new Autenticacion - { - Password = string.Empty, - RutEmpresa = string.Empty, - RutUsuario = string.Empty - }, - MedicionSubterranea = new Medicion - { - Caudal = medicion.Caudal.ToString() ?? "", - FechaMedicion = medicion.DateOrigen?.ToString("yyyy-MM-dd") ?? "", - HoraMedicion = medicion.DateOrigen?.ToString("HH:mm:ss") ?? "", - NivelFreaticoDelPozo = "", - Totalizador = medicion.Totalizador.ToString() ?? "", - } - }; + Password = string.Empty, + RutEmpresa = string.Empty, + RutUsuario = string.Empty + }, + MedicionSubterranea = new Medicion + { + Caudal = medicion.Caudal.ToString() ?? "", + FechaMedicion = medicion.DateOrigen?.ToString("yyyy-MM-dd") ?? "", + HoraMedicion = medicion.DateOrigen?.ToString("HH:mm:ss") ?? "", + NivelFreaticoDelPozo = "", + Totalizador = medicion.Totalizador.ToString() ?? "", + } + }; - //TODO: Agregar log texto - - await _registrarMedicion.EnviarMedicionAsync(medicion.Code, body,medicion.Id); - } - - } - catch (Exception) - { - //TODO: Agregar log texto - throw; + await _registrarMedicion.EnviarMedicionAsync(medicion.Code, body,medicion.Id); } } - return true; + + + return mediciones; } } } diff --git a/DAL/JobsDgaRepository.cs b/DAL/JobsDgaRepository.cs index 31d1504..c3e29bb 100644 --- a/DAL/JobsDgaRepository.cs +++ b/DAL/JobsDgaRepository.cs @@ -9,17 +9,25 @@ using System.Text; using System.Threading.Tasks; using Dapper; using Microsoft.Extensions.Configuration; -using Shared.DTO.VariablesEntorno; +using Shared.Utils.Variables_Entorno; namespace DAL { public class JobsDgaRepository { + private readonly IConfiguration _configuration; + private readonly BD_Conexion _bdConexion; + public JobsDgaRepository(IConfiguration configuration,BD_Conexion bdConexion) + { + _configuration = configuration; + _bdConexion = bdConexion; + } + public async Task InsertarDgaMacroResultado(List dgaMacroResultados) { try { - using (SqlConnection connection = new SqlConnection(BdConexion.StringConnection)) + using (SqlConnection connection = new SqlConnection(_bdConexion.stringConnection)) { await connection.OpenAsync(); // 1. Truncar la tabla antes de insertar @@ -41,7 +49,7 @@ namespace DAL { try { - using (var connection = new SqlConnection(BdConexion.StringConnection)) + using (var connection = new SqlConnection(_bdConexion.stringConnection)) { await connection.OpenAsync(); @@ -69,7 +77,7 @@ namespace DAL try { // Configurar la conexión a la base de datos - using (SqlConnection connection = new SqlConnection(BdConexion.StringConnection)) + using (SqlConnection connection = new SqlConnection(_bdConexion.stringConnection)) { await connection.OpenAsync(); // Truncar la tabla antes de insertar @@ -94,7 +102,7 @@ namespace DAL { try { - using (var connection = new SqlConnection(BdConexion.StringConnection)) + using (var connection = new SqlConnection(_bdConexion.stringConnection)) { await connection.OpenAsync(); // Ejecuta el stored procedure sin parámetros @@ -112,7 +120,7 @@ namespace DAL { try { - using (var connection = new SqlConnection(BdConexion.StringConnection)) + using (var connection = new SqlConnection(_bdConexion.stringConnection)) { await connection.OpenAsync(); await connection.ExecuteAsync( @@ -132,7 +140,7 @@ namespace DAL { try { - using (var connection = new SqlConnection(BdConexion.StringConnection)) + using (var connection = new SqlConnection(_bdConexion.stringConnection)) { await connection.OpenAsync(); await connection.ExecuteAsync( diff --git a/DAL/JobsDgaSupFlujRepository.cs b/DAL/JobsDgaSupFlujRepository.cs index 4245aaa..1a2b3fa 100644 --- a/DAL/JobsDgaSupFlujRepository.cs +++ b/DAL/JobsDgaSupFlujRepository.cs @@ -2,7 +2,7 @@ using Microsoft.Data.SqlClient; using Microsoft.Extensions.Configuration; using Shared.DTO.Integracion_DGA; -using Shared.DTO.VariablesEntorno; +using Shared.Utils.Variables_Entorno; using System; using System.Collections.Generic; using System.Data; @@ -14,12 +14,21 @@ namespace DAL { public class JobsDgaSupFlujRepository { + private readonly IConfiguration _configuration; + private readonly BD_Conexion _bdConexion; + + public JobsDgaSupFlujRepository(IConfiguration configuration, BD_Conexion bdConexion) + { + _configuration = configuration; + _bdConexion = bdConexion; + } + public async Task InsertarDgaMacroResultadoSupFluj(List dgaMacroResultadoSupFluj) { try { // Configurar la conexión a la base de datos - using (SqlConnection connection = new SqlConnection(BdConexion.StringConnection)) + using (SqlConnection connection = new SqlConnection(_bdConexion.stringConnection)) { await connection.OpenAsync(); await connection.ExecuteAsync("TRUNCATE TABLE DGA_MACRO_RESULTADO_SUP_FLUJ"); @@ -41,7 +50,7 @@ namespace DAL { try { - using (var connection = new SqlConnection(BdConexion.StringConnection)) + using (var connection = new SqlConnection(_bdConexion.stringConnection)) { await connection.OpenAsync(); await connection.ExecuteAsync( @@ -61,7 +70,7 @@ namespace DAL { try { - using (var connection = new SqlConnection(BdConexion.StringConnection)) + using (var connection = new SqlConnection(_bdConexion.stringConnection)) { await connection.OpenAsync(); await connection.ExecuteAsync( diff --git a/DAL/JobsDgaVilosRepository.cs b/DAL/JobsDgaVilosRepository.cs index 66d67e1..fa6cfd1 100644 --- a/DAL/JobsDgaVilosRepository.cs +++ b/DAL/JobsDgaVilosRepository.cs @@ -2,7 +2,7 @@ using Microsoft.Data.SqlClient; using Microsoft.Extensions.Configuration; using Shared.DTO.Integracion_DGA; -using Shared.DTO.VariablesEntorno; +using Shared.Utils.Variables_Entorno; using System; using System.Collections.Generic; using System.Data; @@ -14,39 +14,47 @@ namespace DAL { public class JobsDgaVilosRepository { + private IConfiguration _configuration; + private readonly BD_Conexion _bdConexion; - public async Task InsertarDgaMacroResultadoVilos(List dgaMacroResultadoVilos) + public JobsDgaVilosRepository(IConfiguration configuration,BD_Conexion bdConexion) + { + _configuration = configuration; + _bdConexion = bdConexion; + } + + public async Task InsertarDgaMacroResultadoVilos(List dgaMacroResultadoVilos) + { + try { - try + using (var connection = new SqlConnection(_bdConexion.stringConnection)) { - using (var connection = new SqlConnection(BdConexion.StringConnection)) - { - await connection.OpenAsync(); + await connection.OpenAsync(); - // 1. Truncar la tabla antes de insertar - await connection.ExecuteAsync("TRUNCATE TABLE DGA_MACRO_RESULTADO_VILOS"); + // 1. Truncar la tabla antes de insertar + await connection.ExecuteAsync("TRUNCATE TABLE DGA_MACRO_RESULTADO_VILOS"); - // 2. Llamar al stored procedure - await connection.ExecuteAsync("SP_CALCULO_DGA_VILOS", commandType: System.Data.CommandType.StoredProcedure); + // 2. Llamar al stored procedure + await connection.ExecuteAsync("SP_CALCULO_DGA_VILOS", commandType: System.Data.CommandType.StoredProcedure); - // 3. Insertar todos los datos de la lista usando Dapper - string sql = "INSERT INTO DGA_MACRO_RESULTADO_VILOS (TagName, TimeStamp, Value, Quality) VALUES (@TagName, @TimeStamp, @Value, @Quality)"; - await connection.ExecuteAsync(sql, dgaMacroResultadoVilos); + // 3. Insertar todos los datos de la lista usando Dapper + string sql = "INSERT INTO DGA_MACRO_RESULTADO_VILOS (TagName, TimeStamp, Value, Quality) VALUES (@TagName, @TimeStamp, @Value, @Quality)"; + await connection.ExecuteAsync(sql, dgaMacroResultadoVilos); - return true; // Éxito - } - } - catch (Exception ex) - { - throw new Exception($"Error: {ex.Message}"); + return true; // Éxito } } + catch (Exception ex) + { + throw new Exception($"Error: {ex.Message}"); + } + } public async Task InsertarDgaSensorResultadoVilos(List dgaSensorResultadoVilos) { try { - using (var connection = new SqlConnection(BdConexion.StringConnection)) + using (var connection = new SqlConnection(_bdConexion.stringConnection)) { await connection.OpenAsync(); @@ -70,7 +78,7 @@ namespace DAL { try { - using (var connection = new SqlConnection(BdConexion.StringConnection)) + using (var connection = new SqlConnection(_bdConexion.stringConnection)) { await connection.OpenAsync(); await connection.ExecuteAsync( @@ -90,7 +98,7 @@ namespace DAL { try { - using (var connection = new SqlConnection(BdConexion.StringConnection)) + using (var connection = new SqlConnection(_bdConexion.stringConnection)) { await connection.OpenAsync(); await connection.ExecuteAsync( diff --git a/DAL/LogMedicionScadaRepository.cs b/DAL/LogMedicionScadaRepository.cs index 1e8e8de..7ada51e 100644 --- a/DAL/LogMedicionScadaRepository.cs +++ b/DAL/LogMedicionScadaRepository.cs @@ -7,17 +7,26 @@ using Dapper; using Microsoft.Data.SqlClient; using Microsoft.Extensions.Configuration; using Shared.DTO.Envios_DGA; -using Shared.DTO.VariablesEntorno; +using Shared.Utils.Variables_Entorno; namespace DAL { public class LogMedicionScadaRepository { + private IConfiguration _configuration; + private readonly BD_Conexion _bdConexion; + + public LogMedicionScadaRepository(IConfiguration configuration,BD_Conexion bdConexion) + { + _configuration = configuration; + _bdConexion = bdConexion; + } + public async Task InsertarLogMedicionScadaAsync(LogMedicionScada logMedicionScada) { try { - using (var connection = new SqlConnection(BdConexion.StringConnection)) + using (var connection = new SqlConnection(_bdConexion.stringConnection)) { await connection.OpenAsync(); diff --git a/DAL/MedicionScadaRepository.cs b/DAL/MedicionScadaRepository.cs index d29b842..805fb8f 100644 --- a/DAL/MedicionScadaRepository.cs +++ b/DAL/MedicionScadaRepository.cs @@ -2,16 +2,25 @@ using Microsoft.Data.SqlClient; using Microsoft.Extensions.Configuration; using Shared.DTO.Envios_DGA; -using Shared.DTO.VariablesEntorno; +using Shared.Utils.Variables_Entorno; using System.Data; namespace DAL { public class MedicionScadaRepository { + private readonly IConfiguration _configuration; + private readonly BD_Conexion _bdConexion; + + public MedicionScadaRepository(IConfiguration configuration,BD_Conexion bdConexion) + { + _configuration = configuration; + _bdConexion = bdConexion; + } + public async Task> ObtenerMedicionesAsync() { - await using var connection = new SqlConnection(BdConexion.StringConnection); + await using var connection = new SqlConnection(_bdConexion.stringConnection); var result = await connection.QueryAsync( "SP_OBTENER_MEDICION_SMARTSCADA_OPERACION", diff --git a/DAS/RegistrarMedicion.cs b/DAS/RegistrarMedicion.cs index f027fb8..472a24e 100644 --- a/DAS/RegistrarMedicion.cs +++ b/DAS/RegistrarMedicion.cs @@ -1,7 +1,6 @@ using DAL; using Microsoft.Extensions.Configuration; using Shared.DTO.Envios_DGA; -using Shared.DTO.VariablesEntorno; using System.Text; using System.Text.Json; @@ -10,19 +9,32 @@ namespace DAS public class RegistrarMedicion { private readonly HttpClient _httpClient; + private readonly IConfiguration _configuration; + private static string? password; + private static string? rutEmpresa; + private static string? rutUsuario; private readonly LogMedicionScadaRepository _logMedicionScadaRepository; - public RegistrarMedicion(HttpClient httpClient, LogMedicionScadaRepository logMedicionScadaRepository) + public RegistrarMedicion(HttpClient httpClient, IConfiguration configuration,LogMedicionScadaRepository logMedicionScadaRepository) { _httpClient = httpClient; + _configuration = configuration; + rutUsuario = _configuration["Credenciales:rutEmpresa"] ?? ""; + rutEmpresa = _configuration["Credenciales:rutUsuario"] ?? "" ?? ""; + password = _configuration["Credenciales:password"] ?? ""; _logMedicionScadaRepository = logMedicionScadaRepository; } public async Task EnviarMedicionAsync(string codigoObra, MedicionSubterraneaRequest request, long idMedicion) { - request.Autenticacion.Password = CredencialDGA.Password; - request.Autenticacion.RutEmpresa = CredencialDGA.RutEmpresa; //TODO: condicionar rut empresa - request.Autenticacion.RutUsuario = CredencialDGA.RutUsuario; + + request.Autenticacion.Password = password; + request.Autenticacion.RutEmpresa = rutEmpresa; + request.Autenticacion.RutUsuario = rutUsuario; + + var baseUrl = _configuration["ApiSubterranea:BaseUrl"]; + var endpoint = _configuration["ApiSubterranea:Endpoint"]; + var url = $"{baseUrl}{endpoint}"; var timeStamp = DateTime.UtcNow.ToString("yyyy-MM-dd'T'HH:mm:ss-0000"); @@ -32,7 +44,7 @@ namespace DAS content.Headers.Add("codigoObra", codigoObra); content.Headers.Add("timeStampOrigen", timeStamp); - var response = await _httpClient.PostAsync($"{SubterraneaApiUrl.BaseUrl}{SubterraneaApiUrl.EndPoint}", content); + var response = await _httpClient.PostAsync(url, content); string jsonRecibido = await response.Content.ReadAsStringAsync(); string estado = response.IsSuccessStatusCode ? "OK" : "ERROR"; string comprobante = string.Empty; @@ -44,7 +56,7 @@ namespace DAS if (doc.RootElement.TryGetProperty("data", out var dataProp) && dataProp.TryGetProperty("numeroComprobante", out var comprobanteProp)) { - comprobante = comprobanteProp.GetString() ?? ""; + comprobante = comprobanteProp.GetString(); } } catch @@ -53,16 +65,14 @@ namespace DAS } } - var logMedicionScada = new LogMedicionScada - { - EstadoEnvio = estado, - JsonEnviado = json, - JsonRecibido = jsonRecibido, - Comprobante = comprobante, - FechaEnvio = DateTime.UtcNow, - IdMedicionSmartscadaOperacion = idMedicion - }; - + var logMedicionScada = new LogMedicionScada(); + logMedicionScada.EstadoEnvio = estado; + logMedicionScada.JsonEnviado = json; + logMedicionScada.JsonRecibido = jsonRecibido; + logMedicionScada.Comprobante = comprobante; + logMedicionScada.FechaEnvio = DateTime.UtcNow; + logMedicionScada.IdMedicionSmartscadaOperacion = idMedicion; + await _logMedicionScadaRepository.InsertarLogMedicionScadaAsync(logMedicionScada); return response.IsSuccessStatusCode; diff --git a/Integracion_DGA/Program.cs b/Integracion_DGA/Program.cs index 3c54774..45214a5 100644 --- a/Integracion_DGA/Program.cs +++ b/Integracion_DGA/Program.cs @@ -6,6 +6,7 @@ using DAS; using BLL.Recuperacion_DGA; using BLL.Integracion_DGA; using Shared.Utils; +using Shared.Utils.Variables_Entorno; namespace Integracion_DGA { @@ -13,16 +14,20 @@ namespace Integracion_DGA { static async Task Main(string[] args) { - ObtenerVariablesEntorno.AmbientarApiUrlNexus("NEXUS_API_URL"); - ObtenerVariablesEntorno.AmbientarUrlApiSubterranea("SUBTERRANEAS_API_URL"); - ObtenerVariablesEntorno.AmbientarConexionBd("CONEXION_BD_ENVIO_DGA"); - ObtenerVariablesEntorno.AmbientarCredencialesDGA("DGA_CREDENCIALES"); + var nexusApiUrl = EnviromentUtils.GetEnvNexusApiUrl("NEXUS_API_URL"); + var subterraneaApiUrl = EnviromentUtils.GetEnvSubterraneaApiUrl("SUBTERRANEAS_API_URL"); + var bdConexion = EnviromentUtils.GetEnvBDConexion("CONEXION_BD_ENVIO_DGA"); + var credencialesDGA = EnviromentUtils.GetEnvCredencialesDGA("DGA_CREDENCIALES"); using IHost host = Host.CreateDefaultBuilder(args) .ConfigureServices((context, services) => { IConfiguration configuration = context.Configuration; services.AddSingleton(configuration); + services.AddSingleton(nexusApiUrl); + services.AddSingleton(subterraneaApiUrl); + services.AddSingleton(bdConexion); + services.AddSingleton(credencialesDGA); services.AddScoped(); services.AddScoped(); @@ -36,14 +41,12 @@ namespace Integracion_DGA services.AddScoped(); }) .Build(); - - //TODO: Controlar si las variables de ambiente existen var envioDGA = host.Services.GetRequiredService(); var bussinessLogic = host.Services.GetRequiredService(); var apiService = host.Services.GetRequiredService(); - - await bussinessLogic.Run(); + //await bussinessLogic.Run(); + //var mediciones = await envioDGA.ObtenerMedicionesAsync(); } } } diff --git a/Recuperacion_DGA/Program.cs b/Recuperacion_DGA/Program.cs index f11a53f..421851f 100644 --- a/Recuperacion_DGA/Program.cs +++ b/Recuperacion_DGA/Program.cs @@ -1,11 +1,10 @@ -using BLL.Integracion_DGA; -using BLL.Recuperacion_DGA; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Configuration; using DAL; using DAS; -using Microsoft.Extensions.Configuration; -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Hosting; -using Shared.Utils; +using BLL.Recuperacion_DGA; +using BLL.Integracion_DGA; namespace Recuperacion_DGA { @@ -13,11 +12,6 @@ namespace Recuperacion_DGA { static async Task Main(string[] args) { - ObtenerVariablesEntorno.AmbientarApiUrlNexus("NEXUS_API_URL"); - ObtenerVariablesEntorno.AmbientarUrlApiSubterranea("SUBTERRANEAS_API_URL"); - ObtenerVariablesEntorno.AmbientarConexionBd("CONEXION_BD_ENVIO_DGA"); - ObtenerVariablesEntorno.AmbientarCredencialesDGA("DGA_CREDENCIALES"); - using IHost host = Host.CreateDefaultBuilder(args) .ConfigureServices((context, services) => { @@ -31,19 +25,15 @@ namespace Recuperacion_DGA services.AddScoped(); services.AddScoped(); services.AddScoped(); - services.AddScoped(); services.AddScoped(); services.AddScoped(); }) .Build(); - //TODO: Controlar si las variables de ambiente existen - //TODO: Log de texto - var envioDGA = host.Services.GetRequiredService(); - await envioDGA.RegistrarMedicionesAsync(); + var mediciones = await envioDGA.ObtenerMedicionesAsync(); - //TODO: Log de texto + Console.WriteLine($"Se obtuvieron {mediciones.Count} registros."); } } } \ No newline at end of file diff --git a/SHARED/DTO/VariablesEntorno/BdConexion.cs b/SHARED/DTO/VariablesEntorno/BdConexion.cs deleted file mode 100644 index 3b6243d..0000000 --- a/SHARED/DTO/VariablesEntorno/BdConexion.cs +++ /dev/null @@ -1,13 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace Shared.DTO.VariablesEntorno -{ - public static class BdConexion - { - public static string StringConnection { get; set; } = string.Empty; - } -} diff --git a/SHARED/DTO/VariablesEntorno/CredencialDGA.cs b/SHARED/DTO/VariablesEntorno/CredencialDGA.cs deleted file mode 100644 index be9460b..0000000 --- a/SHARED/DTO/VariablesEntorno/CredencialDGA.cs +++ /dev/null @@ -1,15 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace Shared.DTO.VariablesEntorno -{ - public static class CredencialDGA - { - public static string RutEmpresa { get; set; } = string.Empty; - public static string RutUsuario { get; set; } = string.Empty; - public static string Password { get; set; } = string.Empty; - } -} diff --git a/SHARED/DTO/VariablesEntorno/NexusApiUrl.cs b/SHARED/DTO/VariablesEntorno/NexusApiUrl.cs deleted file mode 100644 index 9e19624..0000000 --- a/SHARED/DTO/VariablesEntorno/NexusApiUrl.cs +++ /dev/null @@ -1,18 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace Shared.DTO.VariablesEntorno -{ - public static class NexusApiUrl - { - public static string ApiUrl { get; set; } = string.Empty; - public static string ApiKey { get; set; } = string.Empty; - public static string Version { get; set; } = string.Empty; - public static string DataSource { get; set; } = string.Empty; - public static string Resolution { get; set; } = string.Empty; - - } -} diff --git a/SHARED/DTO/VariablesEntorno/SubterraneaApiUrl.cs b/SHARED/DTO/VariablesEntorno/SubterraneaApiUrl.cs deleted file mode 100644 index 93c2d18..0000000 --- a/SHARED/DTO/VariablesEntorno/SubterraneaApiUrl.cs +++ /dev/null @@ -1,15 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace Shared.DTO.VariablesEntorno -{ - public static class SubterraneaApiUrl - { - public static string BaseUrl { get; set; } = string.Empty; - public static string EndPoint { get; set; } = string.Empty; - - } -} diff --git a/SHARED/Utils/EnviromentUtils.cs b/SHARED/Utils/EnviromentUtils.cs new file mode 100644 index 0000000..38906e7 --- /dev/null +++ b/SHARED/Utils/EnviromentUtils.cs @@ -0,0 +1,79 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Shared.Utils.Variables_Entorno; + +namespace Shared.Utils +{ + public static class EnviromentUtils + { + public static Dictionary GetVarEnviromentDict(string key) + { + string variableEnv = Environment.GetEnvironmentVariable(key); + if (string.IsNullOrEmpty(variableEnv)) + { + throw new ArgumentException($"La variable de entorno '{key}' no está definida."); + } + var dictionary = new Dictionary(); + var pares = variableEnv.Split(';'); + + foreach (var data in pares) + { + if(data == null || data.Equals("")) { continue; } + var div = data.IndexOf('='); + var keyValue = data.Substring(0, div).ToUpper(); + var value = data.Substring(div+1); + if (!string.IsNullOrEmpty(keyValue) && !string.IsNullOrEmpty(value)) + { + dictionary.Add(keyValue, value); + } + } + return dictionary; + } + + public static Credenciales_DGA GetEnvCredencialesDGA(string env) + { + Dictionary dictEnv = GetVarEnviromentDict(env); + return new Credenciales_DGA + { + RutEmpresa = dictEnv.ContainsKey("RUTEMPRESA") ? dictEnv["RUTEMPRESA"] : string.Empty, + RutUsuario = dictEnv.ContainsKey("RUTUSUARIO") ? dictEnv["RUTUSUARIO"] : string.Empty, + Password = dictEnv.ContainsKey("PASSWORD") ? dictEnv["PASSWORD"] : string.Empty + }; + } + + public static BD_Conexion GetEnvBDConexion(string env) + { + + return new BD_Conexion + { + stringConnection = Environment.GetEnvironmentVariable(env) ?? string.Empty + }; + } + + public static NexusApiUrl GetEnvNexusApiUrl(string env) + { + Dictionary dictEnv = GetVarEnviromentDict(env); + return new NexusApiUrl + { + ApiUrl = dictEnv.ContainsKey("URL") ? dictEnv["URL"] : string.Empty, + ApiKey = dictEnv.ContainsKey("APIKEY") ? dictEnv["APIKEY"] : string.Empty, + Version = dictEnv.ContainsKey("VERSION") ? dictEnv["VERSION"] : string.Empty, + DataSource = dictEnv.ContainsKey("DATASOURCE") ? dictEnv["DATASOURCE"] : string.Empty, + Resolution = dictEnv.ContainsKey("RESOLUTION") ? dictEnv["RESOLUTION"] : string.Empty + }; + } + + public static SubterraneaApiUrl GetEnvSubterraneaApiUrl(string env) + { + Dictionary dictEnv = GetVarEnviromentDict(env); + return new SubterraneaApiUrl + { + BaseUrl = dictEnv.ContainsKey("URL") ? dictEnv["URL"] : string.Empty, + EndPoint = dictEnv.ContainsKey("ENDPOINT") ? dictEnv["ENDPOINT"] : string.Empty + }; + } + } +} diff --git a/SHARED/Utils/ObtenerVariablesEntorno.cs b/SHARED/Utils/ObtenerVariablesEntorno.cs deleted file mode 100644 index 6dcbaa0..0000000 --- a/SHARED/Utils/ObtenerVariablesEntorno.cs +++ /dev/null @@ -1,83 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using Shared.DTO.VariablesEntorno; - -namespace Shared.Utils -{ - public static class ObtenerVariablesEntorno - { - public static Dictionary GetVarEnviromentDict(string key) - { - string? variableEnv = Environment.GetEnvironmentVariable(key); - if (string.IsNullOrWhiteSpace(variableEnv)) - throw new ArgumentException($"La variable de entorno '{key}' no está definida."); - - var dictionary = new Dictionary(StringComparer.OrdinalIgnoreCase); - foreach (var data in variableEnv.Split(';', StringSplitOptions.RemoveEmptyEntries)) - { - int div = data.IndexOf('='); - if (div <= 0 || div >= data.Length - 1) continue; // Evita keys o values vacíos o sin '=' - - var keyValue = data[..div].Trim().ToUpperInvariant(); - var value = data[(div + 1)..].Trim(); - - if (!string.IsNullOrEmpty(keyValue) && !string.IsNullOrEmpty(value)) - dictionary[keyValue] = value; - } - return dictionary; - } - - public static void AmbientarCredencialesDGA(string env) - { - var dictEnv = GetVarEnviromentDict(env); - - dictEnv.TryGetValue("RUTEMPRESA", out string? rutEmpresa); - CredencialDGA.RutEmpresa = rutEmpresa ?? string.Empty; - - dictEnv.TryGetValue("RUTUSUARIO", out string? rutUsuario); - CredencialDGA.RutUsuario = rutUsuario ?? string.Empty; - - dictEnv.TryGetValue("PASSWORD", out string? password); - CredencialDGA.Password = password ?? string.Empty; - } - - public static void AmbientarConexionBd(string env) - { - BdConexion.StringConnection = Environment.GetEnvironmentVariable(env) ?? string.Empty; - } - - public static void AmbientarApiUrlNexus(string env) - { - var dictEnv = GetVarEnviromentDict(env); - - dictEnv.TryGetValue("URL", out string? url); - NexusApiUrl.ApiUrl = url ?? string.Empty; - - dictEnv.TryGetValue("APIKEY", out string? apiKey); - NexusApiUrl.ApiKey = apiKey ?? string.Empty; - - dictEnv.TryGetValue("VERSION", out string? version); - NexusApiUrl.Version = version ?? string.Empty; - - dictEnv.TryGetValue("DATASOURCE", out string? dataSource); - NexusApiUrl.DataSource = dataSource ?? string.Empty; - - dictEnv.TryGetValue("RESOLUTION", out string? resolution); - NexusApiUrl.Resolution = resolution ?? string.Empty; - } - - public static void AmbientarUrlApiSubterranea(string env) - { - var dictEnv = GetVarEnviromentDict(env); - - dictEnv.TryGetValue("URL", out string? url); - SubterraneaApiUrl.BaseUrl = url ?? string.Empty; - - dictEnv.TryGetValue("ENDPOINT", out string? endpoint); - SubterraneaApiUrl.EndPoint = endpoint ?? string.Empty; - } - } -} diff --git a/SHARED/Utils/Variables_Entorno/BD_Conexion.cs b/SHARED/Utils/Variables_Entorno/BD_Conexion.cs new file mode 100644 index 0000000..e3c03e2 --- /dev/null +++ b/SHARED/Utils/Variables_Entorno/BD_Conexion.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Shared.Utils.Variables_Entorno +{ + public class BD_Conexion + { + public String stringConnection { get; set; } + } +} diff --git a/SHARED/Utils/Variables_Entorno/Credenciales_DGA.cs b/SHARED/Utils/Variables_Entorno/Credenciales_DGA.cs new file mode 100644 index 0000000..bf386f7 --- /dev/null +++ b/SHARED/Utils/Variables_Entorno/Credenciales_DGA.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Shared.Utils.Variables_Entorno +{ + public class Credenciales_DGA + { + public String RutEmpresa { get; set; } + public String RutUsuario { get; set; } + public String Password { get; set; } + + } +} diff --git a/SHARED/Utils/Variables_Entorno/NexusApiUrl.cs b/SHARED/Utils/Variables_Entorno/NexusApiUrl.cs new file mode 100644 index 0000000..037a911 --- /dev/null +++ b/SHARED/Utils/Variables_Entorno/NexusApiUrl.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Shared.Utils.Variables_Entorno +{ + public class NexusApiUrl + { + public String ApiUrl { get; set; } + public String ApiKey { get; set; } + public String Version { get; set; } + public String DataSource { get; set; } + public String Resolution { get; set; } + + } +} diff --git a/SHARED/Utils/Variables_Entorno/SubterraneaApiUrl.cs b/SHARED/Utils/Variables_Entorno/SubterraneaApiUrl.cs new file mode 100644 index 0000000..0f9083c --- /dev/null +++ b/SHARED/Utils/Variables_Entorno/SubterraneaApiUrl.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Shared.Utils.Variables_Entorno +{ + public class SubterraneaApiUrl + { + public string BaseUrl { get; set; } + public string EndPoint { get; set; } + + } +}