144 lines
5.9 KiB
C#
144 lines
5.9 KiB
C#
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 MedicionScadaRepository _dGAMedicionScadaRepository;
|
|
private readonly MedicionScadaVilosRepository _dgaMedicionScadaVilosRepository;
|
|
private readonly RegistrarMedicion _registrarMedicion;
|
|
|
|
public EnvioDGA(MedicionScadaRepository dGAMedicionScadaRepository, RegistrarMedicion registrarMedicion, MedicionScadaVilosRepository dgaMedicionScadaVilosRepository)
|
|
{
|
|
_dGAMedicionScadaRepository = dGAMedicionScadaRepository;
|
|
_registrarMedicion = registrarMedicion;
|
|
_dgaMedicionScadaVilosRepository = dgaMedicionScadaVilosRepository;
|
|
}
|
|
|
|
public async Task<bool> RegistrarMedicionesAsync()
|
|
{
|
|
WriteLineAndLog("Iniciando el proceso de recuperación DGA...");
|
|
WriteLineAndLog("Obteniendo Mediciones Scada", ConsoleColor.Green);
|
|
var mediciones = await _dGAMedicionScadaRepository.ObtenerMedicionesAsync();
|
|
|
|
foreach (var medicion in mediciones)
|
|
{
|
|
try
|
|
{
|
|
if (!string.IsNullOrEmpty(medicion.Code))
|
|
{
|
|
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;
|
|
}
|
|
|
|
|
|
|
|
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.Nivel.ToString() ?? "",
|
|
Totalizador = medicion.Totalizador.ToString() ?? "",
|
|
}
|
|
};
|
|
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);
|
|
}
|
|
}
|
|
|
|
var medicionesVilos = await _dgaMedicionScadaVilosRepository.ObtenerMedicionesVilosAsync();
|
|
foreach (var medicionVilos in medicionesVilos)
|
|
{
|
|
try
|
|
{
|
|
if (!string.IsNullOrEmpty(medicionVilos.Code))
|
|
{
|
|
var rutEmpresa = string.Empty;
|
|
|
|
if (medicionVilos.tipo_empresa.Equals("AV"))
|
|
{
|
|
rutEmpresa = CredencialDGA.RutAv;
|
|
}
|
|
else if (medicionVilos.tipo_empresa.Equals("EV"))
|
|
{
|
|
rutEmpresa = CredencialDGA.RutEsval;
|
|
}
|
|
|
|
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.Nivel.ToString() ?? "",
|
|
Totalizador = medicionVilos.Totalizador.ToString() ?? "",
|
|
}
|
|
};
|
|
|
|
await _registrarMedicion.EnviarMedicionAsync(medicionVilos.Code, body, medicionVilos.Id);
|
|
}
|
|
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
FileLoggerHelper.LogError($"[Error] {ex.Message}.", ex);
|
|
WriteLineAndLog($"Error al enviar la medición vilos con ID {medicionVilos.Code}.", ConsoleColor.Red);
|
|
}
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
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}");
|
|
}
|
|
}
|
|
}
|