diff --git a/BLL/Integracion_DGA/BusinessLogic.cs b/BLL/Integracion_DGA/BusinessLogic.cs index ff4c145..e33c379 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,15 +23,13 @@ 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,NexusApiUrl nexusApiUrl) { + public BusinessLogic(IConfiguration configuration, ApiService apiService, JobsDgaRepository jobs, JobsDgaVilosRepository jobsVilos, JobsDgaSupFlujRepository jobsSupFluj) { _configuration = configuration; _apiService = apiService; _jobs = jobs; _jobsVilos = jobsVilos; _jobsSupFluj = jobsSupFluj; - _nexusApi = nexusApiUrl; FileLoggerHelper.ConfigureLogger(_configuration); } @@ -47,11 +45,11 @@ namespace BLL.Integracion_DGA WriteLineAndLog($"Inicia Proceso DGA"); try { - string apiUrlBase = _nexusApi.ApiUrl; + string apiUrlBase = NexusApiUrl.ApiUrl; Dictionary headers = new Dictionary { - { "nexustoken", _nexusApi.ApiKey }, - { "nexusapiversion", _nexusApi.Version }, + { "nexustoken", NexusApiUrl.ApiKey }, + { "nexusapiversion", NexusApiUrl.Version }, { "accept", "application/json" } }; @@ -76,8 +74,8 @@ namespace BLL.Integracion_DGA } HistoricRequest historicRequest = new HistoricRequest(); - historicRequest.DataSource = _nexusApi.DataSource; - historicRequest.Resolution = _nexusApi.Resolution; + historicRequest.DataSource = NexusApiUrl.DataSource; + historicRequest.Resolution = NexusApiUrl.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 95c0272..476d3cb 100644 --- a/BLL/Recuperacion_DGA/EnvioDGA.cs +++ b/BLL/Recuperacion_DGA/EnvioDGA.cs @@ -17,39 +17,48 @@ namespace BLL.Recuperacion_DGA _registrarMedicion = registrarMedicion; } - public async Task> ObtenerMedicionesAsync() + public async TaskRegistrarMedicionesAsync() { var mediciones = await _dGAMedicionScadaRepository.ObtenerMedicionesAsync(); foreach (var medicion in mediciones) { - if (!string.IsNullOrEmpty(medicion.Code)) + try { - var body = new MedicionSubterraneaRequest + if (!string.IsNullOrEmpty(medicion.Code)) { - Autenticacion = new Autenticacion + var body = new MedicionSubterraneaRequest { - 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() ?? "", - } - }; + 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() ?? "", + } + }; - await _registrarMedicion.EnviarMedicionAsync(medicion.Code, body,medicion.Id); + //TODO: Agregar log texto + + await _registrarMedicion.EnviarMedicionAsync(medicion.Code, body,medicion.Id); + } + + } + catch (Exception) + { + //TODO: Agregar log texto + throw; } } - - - return mediciones; + return true; } } } diff --git a/DAL/JobsDgaRepository.cs b/DAL/JobsDgaRepository.cs index c3e29bb..31d1504 100644 --- a/DAL/JobsDgaRepository.cs +++ b/DAL/JobsDgaRepository.cs @@ -9,25 +9,17 @@ using System.Text; using System.Threading.Tasks; using Dapper; using Microsoft.Extensions.Configuration; -using Shared.Utils.Variables_Entorno; +using Shared.DTO.VariablesEntorno; 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 @@ -49,7 +41,7 @@ namespace DAL { try { - using (var connection = new SqlConnection(_bdConexion.stringConnection)) + using (var connection = new SqlConnection(BdConexion.StringConnection)) { await connection.OpenAsync(); @@ -77,7 +69,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 @@ -102,7 +94,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 @@ -120,7 +112,7 @@ namespace DAL { try { - using (var connection = new SqlConnection(_bdConexion.stringConnection)) + using (var connection = new SqlConnection(BdConexion.StringConnection)) { await connection.OpenAsync(); await connection.ExecuteAsync( @@ -140,7 +132,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 1a2b3fa..4245aaa 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.Utils.Variables_Entorno; +using Shared.DTO.VariablesEntorno; using System; using System.Collections.Generic; using System.Data; @@ -14,21 +14,12 @@ 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"); @@ -50,7 +41,7 @@ namespace DAL { try { - using (var connection = new SqlConnection(_bdConexion.stringConnection)) + using (var connection = new SqlConnection(BdConexion.StringConnection)) { await connection.OpenAsync(); await connection.ExecuteAsync( @@ -70,7 +61,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 fa6cfd1..66d67e1 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.Utils.Variables_Entorno; +using Shared.DTO.VariablesEntorno; using System; using System.Collections.Generic; using System.Data; @@ -14,47 +14,39 @@ namespace DAL { public class JobsDgaVilosRepository { - private IConfiguration _configuration; - private readonly BD_Conexion _bdConexion; - public JobsDgaVilosRepository(IConfiguration configuration,BD_Conexion bdConexion) - { - _configuration = configuration; - _bdConexion = bdConexion; - } - - public async Task InsertarDgaMacroResultadoVilos(List dgaMacroResultadoVilos) - { - try + public async Task InsertarDgaMacroResultadoVilos(List dgaMacroResultadoVilos) { - using (var connection = new SqlConnection(_bdConexion.stringConnection)) + try { - await connection.OpenAsync(); + using (var connection = new SqlConnection(BdConexion.StringConnection)) + { + 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 + return true; // Éxito + } + } + catch (Exception ex) + { + throw new Exception($"Error: {ex.Message}"); } } - 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(); @@ -78,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( @@ -98,7 +90,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 7ada51e..1e8e8de 100644 --- a/DAL/LogMedicionScadaRepository.cs +++ b/DAL/LogMedicionScadaRepository.cs @@ -7,26 +7,17 @@ using Dapper; using Microsoft.Data.SqlClient; using Microsoft.Extensions.Configuration; using Shared.DTO.Envios_DGA; -using Shared.Utils.Variables_Entorno; +using Shared.DTO.VariablesEntorno; 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 805fb8f..d29b842 100644 --- a/DAL/MedicionScadaRepository.cs +++ b/DAL/MedicionScadaRepository.cs @@ -2,25 +2,16 @@ using Microsoft.Data.SqlClient; using Microsoft.Extensions.Configuration; using Shared.DTO.Envios_DGA; -using Shared.Utils.Variables_Entorno; +using Shared.DTO.VariablesEntorno; 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 472a24e..f027fb8 100644 --- a/DAS/RegistrarMedicion.cs +++ b/DAS/RegistrarMedicion.cs @@ -1,6 +1,7 @@ using DAL; using Microsoft.Extensions.Configuration; using Shared.DTO.Envios_DGA; +using Shared.DTO.VariablesEntorno; using System.Text; using System.Text.Json; @@ -9,32 +10,19 @@ 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, IConfiguration configuration,LogMedicionScadaRepository logMedicionScadaRepository) + public RegistrarMedicion(HttpClient httpClient, 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 = password; - request.Autenticacion.RutEmpresa = rutEmpresa; - request.Autenticacion.RutUsuario = rutUsuario; - - var baseUrl = _configuration["ApiSubterranea:BaseUrl"]; - var endpoint = _configuration["ApiSubterranea:Endpoint"]; - var url = $"{baseUrl}{endpoint}"; + request.Autenticacion.Password = CredencialDGA.Password; + request.Autenticacion.RutEmpresa = CredencialDGA.RutEmpresa; //TODO: condicionar rut empresa + request.Autenticacion.RutUsuario = CredencialDGA.RutUsuario; var timeStamp = DateTime.UtcNow.ToString("yyyy-MM-dd'T'HH:mm:ss-0000"); @@ -44,7 +32,7 @@ namespace DAS content.Headers.Add("codigoObra", codigoObra); content.Headers.Add("timeStampOrigen", timeStamp); - var response = await _httpClient.PostAsync(url, content); + var response = await _httpClient.PostAsync($"{SubterraneaApiUrl.BaseUrl}{SubterraneaApiUrl.EndPoint}", content); string jsonRecibido = await response.Content.ReadAsStringAsync(); string estado = response.IsSuccessStatusCode ? "OK" : "ERROR"; string comprobante = string.Empty; @@ -56,7 +44,7 @@ namespace DAS if (doc.RootElement.TryGetProperty("data", out var dataProp) && dataProp.TryGetProperty("numeroComprobante", out var comprobanteProp)) { - comprobante = comprobanteProp.GetString(); + comprobante = comprobanteProp.GetString() ?? ""; } } catch @@ -65,14 +53,16 @@ namespace DAS } } - var logMedicionScada = new LogMedicionScada(); - logMedicionScada.EstadoEnvio = estado; - logMedicionScada.JsonEnviado = json; - logMedicionScada.JsonRecibido = jsonRecibido; - logMedicionScada.Comprobante = comprobante; - logMedicionScada.FechaEnvio = DateTime.UtcNow; - logMedicionScada.IdMedicionSmartscadaOperacion = idMedicion; - + var logMedicionScada = new LogMedicionScada + { + EstadoEnvio = estado, + JsonEnviado = json, + JsonRecibido = jsonRecibido, + Comprobante = comprobante, + FechaEnvio = DateTime.UtcNow, + IdMedicionSmartscadaOperacion = idMedicion + }; + await _logMedicionScadaRepository.InsertarLogMedicionScadaAsync(logMedicionScada); return response.IsSuccessStatusCode; diff --git a/Integracion_DGA/Program.cs b/Integracion_DGA/Program.cs index 45214a5..3c54774 100644 --- a/Integracion_DGA/Program.cs +++ b/Integracion_DGA/Program.cs @@ -6,7 +6,6 @@ using DAS; using BLL.Recuperacion_DGA; using BLL.Integracion_DGA; using Shared.Utils; -using Shared.Utils.Variables_Entorno; namespace Integracion_DGA { @@ -14,20 +13,16 @@ namespace Integracion_DGA { static async Task Main(string[] args) { - 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"); + 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) => { IConfiguration configuration = context.Configuration; services.AddSingleton(configuration); - services.AddSingleton(nexusApiUrl); - services.AddSingleton(subterraneaApiUrl); - services.AddSingleton(bdConexion); - services.AddSingleton(credencialesDGA); services.AddScoped(); services.AddScoped(); @@ -41,12 +36,14 @@ 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(); - //var mediciones = await envioDGA.ObtenerMedicionesAsync(); + + await bussinessLogic.Run(); } } } diff --git a/Recuperacion_DGA/Program.cs b/Recuperacion_DGA/Program.cs index 421851f..f11a53f 100644 --- a/Recuperacion_DGA/Program.cs +++ b/Recuperacion_DGA/Program.cs @@ -1,10 +1,11 @@ -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Hosting; -using Microsoft.Extensions.Configuration; +using BLL.Integracion_DGA; +using BLL.Recuperacion_DGA; using DAL; using DAS; -using BLL.Recuperacion_DGA; -using BLL.Integracion_DGA; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; +using Shared.Utils; namespace Recuperacion_DGA { @@ -12,6 +13,11 @@ 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) => { @@ -25,15 +31,19 @@ namespace Recuperacion_DGA services.AddScoped(); services.AddScoped(); services.AddScoped(); + services.AddScoped(); services.AddScoped(); services.AddScoped(); }) .Build(); - var envioDGA = host.Services.GetRequiredService(); - var mediciones = await envioDGA.ObtenerMedicionesAsync(); + //TODO: Controlar si las variables de ambiente existen + //TODO: Log de texto - Console.WriteLine($"Se obtuvieron {mediciones.Count} registros."); + var envioDGA = host.Services.GetRequiredService(); + await envioDGA.RegistrarMedicionesAsync(); + + //TODO: Log de texto } } } \ No newline at end of file diff --git a/SHARED/DTO/VariablesEntorno/BdConexion.cs b/SHARED/DTO/VariablesEntorno/BdConexion.cs new file mode 100644 index 0000000..3b6243d --- /dev/null +++ b/SHARED/DTO/VariablesEntorno/BdConexion.cs @@ -0,0 +1,13 @@ +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 new file mode 100644 index 0000000..be9460b --- /dev/null +++ b/SHARED/DTO/VariablesEntorno/CredencialDGA.cs @@ -0,0 +1,15 @@ +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 new file mode 100644 index 0000000..9e19624 --- /dev/null +++ b/SHARED/DTO/VariablesEntorno/NexusApiUrl.cs @@ -0,0 +1,18 @@ +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 new file mode 100644 index 0000000..93c2d18 --- /dev/null +++ b/SHARED/DTO/VariablesEntorno/SubterraneaApiUrl.cs @@ -0,0 +1,15 @@ +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 deleted file mode 100644 index 38906e7..0000000 --- a/SHARED/Utils/EnviromentUtils.cs +++ /dev/null @@ -1,79 +0,0 @@ -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 new file mode 100644 index 0000000..6dcbaa0 --- /dev/null +++ b/SHARED/Utils/ObtenerVariablesEntorno.cs @@ -0,0 +1,83 @@ +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 deleted file mode 100644 index e3c03e2..0000000 --- a/SHARED/Utils/Variables_Entorno/BD_Conexion.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.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 deleted file mode 100644 index bf386f7..0000000 --- a/SHARED/Utils/Variables_Entorno/Credenciales_DGA.cs +++ /dev/null @@ -1,16 +0,0 @@ -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 deleted file mode 100644 index 037a911..0000000 --- a/SHARED/Utils/Variables_Entorno/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.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 deleted file mode 100644 index 0f9083c..0000000 --- a/SHARED/Utils/Variables_Entorno/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.Utils.Variables_Entorno -{ - public class SubterraneaApiUrl - { - public string BaseUrl { get; set; } - public string EndPoint { get; set; } - - } -}