2025-07-01 14:00:41 -04:00
|
|
|
|
using DAL;
|
2025-06-24 16:02:27 -04:00
|
|
|
|
using DAS;
|
2025-06-25 13:01:32 -04:00
|
|
|
|
using Shared.DTO.Envios_DGA;
|
2025-07-01 14:00:41 -04:00
|
|
|
|
using Shared.DTO.VariablesEntorno;
|
|
|
|
|
using Shared.Helper;
|
2025-06-24 14:46:32 -04:00
|
|
|
|
|
2025-06-25 13:01:32 -04:00
|
|
|
|
namespace BLL.Recuperacion_DGA
|
2025-06-24 14:46:32 -04:00
|
|
|
|
{
|
|
|
|
|
public class EnvioDGA
|
|
|
|
|
{
|
|
|
|
|
private readonly MedicionScadaRepository _dGAMedicionScadaRepository;
|
2025-07-01 14:00:41 -04:00
|
|
|
|
private readonly MedicionScadaVilosRepository _dgaMedicionScadaVilosRepository;
|
2025-06-24 16:02:27 -04:00
|
|
|
|
private readonly RegistrarMedicion _registrarMedicion;
|
2025-06-24 14:46:32 -04:00
|
|
|
|
|
2025-07-01 14:00:41 -04:00
|
|
|
|
public EnvioDGA(MedicionScadaRepository dGAMedicionScadaRepository, RegistrarMedicion registrarMedicion, MedicionScadaVilosRepository dgaMedicionScadaVilosRepository)
|
2025-06-24 14:46:32 -04:00
|
|
|
|
{
|
|
|
|
|
_dGAMedicionScadaRepository = dGAMedicionScadaRepository;
|
2025-06-24 16:02:27 -04:00
|
|
|
|
_registrarMedicion = registrarMedicion;
|
2025-07-01 14:00:41 -04:00
|
|
|
|
_dgaMedicionScadaVilosRepository = dgaMedicionScadaVilosRepository;
|
2025-06-24 14:46:32 -04:00
|
|
|
|
}
|
|
|
|
|
|
2025-07-01 14:00:41 -04:00
|
|
|
|
public async Task<bool> RegistrarMedicionesAsync()
|
2025-06-24 14:46:32 -04:00
|
|
|
|
{
|
2025-07-01 14:00:41 -04:00
|
|
|
|
WriteLineAndLog("Iniciando el proceso de recuperación DGA...");
|
|
|
|
|
WriteLineAndLog("Obteniendo Mediciones Scada", ConsoleColor.Green);
|
2025-06-24 16:02:27 -04:00
|
|
|
|
var mediciones = await _dGAMedicionScadaRepository.ObtenerMedicionesAsync();
|
|
|
|
|
|
|
|
|
|
foreach (var medicion in mediciones)
|
|
|
|
|
{
|
2025-07-01 09:59:59 -04:00
|
|
|
|
try
|
2025-06-24 16:02:27 -04:00
|
|
|
|
{
|
2025-07-01 09:59:59 -04:00
|
|
|
|
if (!string.IsNullOrEmpty(medicion.Code))
|
2025-06-24 16:02:27 -04:00
|
|
|
|
{
|
2025-07-01 14:00:41 -04:00
|
|
|
|
var rutEmpresa = string.Empty;
|
|
|
|
|
if (medicion.tipo_empresa == null)
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if (medicion.tipo_empresa == "AV")
|
|
|
|
|
{
|
|
|
|
|
rutEmpresa = CredencialDGA.RutAv;
|
|
|
|
|
}
|
|
|
|
|
if (medicion.tipo_empresa =="EV")
|
|
|
|
|
{
|
|
|
|
|
rutEmpresa = CredencialDGA.RutEsval;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-07-01 09:59:59 -04:00
|
|
|
|
var body = new MedicionSubterraneaRequest
|
2025-06-24 16:02:27 -04:00
|
|
|
|
{
|
2025-07-01 09:59:59 -04:00
|
|
|
|
Autenticacion = new Autenticacion
|
|
|
|
|
{
|
2025-07-01 14:00:41 -04:00
|
|
|
|
Password = CredencialDGA.Password,
|
|
|
|
|
RutEmpresa = rutEmpresa,
|
|
|
|
|
RutUsuario = CredencialDGA.RutUsuario
|
2025-07-01 09:59:59 -04:00
|
|
|
|
},
|
|
|
|
|
MedicionSubterranea = new Medicion
|
|
|
|
|
{
|
|
|
|
|
Caudal = medicion.Caudal.ToString() ?? "",
|
|
|
|
|
FechaMedicion = medicion.DateOrigen?.ToString("yyyy-MM-dd") ?? "",
|
|
|
|
|
HoraMedicion = medicion.DateOrigen?.ToString("HH:mm:ss") ?? "",
|
2025-07-01 15:43:03 -04:00
|
|
|
|
NivelFreaticoDelPozo = medicion.nivelFreaticoDelPozo?.ToString() ?? "",
|
2025-07-01 09:59:59 -04:00
|
|
|
|
Totalizador = medicion.Totalizador.ToString() ?? "",
|
|
|
|
|
}
|
|
|
|
|
};
|
2025-07-01 14:00:41 -04:00
|
|
|
|
await _registrarMedicion.EnviarMedicionAsync(medicion.Code, body, medicion.Id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
FileLoggerHelper.LogError($"[Error] {ex.Message}.",ex);
|
|
|
|
|
WriteLineAndLog($"Error al enviar la medición con ID {medicion.Code}.", ConsoleColor.Red);
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-07-01 09:59:59 -04:00
|
|
|
|
|
2025-07-01 15:43:03 -04:00
|
|
|
|
//var medicionesVilos = await _dgaMedicionScadaVilosRepository.ObtenerMedicionesVilosAsync();
|
|
|
|
|
//foreach (var medicionVilos in medicionesVilos)
|
|
|
|
|
//{
|
|
|
|
|
// try
|
|
|
|
|
// {
|
|
|
|
|
// if (!string.IsNullOrEmpty(medicionVilos.Code))
|
|
|
|
|
// {
|
|
|
|
|
// var rutEmpresa = string.Empty;
|
2025-06-24 16:02:27 -04:00
|
|
|
|
|
2025-07-01 15:43:03 -04:00
|
|
|
|
// if (medicionVilos.tipo_empresa.Equals("AV"))
|
|
|
|
|
// {
|
|
|
|
|
// rutEmpresa = CredencialDGA.RutAv;
|
|
|
|
|
// }
|
|
|
|
|
// else if (medicionVilos.tipo_empresa.Equals("EV"))
|
|
|
|
|
// {
|
|
|
|
|
// rutEmpresa = CredencialDGA.RutEsval;
|
|
|
|
|
// }
|
2025-07-01 14:00:41 -04:00
|
|
|
|
|
2025-07-01 15:43:03 -04:00
|
|
|
|
// var body = new MedicionSubterraneaRequest
|
|
|
|
|
// {
|
|
|
|
|
// Autenticacion = new Autenticacion
|
|
|
|
|
// {
|
|
|
|
|
// Password = CredencialDGA.Password,
|
|
|
|
|
// RutEmpresa = rutEmpresa,
|
|
|
|
|
// RutUsuario = CredencialDGA.RutUsuario
|
|
|
|
|
// },
|
|
|
|
|
// MedicionSubterranea = new Medicion
|
|
|
|
|
// {
|
|
|
|
|
// Caudal = medicionVilos.Caudal.ToString() ?? "",
|
|
|
|
|
// FechaMedicion = medicionVilos.DateOrigen?.ToString("yyyy-MM-dd") ?? "",
|
|
|
|
|
// HoraMedicion = medicionVilos.DateOrigen?.ToString("HH:mm:ss") ?? "",
|
|
|
|
|
// NivelFreaticoDelPozo = medicionVilos?.nivelFreaticoDelPozo.ToString() ?? "",
|
|
|
|
|
// Totalizador = medicionVilos.Totalizador.ToString() ?? "",
|
|
|
|
|
// }
|
|
|
|
|
// };
|
2025-07-01 14:00:41 -04:00
|
|
|
|
|
2025-07-01 15:43:03 -04:00
|
|
|
|
// await _registrarMedicion.EnviarMedicionAsync(medicionVilos.Code, body, medicionVilos.Id);
|
|
|
|
|
// }
|
2025-06-24 16:02:27 -04:00
|
|
|
|
|
2025-07-01 15:43:03 -04:00
|
|
|
|
// }
|
|
|
|
|
// catch (Exception ex)
|
|
|
|
|
// {
|
|
|
|
|
// FileLoggerHelper.LogError($"[Error] {ex.Message}.", ex);
|
|
|
|
|
// WriteLineAndLog($"Error al enviar la medición vilos con ID {medicionVilos.Code}.", ConsoleColor.Red);
|
|
|
|
|
// }
|
|
|
|
|
//}
|
2025-06-24 16:02:27 -04:00
|
|
|
|
|
2025-07-01 09:59:59 -04:00
|
|
|
|
return true;
|
2025-06-24 14:46:32 -04:00
|
|
|
|
}
|
2025-07-01 14:00:41 -04:00
|
|
|
|
|
|
|
|
|
static void WriteLineAndLog(string msj, ConsoleColor? color = null)
|
|
|
|
|
{
|
|
|
|
|
if (color.HasValue && Enum.IsDefined(typeof(ConsoleColor), color.Value))
|
|
|
|
|
{
|
|
|
|
|
Console.ForegroundColor = (ConsoleColor)color;
|
|
|
|
|
Console.WriteLine(msj);
|
|
|
|
|
Console.ResetColor();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine($"{msj}");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FileLoggerHelper.LogInformation($"{msj}");
|
|
|
|
|
}
|
2025-06-24 14:46:32 -04:00
|
|
|
|
}
|
|
|
|
|
}
|