2025-07-17 12:40:59 -04:00
|
|
|
|
using System;
|
|
|
|
|
using System.Text.Json;
|
2025-07-03 13:06:58 -04:00
|
|
|
|
using DAL;
|
2025-06-24 16:02:27 -04:00
|
|
|
|
using DAS;
|
2025-07-17 09:22:55 -04:00
|
|
|
|
using Shared.DTO;
|
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
|
|
|
|
|
{
|
2025-07-16 20:39:24 -04:00
|
|
|
|
private readonly MedicionDGARepository _dGAMedicionRepository;
|
2025-06-24 16:02:27 -04:00
|
|
|
|
private readonly RegistrarMedicion _registrarMedicion;
|
2025-07-17 09:22:55 -04:00
|
|
|
|
private readonly LogEnvioRepository _logEnvioRepository;
|
2025-06-24 14:46:32 -04:00
|
|
|
|
|
2025-07-17 09:22:55 -04:00
|
|
|
|
public EnvioDGA(MedicionDGARepository dGAMedicionRepository, RegistrarMedicion registrarMedicion, LogEnvioRepository logEnvioRepository)
|
2025-06-24 14:46:32 -04:00
|
|
|
|
{
|
2025-07-16 20:39:24 -04:00
|
|
|
|
_dGAMedicionRepository = dGAMedicionRepository;
|
2025-06-24 16:02:27 -04:00
|
|
|
|
_registrarMedicion = registrarMedicion;
|
2025-07-17 09:22:55 -04:00
|
|
|
|
_logEnvioRepository = logEnvioRepository;
|
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-02 11:37:41 -04:00
|
|
|
|
try
|
2025-06-24 16:02:27 -04:00
|
|
|
|
{
|
2025-07-17 09:22:55 -04:00
|
|
|
|
await _logEnvioRepository.InsertarLogProcesoAsync(new LogProceso
|
|
|
|
|
{
|
|
|
|
|
FechaEjecucion = DateTime.UtcNow,
|
|
|
|
|
NombreProceso = "ENVIO DATOS DGA"
|
|
|
|
|
});
|
|
|
|
|
|
2025-07-16 20:39:24 -04:00
|
|
|
|
ConsoleLoggerHelper.WriteLineAndLogEventoAsync("INICIO", "Inicio proceso de recuperación DGA", "");
|
|
|
|
|
ConsoleLoggerHelper.WriteLineAndLogInfo("Obteniendo Mediciones Scada", ConsoleColor.Green);
|
|
|
|
|
|
2025-07-17 12:40:59 -04:00
|
|
|
|
var logsEnviados = new List<LogMedicionEnvio>();
|
2025-07-16 20:39:24 -04:00
|
|
|
|
|
2025-07-17 12:40:59 -04:00
|
|
|
|
var pageNumber = 1;
|
2025-07-17 14:00:21 -04:00
|
|
|
|
var fechaInicio = DateTime.UtcNow;
|
2025-07-17 12:40:59 -04:00
|
|
|
|
|
|
|
|
|
while (true)
|
2025-06-24 16:02:27 -04:00
|
|
|
|
{
|
2025-07-17 14:00:21 -04:00
|
|
|
|
var mediciones = await _dGAMedicionRepository.ObtenerMedicionesPorLoteAsync(pageNumber, fechaInicio);
|
2025-07-17 12:40:59 -04:00
|
|
|
|
|
2025-07-17 13:42:48 -04:00
|
|
|
|
if (mediciones == null || mediciones.Count == 0)
|
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
2025-07-02 11:37:41 -04:00
|
|
|
|
|
2025-07-17 12:40:59 -04:00
|
|
|
|
foreach (var medicion in mediciones)
|
|
|
|
|
{
|
|
|
|
|
try
|
2025-07-17 09:43:13 -04:00
|
|
|
|
{
|
2025-07-17 12:40:59 -04:00
|
|
|
|
var fechaEnvio = DateTime.UtcNow;
|
|
|
|
|
|
|
|
|
|
var body = new MedicionSubterraneaRequest
|
2025-07-17 09:43:13 -04:00
|
|
|
|
{
|
2025-07-17 12:40:59 -04:00
|
|
|
|
Autenticacion = new Autenticacion
|
|
|
|
|
{
|
|
|
|
|
Password = CredencialDGA.Password,
|
|
|
|
|
RutEmpresa = medicion.TIPO_EMPRESA == "EV" ? CredencialDGA.RutEsval : CredencialDGA.RutAv,
|
|
|
|
|
RutUsuario = CredencialDGA.RutUsuario
|
|
|
|
|
},
|
|
|
|
|
MedicionSubterranea = new Medicion
|
|
|
|
|
{
|
|
|
|
|
Caudal = medicion.CAUDAL ?? "",
|
|
|
|
|
FechaMedicion = medicion.FECHA_MEDICION_CAUDAL?.ToString("yyyy-MM-dd") ?? "",
|
|
|
|
|
HoraMedicion = medicion.FECHA_MEDICION_CAUDAL?.ToString("HH:mm:ss") ?? "",
|
|
|
|
|
NivelFreaticoDelPozo = medicion.NIVEL_FREATICO_DEL_POZO ?? "",
|
|
|
|
|
Totalizador = medicion.TOTALIZADOR_CAUDAL ?? "",
|
|
|
|
|
}
|
|
|
|
|
};
|
2025-07-02 11:37:41 -04:00
|
|
|
|
|
2025-07-17 12:40:59 -04:00
|
|
|
|
var response = await _registrarMedicion.EnviarMedicionAsync(medicion, body, fechaEnvio);
|
2025-07-17 09:22:55 -04:00
|
|
|
|
|
2025-07-17 12:40:59 -04:00
|
|
|
|
logsEnviados.Add(response);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
ConsoleLoggerHelper.WriteLineAndLogInfo($"[Error]: medicion[{medicion.ID}], mensaje: {ex.Message}");
|
|
|
|
|
}
|
2025-07-02 11:37:41 -04:00
|
|
|
|
}
|
2025-07-17 12:40:59 -04:00
|
|
|
|
try
|
2025-07-02 11:37:41 -04:00
|
|
|
|
{
|
2025-07-17 12:40:59 -04:00
|
|
|
|
var idMediciones = mediciones.Select(x => x.ID).ToList();
|
|
|
|
|
await _dGAMedicionRepository.GuardarMedicionesEnviadasAsync(idMediciones);
|
|
|
|
|
await _logEnvioRepository.InsertarLogRespuesta(logsEnviados);
|
2025-07-02 11:37:41 -04:00
|
|
|
|
}
|
2025-07-17 12:40:59 -04:00
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
ConsoleLoggerHelper.WriteLineAndLogInfo($"[Error] {e.Message}");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
logsEnviados.Clear();
|
|
|
|
|
mediciones.Clear();
|
|
|
|
|
pageNumber++;
|
2025-07-01 14:00:41 -04:00
|
|
|
|
}
|
2025-07-03 13:06:58 -04:00
|
|
|
|
|
2025-07-16 20:39:24 -04:00
|
|
|
|
ConsoleLoggerHelper.WriteLineAndLogEventoAsync("FIN","Fin proceso de recuperación DGA","");
|
2025-07-02 11:37:41 -04:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
FileLoggerHelper.LogError($"[Error] {ex.Message}.", ex);
|
2025-07-16 20:39:24 -04:00
|
|
|
|
ConsoleLoggerHelper.WriteLineAndLogInfo($"Error al procesar las mediciones.", ConsoleColor.Red);
|
2025-07-02 11:37:41 -04:00
|
|
|
|
return false;
|
2025-07-01 09:59:59 -04:00
|
|
|
|
}
|
2025-07-17 09:22:55 -04:00
|
|
|
|
|
2025-07-01 09:59:59 -04:00
|
|
|
|
return true;
|
2025-06-24 14:46:32 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|