327 lines
12 KiB
C#
327 lines
12 KiB
C#
using EgwProxy.LiMan.DTO;
|
|
using Newtonsoft.Json;
|
|
using NLog;
|
|
using NLog.Fluent;
|
|
using RestSharp;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Net;
|
|
using System.Net.NetworkInformation;
|
|
using System.Text;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using System.Web;
|
|
|
|
namespace EgwProxy.LiMan
|
|
{
|
|
public class DataSyncro
|
|
{
|
|
#region Public Constructors
|
|
|
|
/// <summary>
|
|
/// Inizializza la libreria di comunicazione con il token assegnato
|
|
/// </summary>
|
|
/// <param name="serverUrl">URL del server</param>
|
|
public DataSyncro(string serverUrl)
|
|
{
|
|
servAddr = serverUrl;
|
|
apiUrl = $"https://{servAddr}/api/";
|
|
rcOptions = new RestClientOptions { BaseUrl = new Uri(apiUrl), Timeout = TimeSpan.FromMilliseconds(callTimeout) };
|
|
Log.Info($"DataSyncro initialized | api: {apiUrl} | timeout: {callTimeout}");
|
|
}
|
|
|
|
#endregion Public Constructors
|
|
|
|
#region Public Methods
|
|
|
|
/// <summary>
|
|
/// Test ping x indirizzo indicato
|
|
/// </summary>
|
|
/// <param name="tgtAddr">Indirizzo da pingare</param>
|
|
/// <returns></returns>
|
|
public static IPStatus pingAddress(string tgtAddr)
|
|
{
|
|
IPStatus answ = IPStatus.Unknown;
|
|
IPAddress address;
|
|
PingReply reply;
|
|
using (Ping pingSender = new Ping())
|
|
{
|
|
address = IPAddress.Loopback;
|
|
int pingMsTimeout = 500;
|
|
IPAddress.TryParse(tgtAddr, out address);
|
|
try
|
|
{
|
|
// se != null --> uso tgtAddr...
|
|
if (address != null)
|
|
{
|
|
reply = pingSender.Send(address, pingMsTimeout);
|
|
}
|
|
else
|
|
{
|
|
reply = pingSender.Send(tgtAddr, pingMsTimeout);
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
reply = pingSender.Send(IPAddress.Loopback, pingMsTimeout);
|
|
}
|
|
answ = reply.Status;
|
|
}
|
|
return answ;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Test ping x indirizzo indicato
|
|
/// </summary>
|
|
/// <param name="tgtAddr">Indirizzo da pingare</param>
|
|
/// <param name="timeout">Timeout chiamata in ms</param>
|
|
/// <returns></returns>
|
|
public static IPStatus pingAddress(string tgtAddr, int timeout)
|
|
{
|
|
IPStatus answ = IPStatus.Unknown;
|
|
IPAddress address;
|
|
PingReply reply;
|
|
using (Ping pingSender = new Ping())
|
|
{
|
|
address = IPAddress.Loopback;
|
|
int pingMsTimeout = timeout;
|
|
IPAddress.TryParse(tgtAddr, out address);
|
|
try
|
|
{
|
|
// se != null --> uso tgtAddr...
|
|
if (address != null && address != IPAddress.Loopback)
|
|
{
|
|
reply = pingSender.Send(address, pingMsTimeout);
|
|
}
|
|
else
|
|
{
|
|
reply = pingSender.Send(tgtAddr, pingMsTimeout);
|
|
}
|
|
answ = reply.Status;
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
answ = IPStatus.Unknown;
|
|
Log.Error($"Errore in ping:{Environment.NewLine}{exc}");
|
|
}
|
|
}
|
|
return answ;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Effettua chiamata test sul server (ping), se fallisse riprova fino a maxTry volte
|
|
/// </summary>
|
|
/// <param name="maxTry">num tentativi in caso di KO</param>
|
|
/// <param name="waitTime">tempo di attesa medio tra tentativi in ms</param>
|
|
/// <returns></returns>
|
|
public bool CheckRemote(int maxTry = 5, int waitTime = 200)
|
|
{
|
|
bool res = false;
|
|
int numTry = 0;
|
|
IPAddress address = IPAddress.Loopback;
|
|
string srvName = servAddr;
|
|
// tolgo eventuale nome con ":"
|
|
if (servAddr.Contains(":"))
|
|
{
|
|
srvName = servAddr.Substring(0, servAddr.IndexOf(":"));
|
|
}
|
|
// tolgo eventuale nome con "/"
|
|
if (servAddr.Contains("/"))
|
|
{
|
|
srvName = servAddr.Substring(0, servAddr.IndexOf("/"));
|
|
}
|
|
while (!res && numTry < maxTry)
|
|
{
|
|
res = pingAddress(srvName, callTimeout) == IPStatus.Success;
|
|
numTry++;
|
|
if (!res)
|
|
{
|
|
Thread.Sleep(waitTime);
|
|
}
|
|
}
|
|
return res;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Elenco Release applicazione
|
|
/// </summary>
|
|
/// <param name="CodApp">CodApp richiesta</param>
|
|
/// <returns></returns>
|
|
public List<ReleaseDTO> ReleaseGetAll(string CodApp)
|
|
{
|
|
List<ReleaseDTO> answ = new List<ReleaseDTO>();
|
|
using (RestClient client = new RestClient(rcOptions))
|
|
{
|
|
string CodAppEnc = HttpUtility.UrlEncode(CodApp);
|
|
var request = new RestRequest($"Release/{CodAppEnc}", Method.Get);
|
|
var response = client.Get(request);
|
|
// controllo risposta
|
|
if (response.StatusCode == HttpStatusCode.OK)
|
|
{
|
|
// contenuto serializzato
|
|
string rawData = $"{response.Content}";
|
|
answ = JsonConvert.DeserializeObject<List<ReleaseDTO>>(rawData);
|
|
}
|
|
}
|
|
return answ;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Versione Async Elenco Release applicazione
|
|
/// </summary>
|
|
/// <param name="CodApp">CodApp richiesta</param>
|
|
/// <returns></returns>
|
|
public async Task<List<ReleaseDTO>> ReleaseGetAllAsync(string CodApp)
|
|
{
|
|
List<ReleaseDTO> answ = new List<ReleaseDTO>();
|
|
|
|
using (RestClient client = new RestClient(rcOptions))
|
|
{
|
|
string CodAppEnc = HttpUtility.UrlEncode(CodApp);
|
|
var request = new RestRequest($"Release/{CodAppEnc}", Method.Get);
|
|
var response = await client.GetAsync(request);
|
|
// controllo risposta
|
|
if (response.StatusCode == HttpStatusCode.OK)
|
|
{
|
|
// contenuto serializzato
|
|
string rawData = $"{response.Content}";
|
|
answ = JsonConvert.DeserializeObject<List<ReleaseDTO>>(rawData);
|
|
}
|
|
}
|
|
return await Task.FromResult(answ);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Elenco Release applicazione
|
|
/// </summary>
|
|
/// <param name="CodApp">CodApp richiesta</param>
|
|
/// <param name="VersMin">Versione minima richiesta</param>
|
|
/// <returns></returns>
|
|
public List<ReleaseDTO> ReleaseGetFilt(string CodApp, string VersMin)
|
|
{
|
|
List<ReleaseDTO> answ = new List<ReleaseDTO>();
|
|
using (RestClient client = new RestClient(rcOptions))
|
|
{
|
|
string CodAppEnc = HttpUtility.UrlEncode(CodApp);
|
|
var request = new RestRequest($"Release/filt/{CodAppEnc}?VersMin={VersMin}", Method.Get);
|
|
var response = client.Get(request);
|
|
// controllo risposta
|
|
if (response.StatusCode == HttpStatusCode.OK)
|
|
{
|
|
// contenuto serializzato
|
|
string rawData = $"{response.Content}";
|
|
answ = JsonConvert.DeserializeObject<List<ReleaseDTO>>(rawData);
|
|
}
|
|
}
|
|
return answ;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Versione Async Elenco Release applicazione
|
|
/// </summary>
|
|
/// <param name="CodApp">CodApp richiesta</param>
|
|
/// <param name="VersMin">Versione minima richiesta</param>
|
|
/// <returns></returns>
|
|
public async Task<List<ReleaseDTO>> ReleaseGetFiltAsync(string CodApp, string VersMin)
|
|
{
|
|
List<ReleaseDTO> answ = new List<ReleaseDTO>();
|
|
|
|
using (RestClient client = new RestClient(rcOptions))
|
|
{
|
|
string CodAppEnc = HttpUtility.UrlEncode(CodApp);
|
|
var request = new RestRequest($"Release/filt/{CodAppEnc}?VersMin={VersMin}", Method.Get);
|
|
var response = await client.GetAsync(request);
|
|
// controllo risposta
|
|
if (response.StatusCode == HttpStatusCode.OK)
|
|
{
|
|
// contenuto serializzato
|
|
string rawData = $"{response.Content}";
|
|
answ = JsonConvert.DeserializeObject<List<ReleaseDTO>>(rawData);
|
|
}
|
|
}
|
|
return await Task.FromResult(answ);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Elenco Release applicazione
|
|
/// </summary>
|
|
/// <param name="CodApp">CodApp richiesta</param>
|
|
/// <param name="VersMin">Versione minima richiesta</param>
|
|
/// <param name="VersMax">Versione massima consentita</param>
|
|
/// <returns></returns>
|
|
public List<ReleaseDTO> ReleaseGetFiltLimit(string CodApp, string VersMin, string VersMax)
|
|
{
|
|
List<ReleaseDTO> answ = new List<ReleaseDTO>();
|
|
using (RestClient client = new RestClient(rcOptions))
|
|
{
|
|
string CodAppEnc = HttpUtility.UrlEncode(CodApp);
|
|
var request = new RestRequest($"Release/filtLimit/{CodAppEnc}?VersMin={VersMin}&VersMax={VersMax}", Method.Get);
|
|
var response = client.Get(request);
|
|
// controllo risposta
|
|
if (response.StatusCode == HttpStatusCode.OK)
|
|
{
|
|
// contenuto serializzato
|
|
string rawData = $"{response.Content}";
|
|
answ = JsonConvert.DeserializeObject<List<ReleaseDTO>>(rawData);
|
|
}
|
|
}
|
|
return answ;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Versione Async Elenco Release applicazione
|
|
/// </summary>
|
|
/// <param name="CodApp">CodApp richiesta</param>
|
|
/// <param name="VersMin">Versione minima richiesta</param>
|
|
/// <param name="VersMax">Versione massima consentita</param>
|
|
/// <returns></returns>
|
|
public async Task<List<ReleaseDTO>> ReleaseGetFiltLimitAsync(string CodApp, string VersMin, string VersMax)
|
|
{
|
|
List<ReleaseDTO> answ = new List<ReleaseDTO>();
|
|
|
|
using (RestClient client = new RestClient(rcOptions))
|
|
{
|
|
string CodAppEnc = HttpUtility.UrlEncode(CodApp);
|
|
var request = new RestRequest($"Release/filtLimit/{CodAppEnc}?VersMin={VersMin}&VersMax={VersMax}", Method.Get);
|
|
var response = await client.GetAsync(request);
|
|
// controllo risposta
|
|
if (response.StatusCode == HttpStatusCode.OK)
|
|
{
|
|
// contenuto serializzato
|
|
string rawData = $"{response.Content}";
|
|
answ = JsonConvert.DeserializeObject<List<ReleaseDTO>>(rawData);
|
|
}
|
|
}
|
|
return await Task.FromResult(answ);
|
|
}
|
|
|
|
#endregion Public Methods
|
|
|
|
#region Private Fields
|
|
|
|
/// <summary>
|
|
/// Istanza logger
|
|
/// </summary>
|
|
private static Logger Log = LogManager.GetCurrentClassLogger();
|
|
|
|
/// <summary>
|
|
/// URL dell'API x chiamate gestione licenze
|
|
/// </summary>
|
|
private string apiUrl = $"";
|
|
|
|
private int callTimeout = 10000;
|
|
|
|
/// <summary>
|
|
/// Opzioni standard di chiamata
|
|
/// </summary>
|
|
private RestClientOptions rcOptions = new RestClientOptions();
|
|
|
|
/// <summary>
|
|
/// Indirizzo server (URL con eventuale porta)
|
|
/// </summary>
|
|
private string servAddr = $"";
|
|
|
|
#endregion Private Fields
|
|
}
|
|
} |