using MathNet.Numerics.Distributions; using MP.MONO.Core; using MP.MONO.Core.DTO; using Newtonsoft.Json; namespace MachineSim { /// /// Definizione parametri simulati /// public class Simulator { #region Protected Fields protected string basePath = ""; protected List currSimActLog = new List(); protected List currSimEvHist = new List(); protected List currSimMachStat = new List(); protected List currSimMaint = new List(); protected List currSimPar = new List(); protected List currSimTools = new List(); protected Random rand = new Random(); #endregion Protected Fields #region Public Constructors public Simulator(string _basePath) { basePath = _basePath; setupConfig(); } #endregion Public Constructors #region Private Methods private void setupConf(string paramFileName, ref List localObj) { string fullPath = Path.Combine(basePath, paramFileName); if (File.Exists(fullPath)) { var rawData = File.ReadAllText(fullPath); if (!string.IsNullOrEmpty(rawData)) { localObj = JsonConvert.DeserializeObject>(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 getActLog() { List answ = new List(); // 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 getAlarms() { List activeAlarms = new List(); //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 getEvents() { List answ = new List(); // 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 getMaint() { List answ = new List(); // 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 getParameters() { List answ = new List(); // 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 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 getProdStats() { List answ = new List(); // 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; } /// /// Restituisce stato Macchina simulato come DTO /// /// public MachineDTO getStatus() { int maxRun = 5; int maxExe = 7; MachineDTO currMachDto = new MachineDTO() { Manufacturer = "Egalware", Model = "SIM Machine", Name = "DEMO 01", SerNumber = "2022-0001", Mode = $"{(Enums.MachRunMode)rand.Next(0, maxRun)}", Status = $"{(Enums.MachExeStatus)rand.Next(0, maxExe)}" }; return currMachDto; } public List getTools() { List answ = new List(); // 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; } /// /// Calcola prox valore simulato dati i parametri rand configurati /// /// /// /// /// /// /// public double simNext(double CurrVal, double MinVal, double MaxVal, double sMean, double sStd) { double innov = Normal.Sample(sMean, sStd); CurrVal += innov; // verifico estremi... CurrVal = CurrVal > MaxVal ? MaxVal : CurrVal; CurrVal = CurrVal < MinVal ? MinVal : CurrVal; return CurrVal; } #endregion Public Methods } }