82 lines
3.6 KiB
C#
82 lines
3.6 KiB
C#
using System.Text.Json;
|
|
using DAL;
|
|
using DAS;
|
|
using Shared.DTO.Envios_DGA;
|
|
using Shared.DTO.VariablesEntorno;
|
|
using Shared.Helper;
|
|
|
|
namespace BLL.Recuperacion_DGA
|
|
{
|
|
public class EnvioDGA
|
|
{
|
|
private readonly MedicionDGARepository _dGAMedicionRepository;
|
|
private readonly RegistrarMedicion _registrarMedicion;
|
|
|
|
public EnvioDGA(MedicionDGARepository dGAMedicionRepository, RegistrarMedicion registrarMedicion)
|
|
{
|
|
_dGAMedicionRepository = dGAMedicionRepository;
|
|
_registrarMedicion = registrarMedicion;
|
|
}
|
|
|
|
public async Task<bool> RegistrarMedicionesAsync()
|
|
{
|
|
try
|
|
{
|
|
ConsoleLoggerHelper.WriteLineAndLogEventoAsync("INICIO", "Inicio proceso de recuperación DGA", "");
|
|
ConsoleLoggerHelper.WriteLineAndLogInfo("Obteniendo Mediciones Scada", ConsoleColor.Green);
|
|
|
|
var mediciones = await _dGAMedicionRepository.ObtenerMedicionesAsync();
|
|
var listaMediciones = new List<Object>();
|
|
|
|
foreach (var medicion in mediciones)
|
|
{
|
|
try
|
|
{
|
|
var fechaEnvio = DateTime.UtcNow;
|
|
|
|
var body = new MedicionSubterraneaRequest
|
|
{
|
|
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 ?? "",
|
|
}
|
|
};
|
|
await _registrarMedicion.EnviarMedicionAsync(medicion, body, fechaEnvio);
|
|
|
|
listaMediciones.Add(new {Id = medicion.ID,FechaEnvio = fechaEnvio.ToString("yyyy-MM-dd HH:mm:ss"), Enviado = medicion.ENVIADO + 1});
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
FileLoggerHelper.LogError($"[Error] {ex.Message}.", ex);
|
|
ConsoleLoggerHelper.WriteLineAndLogInfo($"Error al enviar la medición con ID {medicion.CODIGO_DGA}.", ConsoleColor.Red);
|
|
}
|
|
}
|
|
var listaMedicionesJson = JsonSerializer.Serialize(listaMediciones);
|
|
|
|
if (listaMediciones.Count > 0)
|
|
{
|
|
await MedicionDGARepository.ActualizarMedicionesAsync(listaMedicionesJson);
|
|
}
|
|
|
|
ConsoleLoggerHelper.WriteLineAndLogEventoAsync("FIN","Fin proceso de recuperación DGA","");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
FileLoggerHelper.LogError($"[Error] {ex.Message}.", ex);
|
|
ConsoleLoggerHelper.WriteLineAndLogInfo($"Error al procesar las mediciones.", ConsoleColor.Red);
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
}
|
|
}
|