integración nueva tabla
This commit is contained in:
parent
5db07294f9
commit
657fd50ac9
22 changed files with 203 additions and 447 deletions
|
@ -7,33 +7,28 @@ using Shared.Helper;
|
|||
|
||||
namespace BLL.Integracion_DGA
|
||||
{
|
||||
public class BusinessLogic
|
||||
public class ObtencionDatosDga
|
||||
{
|
||||
private readonly IConfiguration _configuration;
|
||||
private readonly ApiService _apiService;
|
||||
private readonly JobsDgaRepository _jobs;
|
||||
private readonly JobsDgaSupFlujRepository _jobsSupFluj;
|
||||
private readonly FileLoggerHelper _fileLoggerHelper;
|
||||
|
||||
public BusinessLogic(IConfiguration configuration, ApiService apiService, JobsDgaRepository jobs, JobsDgaSupFlujRepository jobsSupFluj)
|
||||
public ObtencionDatosDga(IConfiguration configuration, ApiService apiService, JobsDgaRepository jobs)
|
||||
{
|
||||
_configuration = configuration;
|
||||
_apiService = apiService;
|
||||
_jobs = jobs;
|
||||
_jobsSupFluj = jobsSupFluj;
|
||||
FileLoggerHelper.ConfigureLogger(_configuration);
|
||||
}
|
||||
|
||||
public async Task Run()
|
||||
public async Task ObtenerDatosDga()
|
||||
{
|
||||
|
||||
DateTimeOffset dateEnd = DateTimeOffset.Now;
|
||||
DateTimeOffset dateStart = dateEnd.AddHours(-1);
|
||||
|
||||
JsonSerializerOptions options = new JsonSerializerOptions() { PropertyNameCaseInsensitive = true };
|
||||
|
||||
//FileLoggerHelper.ConfigureLogger(_configuration);
|
||||
WriteLineAndLog($"Inicia Proceso DGA");
|
||||
ConsoleLoggerHelper.WriteLineAndLogInfo($"INICIO OBTENER DATOS DGA");
|
||||
try
|
||||
{
|
||||
string apiUrlBase = NexusApiUrl.ApiUrl;
|
||||
|
@ -44,19 +39,25 @@ namespace BLL.Integracion_DGA
|
|||
{ "accept", "application/json" }
|
||||
};
|
||||
|
||||
WriteLineAndLog($"Obteniendo Documentos");
|
||||
ConsoleLoggerHelper.WriteLineAndLogInfo($"OBTENIENDO DOCUMENTOS");
|
||||
string apiUrlDocuments = apiUrlBase + "/api/Documents";
|
||||
|
||||
// Utiliza el servicio para realizar la solicitud a la API con URL y encabezados personalizados
|
||||
string responseData = await _apiService.GetApiResponseAsync(apiUrlDocuments, headers);
|
||||
|
||||
var documento = JsonSerializer.Deserialize<List<DocumentResponse>>(responseData, options);
|
||||
|
||||
if (documento == null || documento.Count == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
foreach (DocumentResponse item in documento)
|
||||
{
|
||||
WriteLineAndLog($"Obteniendo Tagviews");
|
||||
ConsoleLoggerHelper.WriteLineAndLogInfo($"OBTENIENDO TAGVIEWS");
|
||||
string apiUrlTagViews = $"{apiUrlBase}/api/Documents/tagviews/{item.uid}";
|
||||
responseData = await _apiService.GetApiResponseAsync(apiUrlTagViews, headers);
|
||||
TagviewsResponse tagviews = JsonSerializer.Deserialize<TagviewsResponse>(responseData, options);
|
||||
TagviewsResponse tagviews = JsonSerializer.Deserialize<TagviewsResponse>(responseData, options) ?? new TagviewsResponse();
|
||||
|
||||
List<string> listTagsID = tagviews.Columns.Select(x => x.Uid).ToList();
|
||||
|
||||
|
@ -69,15 +70,16 @@ namespace BLL.Integracion_DGA
|
|||
EndTs = dateEnd.ToUnixTimeSeconds()
|
||||
};
|
||||
|
||||
WriteLineAndLog($"Obteniendo Tagviews Historic");
|
||||
ConsoleLoggerHelper.WriteLineAndLogInfo($"OBTENIENDO TAGVIEWS HISTORIC");
|
||||
string apiUrlHistoric = $"{apiUrlBase}/api/Documents/tagviews/{item.uid}/historic";
|
||||
responseData = await _apiService.PostApiResponseAsync(apiUrlHistoric, headers, historicRequest);
|
||||
List<HistoricResponse> historicResponse = JsonSerializer.Deserialize<List<HistoricResponse>>(responseData, options);
|
||||
List<HistoricResponse> historicResponse = JsonSerializer.Deserialize<List<HistoricResponse>>(responseData, options) ?? new List<HistoricResponse>();
|
||||
|
||||
TimeZoneInfo zonaHorariaChile = TimeZoneInfo.FindSystemTimeZoneById("Pacific SA Standard Time");
|
||||
|
||||
if (item.name == "API - DGA - CAUDALES")
|
||||
{
|
||||
ConsoleLoggerHelper.WriteLineAndLogInfo($"OBTENIENDO DATOS DGA CAUDAL");
|
||||
var caudalData = tagviews.Columns
|
||||
.SelectMany(tag =>
|
||||
historicResponse.Where(h => h.Uid == tag.Uid)
|
||||
|
@ -95,6 +97,7 @@ namespace BLL.Integracion_DGA
|
|||
}
|
||||
else if (item.name == "API - DGA - NIVELES")
|
||||
{
|
||||
ConsoleLoggerHelper.WriteLineAndLogInfo($"OBTENIENDO DATOS DGA NIVEL");
|
||||
var nivelData = tagviews.Columns
|
||||
.SelectMany(tag =>
|
||||
historicResponse.Where(h => h.Uid == tag.Uid)
|
||||
|
@ -110,51 +113,17 @@ namespace BLL.Integracion_DGA
|
|||
|
||||
await _jobs.InsertarDgaNivel(nivelData);
|
||||
}
|
||||
else if (item.name == "API - DGA")
|
||||
{
|
||||
var nivelData = tagviews.Columns
|
||||
.SelectMany(tag =>
|
||||
historicResponse.Where(h => h.Uid == tag.Uid)
|
||||
.Select(h => new
|
||||
{
|
||||
TAG = tag.Name,
|
||||
NIVEL_FREATICO = h.Value?.ToString(),
|
||||
FECHAMEDICION = TimeZoneInfo.ConvertTimeFromUtc(
|
||||
new DateTime(1970, 1, 1).AddSeconds(h.TimeStamp).ToUniversalTime(),
|
||||
zonaHorariaChile)
|
||||
})
|
||||
).ToList();
|
||||
}
|
||||
}
|
||||
|
||||
WriteLineAndLog($"INICIO REGISTRO DE MEDICIONES DGA");
|
||||
ConsoleLoggerHelper.WriteLineAndLogInfo($"INICIO REGISTRO DE MEDICIONES DGA");
|
||||
await _jobs.SpRegistrarMedicionesDga();
|
||||
WriteLineAndLog($"FIN REGISTRO DE MEDICIONES DGA");
|
||||
|
||||
WriteLineAndLog($"FIN PROCESO DGA");
|
||||
ConsoleLoggerHelper.WriteLineAndLogInfo($"FIN REGISTRO DE MEDICIONES DGA");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
FileLoggerHelper.LogError($"{ex.Message}", ex);
|
||||
WriteLineAndLog($"{ex.Message}", ConsoleColor.Red);
|
||||
ConsoleLoggerHelper.WriteLineAndLogInfo($"{ex.Message}", ConsoleColor.Red);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
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}");
|
||||
}
|
||||
}
|
||||
}
|
|
@ -9,12 +9,12 @@ namespace BLL.Recuperacion_DGA
|
|||
{
|
||||
public class EnvioDGA
|
||||
{
|
||||
private readonly MedicionScadaRepository _dGAMedicionScadaRepository;
|
||||
private readonly MedicionDGARepository _dGAMedicionRepository;
|
||||
private readonly RegistrarMedicion _registrarMedicion;
|
||||
|
||||
public EnvioDGA(MedicionScadaRepository dGAMedicionScadaRepository, RegistrarMedicion registrarMedicion)
|
||||
public EnvioDGA(MedicionDGARepository dGAMedicionRepository, RegistrarMedicion registrarMedicion)
|
||||
{
|
||||
_dGAMedicionScadaRepository = dGAMedicionScadaRepository;
|
||||
_dGAMedicionRepository = dGAMedicionRepository;
|
||||
_registrarMedicion = registrarMedicion;
|
||||
}
|
||||
|
||||
|
@ -22,55 +22,16 @@ namespace BLL.Recuperacion_DGA
|
|||
{
|
||||
try
|
||||
{
|
||||
await WriteLineAndLog("INICIO", "Inicio proceso de recuperación DGA", "");
|
||||
WriteLineAndLog("Obteniendo Mediciones Scada", ConsoleColor.Green);
|
||||
var mediciones = await _dGAMedicionScadaRepository.ObtenerMedicionesAsync();
|
||||
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
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
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
|
||||
|
@ -78,76 +39,44 @@ namespace BLL.Recuperacion_DGA
|
|||
Autenticacion = new Autenticacion
|
||||
{
|
||||
Password = CredencialDGA.Password,
|
||||
RutEmpresa = rutEmpresa,
|
||||
RutEmpresa = medicion.TIPO_EMPRESA == "EV" ? CredencialDGA.RutEsval : CredencialDGA.RutAv,
|
||||
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() ?? "",
|
||||
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});
|
||||
}
|
||||
|
||||
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);
|
||||
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 MedicionScadaRepository.ActualizarMedicionesAsync(listaMedicionesJson);
|
||||
await MedicionDGARepository.ActualizarMedicionesAsync(listaMedicionesJson);
|
||||
}
|
||||
await WriteLineAndLog("FIN","Fin proceso de recuperación DGA","");
|
||||
|
||||
ConsoleLoggerHelper.WriteLineAndLogEventoAsync("FIN","Fin proceso de recuperación DGA","");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
FileLoggerHelper.LogError($"[Error] {ex.Message}.", ex);
|
||||
WriteLineAndLog($"Error al procesar las mediciones.", ConsoleColor.Red);
|
||||
ConsoleLoggerHelper.WriteLineAndLogInfo($"Error al procesar las mediciones.", ConsoleColor.Red);
|
||||
return false;
|
||||
}
|
||||
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}");
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue