Files
web3d/3MF_test/Web3D.pack/Pages/W3D.razor.cs
T
Samuele Locatelli cd20574a81 Aggiunta tests
2023-09-27 11:29:44 +02:00

153 lines
4.1 KiB
C#

using Microsoft.AspNetCore.Components;
using Microsoft.JSInterop;
using System.Diagnostics;
using System;
using System.Reflection.Metadata.Ecma335;
namespace Web3D.pack.Pages
{
public partial class W3D : IDisposable
{
#region Protected Properties
[Inject]
protected IJSRuntime JSRuntime { get; set; } = null!;
protected int _lastLayer = 1;
protected int LastLayer
{
get => _lastLayer;
set
{
_lastLayer = value;
var pUpd = Task.Run(async () => await JSRuntime.InvokeVoidAsync("refresh", 1, value));
}
}
protected int MaxLayer { get; set; } = 30;
protected int MinLayer { get; set; } = 1;
protected string pathDir { get; set; } = "";
protected string pathFile { get; set; } = "";
#endregion Protected Properties
#region Protected Methods
protected async Task startSim()
{
await Task.Delay(1);
ClearTimer();
StartTimer();
}
protected async Task restartSim()
{
await Task.Delay(1);
ClearTimer();
LastLayer = 1;
StartTimer();
}
private void ClearTimer()
{
if (aTimer != null)
{
aTimer.Stop();
aTimer.Close();
aTimer.Dispose();
}
}
protected async Task pauseSim()
{
await Task.Delay(1);
PauseTimer();
}
protected override async Task OnAfterRenderAsync(bool firstRender)
{
await Task.Delay(1);
if (firstRender)
{
var options = new
{
dimX = 1670,
dimY = 941,
fileName = "Cubo.3mf"
};
await JSRuntime.InvokeVoidAsync("setup", options);
}
}
public void Dispose()
{
if (aTimer != null)
{
aTimer.Elapsed -= ElapsedTimer;
aTimer.Stop();
aTimer.Close();
aTimer.Dispose();
}
GC.Collect();
}
private System.Timers.Timer aTimer = null!;
public void ElapsedTimer(object? source, System.Timers.ElapsedEventArgs e)
{
if (aTimer != null)
{
if (LastLayer < 250)
{
// controlo se URL diverso da apgina params...
aTimer.Enabled = true;
// inizio misura esecuzione
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
var pUpd = Task.Run(async () =>
{
await Task.Delay(1);
LastLayer++;
await InvokeAsync(() => StateHasChanged());
});
pUpd.Wait();
// misuro tempo esecuzione
stopWatch.Stop();
TimeSpan ts = stopWatch.Elapsed;
double deltaTime = RefreshPeriod - ts.TotalMilliseconds;
// aggiungo fattore di disturbo random...
double mFact = 0.01 * random.Next(80, 120);
aTimer.Interval = mFact * (deltaTime > 100 ? deltaTime : 100);
//aTimer.Start();
}
else
{
aTimer.Stop();
}
}
}
protected Random random = new Random();
protected int RefreshPeriod { get; set; } = 800;
public void StartTimer()
{
aTimer = new System.Timers.Timer(RefreshPeriod);
aTimer.Elapsed += ElapsedTimer;
aTimer.Enabled = true;
aTimer.AutoReset = true;
aTimer.Start();
}
public void PauseTimer()
{
if (aTimer != null)
{
aTimer.Stop();
}
}
#endregion Protected Methods
}
}