Compare commits
2 commits
222d5fdc08
...
5bd9c2a1a6
Author | SHA1 | Date | |
---|---|---|---|
5bd9c2a1a6 | |||
|
0d67a44d36 |
20 changed files with 261 additions and 297 deletions
|
@ -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<string, string> headers = new Dictionary<string, string>
|
||||
{
|
||||
{ "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();
|
||||
|
|
|
@ -17,39 +17,48 @@ namespace BLL.Recuperacion_DGA
|
|||
_registrarMedicion = registrarMedicion;
|
||||
}
|
||||
|
||||
public async Task<List<MedicionScada>> ObtenerMedicionesAsync()
|
||||
public async Task<bool>RegistrarMedicionesAsync()
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<bool> InsertarDgaMacroResultado(List<DgaMacroResultado> 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(
|
||||
|
|
|
@ -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<bool> InsertarDgaMacroResultadoSupFluj(List<DgaMacroResultadoSupFluj> 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(
|
||||
|
|
|
@ -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<bool> InsertarDgaMacroResultadoVilos(List<DgaMacroResultadoVilos> dgaMacroResultadoVilos)
|
||||
{
|
||||
try
|
||||
public async Task<bool> InsertarDgaMacroResultadoVilos(List<DgaMacroResultadoVilos> 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<bool> InsertarDgaSensorResultadoVilos(List<DgaSensorResultadoVilos> 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(
|
||||
|
|
|
@ -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<bool> InsertarLogMedicionScadaAsync(LogMedicionScada logMedicionScada)
|
||||
{
|
||||
try
|
||||
{
|
||||
using (var connection = new SqlConnection(_bdConexion.stringConnection))
|
||||
using (var connection = new SqlConnection(BdConexion.StringConnection))
|
||||
{
|
||||
await connection.OpenAsync();
|
||||
|
||||
|
|
|
@ -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<List<MedicionScada>> ObtenerMedicionesAsync()
|
||||
{
|
||||
await using var connection = new SqlConnection(_bdConexion.stringConnection);
|
||||
await using var connection = new SqlConnection(BdConexion.StringConnection);
|
||||
|
||||
var result = await connection.QueryAsync<MedicionScada>(
|
||||
"SP_OBTENER_MEDICION_SMARTSCADA_OPERACION",
|
||||
|
|
|
@ -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<bool> 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;
|
||||
|
|
|
@ -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<IConfiguration>(configuration);
|
||||
services.AddSingleton(nexusApiUrl);
|
||||
services.AddSingleton(subterraneaApiUrl);
|
||||
services.AddSingleton(bdConexion);
|
||||
services.AddSingleton(credencialesDGA);
|
||||
|
||||
services.AddScoped<MedicionScadaRepository>();
|
||||
services.AddScoped<EnvioDGA>();
|
||||
|
@ -41,12 +36,14 @@ namespace Integracion_DGA
|
|||
services.AddScoped<BusinessLogic>();
|
||||
})
|
||||
.Build();
|
||||
|
||||
//TODO: Controlar si las variables de ambiente existen
|
||||
|
||||
var envioDGA = host.Services.GetRequiredService<EnvioDGA>();
|
||||
var bussinessLogic = host.Services.GetRequiredService<BusinessLogic>();
|
||||
var apiService = host.Services.GetRequiredService<ApiService>();
|
||||
//await bussinessLogic.Run();
|
||||
//var mediciones = await envioDGA.ObtenerMedicionesAsync();
|
||||
|
||||
await bussinessLogic.Run();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<JobsDgaRepository>();
|
||||
services.AddScoped<JobsDgaVilosRepository>();
|
||||
services.AddScoped<JobsDgaSupFlujRepository>();
|
||||
services.AddScoped<LogMedicionScadaRepository>();
|
||||
services.AddScoped<ApiService>();
|
||||
services.AddScoped<BusinessLogic>();
|
||||
})
|
||||
.Build();
|
||||
|
||||
var envioDGA = host.Services.GetRequiredService<EnvioDGA>();
|
||||
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<EnvioDGA>();
|
||||
await envioDGA.RegistrarMedicionesAsync();
|
||||
|
||||
//TODO: Log de texto
|
||||
}
|
||||
}
|
||||
}
|
13
SHARED/DTO/VariablesEntorno/BdConexion.cs
Normal file
13
SHARED/DTO/VariablesEntorno/BdConexion.cs
Normal file
|
@ -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;
|
||||
}
|
||||
}
|
15
SHARED/DTO/VariablesEntorno/CredencialDGA.cs
Normal file
15
SHARED/DTO/VariablesEntorno/CredencialDGA.cs
Normal file
|
@ -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;
|
||||
}
|
||||
}
|
18
SHARED/DTO/VariablesEntorno/NexusApiUrl.cs
Normal file
18
SHARED/DTO/VariablesEntorno/NexusApiUrl.cs
Normal file
|
@ -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;
|
||||
|
||||
}
|
||||
}
|
15
SHARED/DTO/VariablesEntorno/SubterraneaApiUrl.cs
Normal file
15
SHARED/DTO/VariablesEntorno/SubterraneaApiUrl.cs
Normal file
|
@ -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;
|
||||
|
||||
}
|
||||
}
|
|
@ -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<string,string> 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<string, string>();
|
||||
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<string,string> 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<string, string> 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<string, string> dictEnv = GetVarEnviromentDict(env);
|
||||
return new SubterraneaApiUrl
|
||||
{
|
||||
BaseUrl = dictEnv.ContainsKey("URL") ? dictEnv["URL"] : string.Empty,
|
||||
EndPoint = dictEnv.ContainsKey("ENDPOINT") ? dictEnv["ENDPOINT"] : string.Empty
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
83
SHARED/Utils/ObtenerVariablesEntorno.cs
Normal file
83
SHARED/Utils/ObtenerVariablesEntorno.cs
Normal file
|
@ -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<string, string> 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<string, string>(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;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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; }
|
||||
}
|
||||
}
|
|
@ -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; }
|
||||
|
||||
}
|
||||
}
|
|
@ -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; }
|
||||
|
||||
}
|
||||
}
|
|
@ -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; }
|
||||
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue