using Microsoft.AspNetCore.Components; using Microsoft.Extensions.Configuration; using MP.AppAuth; using MP.Land.Data; using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Threading.Tasks; namespace MP.Land.Pages { public partial class UpdateManager : IDisposable { #region Public Methods public void Dispose() { ListRecords = null; GC.Collect(); } #endregion Public Methods #region Protected Fields protected int numDone = 0; protected int numTot = 0; protected int totalCount = 0; protected double TotalMb = 0; protected UpdateMan updateManAuth = new UpdateMan("SWDownloader", "viaD@nte16"); #endregion Protected Fields #region Protected Properties [Inject] protected MessageService AppMService { get; set; } [Inject] protected IConfiguration Configuration { get; set; } [Inject] protected AppAuthService DataService { get; set; } [Inject] protected LicenseService LicServ { get; set; } = null!; protected string outMessages { get; set; } = ""; protected int percLoading { get; set; } = 0; protected bool showProgress { get; set; } = false; protected bool showUpdate { get; set; } = false; #endregion Protected Properties #region Protected Methods /// /// Cicla su tutti i record ed effettua il download /// protected async Task DownloadAll() { // init progress... showProgress = true; showUpdate = true; outMessages = $"Iniziato download per {numTot} packages"; StateHasChanged(); percLoading = 0; TotalMb = 0; numDone = 0; Stopwatch stopWatch = new Stopwatch(); stopWatch.Start(); // ciclo su tutti quelli con licenza valida... List rawList = ListRecords .Where(x => !string.IsNullOrEmpty(x.LicenseKey)).ToList(); List authList = new List(); // ciclo SOLO tra quelli davvero autorizzati... foreach (var item in rawList) { if (LicServ.checkLicenseActive(item.LicenseKey)) { authList.Add(item); } } numTot = authList.Count; foreach (var item in authList) { long size = 0; size = await scaricaSingolo(item); TotalMb += (double)size / (1024 * 1024); outMessages = $"Scaricati {numDone}/{numTot} packages | {TotalMb:N2} Mb"; StateHasChanged(); await Task.Delay(1); } stopWatch.Stop(); TimeSpan ts = stopWatch.Elapsed; outMessages = $"Effettuato download di {numDone} update | {TotalMb:N2} Mb in {ts.TotalSeconds:N2} s"; StateHasChanged(); await Task.Delay(1000); showProgress = false; StateHasChanged(); } protected string localPath(string localRepo) { return @$"{Configuration["ServerConf:downloadPath"]}\{localRepo}\{Configuration["appVers"]}"; ; } protected override async Task OnInitializedAsync() { ListRecords = null; await Task.Delay(1); AppMService.ShowSearch = false; AppMService.PageName = "Update Manager"; AppMService.PageIcon = "fas fa-download pr-2"; await ReloadData(); } protected async Task ReloadData() { // importante altrimenti NON mostra update UI await Task.Delay(1); ListRecords = await DataService.UpdManList(); totalCount = ListRecords.Count(); await Task.Delay(1); } #endregion Protected Methods #region Private Fields private List ListRecords; #endregion Private Fields #region Private Methods private async Task scaricaSingolo(AppAuth.Models.UpdMan item) { long size = 0; if (item.IsAuth) { size = updateManAuth.downloadLatest(item.ManifestUrl, localPath(item.LocalRepo), item.PackName); } else { size = UpdateMan.obj.downloadLatest(item.ManifestUrl, localPath(item.LocalRepo), item.PackName); } numDone++; percLoading = 100 * numDone / numTot; return await Task.FromResult(size); } #endregion Private Methods } }