Files
mapo-core/MP.Land/Pages/UpdateManager.razor.cs
T
2022-11-04 15:50:40 +01:00

161 lines
4.8 KiB
C#

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; }
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
/// <summary>
/// Cicla su tutti i record ed effettua il download
/// </summary>
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<AppAuth.Models.UpdMan> rawList = ListRecords
.Where(x => !string.IsNullOrEmpty(x.LicenseKey)).ToList();
List<AppAuth.Models.UpdMan> authList = new List<AppAuth.Models.UpdMan>();
// 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 void OnInitialized()
{
AppMService.ShowSearch = false;
AppMService.PageName = "Update Manager";
AppMService.PageIcon = "fas fa-download pr-2";
}
protected override async Task OnInitializedAsync()
{
await ReloadAllData();
}
protected async Task ReloadAllData()
{
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<MP.AppAuth.Models.UpdMan> ListRecords;
#endregion Private Fields
#region Private Methods
private async Task<long> 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
}
}