348 lines
12 KiB
C#
348 lines
12 KiB
C#
using MathNet.Numerics.Distributions;
|
|
using MP.MONO.Core.DTO;
|
|
using Newtonsoft.Json;
|
|
|
|
namespace MachineSim
|
|
{
|
|
/// <summary>
|
|
/// Definizione parametri simulati
|
|
/// </summary>
|
|
public class Simulator
|
|
{
|
|
#region Protected Fields
|
|
|
|
protected string basePath = "";
|
|
protected List<SimDisplayDataDTO> currSimActLog = new List<SimDisplayDataDTO>();
|
|
protected List<SimDisplayDataDTO> currSimEvHist = new List<SimDisplayDataDTO>();
|
|
protected List<SimDisplayDataDTO> currSimMachStat = new List<SimDisplayDataDTO>();
|
|
protected List<SimDisplayDataDTO> currSimMaint = new List<SimDisplayDataDTO>();
|
|
protected List<SimDisplayDataDTO> currSimPar = new List<SimDisplayDataDTO>();
|
|
protected List<SimDisplayDataDTO> currSimTools = new List<SimDisplayDataDTO>();
|
|
protected Random rand = new Random();
|
|
|
|
#endregion Protected Fields
|
|
|
|
#region Public Constructors
|
|
|
|
public Simulator(string _basePath, int nMode, int nStatus)
|
|
{
|
|
basePath = _basePath;
|
|
numMode = nMode;
|
|
numStatus = nStatus;
|
|
setupConfig();
|
|
}
|
|
|
|
#endregion Public Constructors
|
|
|
|
#region Protected Properties
|
|
|
|
protected int numMode { get; set; } = 1;
|
|
protected int numStatus { get; set; } = 1;
|
|
|
|
#endregion Protected Properties
|
|
|
|
#region Private Methods
|
|
|
|
private void setupConf(string paramFileName, ref List<SimDisplayDataDTO> localObj)
|
|
{
|
|
string fullPath = Path.Combine(basePath, paramFileName);
|
|
if (File.Exists(fullPath))
|
|
{
|
|
var rawData = File.ReadAllText(fullPath);
|
|
if (!string.IsNullOrEmpty(rawData))
|
|
{
|
|
localObj = JsonConvert.DeserializeObject<List<SimDisplayDataDTO>>(rawData);
|
|
}
|
|
}
|
|
}
|
|
|
|
private void setupConfig()
|
|
{
|
|
// setup conf Parametri
|
|
setupConf("SimParams.json", ref currSimPar);
|
|
// setup conf MachStat
|
|
setupConf("SimMachStat.json", ref currSimMachStat);
|
|
// setup conf Maintenance
|
|
setupConf("SimMaint.json", ref currSimMaint);
|
|
// setup conf Tools
|
|
setupConf("SimTools.json", ref currSimTools);
|
|
// setup conf Event History
|
|
setupConf("SimEvHist.json", ref currSimEvHist);
|
|
// setup conf Activity Log
|
|
setupConf("SimActLog.json", ref currSimActLog);
|
|
}
|
|
|
|
#endregion Private Methods
|
|
|
|
#region Public Methods
|
|
|
|
public List<DisplayDataDTO> getActLog()
|
|
{
|
|
List<DisplayDataDTO> answ = new List<DisplayDataDTO>();
|
|
|
|
// genero a partire dall'elenco configurato da simulare...
|
|
answ = currSimActLog.Select(i => new DisplayDataDTO()
|
|
{
|
|
IsNumeric = i.IsNumeric,
|
|
MaxVal = i.MaxVal,
|
|
MinVal = i.MinVal,
|
|
Order = i.Order,
|
|
Title = i.Title,
|
|
Type = i.Type,
|
|
ValueNum = simNext(i.ValueNum, i.MinVal, i.MaxVal, i.SimMean, i.SimStd),
|
|
DisplFormat = i.DisplFormat
|
|
//Value = $"{getNext(i.ValueNum, i.MinVal, i.MaxVal, i.SimMean, i.SimStd):N3}",
|
|
}).ToList();
|
|
|
|
foreach (var item in answ)
|
|
{
|
|
item.Value = $"{item.ValueNum.ToString(item.DisplFormat)}";
|
|
}
|
|
|
|
return answ;
|
|
}
|
|
|
|
public List<String> getAlarms()
|
|
{
|
|
List<string> activeAlarms = new List<string>();
|
|
//int alarmCode = rand.Next(0, 255);
|
|
int alarmCode = rand.Next(0, 160);
|
|
// se >= 128 --> 0 (no alarm)
|
|
alarmCode = alarmCode <= 127 ? alarmCode : 0;
|
|
for (int i = 0; i < 8; i++)
|
|
{
|
|
if ((alarmCode & (1 << i)) != 0)
|
|
{
|
|
activeAlarms.Add($"Alarm {i:000}");
|
|
}
|
|
}
|
|
return activeAlarms;
|
|
}
|
|
|
|
public List<DisplayDataDTO> getEvents()
|
|
{
|
|
List<DisplayDataDTO> answ = new List<DisplayDataDTO>();
|
|
|
|
// genero a partire dall'elenco configurato da simulare...
|
|
answ = currSimEvHist.Select(i => new DisplayDataDTO()
|
|
{
|
|
IsNumeric = i.IsNumeric,
|
|
MaxVal = i.MaxVal,
|
|
MinVal = i.MinVal,
|
|
Order = i.Order,
|
|
Title = i.Title,
|
|
Type = i.Type,
|
|
ValueNum = simNext(i.ValueNum, i.MinVal, i.MaxVal, i.SimMean, i.SimStd),
|
|
DisplFormat = i.DisplFormat
|
|
//Value = $"{getNext(i.ValueNum, i.MinVal, i.MaxVal, i.SimMean, i.SimStd):N3}",
|
|
}).ToList();
|
|
|
|
foreach (var item in answ)
|
|
{
|
|
item.Value = $"{item.ValueNum.ToString(item.DisplFormat)}";
|
|
}
|
|
|
|
return answ;
|
|
}
|
|
|
|
public List<DisplayDataDTO> getMaint()
|
|
{
|
|
List<DisplayDataDTO> answ = new List<DisplayDataDTO>();
|
|
|
|
// genero a partire dall'elenco configurato da simulare...
|
|
answ = currSimMaint.Select(i => new DisplayDataDTO()
|
|
{
|
|
IsNumeric = i.IsNumeric,
|
|
MaxVal = i.MaxVal,
|
|
MinVal = i.MinVal,
|
|
Order = i.Order,
|
|
Title = i.Title,
|
|
Type = i.Type,
|
|
ValueNum = simNext(i.ValueNum, i.MinVal, i.MaxVal, i.SimMean, i.SimStd),
|
|
DisplFormat = i.DisplFormat
|
|
//Value = $"{getNext(i.ValueNum, i.MinVal, i.MaxVal, i.SimMean, i.SimStd):N3}",
|
|
}).ToList();
|
|
|
|
foreach (var item in answ)
|
|
{
|
|
item.Value = $"{item.ValueNum.ToString(item.DisplFormat)}";
|
|
}
|
|
|
|
return answ;
|
|
}
|
|
|
|
public List<DisplayDataDTO> getParameters()
|
|
{
|
|
List<DisplayDataDTO> answ = new List<DisplayDataDTO>();
|
|
|
|
// genero a partire dall'elenco configurato da simulare...
|
|
answ = currSimPar.Select(i => new DisplayDataDTO()
|
|
{
|
|
IsNumeric = i.IsNumeric,
|
|
MaxVal = i.MaxVal,
|
|
MinVal = i.MinVal,
|
|
Order = i.Order,
|
|
Title = i.Title,
|
|
Type = i.Type,
|
|
ValueNum = simNext(i.ValueNum, i.MinVal, i.MaxVal, i.SimMean, i.SimStd),
|
|
DisplFormat = i.DisplFormat,
|
|
CssIcon = i.CssIcon,
|
|
EnablePlot = i.EnablePlot,
|
|
ShowBar = i.ShowBar,
|
|
ShowGauge = i.ShowGauge,
|
|
}).ToList();
|
|
|
|
foreach (var item in answ)
|
|
{
|
|
item.Value = $"{item.ValueNum.ToString(item.DisplFormat)}";
|
|
}
|
|
return answ;
|
|
}
|
|
public Dictionary<string, double> getParamsVal()
|
|
{
|
|
Dictionary<string, double> answ = new Dictionary<string, double>();
|
|
// genero a partire dall'elenco configurato da simulare...
|
|
answ = currSimPar.ToDictionary(i => i.Title, i => simNext(i.ValueNum, i.MinVal, i.MaxVal, i.SimMean, i.SimStd));
|
|
return answ;
|
|
}
|
|
|
|
public ProductionDTO getProd()
|
|
{
|
|
int stdCycle = 5;
|
|
|
|
ProductionDTO currMachDto = new ProductionDTO()
|
|
{
|
|
Order = "ODL Test",
|
|
ItemCode = "ART.0000123",
|
|
ProgName = "P000012",
|
|
CurrQty = DateTime.Now.Minute + rand.Next(1, 40),
|
|
OrderQty = 100,
|
|
CycleTimeMin = rand.NextDouble() * stdCycle,
|
|
Message = "...simulated data..."
|
|
};
|
|
return currMachDto;
|
|
}
|
|
|
|
public List<DisplayDataDTO> getProdStats()
|
|
{
|
|
List<DisplayDataDTO> answ = new List<DisplayDataDTO>();
|
|
|
|
// genero a partire dall'elenco configurato da simulare...
|
|
answ = currSimMachStat.Select(i => new DisplayDataDTO()
|
|
{
|
|
IsNumeric = i.IsNumeric,
|
|
MaxVal = i.MaxVal,
|
|
MinVal = i.MinVal,
|
|
Order = i.Order,
|
|
Title = i.Title,
|
|
Type = i.Type,
|
|
ValueNum = simNext(i.ValueNum, i.MinVal, i.MaxVal, i.SimMean, i.SimStd),
|
|
DisplFormat = i.DisplFormat
|
|
//Value = $"{getNext(i.ValueNum, i.MinVal, i.MaxVal, i.SimMean, i.SimStd):N3}",
|
|
}).ToList();
|
|
|
|
foreach (var item in answ)
|
|
{
|
|
item.Value = $"{item.ValueNum.ToString(item.DisplFormat)}";
|
|
}
|
|
|
|
return answ;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Restituisce stato Macchina simulato come DTO
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public MachineDTO getStatus()
|
|
{
|
|
int maxRun = 7;
|
|
int maxExe = 7;
|
|
|
|
MachineDTO currMachDto = new MachineDTO()
|
|
{
|
|
Manufacturer = "Egalware",
|
|
Model = "SIM Machine",
|
|
Name = "DEMO 01",
|
|
SerNumber = "2022-0001",
|
|
ModeId = rand.Next(0, maxRun),
|
|
StatusId = rand.Next(0, maxExe)
|
|
//Mode = $"{(Enums.MachRunMode)rand.Next(0, maxRun)}",
|
|
//Status = $"{(Enums.MachExeStatus)rand.Next(0, maxExe)}"
|
|
};
|
|
|
|
return currMachDto;
|
|
}
|
|
|
|
public List<DisplayDataDTO> getTools()
|
|
{
|
|
List<DisplayDataDTO> answ = new List<DisplayDataDTO>();
|
|
|
|
// genero a partire dall'elenco configurato da simulare...
|
|
answ = currSimTools.Select(i => new DisplayDataDTO()
|
|
{
|
|
IsNumeric = i.IsNumeric,
|
|
MaxVal = i.MaxVal,
|
|
MinVal = i.MinVal,
|
|
Order = i.Order,
|
|
Title = i.Title,
|
|
Type = i.Type,
|
|
ValueNum = simNext(i.ValueNum, i.MinVal, i.MaxVal, i.SimMean, i.SimStd),
|
|
DisplFormat = i.DisplFormat
|
|
//Value = $"{getNext(i.ValueNum, i.MinVal, i.MaxVal, i.SimMean, i.SimStd):N3}",
|
|
}).ToList();
|
|
|
|
foreach (var item in answ)
|
|
{
|
|
item.Value = $"{item.ValueNum.ToString(item.DisplFormat)}";
|
|
}
|
|
|
|
return answ;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Calcola prox valore simulato dati i parametri rand configurati, con innovazione stocastica + deterministica:
|
|
/// - ciclo ogni 4 minuti - con operazione modulo (4)
|
|
/// * 0 fermo
|
|
/// * 1 --> sale
|
|
/// * 2 fermo
|
|
/// * 3 --> scende
|
|
/// </summary>
|
|
/// <param name="CurrVal"></param>
|
|
/// <param name="MinVal"></param>
|
|
/// <param name="MaxVal"></param>
|
|
/// <param name="sMean"></param>
|
|
/// <param name="sStd"></param>
|
|
/// <returns></returns>
|
|
public double simNext(double CurrVal, double MinVal, double MaxVal, double sMean, double sStd)
|
|
{
|
|
// innovazione stocastica di base
|
|
double innov = Normal.Sample(sMean, sStd);
|
|
// innovazione deterministica:
|
|
int resto = (DateTime.Now.Second % 10);
|
|
double trend = 0;
|
|
switch (resto)
|
|
{
|
|
case 0:
|
|
case 1:
|
|
case 2:
|
|
trend = sMean + 15 * sStd;
|
|
break;
|
|
case 5:
|
|
case 6:
|
|
case 7:
|
|
trend = -(sMean + 15 * sStd);
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
|
|
CurrVal += innov + trend;
|
|
// verifico estremi...
|
|
CurrVal = CurrVal > MaxVal ? MaxVal : CurrVal;
|
|
CurrVal = CurrVal < MinVal ? MinVal : CurrVal;
|
|
return CurrVal;
|
|
}
|
|
|
|
#endregion Public Methods
|
|
}
|
|
} |