397 lines
12 KiB
C#
397 lines
12 KiB
C#
using EgwControlCenter.Core.DTO;
|
|
using EgwControlCenter.Core.Models;
|
|
using EgwCoreLib.Utils;
|
|
using Newtonsoft.Json;
|
|
using NLog;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Reflection;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace EgwControlCenter.Core
|
|
{
|
|
public class AppControlService : IAppControlService
|
|
{
|
|
#region Public Constructors
|
|
|
|
public AppControlService()
|
|
{
|
|
try
|
|
{
|
|
Assembly assembly = Assembly.GetExecutingAssembly();
|
|
string startDir = Path.GetDirectoryName(assembly.Location)!;
|
|
ConfDir = startDir;
|
|
//DataDir = Environment.GetEnvironmentVariable("ClickOnce_DataDirectory") ?? startDir;
|
|
string appData = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);
|
|
if (string.IsNullOrEmpty(appData))
|
|
{
|
|
appData = "C:\\ProgramData";
|
|
}
|
|
DataDir = Path.Combine(appData, "EgalWare", "AppControlCenter");
|
|
Log.Trace($"appData: {appData} | EnvData: {Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData)}");
|
|
// verifico esistenza directory...
|
|
Directory.CreateDirectory(DataDir);
|
|
CurrCheck = new ReleaseChecker(ConfDir, DataDir);
|
|
Log.Trace($"Folder Setup | {ConfDir} | {DataDir}");
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error($"Error in AppControlService.init:{Environment.NewLine}{exc}");
|
|
}
|
|
}
|
|
|
|
#endregion Public Constructors
|
|
|
|
#region Public Events
|
|
|
|
/// <summary>
|
|
/// Evento update configurazione
|
|
/// </summary>
|
|
public event Action EA_ConfigUpdated = null!;
|
|
|
|
/// <summary>
|
|
/// Evento calling a remote in corso
|
|
/// </summary>
|
|
public event Action EA_RemoteCalling = null!;
|
|
|
|
/// <summary>
|
|
/// Evento udpate status controlli
|
|
/// </summary>
|
|
public event Action EA_StatusUpdated = null!;
|
|
|
|
#endregion Public Events
|
|
|
|
#region Public Properties
|
|
|
|
public string AppKey
|
|
{
|
|
get => CurrCheck.CurrPatrolCont.AppKey;
|
|
set
|
|
{
|
|
if (CurrCheck.CurrPatrolCont.AppKey != value)
|
|
{
|
|
// verifico ammissibilità
|
|
CurrCheck.CurrPatrolCont.AppKey = value;
|
|
ReportConfigUpd();
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// verifica autorizzazione al canale, ovvero
|
|
/// - deve avere tutte le chiavi/registri di comunicazione
|
|
/// - CodImpiego e AppKey devono essere validate
|
|
/// </summary>
|
|
public bool ChannelAuth
|
|
{
|
|
get
|
|
{
|
|
// in primis deve avere i dati di configurazione
|
|
bool answ = CurrCheck.CurrPatrolCont.HasCommData;
|
|
if (answ)
|
|
{
|
|
// verifico CodImpiego locale sia valido
|
|
answ = CurrCheck.CurrPatrolCont.CodImpiego == SubLicManager.CodImpiego();
|
|
// verifica sia valida (scadenza a tempo da ultimo controllo) chiave auth in remoto
|
|
if (answ)
|
|
{
|
|
}
|
|
}
|
|
return answ;
|
|
}
|
|
}
|
|
|
|
public bool CloudCallActive
|
|
{
|
|
get => cloudCallActive;
|
|
set
|
|
{
|
|
if (cloudCallActive != value)
|
|
{
|
|
cloudCallActive = value;
|
|
ReportRemoteCall();
|
|
}
|
|
}
|
|
}
|
|
|
|
public string CodApp
|
|
{
|
|
get => CurrCheck.CurrPatrolCont.CodApp;
|
|
set
|
|
{
|
|
if (CurrCheck.CurrPatrolCont.CodApp != value)
|
|
{
|
|
// verifico ammissibilità
|
|
CurrCheck.CurrPatrolCont.CodApp = value;
|
|
ReportConfigUpd();
|
|
}
|
|
}
|
|
}
|
|
|
|
public string CodImpiego
|
|
{
|
|
get => CurrCheck.CurrPatrolCont.CodImpiego;
|
|
set
|
|
{
|
|
if (CurrCheck.CurrPatrolCont.CodImpiego != value)
|
|
{
|
|
// verifico ammissibilità
|
|
CurrCheck.CurrPatrolCont.CodImpiego = value;
|
|
ReportConfigUpd();
|
|
}
|
|
}
|
|
}
|
|
|
|
public List<VersStatusDTO> ListAppStatus
|
|
{
|
|
get => CurrCheck.ListAppStatus;
|
|
set
|
|
{
|
|
CurrCheck.ListAppStatus = value;
|
|
ReportStatusUpd();
|
|
}
|
|
}
|
|
|
|
public string MainKey
|
|
{
|
|
get => CurrCheck.CurrPatrolCont.MainKey;
|
|
set
|
|
{
|
|
if (CurrCheck.CurrPatrolCont.MainKey != value)
|
|
{
|
|
// verifico ammissibilità
|
|
CurrCheck.CurrPatrolCont.MainKey = value;
|
|
ReportConfigUpd();
|
|
}
|
|
}
|
|
}
|
|
|
|
public int RefreshPeriod
|
|
{
|
|
get => CurrCheck.CurrPatrolCont.RefreshIntSec;
|
|
set
|
|
{
|
|
if (CurrCheck.CurrPatrolCont.RefreshIntSec != value)
|
|
{
|
|
// verifico ammissibilità
|
|
CurrCheck.CurrPatrolCont.RefreshIntSec = value < refPMin ? refPMin : value > refPMax ? refPMax : value;
|
|
ReportConfigUpd();
|
|
}
|
|
}
|
|
}
|
|
|
|
public List<ControlTarget> TargetList
|
|
{
|
|
get => CurrCheck.CurrPatrolCont.TargetList;
|
|
set
|
|
{
|
|
CurrCheck.CurrPatrolCont.TargetList = value;
|
|
ReportConfigUpd();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// num minuti veto controllo
|
|
/// </summary>
|
|
public int VetoCheck
|
|
{
|
|
get => CurrCheck.CurrPatrolCont.VetoCheckMinutes;
|
|
set
|
|
{
|
|
if (CurrCheck.CurrPatrolCont.VetoCheckMinutes != value)
|
|
{
|
|
// verifico ammissibilità
|
|
CurrCheck.CurrPatrolCont.VetoCheckMinutes = value < vetoPMin ? vetoPMin : value > vetoPMax ? vetoPMax : value;
|
|
ReportConfigUpd();
|
|
}
|
|
}
|
|
}
|
|
|
|
#endregion Public Properties
|
|
|
|
#region Public Methods
|
|
|
|
public async Task<bool> CheckAttivazioni()
|
|
{
|
|
bool fatto = await CurrCheck.CheckAttivazioni();
|
|
return fatto;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Effettua un controllo completo (loacele remoto)
|
|
/// </summary>
|
|
/// <param name="doForce">se true esegue anche prima della scadenza veto</param>
|
|
public async Task DoFullCheckAsync(bool doForce)
|
|
{
|
|
CloudCallActive = true;
|
|
// in primis controllo se il remote è ok sennò mi fermo...
|
|
bool remoteOk = await CurrCheck.CheckRemote();
|
|
if (remoteOk)
|
|
{
|
|
// effettua un refresh del controllo status...
|
|
bool locCheckOk = CurrCheck.UpdateLocalStatus(doForce);
|
|
bool remCheckOk = false;
|
|
bool critCheckOk = false;
|
|
if (locCheckOk)
|
|
{
|
|
remCheckOk = await CurrCheck.CheckRemoteReleases();
|
|
}
|
|
// infine effettua una verifica dello status "release critiche"
|
|
critCheckOk = await CurrCheck.CheckCriticalReport();
|
|
if (remCheckOk || critCheckOk)
|
|
{
|
|
ReportStatusUpd();
|
|
}
|
|
}
|
|
CloudCallActive = false;
|
|
}
|
|
|
|
/// <summary> Effettua rilettura configurazione e setup controlli... </summary
|
|
public void DoReloadConfig()
|
|
{
|
|
try
|
|
{
|
|
CurrCheck = new ReleaseChecker(ConfDir, DataDir);
|
|
Log.Trace($"Riletta config Setup | {ConfDir} | {DataDir}");
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Equals($"Eccezione in DoReloadConfig{Environment.NewLine}{exc}");
|
|
}
|
|
ReportConfigUpd();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Effettua salvataggio configurazione
|
|
/// </summary>
|
|
public void DoSaveConfig()
|
|
{
|
|
try
|
|
{
|
|
var rawData = JsonConvert.SerializeObject(CurrCheck.CurrPatrolCont, Formatting.Indented);
|
|
if (rawData != null && rawData.Length > 2)
|
|
{
|
|
File.WriteAllText(CurrCheck.ConfPath, rawData);
|
|
Log.Trace($"Effettuato salvataggio Config Setup! | {ConfDir} | {DataDir}");
|
|
}
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Equals($"Eccezione in DoSaveConfig{Environment.NewLine}{exc}");
|
|
}
|
|
ReportConfigUpd();
|
|
}
|
|
|
|
public void ResetConf()
|
|
{
|
|
bool fatto = CurrCheck.ResetConf();
|
|
if (fatto)
|
|
{
|
|
ReportConfigUpd();
|
|
ReportStatusUpd();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Reimposta CodImpiego / AppKey da librerie Egw secondo richiesta...
|
|
/// </summary>
|
|
/// <param name="resCodImpiego"></param>
|
|
/// <param name="resAppKey"></param>
|
|
public void ResetSubLic(bool resCodImpiego, bool resAppKey)
|
|
{
|
|
if (resCodImpiego)
|
|
{
|
|
CurrCheck.CurrPatrolCont.CodImpiego = SubLicManager.CodImpiego();
|
|
}
|
|
if (resAppKey)
|
|
{
|
|
CurrCheck.CurrPatrolCont.AppKey = SubLicManager.GenKey(CodApp);
|
|
}
|
|
ReportConfigUpd();
|
|
}
|
|
|
|
#endregion Public Methods
|
|
|
|
#region Protected Properties
|
|
|
|
protected SubLicManager SubLicManager { get; set; } = new SubLicManager();
|
|
|
|
#endregion Protected Properties
|
|
|
|
#region Private Fields
|
|
|
|
/// <summary>
|
|
/// Classe logger
|
|
/// </summary>
|
|
private static Logger Log = LogManager.GetCurrentClassLogger();
|
|
|
|
/// <summary>
|
|
/// Valore massimo ammesso refresh sec
|
|
/// </summary>
|
|
private int refPMax = 3600;
|
|
|
|
/// <summary>
|
|
/// Valore minimo ammesso refresh sec
|
|
/// </summary>
|
|
private int refPMin = 1;
|
|
|
|
/// <summary>
|
|
/// Valore massimo ammesso veto minuti
|
|
/// </summary>
|
|
private int vetoPMax = 14400;
|
|
|
|
/// <summary>
|
|
/// Valore minimo ammesso veto minuti
|
|
/// </summary>
|
|
private int vetoPMin = 30;
|
|
|
|
#endregion Private Fields
|
|
|
|
#region Private Properties
|
|
|
|
private bool cloudCallActive { get; set; } = false;
|
|
private string ConfDir { get; set; } = "";
|
|
private ReleaseChecker CurrCheck { get; set; } = null!;
|
|
private string DataDir { get; set; } = "";
|
|
|
|
#endregion Private Properties
|
|
|
|
#region Private Methods
|
|
|
|
private void ReportConfigUpd()
|
|
{
|
|
if (EA_ConfigUpdated != null)
|
|
{
|
|
EA_ConfigUpdated?.Invoke();
|
|
}
|
|
}
|
|
|
|
private void ReportRemoteCall()
|
|
{
|
|
if (EA_RemoteCalling != null)
|
|
{
|
|
EA_RemoteCalling?.Invoke();
|
|
}
|
|
}
|
|
|
|
private void ReportStatusUpd()
|
|
{
|
|
if (EA_StatusUpdated != null)
|
|
{
|
|
// controllo se almeno 1 app abbia update...
|
|
foreach (var item in ListAppStatus)
|
|
{
|
|
if (item.HasUpdate)
|
|
{
|
|
EA_StatusUpdated?.Invoke();
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
#endregion Private Methods
|
|
}
|
|
} |