From 78dba42a2d885e2bd98838b61cec21f692f59692 Mon Sep 17 00:00:00 2001 From: Samuele Locatelli Date: Mon, 7 Mar 2022 17:58:37 +0100 Subject: [PATCH] Bozza sim ulazione produzione --- MP.MONO.UI/Components/ProdOverview.razor | 26 ------ MP.MONO.UI/Components/ProdOverview.razor.cs | 69 +++++++++++++++ MP.MONO.UI/Data/CurrentDataService.cs | 14 ++- MachineSim/MachineSim.csproj | 6 ++ MachineSim/Program.cs | 21 ++++- MachineSim/SimProd.json | 74 ++++++++++++++++ MachineSim/SimStatus.json | 74 ++++++++++++++++ MachineSim/Simulator.cs | 95 ++++----------------- 8 files changed, 273 insertions(+), 106 deletions(-) create mode 100644 MP.MONO.UI/Components/ProdOverview.razor.cs create mode 100644 MachineSim/SimProd.json create mode 100644 MachineSim/SimStatus.json diff --git a/MP.MONO.UI/Components/ProdOverview.razor b/MP.MONO.UI/Components/ProdOverview.razor index 1dcbc57..7f67b0a 100644 --- a/MP.MONO.UI/Components/ProdOverview.razor +++ b/MP.MONO.UI/Components/ProdOverview.razor @@ -69,30 +69,4 @@ else } -@code { - protected bool dataLoaded = false; - private ProductionDTO? currProd { get; set; } = null; - protected override async Task OnInitializedAsync() - { - await ReloadData(); - } - - protected string percProgress(int num, int den) - { - string answ = "width: 0%;"; - den = den != 0 ? den : 1; - double ratio = ((double)num) / den; - answ = $"width: {ratio:P0};"; - return answ; - } - - protected async Task ReloadData() - { - await Task.Delay(10); - currProd = await MMDataService.MachineProd(); - await Task.Delay(500); - dataLoaded = true; - } - -} diff --git a/MP.MONO.UI/Components/ProdOverview.razor.cs b/MP.MONO.UI/Components/ProdOverview.razor.cs new file mode 100644 index 0000000..b4e013b --- /dev/null +++ b/MP.MONO.UI/Components/ProdOverview.razor.cs @@ -0,0 +1,69 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Components; +using System.Net.Http; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Components.Authorization; +using Microsoft.AspNetCore.Components.Forms; +using Microsoft.AspNetCore.Components.Routing; +using Microsoft.AspNetCore.Components.Web; +using Microsoft.AspNetCore.Components.Web.Virtualization; +using Microsoft.JSInterop; +using MP.MONO.UI; +using MP.MONO.UI.Shared; +using MP.MONO.UI.Components; +using MP.MONO.Core.DTO; +using MP.MONO.UI.Data; +using MP.MONO.Data; +using Newtonsoft.Json; + +namespace MP.MONO.UI.Components +{ + public partial class ProdOverview + { + protected bool dataLoaded = false; + private ProductionDTO? currProd { get; set; } = null; + protected override async Task OnInitializedAsync() + { + await ReloadData(); + MMDataService.productionPipe.EA_NewMessage += ProductionPipe_EA_NewMessage; + } + + private void ProductionPipe_EA_NewMessage(object? sender, EventArgs e) + { + PubSubEventArgs currArgs = (PubSubEventArgs)e; + if (!string.IsNullOrEmpty(currArgs.newMessage)) + { + try + { + currProd = JsonConvert.DeserializeObject(currArgs.newMessage); + } + catch + { } + } + InvokeAsync(() => + { + StateHasChanged(); + }); + } + + protected string percProgress(int num, int den) + { + string answ = "width: 0%;"; + den = den != 0 ? den : 1; + double ratio = ((double)num) / den; + answ = $"width: {ratio:P0};"; + return answ; + } + + protected async Task ReloadData() + { + await Task.Delay(10); + currProd = await MMDataService.MachineProd(); + await Task.Delay(500); + dataLoaded = true; + } + } +} \ No newline at end of file diff --git a/MP.MONO.UI/Data/CurrentDataService.cs b/MP.MONO.UI/Data/CurrentDataService.cs index 6287ca4..cd4f941 100644 --- a/MP.MONO.UI/Data/CurrentDataService.cs +++ b/MP.MONO.UI/Data/CurrentDataService.cs @@ -60,6 +60,7 @@ namespace MP.MONO.UI.Data public MessagePipe alarmPipe { get; set; } = null!; public MessagePipe parametersPipe { get; set; } = null!; public MessagePipe statusPipe { get; set; } = null!; + public MessagePipe productionPipe { get; set; } = null!; #region Public Constructors @@ -75,6 +76,7 @@ namespace MP.MONO.UI.Data alarmPipe = new MessagePipe(redisConn, "allarms"); parametersPipe = new MessagePipe(redisConn, "parameters"); statusPipe = new MessagePipe(redisConn, "status"); + productionPipe = new MessagePipe(redisConn, "production"); // conf cache this.distributedCache = distributedCache; @@ -206,7 +208,14 @@ namespace MP.MONO.UI.Data public async Task MachineProd() { ProductionDTO? dbResult = new ProductionDTO(); - string cacheKey = mHash("DATA:Machine:Prod"); + string cacheKey = mHash("Machine:Production:Current"); + string rawData = await redisDb.StringGetAsync(cacheKey); + if (!string.IsNullOrEmpty(rawData)) + { + dbResult = JsonConvert.DeserializeObject(rawData); + } + +#if false string rawData; var redisDataList = await distributedCache.GetAsync(cacheKey); if (redisDataList != null) @@ -226,7 +235,8 @@ namespace MP.MONO.UI.Data stopWatch.Stop(); TimeSpan ts = stopWatch.Elapsed; Log.Trace($"Effettuata lettura da DB + caching per MachineProd: {ts.TotalMilliseconds} ms"); - } + } +#endif if (dbResult == null) { dbResult = new ProductionDTO(); diff --git a/MachineSim/MachineSim.csproj b/MachineSim/MachineSim.csproj index 1d68fe7..de17984 100644 --- a/MachineSim/MachineSim.csproj +++ b/MachineSim/MachineSim.csproj @@ -21,6 +21,12 @@ + + Always + + + Always + Always diff --git a/MachineSim/Program.cs b/MachineSim/Program.cs index c426257..e3e586b 100644 --- a/MachineSim/Program.cs +++ b/MachineSim/Program.cs @@ -30,10 +30,12 @@ var currSimGen = new Simulator(Directory.GetCurrentDirectory()); Thread threadStatus = new Thread(simStatus); Thread threadAlarms = new Thread(simAlarms); Thread threadParams = new Thread(simParameters); +Thread threadProd = new Thread(simProd); threadStatus.Start(); threadAlarms.Start(); threadParams.Start(); +threadProd.Start(); /// /// Hash Redis contenente i dati MP di una specifico TYPE (es StatusMacchina, StateMachineIngressi, ...) @@ -89,7 +91,7 @@ void simParameters() do { - var newParams = currSimGen.MachineGetParameters(); + var newParams = currSimGen.getParameters(); string rawData = JsonConvert.SerializeObject(newParams); saveAndSendMessage(mHash("Machine:Parameters:Current"), rawData, "parameters", rawData); // attesa random @@ -108,6 +110,23 @@ void simStatus() string rawData = JsonConvert.SerializeObject(newStatus); saveAndSendMessage(mHash("Machine:Status:Current"), rawData, "status", rawData); + // attesa random + Thread.Sleep(rand.Next(minPeriod, maxPeriod)); + } while (true); +} + +void simProd() +{ + int minPeriod = 3000; + int maxPeriod = 10000; + + do + { + // recupero uno stato simulato + var newStatus = currSimGen.MachineGetProd(); + string rawData = JsonConvert.SerializeObject(newStatus); + saveAndSendMessage(mHash("Machine:Production:Current"), rawData, "production", rawData); + // attesa random Thread.Sleep(rand.Next(minPeriod, maxPeriod)); } while (true); diff --git a/MachineSim/SimProd.json b/MachineSim/SimProd.json new file mode 100644 index 0000000..d5d495e --- /dev/null +++ b/MachineSim/SimProd.json @@ -0,0 +1,74 @@ +[ + { + "Order": 0, + "Type": "SPEED-5000-10000", + "Title": "SPEED", + "Value": "6000", + "ValueNum": 6000, + "MinVal": 1000, + "MaxVal": 10000, + "SimMean": 100, + "SimStd": 50, + "DisplFormat": "N0" + }, + { + "Order": 0, + "Type": "FEED-3000-5000", + "Title": "FEED", + "Value": "3500", + "ValueNum": 3500, + "MinVal": 1000, + "MaxVal": 5000, + "SimMean": 50, + "SimStd": 5, + "DisplFormat": "N0" + }, + { + "Order": 0, + "Type": "LOAD", + "Title": "SPINDLE LOAD", + "Value": "30", + "ValueNum": 30, + "MinVal": 0, + "MaxVal": 100, + "SimMean": 5, + "SimStd": 1, + "DisplFormat": "N1" + }, + { + "Order": 0, + "Type": "POS", + "Title": "X POS", + "Value": "500", + "ValueNum": 500, + "MinVal": 0, + "MaxVal": 5000, + "SimMean": 100, + "SimStd": 20, + "DisplFormat": "N2" + }, + { + "Order": 0, + "Type": "POS", + "Title": "Y POS", + "Value": "3000", + "ValueNum": 3000, + "MinVal": 0, + "MaxVal": 10000, + "SimMean": 100, + "SimStd": 10, + "DisplFormat": "N2" + }, + { + "Order": 0, + "Type": "POS", + "Title": "Z POS", + "Value": "-500", + "ValueNum": -500, + "MinVal": -3000, + "MaxVal": 0, + "SimMean": 50, + "SimStd": 5, + "DisplFormat": "N2" + } +] \ No newline at end of file diff --git a/MachineSim/SimStatus.json b/MachineSim/SimStatus.json new file mode 100644 index 0000000..d5d495e --- /dev/null +++ b/MachineSim/SimStatus.json @@ -0,0 +1,74 @@ +[ + { + "Order": 0, + "Type": "SPEED-5000-10000", + "Title": "SPEED", + "Value": "6000", + "ValueNum": 6000, + "MinVal": 1000, + "MaxVal": 10000, + "SimMean": 100, + "SimStd": 50, + "DisplFormat": "N0" + }, + { + "Order": 0, + "Type": "FEED-3000-5000", + "Title": "FEED", + "Value": "3500", + "ValueNum": 3500, + "MinVal": 1000, + "MaxVal": 5000, + "SimMean": 50, + "SimStd": 5, + "DisplFormat": "N0" + }, + { + "Order": 0, + "Type": "LOAD", + "Title": "SPINDLE LOAD", + "Value": "30", + "ValueNum": 30, + "MinVal": 0, + "MaxVal": 100, + "SimMean": 5, + "SimStd": 1, + "DisplFormat": "N1" + }, + { + "Order": 0, + "Type": "POS", + "Title": "X POS", + "Value": "500", + "ValueNum": 500, + "MinVal": 0, + "MaxVal": 5000, + "SimMean": 100, + "SimStd": 20, + "DisplFormat": "N2" + }, + { + "Order": 0, + "Type": "POS", + "Title": "Y POS", + "Value": "3000", + "ValueNum": 3000, + "MinVal": 0, + "MaxVal": 10000, + "SimMean": 100, + "SimStd": 10, + "DisplFormat": "N2" + }, + { + "Order": 0, + "Type": "POS", + "Title": "Z POS", + "Value": "-500", + "ValueNum": -500, + "MinVal": -3000, + "MaxVal": 0, + "SimMean": 50, + "SimStd": 5, + "DisplFormat": "N2" + } +] \ No newline at end of file diff --git a/MachineSim/Simulator.cs b/MachineSim/Simulator.cs index c3bc040..9109cd1 100644 --- a/MachineSim/Simulator.cs +++ b/MachineSim/Simulator.cs @@ -47,7 +47,7 @@ namespace MachineSim /// /// /// - public double getNext(double CurrVal, double MinVal, double MaxVal, double sMean, double sStd) + public double simNext(double CurrVal, double MinVal, double MaxVal, double sMean, double sStd) { double innov = Normal.Sample(sMean, sStd); CurrVal += innov; @@ -57,7 +57,7 @@ namespace MachineSim return CurrVal; } - public List MachineGetParameters() + public List getParameters() { List answ = new List(); @@ -70,7 +70,7 @@ namespace MachineSim Order = i.Order, Title = i.Title, Type = i.Type, - ValueNum = getNext(i.ValueNum, i.MinVal, i.MaxVal, i.SimMean, i.SimStd), + 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(); @@ -133,85 +133,26 @@ namespace MachineSim answ.Add(displ04); #endif -#if false + return answ; + } + + public ProductionDTO MachineGetProd() + { + Random rand = new Random(); int stdCycle = 5; - double currVal = 0; - currVal = rand.Next(10, 120) * 100; - DisplayDataDTO displ01 = new DisplayDataDTO() + ProductionDTO currMachDto = new ProductionDTO() { - Order = 0, - Title = "SPEED", - ValueNum = currVal, - IsNumeric = true, - Value = currVal.ToString("N0"), - Type = "SPEED-5000-10000" + 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..." }; - answ.Add(displ01); - - currVal = rand.Next(10, 100) * 100; - DisplayDataDTO displ02 = new DisplayDataDTO() - { - Order = 0, - Title = "FEED", - ValueNum = currVal, - IsNumeric = true, - Value = currVal.ToString("N0"), - Type = "FEED-3000-5000" - }; - answ.Add(displ02); - - currVal = rand.NextDouble() * 1.2; - DisplayDataDTO displ03 = new DisplayDataDTO() - { - Order = 0, - Title = "SPINDLE LOAD", - ValueNum = currVal, - IsNumeric = true, - Value = currVal.ToString("P0"), - Type = "LOAD" - }; - answ.Add(displ03); - - currVal = rand.NextDouble() * 5000; - DisplayDataDTO displ04 = new DisplayDataDTO() - { - Order = 0, - Title = "X POS", - ValueNum = currVal, - IsNumeric = true, - Value = currVal.ToString("N1"), - Type = "POS" - }; - answ.Add(displ04); - - currVal = rand.NextDouble() * 10000; - DisplayDataDTO displ05 = new DisplayDataDTO() - { - Order = 0, - Title = "Y POS", - ValueNum = currVal, - IsNumeric = true, - Value = currVal.ToString("N1"), - Type = "POS" - }; - answ.Add(displ05); - - currVal = rand.NextDouble() * -3000; - DisplayDataDTO displ06 = new DisplayDataDTO() - { - Order = 0, - Title = "Z POS", - ValueNum = currVal, - IsNumeric = true, - Value = currVal.ToString("N1"), - Type = "POS" - }; - answ.Add(displ06); -#endif - - return answ; + return currMachDto; } } }