d3c95a9fe1
- continuo modifiche x gestione task eseguiti
266 lines
9.4 KiB
C#
266 lines
9.4 KiB
C#
using Microsoft.Extensions.Configuration;
|
|
using MP.AppAuth.DTO;
|
|
using MP.AppAuth.Models;
|
|
using Newtonsoft.Json;
|
|
using NLog;
|
|
using RestSharp;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Web;
|
|
using static System.Net.Mime.MediaTypeNames;
|
|
|
|
namespace MP.AppAuth.Services
|
|
{
|
|
public class SyncService
|
|
{
|
|
#region Public Constructors
|
|
|
|
/// <summary>
|
|
/// Init classe
|
|
/// </summary>
|
|
/// <param name="configuration"></param>
|
|
public SyncService(IConfiguration configuration)
|
|
{
|
|
_configuration = configuration;
|
|
|
|
// sistemo conf secondo Target compilazione
|
|
#if DEBUG_LIMANDEBUG
|
|
apiUrl = "https://localhost:5003/";
|
|
#else
|
|
apiUrl = "https://liman.egalware.com/ELM.API/";
|
|
#endif
|
|
// fix opzioni base del RestClient
|
|
restOptStd = new RestClientOptions
|
|
{
|
|
Timeout = TimeSpan.FromSeconds(60),
|
|
BaseUrl = new Uri(apiUrl)
|
|
};
|
|
}
|
|
|
|
#endregion Public Constructors
|
|
|
|
#region Public Properties
|
|
|
|
public List<AnagKeyValueModel> AKVList { get; set; } = new List<AnagKeyValueModel>();
|
|
public string Applicazione { get; set; } = "";
|
|
|
|
/// <summary>
|
|
/// Codice cliente/installazione
|
|
/// </summary>
|
|
public string Installazione { get; set; } = "";
|
|
|
|
/// <summary>
|
|
/// Master key licenza principale
|
|
/// </summary>
|
|
public string MasterKey { get; set; } = "";
|
|
|
|
#endregion Public Properties
|
|
|
|
#region Public Methods
|
|
|
|
/// <summary>
|
|
/// Stato server gestione licenze
|
|
/// </summary>
|
|
public async Task<string> checkLimanServer()
|
|
{
|
|
string answ = "ND";
|
|
// cerco online
|
|
RestClient client = new RestClient(apiUrl);
|
|
var request = new RestRequest($"api/health", Method.Get);
|
|
var response = await client.GetAsync(request);
|
|
// controllo risposta
|
|
if (response.StatusCode == System.Net.HttpStatusCode.OK)
|
|
{
|
|
// verifico risposta
|
|
if (response.Content != null)
|
|
{
|
|
answ = response.Content.Replace("\"", "");
|
|
}
|
|
}
|
|
return answ;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Init della classe con variabili di base da Redis/DB
|
|
/// </summary>
|
|
public bool InitAkv()
|
|
{
|
|
bool fatto = false;
|
|
Applicazione = "MAPO";
|
|
Installazione = getAVKStr("Installazione");
|
|
MasterKey = getAVKStr(Applicazione);
|
|
fatto = !string.IsNullOrEmpty($"{Installazione}{MasterKey}");
|
|
return fatto;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Lista record AnagKeyVal
|
|
/// </summary>
|
|
public async Task<List<AnagKeyValueModel>?> OnlineListAnagKeyVal()
|
|
{
|
|
List<AnagKeyValueModel>? answ = new List<AnagKeyValueModel>();
|
|
// cerco online
|
|
RestClient client = new RestClient(apiUrl);
|
|
string MKeyEnc = HttpUtility.UrlEncode(MasterKey);
|
|
var request = new RestRequest($"api/dbsync/anagkeyval/{Installazione}?CodApp={Applicazione}", Method.Get);
|
|
var response = await client.GetAsync(request);
|
|
// controllo risposta
|
|
if (response.StatusCode == System.Net.HttpStatusCode.OK)
|
|
{
|
|
// salvo in redis contenuto serializzato
|
|
string rawData = $"{response.Content}";
|
|
answ = JsonConvert.DeserializeObject<List<AnagKeyValueModel>?>(rawData);
|
|
}
|
|
return await Task.FromResult(answ);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Lista record Config
|
|
/// </summary>
|
|
public async Task<List<ConfigModel>?> OnlineListConfig()
|
|
{
|
|
List<ConfigModel>? answ = new List<ConfigModel>();
|
|
// cerco online
|
|
RestClient client = new RestClient(apiUrl);
|
|
string MKeyEnc = HttpUtility.UrlEncode(MasterKey);
|
|
var request = new RestRequest($"api/dbsync/conf/{Installazione}?CodApp={Applicazione}", Method.Get);
|
|
var response = await client.GetAsync(request);
|
|
// controllo risposta
|
|
if (response.StatusCode == System.Net.HttpStatusCode.OK)
|
|
{
|
|
// salvo in redis contenuto serializzato
|
|
string rawData = $"{response.Content}";
|
|
answ = JsonConvert.DeserializeObject<List<ConfigModel>?>(rawData);
|
|
}
|
|
return await Task.FromResult(answ);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Lista record Vocabolario
|
|
/// </summary>
|
|
public async Task<List<VocabolarioModel>?> OnlineListVocabolario()
|
|
{
|
|
List<VocabolarioModel>? answ = new List<VocabolarioModel>();
|
|
// cerco online
|
|
RestClient client = new RestClient(apiUrl);
|
|
string MKeyEnc = HttpUtility.UrlEncode(MasterKey);
|
|
var request = new RestRequest($"api/dbsync/vocabolario/{Installazione}?CodApp={Applicazione}", Method.Get);
|
|
var response = await client.GetAsync(request);
|
|
// controllo risposta
|
|
if (response.StatusCode == System.Net.HttpStatusCode.OK)
|
|
{
|
|
// salvo in redis contenuto serializzato
|
|
string rawData = $"{response.Content}";
|
|
answ = JsonConvert.DeserializeObject<List<VocabolarioModel>?>(rawData);
|
|
}
|
|
return await Task.FromResult(answ);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Invio file zip di backup al server centrale
|
|
/// </summary>
|
|
/// <param name="CodApp">Cod Applicazione</param>
|
|
/// <param name="CodInst">CLiente / Installazione</param>
|
|
/// <param name="DoUnzip">Invia richiesta UnZip</param>
|
|
/// <param name="ForceApprov">Invia richiesta Approvazione</param>
|
|
/// <param name="ZipFile">ZipFile da inviare</param>
|
|
/// <returns></returns>
|
|
public async Task<bool> SendZipFile(string CodApp, string CodInst, bool DoUnzip, bool ForceApprov, FileInfo ZipFile)
|
|
{
|
|
bool answ = false;
|
|
try
|
|
{
|
|
string apiUrl = "/api/filesave/zipbackup";
|
|
Log.Info($"Inizio chiamata REST | URL: {restOptStd.BaseUrl} | api: {apiUrl}");
|
|
// client chiamate rest
|
|
var client = new RestClient(restOptStd);
|
|
// Chiamo il metodo!
|
|
var actReq = new RestRequest(apiUrl, Method.Post);
|
|
actReq.AddParameter("CodApp", $"{CodApp}");
|
|
actReq.AddParameter("CodInst", $"{CodInst}");
|
|
actReq.AddParameter("DoUnzip", $"{DoUnzip}");
|
|
actReq.AddParameter("ForceApprov", $"{ForceApprov}");
|
|
actReq.AddFile("ZipFile", ZipFile.FullName);
|
|
actReq.AddHeader("Content-Type", "multipart/form-data");
|
|
// effettuo vera chiamata
|
|
var currResp = await client.ExecuteAsync(actReq);
|
|
if ((currResp.StatusCode == System.Net.HttpStatusCode.OK || currResp.StatusCode == System.Net.HttpStatusCode.Created) && currResp.Content != null)
|
|
{
|
|
// mi restituisce esito upload
|
|
var currList = JsonConvert.DeserializeObject<UploadResult>(currResp.Content);
|
|
if (currList != null)
|
|
{
|
|
answ = currList.Uploaded;
|
|
Log.Info($"Invio file | ZipName: {ZipFile.Name} | uploaded: {answ}");
|
|
}
|
|
}
|
|
else
|
|
{
|
|
Log.Error($"Errore in invio | StatusCode: {currResp.StatusCode}{Environment.NewLine}{currResp.Content}");
|
|
}
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error($"Eccezione in fase gestione REST services SendZipFile{Environment.NewLine}{exc}");
|
|
}
|
|
|
|
return answ;
|
|
}
|
|
|
|
#endregion Public Methods
|
|
|
|
#region Protected Methods
|
|
|
|
/// <summary>
|
|
/// Cerca di recuperare valore string da elenco AKV
|
|
/// </summary>
|
|
/// <param name="varReq">Chiave AKV richiesta</param>
|
|
/// <returns></returns>
|
|
protected string getAVKStr(string varReq)
|
|
{
|
|
string answ = "";
|
|
if (AKVList != null && AKVList.Count > 0)
|
|
{
|
|
var currRec = AKVList.Where(x => x.NomeVar == varReq).FirstOrDefault();
|
|
if (currRec != null)
|
|
{
|
|
answ = $"{currRec.ValString}";
|
|
}
|
|
}
|
|
return answ;
|
|
}
|
|
|
|
#endregion Protected Methods
|
|
|
|
#region Private Fields
|
|
|
|
private static IConfiguration? _configuration;
|
|
|
|
/// <summary>
|
|
/// Classe logger
|
|
/// </summary>
|
|
private static Logger Log = LogManager.GetCurrentClassLogger();
|
|
|
|
#endregion Private Fields
|
|
|
|
#region Private Properties
|
|
|
|
/// <summary>
|
|
/// URL dell'API x chiamate gestione licenze
|
|
/// </summary>
|
|
private string apiUrl { get; set; } = "https://localhost:5003/";
|
|
|
|
/// <summary>
|
|
/// Conf client RestSharp standard:
|
|
/// - base URI al sito
|
|
/// - timeout 1 min
|
|
/// </summary>
|
|
private RestClientOptions restOptStd { get; set; } = new RestClientOptions();
|
|
|
|
#endregion Private Properties
|
|
}
|
|
} |