feat: migracion y ajustes de configuracion, servicios y el sistema de logueo a la nueva app
This commit is contained in:
parent
45f5ec5f84
commit
3348272225
92 changed files with 2676 additions and 19 deletions
|
@ -6,6 +6,10 @@
|
|||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\DAL\DAL.csproj" />
|
||||
<ProjectReference Include="..\DAS\DAS.csproj" />
|
||||
|
|
111
BLL/Integracion_DGA/ApiService.cs
Normal file
111
BLL/Integracion_DGA/ApiService.cs
Normal file
|
@ -0,0 +1,111 @@
|
|||
using System.Text.Json;
|
||||
using System.Text;
|
||||
using Azure.Core;
|
||||
using System;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace BLL.Integracion_DGA
|
||||
{
|
||||
public class ApiService
|
||||
{
|
||||
private readonly HttpClient _httpClient;
|
||||
|
||||
public ApiService(HttpClient httpClient)
|
||||
{
|
||||
_httpClient = httpClient ?? throw new ArgumentNullException(nameof(httpClient));
|
||||
}
|
||||
|
||||
public async Task<string> GetApiResponseAsync(string apiUrl, Dictionary<string, string> headers = null)
|
||||
{
|
||||
|
||||
using (var httpClient = new HttpClient())
|
||||
{
|
||||
// Configura la URL base
|
||||
httpClient.BaseAddress = new Uri(apiUrl);
|
||||
|
||||
var request = new HttpRequestMessage(HttpMethod.Get, apiUrl);
|
||||
|
||||
// Configura los encabezados personalizados si se proporcionan
|
||||
if (headers != null)
|
||||
{
|
||||
foreach (var header in headers)
|
||||
{
|
||||
request.Headers.Add(header.Key, header.Value);
|
||||
}
|
||||
}
|
||||
|
||||
// Realiza la solicitud con la solicitud personalizada
|
||||
try
|
||||
{
|
||||
var response = await httpClient.SendAsync(request);
|
||||
if (response.IsSuccessStatusCode)
|
||||
{
|
||||
return await response.Content.ReadAsStringAsync();
|
||||
}
|
||||
else
|
||||
{
|
||||
// Maneja errores aquí según sea necesario.
|
||||
Console.WriteLine($"Error en la solicitud: {response.StatusCode}");
|
||||
throw new HttpRequestException($"Error en la solicitud HTTP. Código de estado: {response.StatusCode}");
|
||||
}
|
||||
}catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine("Error: " + ex.Message);
|
||||
throw new Exception($"Error en la solicitud HTTP: {ex.Message}");
|
||||
// Maneja la excepción según sea necesario
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<string> PostApiResponseAsync(string apiUrl, Dictionary<string, string> headers = null, object data = null)
|
||||
{
|
||||
using (var httpClient = new HttpClient())
|
||||
{
|
||||
try
|
||||
{
|
||||
httpClient.BaseAddress = new Uri(apiUrl);
|
||||
var request = new HttpRequestMessage(HttpMethod.Post, apiUrl);
|
||||
|
||||
// Serializa el objeto data a JSON
|
||||
//string jsonData = JsonSerializer.Serialize(data);
|
||||
string jsonData = JsonConvert.SerializeObject(data);
|
||||
// Crea el contenido de la solicitud HTTP con el cuerpo JSON
|
||||
HttpContent contenido = new StringContent(jsonData, Encoding.UTF8, "application/json");
|
||||
request.Content = contenido;
|
||||
|
||||
// Configura los encabezados personalizados si se proporcionan
|
||||
if (headers != null)
|
||||
{
|
||||
foreach (var header in headers)
|
||||
{
|
||||
request.Headers.Add(header.Key, header.Value);
|
||||
}
|
||||
}
|
||||
|
||||
// Realiza la solicitud POST
|
||||
HttpResponseMessage response = await httpClient.SendAsync(request);
|
||||
|
||||
// Verifica si la solicitud fue exitosa
|
||||
if (response.IsSuccessStatusCode)
|
||||
{
|
||||
// Lee la respuesta como una cadena
|
||||
return await response.Content.ReadAsStringAsync();
|
||||
}
|
||||
else
|
||||
{
|
||||
// La solicitud no fue exitosa, maneja el error según sea necesario
|
||||
Console.WriteLine($"Error en la solicitud HTTP. Código de estado: {response.StatusCode}");
|
||||
throw new HttpRequestException($"Error en la solicitud HTTP. Código de estado: {response.StatusCode}");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
// Maneja excepciones si ocurren
|
||||
Console.WriteLine($"Error en la solicitud HTTP: {ex.Message}");
|
||||
throw new Exception($"Error en la solicitud HTTP: {ex.Message}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
228
BLL/Integracion_DGA/BusinessLogic.cs
Normal file
228
BLL/Integracion_DGA/BusinessLogic.cs
Normal file
|
@ -0,0 +1,228 @@
|
|||
using Azure;
|
||||
using DAL;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Shared.DTO;
|
||||
using Shared.DTO.Integracion_DGA;
|
||||
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()
|
||||
{
|
||||
|
||||
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 = _configuration["ApiSettings:ApiUrl"];
|
||||
Dictionary<string, string> headers = new Dictionary<string, string>
|
||||
{
|
||||
{ "nexustoken", _configuration["ApiSettings:ApiKey"] },
|
||||
{ "nexusapiversion", _configuration["ApiSettings:Version"] },
|
||||
{ "accept", "application/json" }
|
||||
};
|
||||
|
||||
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);
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
HistoricRequest historicRequest = new HistoricRequest();
|
||||
historicRequest.DataSource = _configuration["ApiSettings:DataSource"];
|
||||
historicRequest.Resolution = _configuration["ApiSettings: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);
|
||||
|
||||
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;
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
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}");
|
||||
}
|
||||
}
|
||||
}
|
|
@ -2,8 +2,9 @@
|
|||
using Shared.DTO;
|
||||
using DAL;
|
||||
using DAS;
|
||||
using Shared.DTO.Envios_DGA;
|
||||
|
||||
namespace BLL
|
||||
namespace BLL.Recuperacion_DGA
|
||||
{
|
||||
public class EnvioDGA
|
||||
{
|
Loading…
Add table
Add a link
Reference in a new issue