diff --git a/MP.MONO.Core/Enums.cs b/MP.MONO.Core/Enums.cs index db436eb..fb45b9a 100644 --- a/MP.MONO.Core/Enums.cs +++ b/MP.MONO.Core/Enums.cs @@ -2,6 +2,36 @@ { public class Enums { + #region Public Enums + + /// + /// ENUM degli stati (es path OPC-UA) + /// + public enum MachExeStatus + { + UNDEFINED = 0, + EXE, + READY, + HOLD, + FEED_HOLD, + OPTIONAL_STOP, + PROGRAM_STOPPED, + DONE + } + + /// + /// ENUM dei MODE (es path OPC-UA) + /// + public enum MachRunMode + { + UNDEFINED = 0, + AUTOMATIC, + EDIT, + SEMIAUTOMATIC, + MANUAL_JOG, + SEMIAUTOMATIC_MTC + } + /// /// Elenco dei tipi di valore gestiti da PLC (inizialmente OSAI) /// @@ -113,7 +143,7 @@ /// Indica che è FINITA la produzione (e quindi cancello dati backup) /// endProd, - } + } #endif public enum UserLevel @@ -155,5 +185,7 @@ /// MEDIAN } + + #endregion Public Enums } } \ No newline at end of file diff --git a/MP.MONO.Data/Controllers/MpDbController.cs b/MP.MONO.Data/Controllers/MpDbController.cs index 66c9d60..d65a455 100644 --- a/MP.MONO.Data/Controllers/MpDbController.cs +++ b/MP.MONO.Data/Controllers/MpDbController.cs @@ -1,4 +1,6 @@ using Microsoft.Extensions.Configuration; +using MP.MONO.Core; +using MP.MONO.Core.DTO; using NLog; using System; using System.Collections.Generic; @@ -23,5 +25,55 @@ namespace MP.MONO.Data.Controllers // Clear database context //Log.Info("Dispose di GWMSController"); } + + /// + /// Restituisce stato Macchina + /// + /// + public MachineDTO MachineGetStatus() + { + // !!!FIXME TODO... è fake... + + Random rand = new Random(); + 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)}" + }; + + Task.Delay(200).Wait(); + + return currMachDto; + } + + public ProductionDTO MachineGetProd() + { + // !!!FIXME TODO... è fake... + + Random rand = new Random(); + int stdCycle = 5; + + ProductionDTO currMachDto = new ProductionDTO() + { + Order = "ODL Test", + ItemCode = "ART.0000123", + ProgName = "P000012", + CurrQty = DateTime.Now.Minute, + OrderQty = 100, + CycleTime = rand.NextDouble() * stdCycle, + Message = "Sim data" + }; + + Task.Delay(200).Wait(); + + return currMachDto; + } } } diff --git a/MP.MONO.UI/Data/CurrentDataService.cs b/MP.MONO.UI/Data/CurrentDataService.cs index c406d07..736c21d 100644 --- a/MP.MONO.UI/Data/CurrentDataService.cs +++ b/MP.MONO.UI/Data/CurrentDataService.cs @@ -1,13 +1,18 @@ using Microsoft.AspNetCore.Identity.UI.Services; using Microsoft.Extensions.Caching.Distributed; using Microsoft.Extensions.Caching.Memory; +using MP.MONO.Core.DTO; using MP.MONO.Data.Controllers; +using Newtonsoft.Json; using NLog; +using System.Diagnostics; +using System.Text; namespace MP.MONO.UI.Data { public class CurrentDataService : IDisposable { + #region Private Fields private static IConfiguration _configuration; private static ILogger _logger; @@ -16,8 +21,6 @@ namespace MP.MONO.UI.Data private readonly IDistributedCache distributedCache; private readonly IMemoryCache memoryCache; - public static MpDbController dbController; - /// /// Durata assoluta massima della cache IN SECONDI /// @@ -29,6 +32,16 @@ namespace MP.MONO.UI.Data /// private int chSliExp = 60 * 1; + #endregion Private Fields + + #region Public Fields + + public static MpDbController dbController; + + #endregion Public Fields + + #region Public Constructors + public CurrentDataService(IConfiguration configuration, ILogger logger, IMemoryCache memoryCache, IDistributedCache distributedCache, IEmailSender emailSender) { _logger = logger; @@ -50,18 +63,15 @@ namespace MP.MONO.UI.Data } } - private DistributedCacheEntryOptions cacheOpt(bool fastCache) - { - var numSecAbsExp = fastCache ? chAbsExp : chAbsExp * 10; - var numSecSliExp = fastCache ? chSliExp : chSliExp * 10; - return new DistributedCacheEntryOptions().SetAbsoluteExpiration(DateTime.Now.AddSeconds(numSecAbsExp)).SetSlidingExpiration(TimeSpan.FromSeconds(numSecSliExp)); - } - public void Dispose() - { - // Clear database controller - dbController.Dispose(); - } + #endregion Public Constructors + #region Protected Properties + + protected Dictionary ParametersList { get; set; } = new Dictionary(); + + #endregion Protected Properties + + #region Private Methods /// /// Hash Redis contenente i dati MP di una specifico TYPE (es StatusMacchina, StateMachineIngressi, ...) @@ -73,7 +83,22 @@ namespace MP.MONO.UI.Data return $"DATA:{dataType}"; } - protected Dictionary ParametersList { get; set; } = new Dictionary(); + private DistributedCacheEntryOptions cacheOpt(bool fastCache) + { + var numSecAbsExp = fastCache ? chAbsExp : chAbsExp * 10; + var numSecSliExp = fastCache ? chSliExp : chSliExp * 10; + return new DistributedCacheEntryOptions().SetAbsoluteExpiration(DateTime.Now.AddSeconds(numSecAbsExp)).SetSlidingExpiration(TimeSpan.FromSeconds(numSecSliExp)); + } + + #endregion Private Methods + + #region Public Methods + + public void Dispose() + { + // Clear database controller + dbController.Dispose(); + } public async Task> GetCurrentParameters() { @@ -87,5 +112,69 @@ namespace MP.MONO.UI.Data await Task.Delay(1); return ParametersList; } + + public async Task MachineProd() + { + ProductionDTO? dbResult = new ProductionDTO(); + string cacheKey = mHash("MAPO.MONO:Machine:Prod"); + string rawData; + var redisDataList = await distributedCache.GetAsync(cacheKey); + if (redisDataList != null) + { + rawData = Encoding.UTF8.GetString(redisDataList); + dbResult = JsonConvert.DeserializeObject(rawData); + } + else + { + Stopwatch stopWatch = new Stopwatch(); + stopWatch.Start(); + dbResult = dbController.MachineGetProd(); + rawData = JsonConvert.SerializeObject(dbResult); + redisDataList = Encoding.UTF8.GetBytes(rawData); + // tolgo salvataggio x sim meglio + //await distributedCache.SetAsync(cacheKey, redisDataList, cacheOpt(true)); + stopWatch.Stop(); + TimeSpan ts = stopWatch.Elapsed; + Log.Trace($"Effettuata lettura da DB + caching per MachineProd: {ts.TotalMilliseconds} ms"); + } + if (dbResult == null) + { + dbResult = new ProductionDTO(); + } + return await Task.FromResult(dbResult); + } + + public async Task MachineStatus() + { + MachineDTO? dbResult = new MachineDTO(); + string cacheKey = mHash("MAPO.MONO:Machine:Status"); + string rawData; + var redisDataList = await distributedCache.GetAsync(cacheKey); + if (redisDataList != null) + { + rawData = Encoding.UTF8.GetString(redisDataList); + dbResult = JsonConvert.DeserializeObject(rawData); + } + else + { + Stopwatch stopWatch = new Stopwatch(); + stopWatch.Start(); + dbResult = dbController.MachineGetStatus(); + rawData = JsonConvert.SerializeObject(dbResult); + redisDataList = Encoding.UTF8.GetBytes(rawData); + // tolgo salvataggio x sim meglio + //await distributedCache.SetAsync(cacheKey, redisDataList, cacheOpt(true)); + stopWatch.Stop(); + TimeSpan ts = stopWatch.Elapsed; + Log.Trace($"Effettuata lettura da DB + caching per MachineStatus: {ts.TotalMilliseconds} ms"); + } + if (dbResult == null) + { + dbResult = new MachineDTO(); + } + return await Task.FromResult(dbResult); + } + + #endregion Public Methods } } \ No newline at end of file diff --git a/MP.MONO.UI/MP.MONO.UI.csproj b/MP.MONO.UI/MP.MONO.UI.csproj index 654ff4a..b99f325 100644 --- a/MP.MONO.UI/MP.MONO.UI.csproj +++ b/MP.MONO.UI/MP.MONO.UI.csproj @@ -20,6 +20,7 @@ all runtime; build; native; contentfiles; analyzers; buildtransitive +