Files
gwms/GWMS.UI/Components/PlantOverview.razor.cs
T
2021-09-28 18:50:06 +02:00

277 lines
7.8 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using GWMS.UI.Data;
using GWMS.Data.DTO;
using Microsoft.AspNetCore.Components;
using Blazorise.Charts;
using System.Threading;
using Microsoft.Extensions.Configuration;
namespace GWMS.UI.Components
{
public partial class PlantOverview
{
#region Protected Fields
protected PlantDTO _currItem = new PlantDTO();
protected LineChart<double> LevelVal = new LineChart<double>();
protected object lineChartOptions = new
{
Scales = new
{
XAxes = new object[]
{
new {
display = true,
//type = "timeseries",
//type = "time",
//Time = new {
// unit="day"
//}
}
},
YAxes = new object[]
{
new {
display = true,
position = "right",
//text = "Kg",
ticks = new {
suggestedMin = 0,
suggestedMax = 30000
}
}
}
},
Tooltips = new
{
Mode = "nearest",
Intersect = false
},
Hover = new
{
Mode = "nearest",
Intersect = false
},
Animation = false,
Responsive = true,
AspectRatio = 2,
type = "line",
Legend = new
{
display = false
}
};
/// <summary>
/// fattore di riduzione x visualizzare meno punti (in base alla numerosità...
/// </summary>
protected int redFact = 1;
#endregion Protected Fields
#region Private Properties
[Inject]
private IConfiguration Configuration { get; set; }
[Inject]
private NavigationManager NavManager { get; set; }
private int SelPlantId
{
get
{
int answ = 0;
if (AppMService.Order_Filter != null)
{
answ = AppMService.Order_Filter.PlantId;
}
return answ;
}
set
{
if (!AppMService.Order_Filter.PlantId.Equals(value))
{
AppMService.Order_Filter.PlantId = value;
}
}
}
#endregion Private Properties
#region Protected Properties
[Inject]
protected MessageService AppMService { get; set; }
[Inject]
protected GWMSDataService DataService { get; set; }
#endregion Protected Properties
#region Public Properties
public string checkRTime
{
get => DateTime.Now.Subtract(_currItem.LastUpdate).TotalMinutes > 2 ? $"Mancata ricezione: ultimo aggiornamento {_currItem.LastUpdate}" : $"Dati Realtime aggiornati al {_currItem.LastUpdate}";
}
[Parameter]
public PlantDTO currItem
{
get
{
return _currItem;
}
set
{
_currItem = value;
if (value != null)
{
var dataReload = Task.Run(async () =>
{
// aggiunta delay o non riesce a disegnare
int ChartWaitDelay = 150;
int.TryParse(Configuration["ChartWaitDelay"], out ChartWaitDelay);
Thread.Sleep(ChartWaitDelay);
await HandleRedraw();
});
}
}
}
public string headerStatus
{
get
{
string answ = "";
int TimeoutOffline = 5;
int.TryParse(Configuration["TimeoutOffline"], out TimeoutOffline);
answ = DateTime.Now.Subtract(_currItem.LastUpdate).TotalMinutes > TimeoutOffline ? "text-secondary" : "active";
return answ;
}
}
public string playStatus
{
get => DateTime.Now.Subtract(_currItem.LastUpdate).TotalMinutes > 2 ? "text-danger" : "text-success";
}
#endregion Public Properties
#region Private Methods
private void fixRedFactor()
{
int answ = 1;
int numCount = _currItem.LevelTS.Count;
// passo a 2h se > 3 gg
if (numCount > 72)
answ = 3;
// passo a 3h se > 5 gg
else if (numCount > 120)
answ = 4;
// passo a 4h se > 10 gg
else if (numCount > 240)
answ = 5;
redFact = answ;
}
private LineChartDataset<double> GetLineChartDataset()
{
fixRedFactor();
var answ = new LineChartDataset<double>
{
//Label = "Livello",
Data = _currItem.LevelTS.Where((cat, index) => index % redFact == 0).Select(x => x.ValDouble).ToList(),
BorderColor = getLineColors(1f),
BackgroundColor = getFillColors(0.25f),
Fill = true,
PointRadius = 3,
BorderWidth = 2,
LineTension = 0,
BorderDash = new List<int> { }
};
return answ;
}
private List<string> GetLineChartLabels()
{
fixRedFactor();
var answ = _currItem.LevelTS.Where((cat, index) => index % redFact == 0).Select(x => x.DtEvent.ToString("dd/MM HH")).ToList();
return answ;
}
#endregion Private Methods
#region Protected Methods
/// <summary>
/// Genera colori sfondo
/// </summary>
/// <param name="numRecords"></param>
/// <returns></returns>
protected List<string> getFillColors(float alpha)
{
List<string> answ = new List<string>();
answ.Add(ChartColor.FromRgba(108, 164, 254, alpha));
return answ;
}
/// <summary>
/// Genera colori linea
/// </summary>
/// <param name="numRecords"></param>
/// <returns></returns>
protected List<string> getLineColors(float alpha)
{
List<string> answ = new List<string>();
answ.Add(ChartColor.FromRgba(54, 82, 254, alpha));
return answ;
}
protected async Task HandleRedraw()
{
if (LevelVal != null)
{
await LevelVal.Clear();
await LevelVal.AddLabelsDatasetsAndUpdate(GetLineChartLabels(), GetLineChartDataset());
}
}
protected void ShowDetail(int currPlantId)
{
SelPlantId = currPlantId;
// rimando...
NavManager.NavigateTo($"PlantAnalisys");
}
protected void ShowOrders(int currPlantId)
{
SelPlantId = currPlantId;
// rimando...
NavManager.NavigateTo($"Orders");
}
#endregion Protected Methods
#region Public Methods
public string getPressData(string valore, string formato)
{
string answ = "";
if (currItem.PressAct.ContainsKey(valore))
{
answ = currItem.PressAct[valore].ToString(formato);
}
return answ;
}
#endregion Public Methods
}
}