From 222d5fdc0870df4e204743bd83455ba7222fe8cd Mon Sep 17 00:00:00 2001 From: Leonel Toro Date: Tue, 1 Jul 2025 01:33:49 -0400 Subject: [PATCH] =?UTF-8?q?feat:=20se=20centralizan=20las=20variables=20de?= =?UTF-8?q?=20entorno=20y=20utilidades,=20ademas=20de=20agregar=20entidade?= =?UTF-8?q?s=20para=20poder=20hacer=20m=C3=A1s=20legible=20el=20codigo?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- BLL/Integracion_DGA/BusinessLogic.cs | 15 ++-- DAL/JobsDgaRepository.cs | 20 ++--- DAL/JobsDgaSupFlujRepository.cs | 15 ++-- DAL/JobsDgaVilosRepository.cs | 15 ++-- DAL/LogMedicionScadaRepository.cs | 10 ++- DAL/MedicionScadaRepository.cs | 8 +- Integracion_DGA/Program.cs | 17 +++- Integracion_DGA/appsettings.json | 19 ----- SHARED/DTO/Envios_DGA/MedicionScada.cs | 2 + SHARED/Utils/EnviromentUtils.cs | 79 +++++++++++++++++++ SHARED/Utils/Variables_Entorno/BD_Conexion.cs | 13 +++ .../Variables_Entorno/Credenciales_DGA.cs | 16 ++++ SHARED/Utils/Variables_Entorno/NexusApiUrl.cs | 18 +++++ .../Variables_Entorno/SubterraneaApiUrl.cs | 15 ++++ 14 files changed, 203 insertions(+), 59 deletions(-) create mode 100644 SHARED/Utils/EnviromentUtils.cs create mode 100644 SHARED/Utils/Variables_Entorno/BD_Conexion.cs create mode 100644 SHARED/Utils/Variables_Entorno/Credenciales_DGA.cs create mode 100644 SHARED/Utils/Variables_Entorno/NexusApiUrl.cs create mode 100644 SHARED/Utils/Variables_Entorno/SubterraneaApiUrl.cs diff --git a/BLL/Integracion_DGA/BusinessLogic.cs b/BLL/Integracion_DGA/BusinessLogic.cs index bfd21f8..ff4c145 100644 --- a/BLL/Integracion_DGA/BusinessLogic.cs +++ b/BLL/Integracion_DGA/BusinessLogic.cs @@ -4,6 +4,7 @@ using Microsoft.Extensions.Configuration; using Shared.DTO; using Shared.DTO.Integracion_DGA; using Shared.Helper; +using Shared.Utils.Variables_Entorno; using System; using System.Collections.Generic; using System.Linq; @@ -22,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); } @@ -44,11 +47,11 @@ namespace BLL.Integracion_DGA WriteLineAndLog($"Inicia Proceso DGA"); try { - string apiUrlBase = _configuration["ApiSettings:ApiUrl"]; + string apiUrlBase = _nexusApi.ApiUrl; Dictionary headers = new Dictionary { - { "nexustoken", _configuration["ApiSettings:ApiKey"] }, - { "nexusapiversion", _configuration["ApiSettings:Version"] }, + { "nexustoken", _nexusApi.ApiKey }, + { "nexusapiversion", _nexusApi.Version }, { "accept", "application/json" } }; @@ -73,8 +76,8 @@ namespace BLL.Integracion_DGA } HistoricRequest historicRequest = new HistoricRequest(); - historicRequest.DataSource = _configuration["ApiSettings:DataSource"]; - historicRequest.Resolution = _configuration["ApiSettings:Resolution"]; + historicRequest.DataSource = _nexusApi.DataSource; + historicRequest.Resolution = _nexusApi.Resolution; historicRequest.Uids = listTagsID; historicRequest.StartTs = dateStart.ToUnixTimeSeconds(); historicRequest.EndTs = dateEnd.ToUnixTimeSeconds(); diff --git a/DAL/JobsDgaRepository.cs b/DAL/JobsDgaRepository.cs index d68c233..c3e29bb 100644 --- a/DAL/JobsDgaRepository.cs +++ b/DAL/JobsDgaRepository.cs @@ -9,25 +9,25 @@ using System.Text; using System.Threading.Tasks; using Dapper; using Microsoft.Extensions.Configuration; +using Shared.Utils.Variables_Entorno; namespace DAL { public class JobsDgaRepository { - private static string connectionString = string.Empty; private readonly IConfiguration _configuration; - public JobsDgaRepository(IConfiguration configuration) + private readonly BD_Conexion _bdConexion; + public JobsDgaRepository(IConfiguration configuration,BD_Conexion bdConexion) { _configuration = configuration; - connectionString = _configuration.GetConnectionString("DefaultConnection") ?? ""; - + _bdConexion = bdConexion; } public async Task InsertarDgaMacroResultado(List dgaMacroResultados) { try { - using (SqlConnection connection = new SqlConnection(connectionString)) + using (SqlConnection connection = new SqlConnection(_bdConexion.stringConnection)) { await connection.OpenAsync(); // 1. Truncar la tabla antes de insertar @@ -49,7 +49,7 @@ namespace DAL { try { - using (var connection = new SqlConnection(connectionString)) + using (var connection = new SqlConnection(_bdConexion.stringConnection)) { await connection.OpenAsync(); @@ -77,7 +77,7 @@ namespace DAL try { // Configurar la conexión a la base de datos - using (SqlConnection connection = new SqlConnection(connectionString)) + using (SqlConnection connection = new SqlConnection(_bdConexion.stringConnection)) { await connection.OpenAsync(); // Truncar la tabla antes de insertar @@ -102,7 +102,7 @@ namespace DAL { try { - using (var connection = new SqlConnection(connectionString)) + using (var connection = new SqlConnection(_bdConexion.stringConnection)) { await connection.OpenAsync(); // Ejecuta el stored procedure sin parámetros @@ -120,7 +120,7 @@ namespace DAL { try { - using (var connection = new SqlConnection(connectionString)) + using (var connection = new SqlConnection(_bdConexion.stringConnection)) { await connection.OpenAsync(); await connection.ExecuteAsync( @@ -140,7 +140,7 @@ namespace DAL { try { - using (var connection = new SqlConnection(connectionString)) + using (var connection = new SqlConnection(_bdConexion.stringConnection)) { await connection.OpenAsync(); await connection.ExecuteAsync( diff --git a/DAL/JobsDgaSupFlujRepository.cs b/DAL/JobsDgaSupFlujRepository.cs index 9ccd075..1a2b3fa 100644 --- a/DAL/JobsDgaSupFlujRepository.cs +++ b/DAL/JobsDgaSupFlujRepository.cs @@ -2,6 +2,7 @@ using Microsoft.Data.SqlClient; using Microsoft.Extensions.Configuration; using Shared.DTO.Integracion_DGA; +using Shared.Utils.Variables_Entorno; using System; using System.Collections.Generic; using System.Data; @@ -13,13 +14,13 @@ namespace DAL { public class JobsDgaSupFlujRepository { - private static string connectionString = string.Empty; private readonly IConfiguration _configuration; + private readonly BD_Conexion _bdConexion; - public JobsDgaSupFlujRepository(IConfiguration configuration) + public JobsDgaSupFlujRepository(IConfiguration configuration, BD_Conexion bdConexion) { - _configuration = configuration; - connectionString = _configuration.GetConnectionString("DefaultConnection") ?? ""; + _configuration = configuration; + _bdConexion = bdConexion; } public async Task InsertarDgaMacroResultadoSupFluj(List dgaMacroResultadoSupFluj) @@ -27,7 +28,7 @@ namespace DAL try { // Configurar la conexión a la base de datos - using (SqlConnection connection = new SqlConnection(connectionString)) + using (SqlConnection connection = new SqlConnection(_bdConexion.stringConnection)) { await connection.OpenAsync(); await connection.ExecuteAsync("TRUNCATE TABLE DGA_MACRO_RESULTADO_SUP_FLUJ"); @@ -49,7 +50,7 @@ namespace DAL { try { - using (var connection = new SqlConnection(connectionString)) + using (var connection = new SqlConnection(_bdConexion.stringConnection)) { await connection.OpenAsync(); await connection.ExecuteAsync( @@ -69,7 +70,7 @@ namespace DAL { try { - using (var connection = new SqlConnection(connectionString)) + using (var connection = new SqlConnection(_bdConexion.stringConnection)) { await connection.OpenAsync(); await connection.ExecuteAsync( diff --git a/DAL/JobsDgaVilosRepository.cs b/DAL/JobsDgaVilosRepository.cs index 822c6bb..fa6cfd1 100644 --- a/DAL/JobsDgaVilosRepository.cs +++ b/DAL/JobsDgaVilosRepository.cs @@ -2,6 +2,7 @@ using Microsoft.Data.SqlClient; using Microsoft.Extensions.Configuration; using Shared.DTO.Integracion_DGA; +using Shared.Utils.Variables_Entorno; using System; using System.Collections.Generic; using System.Data; @@ -14,19 +15,19 @@ namespace DAL public class JobsDgaVilosRepository { private IConfiguration _configuration; - private static string connectionString = string.Empty; + private readonly BD_Conexion _bdConexion; - public JobsDgaVilosRepository(IConfiguration configuration) + public JobsDgaVilosRepository(IConfiguration configuration,BD_Conexion bdConexion) { _configuration = configuration; - connectionString = _configuration.GetConnectionString("DefaultConnection") ?? ""; + _bdConexion = bdConexion; } public async Task InsertarDgaMacroResultadoVilos(List dgaMacroResultadoVilos) { try { - using (var connection = new SqlConnection(connectionString)) + using (var connection = new SqlConnection(_bdConexion.stringConnection)) { await connection.OpenAsync(); @@ -53,7 +54,7 @@ namespace DAL { try { - using (var connection = new SqlConnection(connectionString)) + using (var connection = new SqlConnection(_bdConexion.stringConnection)) { await connection.OpenAsync(); @@ -77,7 +78,7 @@ namespace DAL { try { - using (var connection = new SqlConnection(connectionString)) + using (var connection = new SqlConnection(_bdConexion.stringConnection)) { await connection.OpenAsync(); await connection.ExecuteAsync( @@ -97,7 +98,7 @@ namespace DAL { try { - using (var connection = new SqlConnection(connectionString)) + using (var connection = new SqlConnection(_bdConexion.stringConnection)) { await connection.OpenAsync(); await connection.ExecuteAsync( diff --git a/DAL/LogMedicionScadaRepository.cs b/DAL/LogMedicionScadaRepository.cs index fdebd53..7ada51e 100644 --- a/DAL/LogMedicionScadaRepository.cs +++ b/DAL/LogMedicionScadaRepository.cs @@ -7,24 +7,26 @@ using Dapper; using Microsoft.Data.SqlClient; using Microsoft.Extensions.Configuration; using Shared.DTO.Envios_DGA; +using Shared.Utils.Variables_Entorno; namespace DAL { public class LogMedicionScadaRepository { private IConfiguration _configuration; - private static string connectionString = string.Empty; - public LogMedicionScadaRepository(IConfiguration configuration) + private readonly BD_Conexion _bdConexion; + + public LogMedicionScadaRepository(IConfiguration configuration,BD_Conexion bdConexion) { _configuration = configuration; - connectionString = _configuration.GetConnectionString("DefaultConnection") ?? ""; + _bdConexion = bdConexion; } public async Task InsertarLogMedicionScadaAsync(LogMedicionScada logMedicionScada) { try { - using (var connection = new SqlConnection(connectionString)) + using (var connection = new SqlConnection(_bdConexion.stringConnection)) { await connection.OpenAsync(); diff --git a/DAL/MedicionScadaRepository.cs b/DAL/MedicionScadaRepository.cs index f2143cd..805fb8f 100644 --- a/DAL/MedicionScadaRepository.cs +++ b/DAL/MedicionScadaRepository.cs @@ -2,6 +2,7 @@ using Microsoft.Data.SqlClient; using Microsoft.Extensions.Configuration; using Shared.DTO.Envios_DGA; +using Shared.Utils.Variables_Entorno; using System.Data; namespace DAL @@ -9,16 +10,17 @@ namespace DAL public class MedicionScadaRepository { private readonly IConfiguration _configuration; + private readonly BD_Conexion _bdConexion; - public MedicionScadaRepository(IConfiguration configuration) + public MedicionScadaRepository(IConfiguration configuration,BD_Conexion bdConexion) { _configuration = configuration; + _bdConexion = bdConexion; } public async Task> ObtenerMedicionesAsync() { - var connectionString = _configuration.GetConnectionString("DefaultConnection"); - await using var connection = new SqlConnection(connectionString); + await using var connection = new SqlConnection(_bdConexion.stringConnection); var result = await connection.QueryAsync( "SP_OBTENER_MEDICION_SMARTSCADA_OPERACION", diff --git a/Integracion_DGA/Program.cs b/Integracion_DGA/Program.cs index 78fb31c..45214a5 100644 --- a/Integracion_DGA/Program.cs +++ b/Integracion_DGA/Program.cs @@ -5,6 +5,8 @@ using DAL; using DAS; using BLL.Recuperacion_DGA; using BLL.Integracion_DGA; +using Shared.Utils; +using Shared.Utils.Variables_Entorno; namespace Integracion_DGA { @@ -12,11 +14,20 @@ 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"); + 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(); @@ -30,12 +41,12 @@ namespace Integracion_DGA services.AddScoped(); }) .Build(); - + 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(); + //var mediciones = await envioDGA.ObtenerMedicionesAsync(); } } } diff --git a/Integracion_DGA/appsettings.json b/Integracion_DGA/appsettings.json index 49dec1d..8d964a3 100644 --- a/Integracion_DGA/appsettings.json +++ b/Integracion_DGA/appsettings.json @@ -1,23 +1,4 @@ { - "ConnectionStrings": { - "DefaultConnection": "Server=10.224.7.53,1433;Initial Catalog=ENVIO_DGA;Persist Security Info=False;User ID=usrdga;Password=AfX8zE8F740;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=true;Connection Timeout=30;" - }, - "ApiSettings": { - "ApiUrl": "http://smartscada.esval.cl:56000", - "ApiKey": "3c5fa5dd-1b19-422a-b668-10b1b6c566e2", - "Version": "v3.0", - "DataSource": "RAW", - "Resolution": "RES_15_MIN" - }, - "ApiSubterranea": { - "BaseUrl": "https://apimee.mop.gob.cl", - "Endpoint": "/api/v1/mediciones/subterraneas" - }, - "Credenciales": { - "rutEmpresa": "9A4PUqd1t4", - "rutUsuario": "77555666-7", - "password": "20999888-7" - }, "Logging": { "LogFile": { "Path": "logs/log.txt" diff --git a/SHARED/DTO/Envios_DGA/MedicionScada.cs b/SHARED/DTO/Envios_DGA/MedicionScada.cs index 7ffe6a8..9577348 100644 --- a/SHARED/DTO/Envios_DGA/MedicionScada.cs +++ b/SHARED/DTO/Envios_DGA/MedicionScada.cs @@ -26,5 +26,7 @@ namespace Shared.DTO.Envios_DGA public decimal? Caudalsub { get; set; } public decimal? Nivel { get; set; } + + public string? TipoEmpresa { get; set; } } } 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/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; } + + } +}