Integracion_DGA/BLL/Recuperacion_DGA/EnvioDGA.cs

154 lines
6.4 KiB
C#
Raw Normal View History

using System.Text.Json;
using DAL;
2025-06-24 16:02:27 -04:00
using DAS;
using Shared.DTO.Envios_DGA;
using Shared.DTO.VariablesEntorno;
using Shared.Helper;
2025-06-24 14:46:32 -04:00
namespace BLL.Recuperacion_DGA
2025-06-24 14:46:32 -04:00
{
public class EnvioDGA
{
private readonly MedicionScadaRepository _dGAMedicionScadaRepository;
2025-06-24 16:02:27 -04:00
private readonly RegistrarMedicion _registrarMedicion;
2025-06-24 14:46:32 -04:00
public EnvioDGA(MedicionScadaRepository dGAMedicionScadaRepository, RegistrarMedicion registrarMedicion)
2025-06-24 14:46:32 -04:00
{
_dGAMedicionScadaRepository = dGAMedicionScadaRepository;
2025-06-24 16:02:27 -04:00
_registrarMedicion = registrarMedicion;
2025-06-24 14:46:32 -04:00
}
public async Task<bool> RegistrarMedicionesAsync()
2025-06-24 14:46:32 -04:00
{
try
2025-06-24 16:02:27 -04:00
{
await WriteLineAndLog("INICIO", "Inicio proceso de recuperación DGA", "");
WriteLineAndLog("Obteniendo Mediciones Scada", ConsoleColor.Green);
var mediciones = await _dGAMedicionScadaRepository.ObtenerMedicionesAsync();
var listaMediciones = new List<Object>();
foreach (var medicion in mediciones)
2025-06-24 16:02:27 -04:00
{
try
2025-06-24 16:02:27 -04:00
{
if (!string.IsNullOrEmpty(medicion.Code))
2025-06-24 16:02:27 -04:00
{
var rutEmpresa = string.Empty;
if (medicion.tipo_empresa == null)
2025-07-01 09:59:59 -04:00
{
continue;
}
if (medicion.tipo_empresa == "AV")
2025-07-01 09:59:59 -04:00
{
rutEmpresa = CredencialDGA.RutAv;
}
if (medicion.tipo_empresa == "EV")
{
rutEmpresa = CredencialDGA.RutEsval;
2025-07-01 09:59:59 -04:00
}
List<string> vacios = new List<string>();
if (medicion.Caudal == null || medicion.Caudal.Equals(""))
{
vacios.Add("caudal");
}
if (medicion.DateOrigen == null)
{
vacios.Add("fecha origen");
}
if (medicion.nivelFreaticoDelPozo == null || medicion.nivelFreaticoDelPozo.ToString().Equals(""))
{
vacios.Add("nivel freatico");
}
if (medicion.Totalizador == null || medicion.Totalizador.Equals(""))
{
vacios.Add("totalizador");
}
if (vacios.Count > 0)
{
await FileLoggerHelper.InsertarLogsAsync("REGISTRAR", $"Medicion {medicion.Code} no registra {string.Join(", ", vacios)}","");
}
var fechaEnvio = DateTime.UtcNow;
var body = new MedicionSubterraneaRequest
{
Autenticacion = new Autenticacion
{
Password = CredencialDGA.Password,
RutEmpresa = rutEmpresa,
RutUsuario = CredencialDGA.RutUsuario
},
MedicionSubterranea = new Medicion
{
Caudal = medicion.Caudal.ToString() ?? "",
FechaMedicion = medicion.DateOrigen?.ToString("yyyy-MM-dd") ?? "",
HoraMedicion = medicion.DateOrigen?.ToString("HH:mm:ss") ?? "",
NivelFreaticoDelPozo = medicion.nivelFreaticoDelPozo?.ToString() ?? "",
Totalizador = medicion.Totalizador.ToString() ?? "",
}
};
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);
WriteLineAndLog($"Error al enviar la medición con ID {medicion.Code}.", ConsoleColor.Red);
}
}
var listaMedicionesJson = JsonSerializer.Serialize(listaMediciones);
if (listaMediciones.Count > 0)
{
await MedicionScadaRepository.ActualizarMedicionesAsync(listaMedicionesJson);
}
await WriteLineAndLog("FIN","Fin proceso de recuperación DGA","");
}
catch (Exception ex)
{
FileLoggerHelper.LogError($"[Error] {ex.Message}.", ex);
WriteLineAndLog($"Error al procesar las mediciones.", ConsoleColor.Red);
return false;
2025-07-01 09:59:59 -04:00
}
return true;
2025-06-24 14:46:32 -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}");
}
static async Task WriteLineAndLog(string evento, string proceso, string operacion = null, ConsoleColor? color = null)
{
if (color.HasValue && Enum.IsDefined(typeof(ConsoleColor), color.Value))
{
Console.ForegroundColor = (ConsoleColor)color;
Console.WriteLine($"{proceso}");
Console.ResetColor();
}
else
{
Console.WriteLine($"{proceso}");
}
FileLoggerHelper.LogInformation($"{proceso}");
await FileLoggerHelper.InsertarLogsAsync(evento,proceso,operacion);
}
2025-06-24 14:46:32 -04:00
}
}