feat: se agregan logs, y se hacen cambios en el modulo registrar mediciones
This commit is contained in:
parent
5bd9c2a1a6
commit
4b6204d9e7
32 changed files with 648 additions and 625 deletions
|
@ -1,228 +1,221 @@
|
|||
using Azure;
|
||||
using System.Text.Json;
|
||||
using DAL;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Shared.DTO;
|
||||
using Shared.DTO.Integracion_DGA;
|
||||
using Shared.DTO.VariablesEntorno;
|
||||
using Shared.Helper;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace BLL.Integracion_DGA
|
||||
{
|
||||
public class BusinessLogic
|
||||
{
|
||||
private readonly IConfiguration _configuration;
|
||||
private readonly ApiService _apiService;
|
||||
private readonly JobsDgaRepository _jobs;
|
||||
private readonly JobsDgaVilosRepository _jobsVilos;
|
||||
private readonly JobsDgaSupFlujRepository _jobsSupFluj;
|
||||
private readonly FileLoggerHelper _fileLoggerHelper;
|
||||
|
||||
public BusinessLogic(IConfiguration configuration, ApiService apiService, JobsDgaRepository jobs, JobsDgaVilosRepository jobsVilos, JobsDgaSupFlujRepository jobsSupFluj) {
|
||||
_configuration = configuration;
|
||||
_apiService = apiService;
|
||||
_jobs = jobs;
|
||||
_jobsVilos = jobsVilos;
|
||||
_jobsSupFluj = jobsSupFluj;
|
||||
FileLoggerHelper.ConfigureLogger(_configuration);
|
||||
}
|
||||
|
||||
public async Task Run()
|
||||
public class BusinessLogic
|
||||
{
|
||||
private readonly IConfiguration _configuration;
|
||||
private readonly ApiService _apiService;
|
||||
private readonly JobsDgaRepository _jobs;
|
||||
private readonly JobsDgaVilosRepository _jobsVilos;
|
||||
private readonly JobsDgaSupFlujRepository _jobsSupFluj;
|
||||
private readonly FileLoggerHelper _fileLoggerHelper;
|
||||
|
||||
DateTimeOffset dateEnd = DateTimeOffset.Now;
|
||||
DateTimeOffset dateStart = dateEnd.AddHours(-1);
|
||||
public BusinessLogic(IConfiguration configuration, ApiService apiService, JobsDgaRepository jobs, JobsDgaVilosRepository jobsVilos, JobsDgaSupFlujRepository jobsSupFluj)
|
||||
{
|
||||
_configuration = configuration;
|
||||
_apiService = apiService;
|
||||
_jobs = jobs;
|
||||
_jobsVilos = jobsVilos;
|
||||
_jobsSupFluj = jobsSupFluj;
|
||||
FileLoggerHelper.ConfigureLogger(_configuration);
|
||||
}
|
||||
|
||||
JsonSerializerOptions options = new JsonSerializerOptions() { PropertyNameCaseInsensitive = true };
|
||||
public async Task Run()
|
||||
{
|
||||
|
||||
//FileLoggerHelper.ConfigureLogger(_configuration);
|
||||
WriteLineAndLog($"Inicia Proceso DGA");
|
||||
try
|
||||
{
|
||||
string apiUrlBase = NexusApiUrl.ApiUrl;
|
||||
Dictionary<string, string> headers = new Dictionary<string, string>
|
||||
DateTimeOffset dateEnd = DateTimeOffset.Now;
|
||||
DateTimeOffset dateStart = dateEnd.AddHours(-1);
|
||||
|
||||
JsonSerializerOptions options = new JsonSerializerOptions() { PropertyNameCaseInsensitive = true };
|
||||
|
||||
//FileLoggerHelper.ConfigureLogger(_configuration);
|
||||
WriteLineAndLog($"Inicia Proceso DGA");
|
||||
try
|
||||
{
|
||||
string apiUrlBase = NexusApiUrl.ApiUrl;
|
||||
Dictionary<string, string> headers = new Dictionary<string, string>
|
||||
{
|
||||
{ "nexustoken", NexusApiUrl.ApiKey },
|
||||
{ "nexusapiversion", NexusApiUrl.Version },
|
||||
{ "accept", "application/json" }
|
||||
};
|
||||
|
||||
WriteLineAndLog($"Obteniendo Documentos");
|
||||
string apiUrlDocuments = apiUrlBase + "/api/Documents";
|
||||
WriteLineAndLog($"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);
|
||||
// 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);
|
||||
foreach (DocumentResponse item in documento)
|
||||
{
|
||||
WriteLineAndLog($"Obteniendo Tagviews");
|
||||
string apiUrlTagViews = $"{apiUrlBase}/api/Documents/tagviews/{item.uid}";
|
||||
responseData = await _apiService.GetApiResponseAsync(apiUrlTagViews, headers);
|
||||
TagviewsResponse tagviews = JsonSerializer.Deserialize<TagviewsResponse>(responseData, options);
|
||||
var documento = JsonSerializer.Deserialize<List<DocumentResponse>>(responseData, options);
|
||||
foreach (DocumentResponse item in documento)
|
||||
{
|
||||
WriteLineAndLog($"Obteniendo Tagviews");
|
||||
string apiUrlTagViews = $"{apiUrlBase}/api/Documents/tagviews/{item.uid}";
|
||||
responseData = await _apiService.GetApiResponseAsync(apiUrlTagViews, headers);
|
||||
TagviewsResponse tagviews = JsonSerializer.Deserialize<TagviewsResponse>(responseData, options);
|
||||
|
||||
List<string> listTagsID = new List<string>();
|
||||
foreach (Column tag in tagviews.Columns)
|
||||
{
|
||||
listTagsID.Add(tag.Uid);
|
||||
}
|
||||
List<string> listTagsID = new List<string>();
|
||||
foreach (Column tag in tagviews.Columns)
|
||||
{
|
||||
listTagsID.Add(tag.Uid);
|
||||
}
|
||||
|
||||
HistoricRequest historicRequest = new HistoricRequest();
|
||||
historicRequest.DataSource = NexusApiUrl.DataSource;
|
||||
historicRequest.Resolution = NexusApiUrl.Resolution;
|
||||
historicRequest.Uids = listTagsID;
|
||||
historicRequest.StartTs = dateStart.ToUnixTimeSeconds();
|
||||
historicRequest.EndTs = dateEnd.ToUnixTimeSeconds();
|
||||
HistoricRequest historicRequest = new HistoricRequest();
|
||||
historicRequest.DataSource = NexusApiUrl.DataSource;
|
||||
historicRequest.Resolution = NexusApiUrl.Resolution;
|
||||
historicRequest.Uids = listTagsID;
|
||||
historicRequest.StartTs = dateStart.ToUnixTimeSeconds();
|
||||
historicRequest.EndTs = dateEnd.ToUnixTimeSeconds();
|
||||
|
||||
WriteLineAndLog($"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);
|
||||
WriteLineAndLog($"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<DgaMacroResultado> listDgaMacroResultados = new List<DgaMacroResultado>();
|
||||
foreach (Column tag in tagviews.Columns)
|
||||
{
|
||||
IEnumerable<HistoricResponse> filter = historicResponse.Where(h => h.Uid == tag.Uid);
|
||||
foreach (HistoricResponse historic in filter)
|
||||
{
|
||||
TimeZoneInfo zonaHorariaChile = TimeZoneInfo.FindSystemTimeZoneById("Pacific SA Standard Time");
|
||||
DateTime now = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddSeconds(historic.TimeStamp);
|
||||
DateTime fechaHoraChile = TimeZoneInfo.ConvertTimeFromUtc(now, zonaHorariaChile);
|
||||
List<DgaMacroResultado> listDgaMacroResultados = new List<DgaMacroResultado>();
|
||||
foreach (Column tag in tagviews.Columns)
|
||||
{
|
||||
IEnumerable<HistoricResponse> filter = historicResponse.Where(h => h.Uid == tag.Uid);
|
||||
foreach (HistoricResponse historic in filter)
|
||||
{
|
||||
TimeZoneInfo zonaHorariaChile = TimeZoneInfo.FindSystemTimeZoneById("Pacific SA Standard Time");
|
||||
DateTime now = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddSeconds(historic.TimeStamp);
|
||||
DateTime fechaHoraChile = TimeZoneInfo.ConvertTimeFromUtc(now, zonaHorariaChile);
|
||||
|
||||
|
||||
DgaMacroResultado dgaMacroResultado = new DgaMacroResultado();
|
||||
dgaMacroResultado.TagName = $"SCADA001.{tag.Name}.F_CV";
|
||||
dgaMacroResultado.Value = historic.Value;
|
||||
//dgaMacroResultado.TimeStamp = new DateTime(historic.TimeStamp);
|
||||
dgaMacroResultado.TimeStamp = fechaHoraChile;
|
||||
DgaMacroResultado dgaMacroResultado = new DgaMacroResultado();
|
||||
dgaMacroResultado.TagName = $"SCADA001.{tag.Name}.F_CV";
|
||||
dgaMacroResultado.Value = historic.Value;
|
||||
//dgaMacroResultado.TimeStamp = new DateTime(historic.TimeStamp);
|
||||
dgaMacroResultado.TimeStamp = fechaHoraChile;
|
||||
|
||||
listDgaMacroResultados.Add(dgaMacroResultado);
|
||||
listDgaMacroResultados.Add(dgaMacroResultado);
|
||||
}
|
||||
}
|
||||
|
||||
/**************/
|
||||
WriteLineAndLog($"Inicia Envio DGA", ConsoleColor.Green);
|
||||
WriteLineAndLog($"\t Descargar datos historian: Insertar DgaMacroResultado");
|
||||
var resultMacro = await _jobs.InsertarDgaMacroResultado(listDgaMacroResultados);
|
||||
|
||||
WriteLineAndLog($"\t Descargar datos historian: Insertar DgaSensorResultado");
|
||||
List<DgaSensorResultado> listDgaSensorResultado = listDgaMacroResultados.Select(item => new DgaSensorResultado
|
||||
{
|
||||
TagName = item.TagName,
|
||||
TimeStamp = item.TimeStamp,
|
||||
Value = item.Value,
|
||||
Quality = item.Quality
|
||||
}).ToList();
|
||||
|
||||
var resultSensor = await _jobs.InsertarDgaSensorResultado(listDgaSensorResultado);
|
||||
|
||||
WriteLineAndLog($"\t Calculo DGA");
|
||||
var resultCalculo = await _jobs.SpCalculoDga();
|
||||
|
||||
WriteLineAndLog($"\t descargar datos suma reversibilidad : Insertar DgaMacroResultadoSupFlujSuma");
|
||||
List<DgaMacroResultadoSupFlujSuma> listDgaMacroResultadoSupFlujSuma = listDgaMacroResultados.Select(item => new DgaMacroResultadoSupFlujSuma
|
||||
{
|
||||
TagName = item.TagName,
|
||||
TimeStamp = item.TimeStamp,
|
||||
Value = item.Value,
|
||||
Quality = item.Quality
|
||||
}).ToList();
|
||||
|
||||
var resultSupFlujSuma = await _jobs.InsertarDgaMacroResultadoSupFlujSuma(listDgaMacroResultadoSupFlujSuma);
|
||||
|
||||
WriteLineAndLog($"\t Calculo Suma");
|
||||
var resultCalculoDgaSup = await _jobs.SpCalculoDgaSupFlujSumaSnreversibilidad();
|
||||
|
||||
|
||||
WriteLineAndLog($"\t Traspaso datos achird");
|
||||
var result = await _jobs.SpTraspasoDatosAchird();
|
||||
WriteLineAndLog($"Fin Envio DGA", ConsoleColor.Green);
|
||||
/**************/
|
||||
/**************/
|
||||
WriteLineAndLog($"Inicio Envio DGA Vilos", ConsoleColor.Green);
|
||||
WriteLineAndLog($"\t Descargar datos historian: Insertar InsertarDgaMacroResultadoVilos");
|
||||
List<DgaMacroResultadoVilos> listDgaMacroResultadoVilos = listDgaMacroResultados.Select(item => new DgaMacroResultadoVilos
|
||||
{
|
||||
TagName = item.TagName,
|
||||
TimeStamp = item.TimeStamp,
|
||||
Value = item.Value,
|
||||
Quality = item.Quality
|
||||
}).ToList();
|
||||
|
||||
var resultVilos = await _jobsVilos.InsertarDgaMacroResultadoVilos(listDgaMacroResultadoVilos);
|
||||
|
||||
WriteLineAndLog($"\t Descargar datos historian: Insertar InsertarDgaSensorResultadoVilos");
|
||||
List<DgaSensorResultadoVilos> listDgaSensorResultadoVilos = listDgaMacroResultados.Select(item => new DgaSensorResultadoVilos
|
||||
{
|
||||
TagName = item.TagName,
|
||||
TimeStamp = item.TimeStamp,
|
||||
Value = item.Value,
|
||||
Quality = item.Quality
|
||||
}).ToList();
|
||||
|
||||
var resultSensorVilos = await _jobsVilos.InsertarDgaSensorResultadoVilos(listDgaSensorResultadoVilos);
|
||||
|
||||
WriteLineAndLog($"\t Calculo Suma");
|
||||
var resultCalculoVilos = await _jobsVilos.SpCalculoDgaVilos();
|
||||
|
||||
|
||||
WriteLineAndLog($"\t Traspaso datos achird");
|
||||
var resultAchirdVilos = await _jobsVilos.SpTraspasoDatosAchirdVilos();
|
||||
WriteLineAndLog($"Fin Envio DGA Vilos", ConsoleColor.Green);
|
||||
/**************/
|
||||
/**************/
|
||||
WriteLineAndLog($"Inicio Envio DGA Sup Fluj", ConsoleColor.Green);
|
||||
WriteLineAndLog($"\t Descargar datos historian: Insertar InsertarDgaMacroResultadoSupFluj");
|
||||
List<DgaMacroResultadoSupFluj> listDgaMacroResultadoSupFluj = listDgaMacroResultados.Select(item => new DgaMacroResultadoSupFluj
|
||||
{
|
||||
TagName = item.TagName,
|
||||
TimeStamp = item.TimeStamp,
|
||||
Value = item.Value,
|
||||
Quality = item.Quality
|
||||
}).ToList();
|
||||
|
||||
var resultSensorSup = await _jobsSupFluj.InsertarDgaMacroResultadoSupFluj(listDgaMacroResultadoSupFluj);
|
||||
|
||||
WriteLineAndLog($"\t Calculo Suma");
|
||||
var resultCalculoSup = await _jobsSupFluj.SpCalculoDgaSupFluj();
|
||||
|
||||
|
||||
WriteLineAndLog($"\t Traspaso datos achird");
|
||||
var resultAchirdSup = await _jobsSupFluj.SpTraspasoDatosAchirdSupFluj();
|
||||
WriteLineAndLog($"Fin Envio DGA Sup Fluj", ConsoleColor.Green);
|
||||
/**************/
|
||||
/**************/
|
||||
}
|
||||
WriteLineAndLog($"FIN Proceso DGA");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
FileLoggerHelper.LogError($"{ex.Message}", ex);
|
||||
WriteLineAndLog($"{ex.Message}", ConsoleColor.Red);
|
||||
}
|
||||
}
|
||||
|
||||
/**************/
|
||||
WriteLineAndLog($"Inicia Envio DGA", ConsoleColor.Green);
|
||||
WriteLineAndLog($"\t Descargar datos historian: Insertar DgaMacroResultado");
|
||||
var resultMacro = await _jobs.InsertarDgaMacroResultado(listDgaMacroResultados);
|
||||
|
||||
WriteLineAndLog($"\t Descargar datos historian: Insertar DgaSensorResultado");
|
||||
List<DgaSensorResultado> listDgaSensorResultado = listDgaMacroResultados.Select(item => new DgaSensorResultado
|
||||
{
|
||||
TagName = item.TagName,
|
||||
TimeStamp = item.TimeStamp,
|
||||
Value = item.Value,
|
||||
Quality = item.Quality
|
||||
}).ToList();
|
||||
|
||||
var resultSensor = await _jobs.InsertarDgaSensorResultado(listDgaSensorResultado);
|
||||
|
||||
WriteLineAndLog($"\t Calculo DGA");
|
||||
var resultCalculo = await _jobs.SpCalculoDga();
|
||||
|
||||
WriteLineAndLog($"\t descargar datos suma reversibilidad : Insertar DgaMacroResultadoSupFlujSuma");
|
||||
List<DgaMacroResultadoSupFlujSuma> listDgaMacroResultadoSupFlujSuma = listDgaMacroResultados.Select(item => new DgaMacroResultadoSupFlujSuma
|
||||
{
|
||||
TagName = item.TagName,
|
||||
TimeStamp = item.TimeStamp,
|
||||
Value = item.Value,
|
||||
Quality = item.Quality
|
||||
}).ToList();
|
||||
|
||||
var resultSupFlujSuma = await _jobs.InsertarDgaMacroResultadoSupFlujSuma(listDgaMacroResultadoSupFlujSuma);
|
||||
|
||||
WriteLineAndLog($"\t Calculo Suma");
|
||||
var resultCalculoDgaSup = await _jobs.SpCalculoDgaSupFlujSumaSnreversibilidad();
|
||||
|
||||
|
||||
WriteLineAndLog($"\t Traspaso datos achird");
|
||||
var result = await _jobs.SpTraspasoDatosAchird();
|
||||
WriteLineAndLog($"Fin Envio DGA", ConsoleColor.Green);
|
||||
/**************/
|
||||
/**************/
|
||||
WriteLineAndLog($"Inicio Envio DGA Vilos", ConsoleColor.Green);
|
||||
WriteLineAndLog($"\t Descargar datos historian: Insertar InsertarDgaMacroResultadoVilos");
|
||||
List<DgaMacroResultadoVilos> listDgaMacroResultadoVilos = listDgaMacroResultados.Select(item => new DgaMacroResultadoVilos
|
||||
{
|
||||
TagName = item.TagName,
|
||||
TimeStamp = item.TimeStamp,
|
||||
Value = item.Value,
|
||||
Quality = item.Quality
|
||||
}).ToList();
|
||||
|
||||
var resultVilos = await _jobsVilos.InsertarDgaMacroResultadoVilos(listDgaMacroResultadoVilos);
|
||||
|
||||
WriteLineAndLog($"\t Descargar datos historian: Insertar InsertarDgaSensorResultadoVilos");
|
||||
List<DgaSensorResultadoVilos> listDgaSensorResultadoVilos = listDgaMacroResultados.Select(item => new DgaSensorResultadoVilos
|
||||
{
|
||||
TagName = item.TagName,
|
||||
TimeStamp = item.TimeStamp,
|
||||
Value = item.Value,
|
||||
Quality = item.Quality
|
||||
}).ToList();
|
||||
|
||||
var resultSensorVilos = await _jobsVilos.InsertarDgaSensorResultadoVilos(listDgaSensorResultadoVilos);
|
||||
|
||||
WriteLineAndLog($"\t Calculo Suma");
|
||||
var resultCalculoVilos = await _jobsVilos.SpCalculoDgaVilos();
|
||||
|
||||
|
||||
WriteLineAndLog($"\t Traspaso datos achird");
|
||||
var resultAchirdVilos = await _jobsVilos.SpTraspasoDatosAchirdVilos();
|
||||
WriteLineAndLog($"Fin Envio DGA Vilos", ConsoleColor.Green);
|
||||
/**************/
|
||||
/**************/
|
||||
WriteLineAndLog($"Inicio Envio DGA Sup Fluj", ConsoleColor.Green);
|
||||
WriteLineAndLog($"\t Descargar datos historian: Insertar InsertarDgaMacroResultadoSupFluj");
|
||||
List<DgaMacroResultadoSupFluj> listDgaMacroResultadoSupFluj = listDgaMacroResultados.Select(item => new DgaMacroResultadoSupFluj
|
||||
{
|
||||
TagName = item.TagName,
|
||||
TimeStamp = item.TimeStamp,
|
||||
Value = item.Value,
|
||||
Quality = item.Quality
|
||||
}).ToList();
|
||||
|
||||
var resultSensorSup = await _jobsSupFluj.InsertarDgaMacroResultadoSupFluj(listDgaMacroResultadoSupFluj);
|
||||
|
||||
WriteLineAndLog($"\t Calculo Suma");
|
||||
var resultCalculoSup = await _jobsSupFluj.SpCalculoDgaSupFluj();
|
||||
|
||||
|
||||
WriteLineAndLog($"\t Traspaso datos achird");
|
||||
var resultAchirdSup = await _jobsSupFluj.SpTraspasoDatosAchirdSupFluj();
|
||||
WriteLineAndLog($"Fin Envio DGA Sup Fluj", ConsoleColor.Green);
|
||||
/**************/
|
||||
/**************/
|
||||
}
|
||||
WriteLineAndLog($"FIN Proceso DGA");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
FileLoggerHelper.LogError($"{ex.Message}", ex);
|
||||
WriteLineAndLog($"{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}");
|
||||
}
|
||||
|
||||
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}");
|
||||
FileLoggerHelper.LogInformation($"{msj}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,24 +1,28 @@
|
|||
using Microsoft.Extensions.Configuration;
|
||||
using Shared.DTO;
|
||||
using DAL;
|
||||
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)
|
||||
public EnvioDGA(MedicionScadaRepository dGAMedicionScadaRepository, RegistrarMedicion registrarMedicion, MedicionScadaVilosRepository dgaMedicionScadaVilosRepository)
|
||||
{
|
||||
_dGAMedicionScadaRepository = dGAMedicionScadaRepository;
|
||||
_registrarMedicion = registrarMedicion;
|
||||
_dgaMedicionScadaVilosRepository = dgaMedicionScadaVilosRepository;
|
||||
}
|
||||
|
||||
public async Task<bool>RegistrarMedicionesAsync()
|
||||
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)
|
||||
|
@ -27,38 +31,114 @@ namespace BLL.Recuperacion_DGA
|
|||
{
|
||||
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 = string.Empty,
|
||||
RutEmpresa = string.Empty,
|
||||
RutUsuario = string.Empty
|
||||
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 = "",
|
||||
NivelFreaticoDelPozo = medicion.Nivel.ToString() ?? "",
|
||||
Totalizador = medicion.Totalizador.ToString() ?? "",
|
||||
}
|
||||
};
|
||||
|
||||
//TODO: Agregar log texto
|
||||
|
||||
await _registrarMedicion.EnviarMedicionAsync(medicion.Code, body,medicion.Id);
|
||||
await _registrarMedicion.EnviarMedicionAsync(medicion.Code, body, medicion.Id);
|
||||
}
|
||||
|
||||
}
|
||||
catch (Exception)
|
||||
catch (Exception ex)
|
||||
{
|
||||
//TODO: Agregar log texto
|
||||
throw;
|
||||
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}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue