Aggiunta metodi sim x status e prod iniziali

This commit is contained in:
Samuele Locatelli
2022-02-14 16:30:11 +01:00
parent d87262db13
commit d9e8a1d5c0
4 changed files with 189 additions and 15 deletions
+33 -1
View File
@@ -2,6 +2,36 @@
{
public class Enums
{
#region Public Enums
/// <summary>
/// ENUM degli stati (es path OPC-UA)
/// </summary>
public enum MachExeStatus
{
UNDEFINED = 0,
EXE,
READY,
HOLD,
FEED_HOLD,
OPTIONAL_STOP,
PROGRAM_STOPPED,
DONE
}
/// <summary>
/// ENUM dei MODE (es path OPC-UA)
/// </summary>
public enum MachRunMode
{
UNDEFINED = 0,
AUTOMATIC,
EDIT,
SEMIAUTOMATIC,
MANUAL_JOG,
SEMIAUTOMATIC_MTC
}
/// <summary>
/// Elenco dei tipi di valore gestiti da PLC (inizialmente OSAI)
/// </summary>
@@ -113,7 +143,7 @@
/// Indica che è FINITA la produzione (e quindi cancello dati backup)
/// </summary>
endProd,
}
}
#endif
public enum UserLevel
@@ -155,5 +185,7 @@
/// </summary>
MEDIAN
}
#endregion Public Enums
}
}
@@ -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");
}
/// <summary>
/// Restituisce stato Macchina
/// </summary>
/// <returns></returns>
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;
}
}
}
+103 -14
View File
@@ -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<CurrentDataService> _logger;
@@ -16,8 +21,6 @@ namespace MP.MONO.UI.Data
private readonly IDistributedCache distributedCache;
private readonly IMemoryCache memoryCache;
public static MpDbController dbController;
/// <summary>
/// Durata assoluta massima della cache IN SECONDI
/// </summary>
@@ -29,6 +32,16 @@ namespace MP.MONO.UI.Data
/// </summary>
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<CurrentDataService> 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<string, string> ParametersList { get; set; } = new Dictionary<string, string>();
#endregion Protected Properties
#region Private Methods
/// <summary>
/// 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<string, string> ParametersList { get; set; } = new Dictionary<string, string>();
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<Dictionary<string, string>> GetCurrentParameters()
{
@@ -87,5 +112,69 @@ namespace MP.MONO.UI.Data
await Task.Delay(1);
return ParametersList;
}
public async Task<ProductionDTO> 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<ProductionDTO>(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<MachineDTO> 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<MachineDTO>(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
}
}
+1
View File
@@ -20,6 +20,7 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
</ItemGroup>
<ItemGroup>