c96b2eba1d
Fix salvataggio conf
2588 lines
112 KiB
C#
2588 lines
112 KiB
C#
using GWMS.Data;
|
|
using GWMS.Data.DatabaseModels;
|
|
using GWMS.Data.DTO;
|
|
using Microsoft.AspNetCore.Identity;
|
|
using Microsoft.AspNetCore.Identity.UI.Services;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.Logging;
|
|
using Newtonsoft.Json;
|
|
using NLog;
|
|
using StackExchange.Redis;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Diagnostics;
|
|
using System.Globalization;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using static GWMS.Data.IobObjects;
|
|
|
|
namespace GWMS.UI.Data
|
|
{
|
|
public class GWMSDataService : IDisposable
|
|
{
|
|
#region Public Fields
|
|
|
|
public static GWMS.Data.Controllers.GWMSController dbController;
|
|
|
|
#endregion Public Fields
|
|
|
|
#region Public Constructors
|
|
|
|
public GWMSDataService(IConfiguration configuration, ILogger<GWMSDataService> logger, IEmailSender emailSender, UserManager<IdentityUser> userManager, IConnectionMultiplexer redisConnMult)
|
|
{
|
|
_logger = logger;
|
|
_configuration = configuration;
|
|
_emailSender = emailSender;
|
|
_userManager = userManager;
|
|
|
|
// Verifica conf trace...
|
|
traceEnabled = _configuration.GetValue<bool>("Otel:EnableTracing", false);
|
|
|
|
// fix arrotondamento
|
|
roundFactor = _configuration.GetValue<int>("RuntimeOpt:100", 10);
|
|
|
|
// Conf cache
|
|
redisConn = redisConnMult;
|
|
redisDb = this.redisConn.GetDatabase();
|
|
|
|
// json serializer... FIX errore loop circolare https://www.ryadel.com/en/jsonserializationexception-self-referencing-loop-detected-error-fix-entity-framework-asp-net-core/
|
|
JSSettings = new JsonSerializerSettings()
|
|
{
|
|
ReferenceLoopHandling = ReferenceLoopHandling.Ignore
|
|
};
|
|
|
|
// cod app
|
|
CodApp = _configuration.GetValue<string>("RuntimeOpt:CodApp");
|
|
|
|
// gestione parallelismo...
|
|
numPar = _configuration.GetValue<int>("RuntimeOpt:NumPar");
|
|
|
|
// conf DB
|
|
string connStr = _configuration.GetConnectionString("GWMS.Data");
|
|
if (string.IsNullOrEmpty(connStr))
|
|
{
|
|
_logger.LogError("ConnString empty!");
|
|
}
|
|
else
|
|
{
|
|
dbController = new GWMS.Data.Controllers.GWMSController(configuration);
|
|
}
|
|
_logger.LogInformation("GWMSDataService Started!");
|
|
}
|
|
|
|
#endregion Public Constructors
|
|
|
|
#region Public Methods
|
|
|
|
/// <summary>
|
|
/// verifica se sia da reinviare un task alla macchina dall'elenco di quelli salvati (in
|
|
/// modalità upsert) se non scaduti
|
|
/// </summary>
|
|
/// <param name="idxMacchina"></param>
|
|
/// <param name="taskKey"></param>
|
|
/// <param name="taskVal"></param>
|
|
/// <returns></returns>
|
|
public async Task<bool> AddCheckTask4MachineAsync(string idxMacchina, taskType taskKey, string taskVal)
|
|
{
|
|
bool answ = false;
|
|
using var activity = ActivitySource.StartActivity("AddCheckTask4MachineAsync");
|
|
activity?.SetTag("iob.code", idxMacchina);
|
|
activity?.SetTag("iob.task", $"{taskKey}");
|
|
string currHash = exeTaskHash(idxMacchina);
|
|
Log.Info($"AddCheckTask4MachineAsync idxMacchina: {idxMacchina} | taskKey: {taskKey} | taskVal: {taskVal}");
|
|
try
|
|
{
|
|
Dictionary<string, string> savedTask = await mSavedTaskMacchina(idxMacchina);
|
|
// cerco valore saved
|
|
string savedVal = savedTask[taskKey.ToString()];
|
|
// se ho un valore != "" --> rimetto in coda di invio...
|
|
if (!string.IsNullOrEmpty(savedVal) && (savedVal != taskVal))
|
|
{
|
|
// leggo task attuali...
|
|
Dictionary<string, string> currTasks = await mTaskMacchina(idxMacchina);
|
|
// rimetto in task da eseguire...
|
|
currTasks[taskKey.ToString()] = savedVal;
|
|
// salvo in redis!
|
|
string rawData = JsonConvert.SerializeObject(currTasks);
|
|
await redisDb.StringSetAsync(currHash, rawData);
|
|
answ = true;
|
|
string msg = $"Re-issued task4machine | idxMacchina: {idxMacchina} | taskKey: {taskKey} | savedVal: {savedVal}";
|
|
Log.Info(msg);
|
|
activity?.SetTag("iob.job_msg", msg);
|
|
}
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
string exMsg = $"Eccezione in AddCheckTask4MachineAsync | idxMacchina: {idxMacchina} | taskKey: {taskKey} | taskVal: {taskVal}";
|
|
Log.Error($"{exMsg}{Environment.NewLine}{exc}");
|
|
// traccio errore
|
|
activity?.SetStatus(ActivityStatusCode.Error, exc.Message);
|
|
activity?.AddEvent(new ActivityEvent("exception", tags: new ActivityTagsCollection {
|
|
{ "exception.type", exc.GetType().Name },
|
|
{ "exception.message", exc.Message },
|
|
{ "exception.stacktrace", exc.StackTrace }
|
|
}));
|
|
//throw;
|
|
}
|
|
return answ;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Aggiunge un task all'elenco di quelli salvati (in modalità upsert)
|
|
/// </summary>
|
|
/// <param name="idxMacchina"></param>
|
|
/// <param name="taskKey"></param>
|
|
/// <param name="taskVal"></param>
|
|
/// <returns></returns>
|
|
public async Task<bool> addTask4Machine(string idxMacchina, taskType taskKey, string taskVal)
|
|
{
|
|
bool answ = false;
|
|
using var activity = ActivitySource.StartActivity("addTask4Machine");
|
|
string source = "REDIS";
|
|
activity?.SetTag("iob.code", idxMacchina);
|
|
activity?.SetTag("iob.task", $"{taskKey}");
|
|
string rawData = "";
|
|
string currHash = exeTaskHash(idxMacchina);
|
|
string currSavedParHash = savedTaskHash(idxMacchina);
|
|
Dictionary<string, string> currTasks = new Dictionary<string, string>();
|
|
Dictionary<string, string> savedTask = new Dictionary<string, string>();
|
|
try
|
|
{
|
|
Log.Info($"Request: idxMacchina: {idxMacchina} | taskKey: {taskKey} | taskVal: {taskVal}");
|
|
// leggo task attuali...
|
|
currTasks = await mTaskMacchina(idxMacchina);
|
|
if (currTasks.ContainsKey($"{taskKey}"))
|
|
{
|
|
currTasks[$"{taskKey}"] = taskVal;
|
|
}
|
|
else
|
|
{
|
|
currTasks.Add($"{taskKey}", taskVal);
|
|
}
|
|
// salvo in redis!
|
|
rawData = JsonConvert.SerializeObject(currTasks);
|
|
await redisDb.StringSetAsync(currHash, rawData);
|
|
Log.Info($"Task ADD | idxMacchina: {idxMacchina} | taskKey: {taskKey} | taskVal: {taskVal}");
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
string exMsg = $"Errore in addTask4Machine | idxMacchina: {idxMacchina} | taskKey: {taskKey} | taskVal: {taskVal}";
|
|
Log.Error($"{exMsg}{Environment.NewLine}{exc}");
|
|
// traccio errore
|
|
activity?.SetStatus(ActivityStatusCode.Error, exc.Message);
|
|
activity?.AddEvent(new ActivityEvent("exception", tags: new ActivityTagsCollection {
|
|
{ "exception.type", exc.GetType().Name },
|
|
{ "exception.message", exc.Message },
|
|
{ "exception.stacktrace", exc.StackTrace }
|
|
}));
|
|
}
|
|
// verifico in base al tipo di task se fare backup...
|
|
switch (taskKey)
|
|
{
|
|
case taskType.setArt:
|
|
case taskType.setComm:
|
|
case taskType.setPzComm:
|
|
case taskType.setProg:
|
|
// leggo task SALVATI attuali...
|
|
savedTask = await mSavedTaskMacchina(idxMacchina);
|
|
savedTask[$"{taskKey}"] = taskVal;
|
|
// salvo in redis!
|
|
rawData = JsonConvert.SerializeObject(savedTask);
|
|
await redisDb.StringSetAsync(currSavedParHash, rawData);
|
|
break;
|
|
|
|
case taskType.endProd:
|
|
// salvo un DICT vuoto x resettare
|
|
savedTask = new Dictionary<string, string>();
|
|
// salvo in redis!
|
|
rawData = JsonConvert.SerializeObject(savedTask);
|
|
await redisDb.StringSetAsync(currSavedParHash, rawData);
|
|
break;
|
|
|
|
default:
|
|
break;
|
|
}
|
|
activity?.SetTag("data.source", source);
|
|
activity?.Stop();
|
|
LogTrace($"addTask4Machine | {source} | {activity?.Duration.TotalMilliseconds} ms");
|
|
return answ;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Esecuzione cleanup + recupero allarmi
|
|
/// </summary><
|
|
/// <param name="PlantId"></param>
|
|
/// <param name="skipRec"></param>
|
|
/// <param name="numRec"></param>
|
|
/// <returns></returns>
|
|
public async Task<List<AlarmLogModel>> AlarmLogGetFilt(int PlantId, int skipRec, int numRec)
|
|
{
|
|
using var activity = ActivitySource.StartActivity("AlarmLogGetFilt");
|
|
activity?.SetTag("plant.ord", PlantId);
|
|
activity?.SetTag("rec.num", numRec);
|
|
string source = "DB";
|
|
// effettuo pulizia
|
|
int numClean = await dbController.AlarmLogCleanDup(PlantId);
|
|
if (numClean > 0)
|
|
{
|
|
activity?.SetTag("duplicate.deleted", numClean);
|
|
LogTrace($"Rimozione {numClean} record duplicati", NLog.LogLevel.Info);
|
|
}
|
|
// restituisco elenco
|
|
var result = await dbController.AlarmLogGetFilt(PlantId, skipRec, numRec);
|
|
activity?.SetTag("data.source", source);
|
|
activity?.SetTag("result.count", result.Count);
|
|
activity?.Stop();
|
|
LogTrace($"Lettura AlarmLogGetFilt | {source} | {activity?.Duration.TotalMilliseconds} ms");
|
|
return result;
|
|
}
|
|
|
|
public async Task<bool> AlarmLogInsert(AlarmLogModel newItem)
|
|
{
|
|
using var activity = ActivitySource.StartActivity("AlarmLogInsert");
|
|
activity?.SetTag("plant.ord", newItem.PlantId);
|
|
string source = "DB";
|
|
bool fatto = await dbController.AlarmLogInsert(newItem);
|
|
DateTime adesso = DateTime.Now;
|
|
// se registrato --> invio email
|
|
if (fatto)
|
|
{
|
|
// Gestori!
|
|
string emailDest = "";
|
|
string emailSubj = "";
|
|
string emailBody = "";
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
List<PlantDTO> allPlants = await PlantDtoGetAllAsync();
|
|
var currPlant = allPlants.Where(x => x.PlantId == newItem.PlantId).FirstOrDefault();
|
|
|
|
// inizio a comporre email
|
|
emailSubj = $"GWMS: warning/allarme registrato per impianto {currPlant.PlantDesc}";
|
|
sb.AppendLine($"Impianto interessato: <b>{currPlant.PlantDesc}</b>");
|
|
sb.AppendLine("");
|
|
if (newItem.Status > 0)
|
|
{
|
|
sb.AppendLine($"Allarme rilevato in area {newItem.MemAddress}/{newItem.Index} alle {adesso:yyyy.MM.dd HH:mm:ss}");
|
|
sb.AppendLine($"{newItem.ValDecoded}");
|
|
}
|
|
else
|
|
{
|
|
sb.AppendLine($"Allarme cessato per {newItem.MemAddress}/{newItem.Index} alle {adesso:yyyy.MM.dd HH:mm:ss}");
|
|
}
|
|
emailBody = sb.ToString().Replace(Environment.NewLine, "<br/>");
|
|
|
|
System.Security.Claims.Claim suppClaim = new System.Security.Claims.Claim("PlantId", $"{newItem.PlantId}");
|
|
|
|
// recupero elenco users associati al PLANT....
|
|
var rawUserList = UserDataGetFiltAsync("").Result;
|
|
var plantUserList = rawUserList
|
|
.Where(x => x.Roles.Contains("User"))
|
|
.ToList();
|
|
//var plantList = await PlantDtoGetAllAsync();
|
|
var userListSupp = plantUserList
|
|
.Where(x => x.Claims.Where(c => c.Type == "PlantId" && c.Value == $"{newItem.PlantId}").Count() > 0)
|
|
.ToList();
|
|
|
|
foreach (var user in userListSupp)
|
|
{
|
|
// invio email di notifica nuovi ordini inseriti
|
|
emailDest = user.Identity.Email;
|
|
#if RELEASE
|
|
// invio!
|
|
await _emailSender.SendEmailAsync(emailDest, emailSubj, emailBody);
|
|
#endif
|
|
_logger.LogInformation($"Email sento to PLANT USER {emailDest}!");
|
|
}
|
|
}
|
|
|
|
activity?.SetTag("data.source", source);
|
|
activity?.Stop();
|
|
LogTrace($"AlarmLogInsert | {source} | {activity?.Duration.TotalMilliseconds} ms");
|
|
return fatto;
|
|
}
|
|
|
|
/// <summary>
|
|
/// effettua verifica x tutti i plant, se livello > livello riordino e NON ci sono ordini
|
|
/// APERTI in arrivo --> lancia ordine
|
|
/// </summary>
|
|
public async Task<bool> checkLevels()
|
|
{
|
|
bool fatto = false;
|
|
using var activity = ActivitySource.StartActivity("checkLevels");
|
|
string source = "DB+REDIS";
|
|
// effettuo verifica veto ricalcolo ordini da cache...
|
|
DateTime adesso = DateTime.Now;
|
|
DateTime vetoCheck = adesso.AddMinutes(1);
|
|
string cacheKey = mHash("CHECKLEVEL");
|
|
string rawData = "";
|
|
try
|
|
{
|
|
rawData = await redisDb.StringGetAsync(cacheKey);
|
|
|
|
// se non ho veto --> controllo
|
|
if (!string.IsNullOrEmpty(rawData))
|
|
{
|
|
vetoCheck = JsonConvert.DeserializeObject<DateTime>(rawData);
|
|
}
|
|
else
|
|
{
|
|
vetoCheck = adesso.AddMinutes(-1);
|
|
}
|
|
if (adesso > vetoCheck)
|
|
{
|
|
fatto = await CheckCreateOrders();
|
|
// imposto nuovo veto a 5 min...
|
|
vetoCheck = adesso.AddMinutes(5);
|
|
rawData = JsonConvert.SerializeObject(vetoCheck);
|
|
await redisDb.StringSetAsync(cacheKey, rawData, LongCache);
|
|
}
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
string exMsg = $"Eccezione in checkLevels";
|
|
Log.Error($"{exMsg}{Environment.NewLine}{exc}");
|
|
// traccio errore
|
|
activity?.SetStatus(ActivityStatusCode.Error, exc.Message);
|
|
activity?.AddEvent(new ActivityEvent("exception", tags: new ActivityTagsCollection {
|
|
{ "exception.type", exc.GetType().Name },
|
|
{ "exception.message", exc.Message },
|
|
{ "exception.stacktrace", exc.StackTrace }
|
|
}));
|
|
}
|
|
activity?.SetTag("data.source", source);
|
|
activity?.Stop();
|
|
LogTrace($"checkLevels | {source} | {activity?.Duration.TotalMilliseconds} ms");
|
|
return fatto;
|
|
}
|
|
|
|
public async Task<List<ConfigModel>> ConfigGetAll()
|
|
{
|
|
using var activity = ActivitySource.StartActivity("ConfigGetAll");
|
|
string source = "DB";
|
|
List<ConfigModel>? result = new List<ConfigModel>();
|
|
string cacheKey = mHash("CONFIG");
|
|
string rawData = "";
|
|
rawData = await redisDb.StringGetAsync(cacheKey);
|
|
if (!string.IsNullOrEmpty(rawData))
|
|
{
|
|
source = "REDIS";
|
|
var tempResult = JsonConvert.DeserializeObject<List<ConfigModel>>(rawData);
|
|
if (tempResult == null)
|
|
{
|
|
result = new List<ConfigModel>();
|
|
}
|
|
else
|
|
{
|
|
result = tempResult;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
result = dbController.GetConfig();
|
|
rawData = JsonConvert.SerializeObject(result);
|
|
await redisDb.StringSetAsync(cacheKey, rawData, UltraLongCache);
|
|
}
|
|
activity?.SetTag("data.source", source);
|
|
activity?.SetTag("result.count", result.Count);
|
|
activity?.Stop();
|
|
LogTrace($"ConfigGetAll | {source} | {activity?.Duration.TotalMilliseconds} ms");
|
|
return result;
|
|
}
|
|
|
|
public PlantLogModel convertFluxToPL(int plantId, flogData origData)
|
|
{
|
|
// cerco di ottenere un ValDouble in cifre
|
|
TimeSpan delta = origData.dtCurr.Subtract(origData.dtEve);
|
|
PlantLogModel answ = new PlantLogModel()
|
|
{
|
|
FluxType = origData.flux,
|
|
ValNumber = convStr2Double(origData.valore),
|
|
ValString = origData.valore,
|
|
PlantId = plantId,
|
|
DtEvent = DateTime.Now.Subtract(delta)
|
|
};
|
|
|
|
return answ;
|
|
}
|
|
|
|
public double convStr2Double(string origData)
|
|
{
|
|
double valDbl = 0;
|
|
NumberStyles style = NumberStyles.Number;
|
|
CultureInfo culture = CultureInfo.CreateSpecificCulture("it-IT");
|
|
string valString = origData;
|
|
// verifico SE contiene o meno la ","...
|
|
if (valString.IndexOf(',') > 0 && valString.IndexOf('.') > 0)
|
|
{
|
|
valString = origData.Replace(".", "");
|
|
}
|
|
else if (valString.IndexOf(',') < 0)
|
|
{
|
|
valString = origData.Replace(".", ",");
|
|
}
|
|
double.TryParse(valString, style, culture, out valDbl);
|
|
return valDbl;
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
// Clear database controller
|
|
dbController.Dispose();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Svuota ed invalida tutta la cache (tipicamente in caso di update con conseguenze "diffuse")
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public async Task<bool> FlushRedisCache()
|
|
{
|
|
using var activity = ActivitySource.StartActivity("FlushRedisCache");
|
|
string source = "REDIS";
|
|
RedisValue pattern = new RedisValue($"{Const.rKeyConfig}:*");
|
|
bool answ = await ExecFlushRedisPattern(pattern);
|
|
activity?.SetTag("data.source", source);
|
|
activity?.Stop();
|
|
LogTrace($"FlushRedisCache | {source} | {activity?.Duration.TotalMilliseconds} ms");
|
|
return answ;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Restitusice elenco PARAMETRI (da passare a IOB-WIN) per l'impianto indicato
|
|
/// </summary>
|
|
/// <param name="idxMacchina">Impianto richiesto</param>
|
|
/// <returns></returns>
|
|
public async Task<List<objItem>> getCurrObjItems(string idxMacchina)
|
|
{
|
|
using var activity = ActivitySource.StartActivity("getCurrObjItems");
|
|
activity?.SetTag("iob.code", idxMacchina);
|
|
string source = "REDIS";
|
|
List<objItem> answ = new List<objItem>();
|
|
List<objItem> outList = new List<objItem>();
|
|
// ORA recupero da memoria redis...
|
|
try
|
|
{
|
|
string cacheKey = currParametersHash(idxMacchina);
|
|
string rawData = "";
|
|
rawData = await redisDb.StringGetAsync(cacheKey);
|
|
if (!string.IsNullOrEmpty(rawData))
|
|
{
|
|
answ = JsonConvert.DeserializeObject<List<objItem>>(rawData);
|
|
// ordino!
|
|
outList = answ
|
|
.OrderByDescending(x => x.writable)
|
|
.ThenBy(x => x.name)
|
|
.ToList();
|
|
}
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
string exMsg = $"Eccezione in getCurrObjItems (ParametriMacchinaGet) | idxMacchina {idxMacchina}";
|
|
Log.Error($"{exMsg}{Environment.NewLine}{exc}");
|
|
// traccio errore
|
|
activity?.SetStatus(ActivityStatusCode.Error, exc.Message);
|
|
activity?.AddEvent(new ActivityEvent("exception", tags: new ActivityTagsCollection {
|
|
{ "exception.type", exc.GetType().Name },
|
|
{ "exception.message", exc.Message },
|
|
{ "exception.stacktrace", exc.StackTrace }
|
|
}));
|
|
}
|
|
activity?.SetTag("data.source", source);
|
|
activity?.Stop();
|
|
LogTrace($"getCurrObjItems | {source} | {activity?.Duration.TotalMilliseconds} ms");
|
|
return outList;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Restitusice elenco PARAMETRI che richiedono una WRITE x IOB
|
|
/// </summary>
|
|
/// <param name="idxMacchina">Impianto richiesto</param>
|
|
/// <returns></returns>
|
|
public async Task<List<objItem>> getCurrObjItemsPendigWrite(string idxMacchina)
|
|
{
|
|
using var activity = ActivitySource.StartActivity("getCurrObjItemsPendigWrite");
|
|
activity?.SetTag("iob.code", idxMacchina);
|
|
string source = "REDIS";
|
|
List<objItem> actValues = new List<objItem>();
|
|
List<objItem> writeValues = new List<objItem>();
|
|
// ORA recupero da memoria redis...
|
|
try
|
|
{
|
|
string cacheKey = currParametersHash(idxMacchina);
|
|
string rawData = "";
|
|
rawData = await redisDb.StringGetAsync(cacheKey);
|
|
if (!string.IsNullOrEmpty(rawData))
|
|
{
|
|
actValues = JsonConvert.DeserializeObject<List<objItem>>(rawData);
|
|
// cerco e rimuovo parametri sola lettura o SENZA richieste scrittura
|
|
foreach (var item in actValues)
|
|
{
|
|
if (item.writable && !string.IsNullOrEmpty(item.reqValue))
|
|
{
|
|
writeValues.Add(item);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
string exMsg = $"Eccezione in getCurrObjItems (ParametriMacchinaWriteGet) | idxMacchina {idxMacchina}";
|
|
Log.Error($"{exMsg}{Environment.NewLine}{exc}");
|
|
// traccio errore
|
|
activity?.SetStatus(ActivityStatusCode.Error, exc.Message);
|
|
activity?.AddEvent(new ActivityEvent("exception", tags: new ActivityTagsCollection {
|
|
{ "exception.type", exc.GetType().Name },
|
|
{ "exception.message", exc.Message },
|
|
{ "exception.stacktrace", exc.StackTrace }
|
|
}));
|
|
}
|
|
activity?.SetTag("data.source", source);
|
|
activity?.Stop();
|
|
LogTrace($"getCurrObjItemsPendigWrite | {source} | {activity?.Duration.TotalMilliseconds} ms");
|
|
return writeValues;
|
|
}
|
|
|
|
public async Task<bool> HasPlantLog()
|
|
{
|
|
using var activity = ActivitySource.StartActivity("HasPlantLog");
|
|
string source = "DB";
|
|
var result = await dbController.HasPlantLog();
|
|
activity?.SetTag("data.source", source);
|
|
activity?.Stop();
|
|
LogTrace($"HasPlantLog | {source} | {activity?.Duration.TotalMilliseconds} ms");
|
|
return result;
|
|
}
|
|
|
|
public async Task<List<ItemModel>> ItemsGetAll()
|
|
{
|
|
using var activity = ActivitySource.StartActivity("ItemsGetAll");
|
|
string source = "DB";
|
|
List<ItemModel>? dbResult = new List<ItemModel>();
|
|
string cacheKey = mHash("ITEMS");
|
|
string rawData = "";
|
|
rawData = await redisDb.StringGetAsync(cacheKey);
|
|
if (!string.IsNullOrEmpty(rawData))
|
|
{
|
|
source = "REDIS";
|
|
var tempResult = JsonConvert.DeserializeObject<List<ItemModel>>(rawData);
|
|
if (tempResult == null)
|
|
{
|
|
dbResult = new List<ItemModel>();
|
|
}
|
|
else
|
|
{
|
|
dbResult = tempResult;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
dbResult = dbController.GetItems();
|
|
rawData = JsonConvert.SerializeObject(dbResult);
|
|
await redisDb.StringSetAsync(cacheKey, rawData, UltraLongCache);
|
|
}
|
|
activity?.SetTag("data.source", source);
|
|
activity?.Stop();
|
|
LogTrace($"ItemsGetAll | {source} | {activity?.Duration.TotalMilliseconds} ms");
|
|
return dbResult;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Restitusice elenco KVP dei TASK (da passare a IOB-WIN) per l'impianto indicato
|
|
/// </summary>
|
|
/// <param name="idxMacchina"></param>
|
|
/// <returns></returns>
|
|
public async Task<Dictionary<string, string>> mOptParMacchina(string idxMacchina)
|
|
{
|
|
using var activity = ActivitySource.StartActivity("mOptParMacchina");
|
|
activity?.SetTag("iob.code", idxMacchina);
|
|
string source = "REDIS";
|
|
// hard coded dimensione vettore DatiMacchine
|
|
Dictionary<string, string>? answ = new Dictionary<string, string>();
|
|
// ORA recupero da memoria redis...
|
|
try
|
|
{
|
|
string currHash = optParHash(idxMacchina);
|
|
string rawData = "";
|
|
rawData = await redisDb.StringGetAsync(currHash);
|
|
if (!string.IsNullOrEmpty(rawData))
|
|
{
|
|
var tempResult = JsonConvert.DeserializeObject<Dictionary<string, string>>(rawData);
|
|
if (tempResult == null)
|
|
{
|
|
answ = new Dictionary<string, string>();
|
|
}
|
|
else
|
|
{
|
|
answ = tempResult;
|
|
}
|
|
}
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
string exMsg = $"Eccezione compilazione dati OPT PARAM mOptParMacchina | idxMacchina {idxMacchina}";
|
|
Log.Error($"{exMsg}{Environment.NewLine}{exc}");
|
|
// traccio errore
|
|
activity?.SetStatus(ActivityStatusCode.Error, exc.Message);
|
|
activity?.AddEvent(new ActivityEvent("exception", tags: new ActivityTagsCollection {
|
|
{ "exception.type", exc.GetType().Name },
|
|
{ "exception.message", exc.Message },
|
|
{ "exception.stacktrace", exc.StackTrace }
|
|
}));
|
|
}
|
|
activity?.SetTag("data.source", source);
|
|
activity?.Stop();
|
|
LogTrace($"mOptParMacchina | {source} | {activity?.Duration.TotalMilliseconds} ms");
|
|
return answ;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Restitusice elenco KVP dei TASK SALVATI (da passare a IOB-WIN) per l'impianto indicato
|
|
/// </summary>
|
|
/// <param name="idxMacchina"></param>
|
|
/// <returns></returns>
|
|
public async Task<Dictionary<string, string>> mSavedTaskMacchina(string idxMacchina)
|
|
{
|
|
using var activity = ActivitySource.StartActivity("mSavedTaskMacchina");
|
|
activity?.SetTag("iob.code", idxMacchina);
|
|
string source = "REDIS";
|
|
// hard coded dimensione vettore DatiMacchine
|
|
Dictionary<string, string> answ = new Dictionary<string, string>();
|
|
// ORA recupero da memoria redis...
|
|
try
|
|
{
|
|
string cacheKey = savedTaskHash(idxMacchina);
|
|
string rawData = "";
|
|
rawData = await redisDb.StringGetAsync(cacheKey);
|
|
if (!string.IsNullOrEmpty(rawData))
|
|
{
|
|
var tempResult = JsonConvert.DeserializeObject<Dictionary<string, string>>(rawData);
|
|
if (tempResult == null)
|
|
{
|
|
answ = new Dictionary<string, string>();
|
|
}
|
|
else
|
|
{
|
|
answ = tempResult;
|
|
}
|
|
}
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
string exMsg = $"Eccezione recupero dati SAVED TASK x Redis mSavedTaskMacchina | idxMacchina {idxMacchina}";
|
|
Log.Error($"{exMsg}{Environment.NewLine}{exc}");
|
|
// traccio errore
|
|
activity?.SetStatus(ActivityStatusCode.Error, exc.Message);
|
|
activity?.AddEvent(new ActivityEvent("exception", tags: new ActivityTagsCollection {
|
|
{ "exception.type", exc.GetType().Name },
|
|
{ "exception.message", exc.Message },
|
|
{ "exception.stacktrace", exc.StackTrace }
|
|
}));
|
|
}
|
|
activity?.SetTag("data.source", source);
|
|
activity?.Stop();
|
|
LogTrace($"mSavedTaskMacchina | {source} | {activity?.Duration.TotalMilliseconds} ms");
|
|
return answ;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Restitusice elenco KVP dei TASK (da passare a IOB-WIN) per l'impianto indicato
|
|
/// </summary>
|
|
/// <param name="idxMacchina"></param>
|
|
/// <returns></returns>
|
|
public async Task<Dictionary<string, string>> mTaskMacchina(string idxMacchina)
|
|
{
|
|
using var activity = ActivitySource.StartActivity("mTaskMacchina");
|
|
activity?.SetTag("iob.code", idxMacchina);
|
|
string source = "REDIS";
|
|
// hard coded dimensione vettore DatiMacchine
|
|
Dictionary<string, string> answ = new Dictionary<string, string>();
|
|
// ORA recupero da memoria redis...
|
|
try
|
|
{
|
|
string cacheKey = exeTaskHash(idxMacchina);
|
|
string rawData = "";
|
|
rawData = await redisDb.StringGetAsync(cacheKey);
|
|
if (!string.IsNullOrEmpty(rawData))
|
|
{
|
|
var tempResult = JsonConvert.DeserializeObject<Dictionary<string, string>>(rawData);
|
|
if (tempResult == null)
|
|
{
|
|
answ = new Dictionary<string, string>();
|
|
}
|
|
else
|
|
{
|
|
answ = tempResult;
|
|
}
|
|
}
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
string exMsg = $"Eccezione recupero dati EXE TASK x Redis mTaskMacchina | idxMacchina {idxMacchina}";
|
|
Log.Error($"{exMsg}{Environment.NewLine}{exc}");
|
|
// traccio errore
|
|
activity?.SetStatus(ActivityStatusCode.Error, exc.Message);
|
|
activity?.AddEvent(new ActivityEvent("exception", tags: new ActivityTagsCollection {
|
|
{ "exception.type", exc.GetType().Name },
|
|
{ "exception.message", exc.Message },
|
|
{ "exception.stacktrace", exc.StackTrace }
|
|
}));
|
|
}
|
|
activity?.SetTag("data.source", source);
|
|
activity?.Stop();
|
|
LogTrace($"mTaskMacchina | {source} | {activity?.Duration.TotalMilliseconds} ms");
|
|
return answ;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Cancellazione Ordine consegna GAS
|
|
/// </summary>
|
|
/// <param name="currItem"></param>
|
|
/// <returns></returns>
|
|
public async Task<bool> OrderDeleteAsync(OrderModel currItem)
|
|
{
|
|
using var activity = ActivitySource.StartActivity("OrderDeleteAsync");
|
|
activity?.SetTag("order.code", currItem.OrderCode);
|
|
string source = "DB+REDIS";
|
|
bool done = false;
|
|
try
|
|
{
|
|
done = await dbController.OrderDeleteAsync(currItem);
|
|
await FlushRedisCache();
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
string exMsg = $"Eccezione in OrderDeleteAsync | PlantId {currItem.PlantId} | OrderId {currItem.OrderId} | OrderCode {currItem.OrderCode}";
|
|
Log.Error($"{exMsg}{Environment.NewLine}{exc}");
|
|
// traccio errore
|
|
activity?.SetStatus(ActivityStatusCode.Error, exc.Message);
|
|
activity?.AddEvent(new ActivityEvent("exception", tags: new ActivityTagsCollection {
|
|
{ "exception.type", exc.GetType().Name },
|
|
{ "exception.message", exc.Message },
|
|
{ "exception.stacktrace", exc.StackTrace }
|
|
}));
|
|
}
|
|
activity?.SetTag("data.source", source);
|
|
activity?.Stop();
|
|
LogTrace($"OrderDeleteAsync | {source} | {activity?.Duration.TotalMilliseconds} ms");
|
|
return done;
|
|
}
|
|
|
|
public async Task<OrderModel> OrderGetByCodeAsync(string OrderCode)
|
|
{
|
|
using var activity = ActivitySource.StartActivity("OrderGetByCodeAsync");
|
|
activity?.SetTag("order.code", OrderCode);
|
|
string source = "DB";
|
|
OrderModel result = new OrderModel();
|
|
result = await dbController.GetOrderByCodeAsync(OrderCode);
|
|
activity?.SetTag("data.source", source);
|
|
activity?.Stop();
|
|
LogTrace($"OrderGetByCodeAsync | {source} | {activity?.Duration.TotalMilliseconds} ms");
|
|
return result;
|
|
}
|
|
|
|
public async Task<OrderModel> OrderGetByIdAsync(int OrderId)
|
|
{
|
|
using var activity = ActivitySource.StartActivity("OrderGetByIdAsync");
|
|
activity?.SetTag("order.ID", OrderId);
|
|
string source = "DB";
|
|
OrderModel result = new OrderModel();
|
|
result = await dbController.GetOrderByIdAsync(OrderId);
|
|
activity?.SetTag("data.source", source);
|
|
activity?.Stop();
|
|
LogTrace($"OrderGetByIdAsync | {source} | {activity?.Duration.TotalMilliseconds} ms");
|
|
return result;
|
|
}
|
|
|
|
public async Task<List<OrderModel>> OrdersGetFiltAsync(SelectOrderData CurrFilter)
|
|
{
|
|
using var activity = ActivitySource.StartActivity("OrdersGetFiltAsync");
|
|
activity?.SetTag("param.dtStart", CurrFilter.DateStart);
|
|
activity?.SetTag("param.dtEnd", CurrFilter.DateEnd);
|
|
activity?.SetTag("param.closed", CurrFilter.ShowClosed);
|
|
activity?.SetTag("param.PlantId", CurrFilter.PlantId);
|
|
string source = "DB";
|
|
List<OrderModel> result = new List<OrderModel>();
|
|
result = await dbController.GetOrdersFiltAsync(CurrFilter.PlantId, CurrFilter.SupplierId, CurrFilter.TransporterId, CurrFilter.DateStart, CurrFilter.DateEnd, CurrFilter.ShowClosed);
|
|
activity?.SetTag("data.source", source);
|
|
activity?.SetTag("result.count", result.Count);
|
|
activity?.Stop();
|
|
LogTrace($"OrdersGetFiltAsync | {source} | {activity?.Duration.TotalMilliseconds} ms");
|
|
return await Task.FromResult(result);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Elenco degli ordini ancora aperti x il Plant
|
|
/// </summary>
|
|
/// <param name="CurrFilter"></param>
|
|
/// <returns></returns>
|
|
public async Task<List<OrderModel>> OrdersGetOpenAsync(int PlantId)
|
|
{
|
|
using var activity = ActivitySource.StartActivity("OrdersGetOpenAsync");
|
|
activity?.SetTag("param.PlantId", PlantId);
|
|
string source = "DB";
|
|
List<OrderModel> result = new List<OrderModel>();
|
|
result = await dbController.GetOrdersOpenAsync(PlantId);
|
|
activity?.SetTag("data.source", source);
|
|
activity?.SetTag("result.count", result.Count);
|
|
activity?.Stop();
|
|
LogTrace($"OrdersGetOpenAsync | {source} | {activity?.Duration.TotalMilliseconds} ms");
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Aggiornamento ordine
|
|
/// </summary>
|
|
/// <param name="currItem"></param>
|
|
/// <returns></returns>
|
|
public async Task<bool> OrderUpdateAsync(OrderModel currItem)
|
|
{
|
|
bool done = false;
|
|
using var activity = ActivitySource.StartActivity("OrderUpdateAsync");
|
|
activity?.SetTag("order.code", currItem.OrderCode);
|
|
string source = "DB+REDIS";
|
|
try
|
|
{
|
|
done = dbController.OrderUpdate(currItem);
|
|
await FlushRedisCache();
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
string exMsg = $"Eccezione in OrderUpdateAsync | PlantId {currItem.PlantId} | OrderId {currItem.OrderId} | OrderCode {currItem.OrderCode}";
|
|
Log.Error($"{exMsg}{Environment.NewLine}{exc}");
|
|
// traccio errore
|
|
activity?.SetStatus(ActivityStatusCode.Error, exc.Message);
|
|
activity?.AddEvent(new ActivityEvent("exception", tags: new ActivityTagsCollection {
|
|
{ "exception.type", exc.GetType().Name },
|
|
{ "exception.message", exc.Message },
|
|
{ "exception.stacktrace", exc.StackTrace }
|
|
}));
|
|
}
|
|
activity?.SetTag("data.source", source);
|
|
activity?.Stop();
|
|
LogTrace($"OrderUpdateAsync | {source} | {activity?.Duration.TotalMilliseconds}ms");
|
|
return done;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Elenco di TUTTI i parametri send gestiti
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public async Task<List<ParamSendModel>> ParamSendGetAllAsync()
|
|
{
|
|
using var activity = ActivitySource.StartActivity("ParamSendGetAllAsync");
|
|
string source = "DB";
|
|
List<ParamSendModel> result = new List<ParamSendModel>();
|
|
string cacheKey = mHash("ParamSend:List");
|
|
string rawData;
|
|
rawData = await redisDb.StringGetAsync(cacheKey);
|
|
if (!string.IsNullOrEmpty(rawData))
|
|
{
|
|
source = "REDIS";
|
|
var tempResult = JsonConvert.DeserializeObject<List<ParamSendModel>>(rawData);
|
|
if (tempResult == null)
|
|
{
|
|
result = new List<ParamSendModel>();
|
|
}
|
|
else
|
|
{
|
|
result = tempResult;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
result = await dbController.ParamSendGetAllAsync();
|
|
rawData = JsonConvert.SerializeObject(result);
|
|
await redisDb.StringSetAsync(cacheKey, rawData, UltraLongCache);
|
|
}
|
|
activity?.SetTag("data.source", source);
|
|
activity?.SetTag("result.count", result.Count);
|
|
activity?.Stop();
|
|
LogTrace($"ParamSendGetAllAsync | {source} | {activity?.Duration.TotalMilliseconds} ms");
|
|
return await Task.FromResult(result);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Elenco di TUTTI i parametri send gestiti
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public async Task<ParamSendModel> ParamSendGetAsync(int PlantId, string ParamUid)
|
|
{
|
|
ParamSendModel currRecord = new ParamSendModel();
|
|
using var activity = ActivitySource.StartActivity("ParamSendGetAsync");
|
|
activity?.SetTag("param.PlantId", PlantId);
|
|
activity?.SetTag("param.PlantId", ParamUid);
|
|
string source = "DB";
|
|
try
|
|
{
|
|
currRecord = await dbController.ParamSendGetAsync(PlantId, ParamUid);
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
string exMsg = $"Eccezione in ParamSendGetAsync | PlantId {PlantId} | ParamUid {ParamUid} ";
|
|
Log.Error($"{exMsg}{Environment.NewLine}{exc}");
|
|
// traccio errore
|
|
activity?.SetStatus(ActivityStatusCode.Error, exc.Message);
|
|
activity?.AddEvent(new ActivityEvent("exception", tags: new ActivityTagsCollection {
|
|
{ "exception.type", exc.GetType().Name },
|
|
{ "exception.message", exc.Message },
|
|
{ "exception.stacktrace", exc.StackTrace }
|
|
}));
|
|
}
|
|
activity?.SetTag("data.source", source);
|
|
activity?.Stop();
|
|
LogTrace($"ParamSendGetAsync | {source} | {activity?.Duration.TotalMilliseconds}ms");
|
|
return currRecord;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Aggiornamento ParamSend record
|
|
/// </summary>
|
|
/// <param name="currItem"></param>
|
|
/// <returns></returns>
|
|
public async Task<bool> ParamSendUpdateAsync(ParamSendModel currItem)
|
|
{
|
|
bool done = false;
|
|
using var activity = ActivitySource.StartActivity("ParamSendUpdateAsync");
|
|
activity?.SetTag("param.PlantId", currItem.PlantId);
|
|
activity?.SetTag("param.ParamUid", currItem.ParamUid);
|
|
string source = "DB";
|
|
try
|
|
{
|
|
done = await dbController.ParamSendUpdateAsync(currItem);
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
string exMsg = $"Eccezione in ParamSendUpdateAsync | PlantId {currItem.PlantId} | ParamUid {currItem.ParamUid}";
|
|
Log.Error($"{exMsg}{Environment.NewLine}{exc}");
|
|
// traccio errore
|
|
activity?.SetStatus(ActivityStatusCode.Error, exc.Message);
|
|
activity?.AddEvent(new ActivityEvent("exception", tags: new ActivityTagsCollection {
|
|
{ "exception.type", exc.GetType().Name },
|
|
{ "exception.message", exc.Message },
|
|
{ "exception.stacktrace", exc.StackTrace }
|
|
}));
|
|
}
|
|
activity?.SetTag("data.source", source);
|
|
activity?.Stop();
|
|
LogTrace($"ParamSendUpdateAsync | {source} | {activity?.Duration.TotalMilliseconds} ms");
|
|
return done;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Ottiene il valore calcolato x il parametro richiesto al momento della richiesta
|
|
/// </summary>
|
|
/// <param name="PlantId"></param>
|
|
/// <param name="ParamUid"></param>
|
|
/// <returns></returns>
|
|
public async Task<decimal> ParamSetCalcValAsync(int PlantId, string ParamUid)
|
|
{
|
|
decimal answ = 0;
|
|
using var activity = ActivitySource.StartActivity("ParamSetCalcValAsync");
|
|
string source = "DB";
|
|
activity?.SetTag("param.PlantId", PlantId);
|
|
activity?.SetTag("param.ParamUid", ParamUid);
|
|
List<ParamSetModel> result = await ParamSetGetFiltAsync(PlantId, ParamUid);
|
|
if (result != null && result.Count > 1)
|
|
{
|
|
// prendo i 2 valori precedente e successivo
|
|
DateTime oggi = DateTime.Today;
|
|
var prevPar = result.Where(x => x.Scadenza <= oggi).OrderByDescending(x => x.Scadenza).FirstOrDefault();
|
|
var nextPar = result.Where(x => x.Scadenza >= oggi).OrderBy(x => x.Scadenza).FirstOrDefault();
|
|
// se ho valori
|
|
if (prevPar != null && nextPar != null)
|
|
{
|
|
double num = oggi.Subtract(prevPar.Scadenza).TotalDays;
|
|
double den = nextPar.Scadenza.Subtract(prevPar.Scadenza).TotalDays;
|
|
den = den != 0 ? den : 1;
|
|
answ = prevPar.TargetVal + (nextPar.TargetVal - prevPar.TargetVal) * (decimal)(num / den);
|
|
// 2023.09.05 controllo valore ammissibile (compreso tra min/max)
|
|
decimal minVal = Math.Min(prevPar.TargetVal, nextPar.TargetVal);
|
|
decimal maxVal = Math.Max(prevPar.TargetVal, nextPar.TargetVal);
|
|
answ = Math.Min(maxVal, answ);
|
|
answ = Math.Max(minVal, answ);
|
|
// arrotondo il valore x evitare errori...
|
|
answ = Math.Round(answ * roundFactor) / roundFactor;
|
|
}
|
|
}
|
|
activity?.SetTag("data.source", source);
|
|
activity?.SetTag("result.count", result.Count);
|
|
activity?.SetTag("param.CalcVal", answ);
|
|
activity?.Stop();
|
|
LogTrace($"ParamSetCalcValAsync | {source} | {activity?.Duration.TotalMilliseconds} ms");
|
|
return answ;
|
|
}
|
|
|
|
/// <summary>
|
|
/// fattore arrotondamento valori calcolati
|
|
/// </summary>
|
|
private int roundFactor = 100;
|
|
|
|
/// <summary>
|
|
/// Cancellazione ParamSet
|
|
/// </summary>
|
|
/// <param name="currItem"></param>
|
|
/// <returns></returns>
|
|
public async Task<bool> ParamSetDeleteAsync(ParamSetModel currItem)
|
|
{
|
|
bool done = false;
|
|
using var activity = ActivitySource.StartActivity("ParamSetDeleteAsync");
|
|
string source = "DB";
|
|
activity?.SetTag("param.PlantId", currItem.PlantId);
|
|
activity?.SetTag("param.ParamUid", currItem.ParamUid);
|
|
try
|
|
{
|
|
done = await dbController.ParamSetDeleteAsync(currItem);
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
string exMsg = $"Eccezione in ParamSetDeleteAsync | PlantId {currItem.PlantId} | ParamUid {currItem.ParamUid}";
|
|
Log.Error($"{exMsg}{Environment.NewLine}{exc}");
|
|
// traccio errore
|
|
activity?.SetStatus(ActivityStatusCode.Error, exc.Message);
|
|
activity?.AddEvent(new ActivityEvent("exception", tags: new ActivityTagsCollection {
|
|
{ "exception.type", exc.GetType().Name },
|
|
{ "exception.message", exc.Message },
|
|
{ "exception.stacktrace", exc.StackTrace }
|
|
}));
|
|
}
|
|
activity?.SetTag("data.source", source);
|
|
activity?.Stop();
|
|
LogTrace($"ParamSetDeleteAsync | {source} | {activity?.Duration.TotalMilliseconds} ms");
|
|
return done;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Recupera ParamSet dato plant e UID parametro
|
|
/// </summary>
|
|
/// <param name="PlantId"></param>
|
|
/// <returns></returns>
|
|
public async Task<List<ParamSetModel>> ParamSetGetFiltAsync(int PlantId, string ParamUid)
|
|
{
|
|
using var activity = ActivitySource.StartActivity("ParamSetGetFiltAsync");
|
|
string source = "DB";
|
|
activity?.SetTag("param.PlantId", PlantId);
|
|
activity?.SetTag("param.ParamUid", ParamUid);
|
|
List<ParamSetModel> result = new List<ParamSetModel>();
|
|
result = await dbController.ParamSetGetAsync(PlantId, ParamUid);
|
|
activity?.SetTag("data.source", source);
|
|
activity?.Stop();
|
|
LogTrace($"ParamSetGetFiltAsync | {source} | {activity?.Duration.TotalMilliseconds} ms");
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Aggiornamento ParamSet
|
|
/// </summary>
|
|
/// <param name="currItem"></param>
|
|
/// <returns></returns>
|
|
public async Task<bool> ParamSetUpdateAsync(ParamSetModel currItem)
|
|
{
|
|
bool done = false;
|
|
using var activity = ActivitySource.StartActivity("ParamSetUpdateAsync");
|
|
string source = "DB";
|
|
activity?.SetTag("param.PlantId", currItem.PlantId);
|
|
activity?.SetTag("param.ParamUid", currItem.ParamUid);
|
|
if (currItem != null)
|
|
{
|
|
try
|
|
{
|
|
done = await dbController.ParamSetUpdateAsync(currItem);
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
string exMsg = $"Eccezione in ParamSetUpdateAsync | PlantId {currItem.PlantId} | ParamUid {currItem.ParamUid}";
|
|
Log.Error($"{exMsg}{Environment.NewLine}{exc}");
|
|
// traccio errore
|
|
activity?.SetStatus(ActivityStatusCode.Error, exc.Message);
|
|
activity?.AddEvent(new ActivityEvent("exception", tags: new ActivityTagsCollection {
|
|
{ "exception.type", exc.GetType().Name },
|
|
{ "exception.message", exc.Message },
|
|
{ "exception.stacktrace", exc.StackTrace }
|
|
}));
|
|
}
|
|
}
|
|
activity?.SetTag("data.source", source);
|
|
activity?.Stop();
|
|
LogTrace($"ParamSetUpdateAsync | {source} | {activity?.Duration.TotalMilliseconds} ms");
|
|
return done;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Effettua verifica parametri da inviare ed eventualmente invia
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public async Task<bool> ParamsSendCheckAsync()
|
|
{
|
|
bool answ = false;
|
|
using var activity = ActivitySource.StartActivity("ParamsSendCheckAsync");
|
|
string source = "REDIS";
|
|
string cacheKey = mHash("ParamSend:List");
|
|
// prima di tutto invalido cache dei parametri...
|
|
await ExecFlushRedisPattern(cacheKey);
|
|
DateTime adesso = DateTime.Now;
|
|
var currParams = await ParamSendGetAllAsync();
|
|
// ciclo x tutti dove siano attivi e NON ci fosse veto
|
|
var activeParams = currParams
|
|
.Where(x => x.enabled && x.VetoSend <= adesso)
|
|
.ToList();
|
|
if (activeParams != null && activeParams.Count > 0)
|
|
{
|
|
foreach (var item in activeParams)
|
|
{
|
|
await ParamsSetCheckAsync(item.PlantId, item.ParamUid);
|
|
// recupero valore...
|
|
var newVal = await ParamSetCalcValAsync(item.PlantId, item.ParamUid);
|
|
// registro richiesta
|
|
bool fatto = await UpdateMachineParameterAsync(item.PlantId, item.ParamUid, $"{newVal:N3}");
|
|
// registro nuovo veto
|
|
if (fatto)
|
|
{
|
|
// rand attesa...
|
|
Random rnd = new Random();
|
|
DateTime vetoSend = DateTime.Today.AddDays(1).AddHours(item.windStart + rnd.NextDouble() * (item.windEnd - item.windStart));
|
|
// salvo!
|
|
item.LastSend = adesso;
|
|
item.VetoSend = vetoSend;
|
|
await ParamSendUpdateAsync(item);
|
|
}
|
|
}
|
|
}
|
|
|
|
// alla fine di nuovo invalido cache dei parametri...
|
|
await ExecFlushRedisPattern(cacheKey);
|
|
activity?.SetTag("data.source", source);
|
|
activity?.Stop();
|
|
LogTrace($"ParamsSendCheckAsync | {source} | {activity?.Duration.TotalMilliseconds} ms");
|
|
return answ;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Effettua verifica parametri SET ed eventualmente aggiorna (deve esserci 1 record futuro
|
|
/// ed 1 passato x interpolare)
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public async Task<bool> ParamsSetCheckAsync(int PlantId, string ParamUid)
|
|
{
|
|
bool answ = false;
|
|
using var activity = ActivitySource.StartActivity("ParamsSetCheckAsync");
|
|
string source = "REDIS";
|
|
activity?.SetTag("param.PlantId", PlantId);
|
|
activity?.SetTag("param.ParamUid", ParamUid);
|
|
// elenco parametri set...
|
|
List<ParamSetModel> ListRecords = await ParamSetGetFiltAsync(PlantId, ParamUid);
|
|
// per invecchiare DEVONO essere almeno 2
|
|
if (ListRecords != null && ListRecords.Count > 1)
|
|
{
|
|
// cerco se ci sia 1 valore successivo...
|
|
DateTime oggi = DateTime.Today;
|
|
var nextPar = ListRecords.Where(x => x.Scadenza >= oggi).OrderBy(x => x.Scadenza).FirstOrDefault();
|
|
if (nextPar == null)
|
|
{
|
|
// prendo il valore + vecchio e lo sposto avanti 1 anno...
|
|
var oldestPar = ListRecords.Where(x => x.Scadenza <= oggi).OrderBy(x => x.Scadenza).FirstOrDefault();
|
|
oldestPar.Scadenza = oldestPar.Scadenza.AddYears(1);
|
|
// salvo!
|
|
await ParamSetUpdateAsync(oldestPar);
|
|
}
|
|
answ = true;
|
|
}
|
|
activity?.SetTag("data.source", source);
|
|
activity?.Stop();
|
|
LogTrace($"ParamsSetCheckAsync | {source} | {activity?.Duration.TotalMilliseconds} ms");
|
|
return answ;
|
|
}
|
|
|
|
public async Task<List<PlantDTO>> PlantDtoGetAllAsync()
|
|
{
|
|
using var activity = ActivitySource.StartActivity("PlantDtoGetAllAsync");
|
|
string source = "DB";
|
|
List<PlantDTO> result = new List<PlantDTO>();
|
|
string cacheVetoKey = mHash("PLANTS:VetoReadDto");
|
|
string rawData;
|
|
var rand = new Random();
|
|
// controllo se ci sia semaforo lettura
|
|
rawData = await redisDb.StringGetAsync(cacheVetoKey);
|
|
// attendo sino a che non ho scadenza blocco richiesta...
|
|
//while (redVeto != null)
|
|
while (!string.IsNullOrEmpty(rawData))
|
|
{
|
|
var vetoDt = JsonConvert.DeserializeObject<DateTime>(rawData);
|
|
if (vetoDt < DateTime.Now)
|
|
{
|
|
await ExecFlushRedisPattern(cacheVetoKey);
|
|
rawData = "";
|
|
Log.Info("Fine veto attesa");
|
|
}
|
|
else
|
|
{
|
|
Log.Debug($"Veto attivo per PlantDtoGetAllAsync: salto!");
|
|
// controllo se ci sia semaforo lettura
|
|
rawData = await redisDb.StringGetAsync(cacheVetoKey);
|
|
// attesa random 1...5 sec
|
|
await Task.Delay(1000 * rand.Next(1, 5));
|
|
}
|
|
}
|
|
|
|
// inserisco veto x bloccare!
|
|
rawData = JsonConvert.SerializeObject(DateTime.Now.AddSeconds(5));
|
|
await redisDb.StringSetAsync(cacheVetoKey, rawData);
|
|
// proseguo con la vera lettura e serializzazione
|
|
string cacheKey = mHash("PLANTS:ListDTO");
|
|
rawData = await redisDb.StringGetAsync(cacheKey);
|
|
if (!string.IsNullOrEmpty(rawData))
|
|
{
|
|
result = JsonConvert.DeserializeObject<List<PlantDTO>>(rawData);
|
|
source = "REDIS";
|
|
}
|
|
else
|
|
{
|
|
int maxRec = 100;
|
|
int.TryParse(_configuration["MaxLogRecord"], out maxRec);
|
|
result = dbController.GetPlantsDTO(maxRec);
|
|
rawData = JsonConvert.SerializeObject(result);
|
|
await redisDb.StringSetAsync(cacheKey, rawData, LongCache);
|
|
}
|
|
// elimino veto!
|
|
await ExecFlushRedisPattern(cacheVetoKey);
|
|
activity?.SetTag("data.source", source);
|
|
activity?.SetTag("result.count", result.Count);
|
|
activity?.Stop();
|
|
LogTrace($"PlantDtoGetAllAsync | {source} | {activity?.Duration.TotalMilliseconds}ms");
|
|
return result;
|
|
}
|
|
|
|
public async Task<PlantDTO> PlantDtoGetByCodeAsync(string PlantCode)
|
|
{
|
|
using var activity = ActivitySource.StartActivity("PlantDtoGetByCodeAsync");
|
|
string source = "REDIS";
|
|
activity?.SetTag("param.PlantCode", PlantCode);
|
|
PlantDTO answ = new PlantDTO();
|
|
var ListRecords = await PlantDtoGetAllAsync();
|
|
var found = ListRecords.Where(x => x.PlantCode == PlantCode).FirstOrDefault();
|
|
if (found != null)
|
|
{
|
|
answ = found;
|
|
}
|
|
activity?.SetTag("data.source", source);
|
|
activity?.Stop();
|
|
LogTrace($"PlantDtoGetByCodeAsync | {source} | {activity?.Duration.TotalMilliseconds} ms");
|
|
return answ;
|
|
}
|
|
|
|
public async Task<List<PlantLogModel>> PlantLogGetFiltAsync(int PlantId, DateTime DtMaxDate, int numRec)
|
|
{
|
|
using var activity = ActivitySource.StartActivity("PlantLogGetFiltAsync");
|
|
string source = "DB";
|
|
List<PlantLogModel> result = new List<PlantLogModel>();
|
|
string cacheKey = mHash($"PLANTS:LOGS:{PlantId}:{DtMaxDate:yyyyMMddHHmm}:{numRec}");
|
|
string rawData = "";
|
|
rawData = await redisDb.StringGetAsync(cacheKey);
|
|
if (!string.IsNullOrEmpty(rawData))
|
|
{
|
|
source = "REDIS";
|
|
var tempResult = JsonConvert.DeserializeObject<List<PlantLogModel>>(rawData);
|
|
if (tempResult == null)
|
|
{
|
|
result = new List<PlantLogModel>();
|
|
}
|
|
else
|
|
{
|
|
result = tempResult;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
result = dbController.GetPlantLog(PlantId, DtMaxDate, numRec);
|
|
rawData = JsonConvert.SerializeObject(result);
|
|
await redisDb.StringSetAsync(cacheKey, rawData, LongCache);
|
|
}
|
|
activity?.SetTag("data.source", source);
|
|
activity?.SetTag("result.count", result.Count);
|
|
activity?.Stop();
|
|
LogTrace($"PlantLogGetFiltAsync | {source} | {activity?.Duration.TotalMilliseconds}ms");
|
|
return result;
|
|
}
|
|
|
|
public async Task<bool> PlantLogInsertAsync(List<PlantLogModel> newItems)
|
|
{
|
|
using var activity = ActivitySource.StartActivity("PlantLogInsertAsync");
|
|
string source = "DB+REDIS";
|
|
bool fatto = false;
|
|
// init valori
|
|
int IntervalMin = 60;
|
|
int.TryParse(_configuration["IntervalMin"], out IntervalMin);
|
|
List<PlantLogModel> item2insert = new List<PlantLogModel>();
|
|
int PlantId = newItems.FirstOrDefault().PlantId;
|
|
// aggiorno valori ACT x DTO
|
|
await updateCurrDTO(newItems);
|
|
|
|
// recupero ultimi inseriti
|
|
List<PlantLogModel> lastValues = PlantLogGetLastByFlux(PlantId).Result;
|
|
// verifico i flussi presenti tra quelli ricevuti
|
|
List<string> fluxList = newItems
|
|
.GroupBy(g => g.FluxType)
|
|
.Select(s => s.First().FluxType)
|
|
.ToList();
|
|
|
|
foreach (var item in fluxList)
|
|
{
|
|
// cerco se c'è valore...
|
|
var lastInserted = lastValues.Where(x => x.FluxType == item).FirstOrDefault();
|
|
DateTime dateLimit = DateTime.Today.AddDays(-1);
|
|
if (lastInserted != null)
|
|
{
|
|
// per ogni flusso calcolo il valore minimo x inserimento (arrotondando a minInt)
|
|
dateLimit = dbController.DateRoundEnd(lastInserted.DtEvent, IntervalMin);
|
|
}
|
|
|
|
// cerco se ho record > valore minimo x ogni flusso ricevuto
|
|
List<PlantLogModel> insCandidates = newItems.Where(x => x.FluxType == item && x.DtEvent >= dateLimit).OrderBy(x => x.DtEvent).ToList();
|
|
|
|
int num2ins = insCandidates.Count;
|
|
while (num2ins > 0)
|
|
{
|
|
var newRec = insCandidates.First();
|
|
// il primo lo accodo da inserire
|
|
item2insert.Add(newRec);
|
|
// calcolo nuovo veto
|
|
dateLimit = dbController.DateRoundEnd(newRec.DtEvent, IntervalMin);
|
|
// ...e se ho record > data limite accodo
|
|
insCandidates = newItems.Where(x => x.FluxType == item && x.DtEvent > dateLimit).OrderBy(x => x.DtEvent).ToList();
|
|
num2ins = insCandidates.Count;
|
|
}
|
|
}
|
|
|
|
// se ho record da inserire...
|
|
if (item2insert.Count > 0)
|
|
{
|
|
// faccio vero insert
|
|
fatto = dbController.PlantLogInsertNew(item2insert);
|
|
|
|
// invalido i vari valori in cache
|
|
await ExecFlushRedisPattern(mHash($"PLANTS:LastFlux:{PlantId}"));
|
|
await ExecFlushRedisPattern(mHash("PLANTS:ListDTO"));
|
|
Log.Debug($"PlantLogInsertAsync | PlantId: {PlantId} | Completato insert {item2insert.Count} rec");
|
|
}
|
|
|
|
activity?.SetTag("data.source", source);
|
|
activity?.Stop();
|
|
LogTrace($"PlantLogInsertAsync | {source} | {activity?.Duration.TotalMilliseconds}ms");
|
|
// restituisco
|
|
return fatto;
|
|
}
|
|
|
|
public async Task<List<PlantLevSumDTO>> PlantsAnalisysByFiltAsync(SelectOrderData CurrFilter)
|
|
{
|
|
using var activity = ActivitySource.StartActivity("PlantsAnalisysByFiltAsync");
|
|
string source = "DB";
|
|
activity?.SetTag("param.PlantId", CurrFilter.PlantId);
|
|
activity?.SetTag("param.DateStart", CurrFilter.DateStart);
|
|
activity?.SetTag("param.DateEnd", CurrFilter.DateEnd);
|
|
activity?.SetTag("param.Closed", CurrFilter.ShowClosed);
|
|
List<PlantLevSumDTO> result = new List<PlantLevSumDTO>();
|
|
string cacheKey = mHash($"PLANTS:LevelSum:{CurrFilter.PlantId}:{CurrFilter.DateStart:yyMMdd}:{CurrFilter.DateEnd:yyMMdd}");
|
|
string rawData;
|
|
rawData = await redisDb.StringGetAsync(cacheKey);
|
|
if (!string.IsNullOrEmpty(rawData))
|
|
{
|
|
source = "REDIS";
|
|
var tempResult = JsonConvert.DeserializeObject<List<PlantLevSumDTO>>(rawData);
|
|
if (tempResult == null)
|
|
{
|
|
result = new List<PlantLevSumDTO>();
|
|
}
|
|
else
|
|
{
|
|
result = tempResult;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
result = dbController.GetPlantLevSumDto(CurrFilter.PlantId, CurrFilter.DateStart, CurrFilter.DateEnd);
|
|
rawData = JsonConvert.SerializeObject(result);
|
|
await redisDb.StringSetAsync(cacheKey, rawData, UltraLongCache);
|
|
}
|
|
activity?.SetTag("data.source", source);
|
|
activity?.SetTag("result.count", result.Count);
|
|
activity?.Stop();
|
|
LogTrace($"PlantsAnalisysByFiltAsync | {source} | {activity?.Duration.TotalMilliseconds}ms");
|
|
return result;
|
|
}
|
|
|
|
public async Task<bool> PlantsAnalisysResetAsync(SelectOrderData CurrFilter)
|
|
{
|
|
using var activity = ActivitySource.StartActivity("PlantsAnalisysResetAsync");
|
|
string source = "REDIS";
|
|
bool answ = false;
|
|
string cacheKey = mHash($"PLANTS:LevelSum:{CurrFilter.PlantId}:{CurrFilter.DateStart:yyMMdd}:{CurrFilter.DateEnd:yyMMdd}");
|
|
await ExecFlushRedisPattern(cacheKey);
|
|
answ = true;
|
|
activity?.SetTag("data.source", source);
|
|
activity?.Stop();
|
|
LogTrace($"PlantsAnalisysResetAsync | {source} | {activity?.Duration.TotalMilliseconds}ms");
|
|
return answ;
|
|
}
|
|
|
|
public async Task<PlantDetailModel> PlantsGetByCodeAsync(string PlantCode)
|
|
{
|
|
using var activity = ActivitySource.StartActivity("PlantsGetByCodeAsync");
|
|
activity?.SetTag("param.PlantCode", PlantCode);
|
|
string source = "REDIS";
|
|
PlantDetailModel answ = new PlantDetailModel();
|
|
var ListRecords = await PlantsListAsync();
|
|
var found = ListRecords.Where(x => x.PlantCode == PlantCode).FirstOrDefault();
|
|
if (found != null)
|
|
{
|
|
answ = found;
|
|
}
|
|
activity?.SetTag("data.source", source);
|
|
activity?.Stop();
|
|
LogTrace($"PlantsGetByCodeAsync | {source} | {activity?.Duration.TotalMilliseconds}ms");
|
|
return await Task.FromResult(answ);
|
|
}
|
|
|
|
public async Task<List<PlantDetailModel>> PlantsListAsync()
|
|
{
|
|
using var activity = ActivitySource.StartActivity("PlantsListAsync");
|
|
string source = "REDIS";
|
|
List<PlantDetailModel> result = new List<PlantDetailModel>();
|
|
string cacheKey = mHash("PLANTS:ListModel");
|
|
string rawData;
|
|
rawData = await redisDb.StringGetAsync(cacheKey);
|
|
if (!string.IsNullOrEmpty(rawData))
|
|
{
|
|
source = "REDIS";
|
|
var tempResult = JsonConvert.DeserializeObject<List<PlantDetailModel>>(rawData);
|
|
if (tempResult == null)
|
|
{
|
|
result = new List<PlantDetailModel>();
|
|
}
|
|
else
|
|
{
|
|
result = tempResult;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
result = dbController.GetPlants();
|
|
rawData = JsonConvert.SerializeObject(result);
|
|
await redisDb.StringSetAsync(cacheKey, rawData, UltraLongCache);
|
|
}
|
|
activity?.SetTag("data.source", source);
|
|
activity?.SetTag("result.count", result.Count);
|
|
activity?.Stop();
|
|
LogTrace($"PlantsListAsync | {source} | {activity?.Duration.TotalMilliseconds}ms");
|
|
return await Task.FromResult(result);
|
|
}
|
|
|
|
public async Task<bool> PlantUpdateAsync(PlantDTO currItem)
|
|
{
|
|
using var activity = ActivitySource.StartActivity("PlantUpdateAsync");
|
|
activity?.SetTag("param.PlantId", currItem.PlantId);
|
|
activity?.SetTag("param.PlantCode", currItem.PlantCode);
|
|
string source = "DB+REDIS";
|
|
bool done = false;
|
|
try
|
|
{
|
|
done = await dbController.PlantUpdateAsync(currItem);
|
|
await FlushRedisCache();
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
string exMsg = $"Eccezione in PlantUpdateAsync | PlantId: {currItem.PlantId} | PlantCode: {currItem.PlantCode} | LevelAct: {currItem.LevelAct}";
|
|
Log.Error($"{exMsg}{Environment.NewLine}{exc}");
|
|
// traccio errore
|
|
activity?.SetStatus(ActivityStatusCode.Error, exc.Message);
|
|
activity?.AddEvent(new ActivityEvent("exception", tags: new ActivityTagsCollection {
|
|
{ "exception.type", exc.GetType().Name },
|
|
{ "exception.message", exc.Message },
|
|
{ "exception.stacktrace", exc.StackTrace }
|
|
}));
|
|
}
|
|
activity?.SetTag("data.source", source);
|
|
activity?.Stop();
|
|
LogTrace($"PlantUpdateAsync | {source} | {activity?.Duration.TotalMilliseconds}ms");
|
|
return done;
|
|
}
|
|
|
|
public async Task<bool> RebootLogInsertAsync(RebootLogModel newItem)
|
|
{
|
|
bool done = false;
|
|
using var activity = ActivitySource.StartActivity("RebootLogInsertAsync");
|
|
string source = "DB";
|
|
activity?.SetTag("param.Item", newItem.Item);
|
|
try
|
|
{
|
|
done = await dbController.RecordRebootLogAsync(newItem);
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
string exMsg = $"Eccezione in RebootLogInsertAsync";
|
|
Log.Error($"{exMsg}{Environment.NewLine}{exc}");
|
|
// traccio errore
|
|
activity?.SetStatus(ActivityStatusCode.Error, exc.Message);
|
|
activity?.AddEvent(new ActivityEvent("exception", tags: new ActivityTagsCollection {
|
|
{ "exception.type", exc.GetType().Name },
|
|
{ "exception.message", exc.Message },
|
|
{ "exception.stacktrace", exc.StackTrace }
|
|
}));
|
|
}
|
|
activity?.SetTag("data.source", source);
|
|
activity?.Stop();
|
|
LogTrace($"RebootLogInsertAsync | {source} | {activity?.Duration.TotalMilliseconds}ms");
|
|
return done;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Rimuove un item da parametri correnti x IOB
|
|
/// </summary>
|
|
/// <param name="idxMacchina"></param>
|
|
/// <param name="currValues"></param>
|
|
/// <returns></returns>
|
|
public async Task<bool> RemObjItemAsync(string idxMacchina, objItem item2rem)
|
|
{
|
|
bool answ = false;
|
|
using var activity = ActivitySource.StartActivity("RemObjItemAsync");
|
|
string source = "REDIS";
|
|
activity?.SetTag("param.idxMacchina", idxMacchina);
|
|
activity?.SetTag("param.obj", item2rem.name);
|
|
if (item2rem != null)
|
|
{
|
|
string cacheKey = currParametersHash(idxMacchina);
|
|
string rawData;
|
|
rawData = await redisDb.StringGetAsync(cacheKey);
|
|
if (!string.IsNullOrEmpty(rawData))
|
|
{
|
|
var actValues = JsonConvert.DeserializeObject<List<objItem>>(rawData);
|
|
// cerco e rimuovo
|
|
var remCand = actValues
|
|
.Where(x => x.uid == item2rem.uid)
|
|
.FirstOrDefault();
|
|
if (remCand != null)
|
|
{
|
|
actValues.Remove(remCand);
|
|
}
|
|
// salvo!
|
|
rawData = JsonConvert.SerializeObject(actValues);
|
|
await redisDb.StringSetAsync(cacheKey, rawData);
|
|
}
|
|
}
|
|
activity?.SetTag("data.source", source);
|
|
activity?.Stop();
|
|
LogTrace($"RemObjItemAsync | {source} | {activity?.Duration.TotalMilliseconds}ms");
|
|
return answ;
|
|
}
|
|
|
|
/// <summary>
|
|
/// RIMUOVE un PARAMETRO OPZIONALE dall'elenco di quelli salvati
|
|
/// </summary>
|
|
/// <param name="idxMacchina"></param>
|
|
/// <param name="taskKey"></param>
|
|
/// <returns></returns>
|
|
public async Task<bool> RemOptPar4MachineAsync(string idxMacchina, string taskKey)
|
|
{
|
|
bool answ = false;
|
|
using var activity = ActivitySource.StartActivity("RemOptPar4MachineAsync");
|
|
string source = "REDIS";
|
|
activity?.SetTag("param.idxMacchina", idxMacchina);
|
|
activity?.SetTag("param.taskKey", taskKey);
|
|
string currHash = optParHash(idxMacchina);
|
|
try
|
|
{
|
|
// leggo task attuali...
|
|
var currVal = await mOptParMacchina(idxMacchina);
|
|
currVal.Remove(taskKey);
|
|
// salvo in redis!
|
|
string rawData = JsonConvert.SerializeObject(currVal);
|
|
await redisDb.StringSetAsync(currHash, rawData);
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
string exMsg = $"Eccezione in RemOptPar4MachineAsync | idxMacchina: {idxMacchina} | taskKey: {taskKey}";
|
|
Log.Error($"{exMsg}{Environment.NewLine}{exc}");
|
|
// traccio errore
|
|
activity?.SetStatus(ActivityStatusCode.Error, exc.Message);
|
|
activity?.AddEvent(new ActivityEvent("exception", tags: new ActivityTagsCollection {
|
|
{ "exception.type", exc.GetType().Name },
|
|
{ "exception.message", exc.Message },
|
|
{ "exception.stacktrace", exc.StackTrace }
|
|
}));
|
|
}
|
|
activity?.SetTag("data.source", source);
|
|
activity?.Stop();
|
|
LogTrace($"RemOptPar4MachineAsync | {source} | {activity?.Duration.TotalMilliseconds}ms");
|
|
return answ;
|
|
}
|
|
|
|
/// <summary>
|
|
/// RIMUOVE un task dall'elenco di quelli salvati
|
|
/// </summary>
|
|
/// <param name="idxMacchina"></param>
|
|
/// <param name="taskKey"></param>
|
|
/// <returns></returns>
|
|
public async Task<bool> RemTask4MachineAsync(string idxMacchina, taskType taskKey)
|
|
{
|
|
bool answ = false;
|
|
using var activity = ActivitySource.StartActivity("RemTask4MachineAsync");
|
|
string source = "REDIS";
|
|
activity?.SetTag("param.idxMacchina", idxMacchina);
|
|
activity?.SetTag("param.taskKey", taskKey);
|
|
string currHash = exeTaskHash(idxMacchina);
|
|
try
|
|
{
|
|
// leggo task attuali...
|
|
var currTasks = await mTaskMacchina(idxMacchina);
|
|
// cerco se chiave esiste -_> sostituisco
|
|
if (currTasks.ContainsKey($"{taskKey}"))
|
|
{
|
|
currTasks.Remove($"{taskKey}");
|
|
// salvo in redis!
|
|
string rawData = JsonConvert.SerializeObject(currTasks);
|
|
await redisDb.StringSetAsync(currHash, rawData);
|
|
Log.Info($"Task REM | idxMacchina: {idxMacchina} | taskKey: {taskKey}");
|
|
}
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
string exMsg = $"Eccezione in RemOptPar4MachineAsync | idxMacchina: {idxMacchina} | taskKey: {taskKey}";
|
|
Log.Error($"{exMsg}{Environment.NewLine}{exc}");
|
|
// traccio errore
|
|
activity?.SetStatus(ActivityStatusCode.Error, exc.Message);
|
|
activity?.AddEvent(new ActivityEvent("exception", tags: new ActivityTagsCollection {
|
|
{ "exception.type", exc.GetType().Name },
|
|
{ "exception.message", exc.Message },
|
|
{ "exception.stacktrace", exc.StackTrace }
|
|
}));
|
|
}
|
|
activity?.SetTag("data.source", source);
|
|
activity?.Stop();
|
|
LogTrace($"RemTask4MachineAsync | {source} | {activity?.Duration.TotalMilliseconds}ms");
|
|
return answ;
|
|
}
|
|
|
|
public void rollBackEdit(object item)
|
|
{
|
|
dbController.rollBackEntity(item);
|
|
}
|
|
|
|
/// <summary>
|
|
/// salva la conf di memoria della macchina in redis
|
|
/// </summary>
|
|
/// <param name="idxMacchina"></param>
|
|
/// <param name="rawData"></param>
|
|
/// <returns></returns>
|
|
public async Task<bool> SaveMemMapAsync(string idxMacchina, string rawData)
|
|
{
|
|
bool answ = false;
|
|
using var activity = ActivitySource.StartActivity("SaveMemMapAsync");
|
|
string source = "REDIS";
|
|
activity?.SetTag("param.idxMacchina", idxMacchina);
|
|
if (rawData != null)
|
|
{
|
|
string currHash = currMemMapHash(idxMacchina);
|
|
// salvo in redis!
|
|
answ = await redisDb.StringSetAsync(currHash, rawData);
|
|
}
|
|
activity?.SetTag("data.source", source);
|
|
activity?.Stop();
|
|
LogTrace($"SaveMemMapAsync | {source} | {activity?.Duration.TotalMilliseconds}ms");
|
|
return answ;
|
|
}
|
|
|
|
/// <summary>
|
|
/// SET elenco parametri correnti x IOB
|
|
/// </summary>
|
|
/// <param name="idxMacchina"></param>
|
|
/// <param name="currValues"></param>
|
|
/// <returns></returns>
|
|
public async Task<bool> SetCurrObjItemsAsync(string idxMacchina, List<objItem> currValues)
|
|
{
|
|
bool answ = false;
|
|
using var activity = ActivitySource.StartActivity("SetCurrObjItemsAsync");
|
|
string source = "REDIS";
|
|
activity?.SetTag("param.idxMacchina", idxMacchina);
|
|
activity?.SetTag("param.numRec", currValues.Count);
|
|
taskType currTask = taskType.nihil;
|
|
if (currValues != null)
|
|
{
|
|
string currHash = currParametersHash(idxMacchina);
|
|
// salvo in redis!
|
|
string rawData = JsonConvert.SerializeObject(currValues);
|
|
await redisDb.StringSetAsync(currHash, rawData);
|
|
// controllo se ha valori write...
|
|
foreach (var item in currValues)
|
|
{
|
|
// se fosse un valore WRITE e mi ha dato un valore vuoto --> mando un fix x riscrittura
|
|
if (item.writable && string.IsNullOrEmpty(item.value))
|
|
{
|
|
currTask = taskType.nihil;
|
|
// verifico se si tratti di un task da salvare....
|
|
if (Enum.TryParse(item.uid, true, out currTask))
|
|
{
|
|
if (Enum.IsDefined(typeof(taskType), currTask))
|
|
{
|
|
currTask = (taskType)Enum.Parse(typeof(taskType), item.uid);
|
|
await AddCheckTask4MachineAsync(idxMacchina, currTask, item.value);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
answ = true;
|
|
}
|
|
activity?.SetTag("data.source", source);
|
|
activity?.Stop();
|
|
LogTrace($"SetCurrObjItemsAsync | {source} | {activity?.Duration.TotalMilliseconds}ms");
|
|
return answ;
|
|
}
|
|
|
|
public async Task<List<SupplierModel>> SuppliersGetAllAsync()
|
|
{
|
|
using var activity = ActivitySource.StartActivity("SuppliersGetAllAsync");
|
|
string source = "DB";
|
|
List<SupplierModel> result = new List<SupplierModel>();
|
|
string cacheKey = mHash("SUPPL:List");
|
|
string rawData;
|
|
rawData = await redisDb.StringGetAsync(cacheKey);
|
|
if (!string.IsNullOrEmpty(rawData))
|
|
{
|
|
source = "REDIS";
|
|
result = JsonConvert.DeserializeObject<List<SupplierModel>>(rawData);
|
|
}
|
|
else
|
|
{
|
|
result = dbController.GetSuppliers();
|
|
rawData = JsonConvert.SerializeObject(result);
|
|
await redisDb.StringSetAsync(cacheKey, rawData, UltraLongCache);
|
|
}
|
|
activity?.SetTag("data.source", source);
|
|
activity?.SetTag("result.count", result.Count);
|
|
activity?.Stop();
|
|
LogTrace($"SuppliersGetAllAsync | {source} | {activity?.Duration.TotalMilliseconds}ms");
|
|
return result;
|
|
}
|
|
|
|
public async Task<bool> TestSendEmailAsync(string destEmail, string oggetto, string corpo)
|
|
{
|
|
bool answ = false;
|
|
using var activity = ActivitySource.StartActivity("TestSendEmailAsync");
|
|
string source = "SMTP";
|
|
try
|
|
{
|
|
await _emailSender.SendEmailAsync(destEmail, oggetto, corpo);
|
|
answ = true;
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
string exMsg = $"Eccezione in TestSendEmailAsync | destEmail: {destEmail} | oggetto: {oggetto}";
|
|
Log.Error($"{exMsg}{Environment.NewLine}{exc}");
|
|
// traccio errore
|
|
activity?.SetStatus(ActivityStatusCode.Error, exc.Message);
|
|
activity?.AddEvent(new ActivityEvent("exception", tags: new ActivityTagsCollection {
|
|
{ "exception.type", exc.GetType().Name },
|
|
{ "exception.message", exc.Message },
|
|
{ "exception.stacktrace", exc.StackTrace }
|
|
}));
|
|
}
|
|
activity?.SetTag("data.source", source);
|
|
activity?.Stop();
|
|
LogTrace($"TestSendEmailAsync | {source} | {activity?.Duration.TotalMilliseconds}ms");
|
|
return answ;
|
|
}
|
|
|
|
public async Task<List<TransporterModel>> TransportersGetAllAsync()
|
|
{
|
|
using var activity = ActivitySource.StartActivity("TransportersGetAllAsync");
|
|
string source = "DB";
|
|
List<TransporterModel> result = new List<TransporterModel>();
|
|
string cacheKey = mHash($"TRANSP:List");
|
|
string rawData;
|
|
rawData = await redisDb.StringGetAsync(cacheKey);
|
|
if (!string.IsNullOrEmpty(rawData))
|
|
{
|
|
source = "REDIS";
|
|
var tempResult = JsonConvert.DeserializeObject<List<TransporterModel>>(rawData);
|
|
if (tempResult == null)
|
|
{
|
|
result = new List<TransporterModel>();
|
|
}
|
|
else
|
|
{
|
|
result = tempResult;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
result = dbController.GetTransporters();
|
|
rawData = JsonConvert.SerializeObject(result);
|
|
await redisDb.StringSetAsync(cacheKey, rawData, UltraLongCache);
|
|
}
|
|
activity?.SetTag("data.source", source);
|
|
activity?.SetTag("result.count", result.Count);
|
|
activity?.Stop();
|
|
LogTrace($"TransportersGetAllAsync | {source} | {activity?.Duration.TotalMilliseconds}ms");
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Aggiornamento parametro per macchina
|
|
/// </summary>
|
|
/// <param name="PlantId"></param>
|
|
/// <param name="Original_uid">Parametro macchina come definito in file json</param>
|
|
/// <param name="reqValue"></param>
|
|
/// <returns></returns>
|
|
public async Task<bool> UpdateMachineParameterAsync(int PlantId, string Original_uid, string reqValue)
|
|
{
|
|
bool fatto = false;
|
|
using var activity = ActivitySource.StartActivity("UpdateMachineParameterAsync");
|
|
activity?.SetTag("param.PlantId", PlantId);
|
|
activity?.SetTag("param.Original_uid", Original_uid);
|
|
string source = "SRV";
|
|
var plantList = await PlantDtoGetAllAsync();
|
|
var currPlant = plantList
|
|
.Where(x => x.PlantId == PlantId)
|
|
.FirstOrDefault();
|
|
if (currPlant != null)
|
|
{
|
|
fatto = await UpdateMachineParameterAsync(currPlant.PlantCode, Original_uid, reqValue);
|
|
}
|
|
activity?.SetTag("data.source", source);
|
|
activity?.Stop();
|
|
LogTrace($"UpdateMachineParameterAsync | {source} | {activity?.Duration.TotalMilliseconds}ms");
|
|
return fatto;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Aggiornamento parametro per macchina
|
|
/// </summary>
|
|
/// <param name="idxMacchina"></param>
|
|
/// <param name="Original_uid">Parametro macchina come definito in file json</param>
|
|
/// <param name="reqValue"></param>
|
|
/// <returns></returns>
|
|
public async Task<bool> UpdateMachineParameterAsync(string idxMacchina, string Original_uid, string reqValue)
|
|
{
|
|
bool answ = false;
|
|
using var activity = ActivitySource.StartActivity("UpdateMachineParameterAsync");
|
|
activity?.SetTag("param.idxMacchina", idxMacchina);
|
|
activity?.SetTag("param.Original_uid", Original_uid);
|
|
string source = "SRV";
|
|
// recupero items...
|
|
List<objItem> dcList = await getCurrObjItems(idxMacchina);
|
|
List<objItem> list2Update = new List<objItem>();
|
|
// cerco quello da aggiornare
|
|
objItem trovato = dcList.Find(obj => obj.uid == Original_uid);
|
|
// se non trova --> crea ed aggiunge...
|
|
if (trovato == null)
|
|
{
|
|
dcList.Add(new objItem() { uid = Original_uid });
|
|
trovato = dcList.Find(obj => obj.uid == Original_uid);
|
|
}
|
|
|
|
// se trovato procedo
|
|
if (trovato != null)
|
|
{
|
|
// forzo writable se son arrivato qui...
|
|
trovato.writable = true;
|
|
// aggiorno valore richiesto + DtEvent richiesta
|
|
trovato.reqValue = reqValue;
|
|
trovato.lastRequest = DateTime.Now;
|
|
list2Update.Add(trovato);
|
|
await UpsertCurrObjItemsAsync(idxMacchina, list2Update);
|
|
// accodo in Task2Exe la richiesta di processing
|
|
await addTask4Machine(idxMacchina, taskType.setParameter, trovato.uid);
|
|
|
|
if (Enum.IsDefined(typeof(taskType), trovato.uid))
|
|
{
|
|
// salvo ANCHE il valore di setup ASSOCIATO...
|
|
taskType currTask = (taskType)Enum.Parse(typeof(taskType), trovato.uid);
|
|
await addTask4Machine(idxMacchina, currTask, reqValue);
|
|
}
|
|
|
|
answ = true;
|
|
}
|
|
activity?.SetTag("data.source", source);
|
|
activity?.Stop();
|
|
LogTrace($"UpdateMachineParameterAsync | {source} | {activity?.Duration.TotalMilliseconds}ms");
|
|
return answ;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Effettua UPSERT elenco parametri correnti x IOB (se c'è UPDATE, se manca ADD)
|
|
/// </summary>
|
|
/// <param name="idxMacchina"></param>
|
|
/// <param name="innovations"></param>
|
|
/// <returns></returns>
|
|
public async Task<bool> UpsertCurrObjItemsAsync(string idxMacchina, List<objItem> innovations)
|
|
{
|
|
bool answ = false;
|
|
using var activity = ActivitySource.StartActivity("UpsertCurrObjItemsAsync");
|
|
activity?.SetTag("param.idxMacchina", idxMacchina);
|
|
activity?.SetTag("param.numInnov", innovations.Count);
|
|
string source = "REDIS";
|
|
if (innovations != null)
|
|
{
|
|
string currHash = currParametersHash(idxMacchina);
|
|
// leggo i valori attuali...
|
|
List<objItem> actValues = await getCurrObjItems(idxMacchina);
|
|
var oldUid = actValues.Select(x => x.uid).Intersect(innovations.Select(i => i.uid)).ToList();
|
|
actValues.RemoveAll(x => oldUid.Contains(x.uid));
|
|
innovations.AddRange(actValues);
|
|
// serializzo e salvo
|
|
string rawData = JsonConvert.SerializeObject(innovations);
|
|
await redisDb.StringSetAsync(currHash, rawData);
|
|
}
|
|
activity?.SetTag("data.source", source);
|
|
activity?.Stop();
|
|
LogTrace($"UpsertCurrObjItemsAsync | {source} | {activity?.Duration.TotalMilliseconds}ms");
|
|
return answ;
|
|
}
|
|
|
|
public async Task<List<UserData>> UserDataGetFiltAsync(string searchVal)
|
|
{
|
|
using var activity = ActivitySource.StartActivity("UserDataGetFiltAsync");
|
|
activity?.SetTag("param.searchVal", searchVal);
|
|
string source = "USR";
|
|
// Collezione utenti
|
|
List<IdentityUser> RawList = new List<IdentityUser>();
|
|
List<UserData> UsersList = new List<UserData>();
|
|
// recupero utenti da obj _userManager
|
|
var allData = _userManager.Users.ToList();
|
|
if (!string.IsNullOrEmpty(searchVal))
|
|
{
|
|
RawList = allData.Where(x => x.NormalizedEmail.Contains(searchVal.ToUpper()) || x.NormalizedUserName.Contains(searchVal.ToUpper())).ToList();
|
|
}
|
|
else
|
|
{
|
|
RawList = allData;
|
|
}
|
|
var user = RawList.Select(x => new IdentityUser
|
|
{
|
|
Id = x.Id,
|
|
UserName = x.UserName,
|
|
Email = x.Email,
|
|
PhoneNumber = x.PhoneNumber,
|
|
PasswordHash = "*****",
|
|
EmailConfirmed = x.EmailConfirmed
|
|
}).ToList();
|
|
|
|
foreach (var item in user)
|
|
{
|
|
var UserRoles = await _userManager.GetRolesAsync(item);
|
|
var UserClaims = await _userManager.GetClaimsAsync(item);
|
|
|
|
var newItem = new UserData()
|
|
{
|
|
Identity = item,
|
|
Roles = UserRoles.ToList(),
|
|
Claims = UserClaims.ToList()
|
|
};
|
|
UsersList.Add(newItem);
|
|
}
|
|
activity?.SetTag("data.source", source);
|
|
activity?.Stop();
|
|
LogTrace($"UserDataGetFiltAsync | {source} | {activity?.Duration.TotalMilliseconds}ms");
|
|
return UsersList;
|
|
}
|
|
|
|
public async void WeekPlanDeleteAsync(WeekPlanModel currItem)
|
|
{
|
|
using var activity = ActivitySource.StartActivity("WeekPlanDeleteAsync");
|
|
activity?.SetTag("param.PlantId", currItem.PlantId);
|
|
activity?.SetTag("param.WeekPlanId", currItem.WeekPlanId);
|
|
string source = "DB+REDIS";
|
|
try
|
|
{
|
|
//dbController.ResetController();
|
|
dbController.WeekPlanDelete(currItem);
|
|
string cacheKey = mHash($"WEEKPLAN:List");
|
|
await ExecFlushRedisPattern(cacheKey);
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
string exMsg = $"Eccezione in WeekPlanDeleteAsync | PlantId: {currItem.PlantId} | WeekPlanId: {currItem.WeekPlanId}";
|
|
Log.Error($"{exMsg}{Environment.NewLine}{exc}");
|
|
// traccio errore
|
|
activity?.SetStatus(ActivityStatusCode.Error, exc.Message);
|
|
activity?.AddEvent(new ActivityEvent("exception", tags: new ActivityTagsCollection {
|
|
{ "exception.type", exc.GetType().Name },
|
|
{ "exception.message", exc.Message },
|
|
{ "exception.stacktrace", exc.StackTrace }
|
|
}));
|
|
}
|
|
activity?.SetTag("data.source", source);
|
|
activity?.Stop();
|
|
LogTrace($"WeekPlanDeleteAsync | {source} | {activity?.Duration.TotalMilliseconds}ms");
|
|
}
|
|
|
|
public async Task<List<WeekPlanModel>> WeekPlanGetAsync()
|
|
{
|
|
using var activity = ActivitySource.StartActivity("WeekPlanGetAsync");
|
|
string source = "DB";
|
|
List<WeekPlanModel> result = new List<WeekPlanModel>();
|
|
result = await dbController.GetWeekPlanAsync();
|
|
activity?.SetTag("data.source", source);
|
|
activity?.SetTag("result.count", result.Count);
|
|
activity?.Stop();
|
|
LogTrace($"WeekPlanGetAsync | {source} | {activity?.Duration.TotalMilliseconds}ms");
|
|
return result;
|
|
}
|
|
|
|
public async void WeekPlanUpdateAsync(WeekPlanModel currItem)
|
|
{
|
|
using var activity = ActivitySource.StartActivity("WeekPlanUpdateAsync");
|
|
string source = "DB+REDIS";
|
|
try
|
|
{
|
|
//dbController.ResetController();
|
|
dbController.WeekPlanUpdate(currItem);
|
|
string cacheKey = $"WEEKPLAN:List";
|
|
await ExecFlushRedisPattern(cacheKey);
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
string exMsg = $"Eccezione in WeekPlanUpdateAsync | PlantId: {currItem.PlantId} | WeekPlanId: {currItem.WeekPlanId}";
|
|
Log.Error($"{exMsg}{Environment.NewLine}{exc}");
|
|
// traccio errore
|
|
activity?.SetStatus(ActivityStatusCode.Error, exc.Message);
|
|
activity?.AddEvent(new ActivityEvent("exception", tags: new ActivityTagsCollection {
|
|
{ "exception.type", exc.GetType().Name },
|
|
{ "exception.message", exc.Message },
|
|
{ "exception.stacktrace", exc.StackTrace }
|
|
}));
|
|
}
|
|
activity?.SetTag("data.source", source);
|
|
activity?.Stop();
|
|
LogTrace($"WeekPlanUpdateAsync | {source} | {activity?.Duration.TotalMilliseconds}ms");
|
|
}
|
|
|
|
#endregion Public Methods
|
|
|
|
#region Protected Fields
|
|
|
|
protected static string connStringBBM = "";
|
|
|
|
#endregion Protected Fields
|
|
|
|
#region Protected Methods
|
|
|
|
/// <summary>
|
|
/// Hash dati EXE TASK x la macchina specificata
|
|
/// </summary>
|
|
/// <param name="idxMacchina"></param>
|
|
/// <returns></returns>
|
|
protected static string exeTaskHash(string idxMacchina)
|
|
{
|
|
return mHash($"ExeTask:{idxMacchina}");
|
|
}
|
|
|
|
/// <summary>
|
|
/// Funzione verifica eventuale creazione NUOVI ordini di consegna
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
protected async Task<bool> CheckCreateOrders()
|
|
{
|
|
bool fatto = false;
|
|
using var activity = ActivitySource.StartActivity("CheckCreateOrders");
|
|
string source = "DB+REDIS";
|
|
List<OrderModel> NewOrders = new List<OrderModel>();
|
|
DateTime adesso = DateTime.Now;
|
|
int supplierId = 1;
|
|
int transporterId = 1;
|
|
double qtyOrd = 0;
|
|
// faccio ciclo x tutti gli impianti
|
|
List<PlantDTO> currPlantData = await PlantDtoGetAllAsync();
|
|
List<WeekPlanModel> fullWeekPlan = WeekPlanGetAsync().Result;
|
|
foreach (var item in currPlantData)
|
|
{
|
|
// recupero ordini x il plant
|
|
var listOrderOpen = await OrdersGetOpenAsync(item.PlantId);
|
|
|
|
// verifico NON ci siano duplicati,
|
|
var list2del = listOrderOpen
|
|
.GroupBy(o => o.OrderCode)
|
|
.Where(g => g.Count() > 1)
|
|
.SelectMany(g => g.OrderBy(o => o.OrderId).Skip(1))
|
|
//.Select(o => o.OrderId)
|
|
.ToList();
|
|
|
|
if (list2del != null && list2del.Count > 0)
|
|
{
|
|
activity?.SetTag("duplicate.count", list2del.Count);
|
|
// ...nel caso elimino il secondo...
|
|
foreach (var ord in list2del)
|
|
{
|
|
await dbController.OrderDeleteAsync(ord);
|
|
}
|
|
var dupCodeList = list2del.Select(o => o.OrderCode).ToList();
|
|
string stringList = string.Join(", ", dupCodeList);
|
|
activity?.SetTag("duplicate.list", stringList);
|
|
}
|
|
|
|
// calcolo delta ordinato
|
|
qtyOrd = listOrderOpen.Sum(x => x.OrderQty);
|
|
// verifico il livello attuale + i valori degli ordini APERTI rispetto al livello di riordino...
|
|
if (item.LevelAct + qtyOrd < item.LevelReorder)
|
|
{
|
|
// calcolo supplier/transporter da tab WeekPlan
|
|
var dailyPlan = fullWeekPlan.Where(x => x.PlantId == item.PlantId && (int)x.DayNum == ((int)(adesso.AddDays(1).DayOfWeek))).FirstOrDefault();
|
|
if (dailyPlan != null)
|
|
{
|
|
supplierId = dailyPlan.SupplierId;
|
|
transporterId = dailyPlan.TransporterId;
|
|
}
|
|
else
|
|
{
|
|
supplierId = 1;
|
|
transporterId = 1;
|
|
}
|
|
// stacco un NUOVO ordine di quantità finita
|
|
NewOrders.Add(new OrderModel() { DtOrder = adesso, DtETA = adesso.AddDays(1), OrderQty = item.OrderQtyStd, PlantId = item.PlantId, OrderCode = $"O{item.PlantCode}{adesso:yyMMddHHmm}", OrderDesc = $"Ordine {item.PlantDesc} - {adesso} | GWMSDataService", SupplierId = supplierId, TransporterId = transporterId });
|
|
}
|
|
}
|
|
if (NewOrders.Count > 0)
|
|
{
|
|
// effettuo inserimento ordini!
|
|
dbController.OrderInsert(NewOrders);
|
|
|
|
// recupero elenco users associati al FORNITORE....
|
|
var rawUserList = UserDataGetFiltAsync("").Result;
|
|
var supplList = rawUserList
|
|
.Where(x => x.Roles.Contains("ExtUser"))
|
|
.ToList();
|
|
var transpList = rawUserList
|
|
.Where(x => x.Roles.Contains("ExtTransp"))
|
|
.ToList();
|
|
|
|
// recupero NUOVI ordini x plant...
|
|
foreach (var plant in currPlantData)
|
|
{
|
|
var newOrderOpen = await OrdersGetOpenAsync(plant.PlantId);
|
|
string emailDest = "";
|
|
string emailSubj = "";
|
|
string emailBody = "";
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
// invio email x ogni fornitore / trasportatore x i NUOVI ordini inseriti
|
|
foreach (var item in newOrderOpen)
|
|
{
|
|
// FORNITORI!
|
|
emailSubj = $"GWMS: Ordine FORNITURA GAS per {item.Plant.PlantDesc}";
|
|
sb = new StringBuilder();
|
|
sb.AppendLine($"<h3>Fornitore {item.Supplier.SupplierDesc}</h3>");
|
|
sb.AppendLine($"<b>Trasportatore {item.Transporter.TransporterDesc}</b>");
|
|
sb.AppendLine($"Impianto di destinazione {item.Plant.PlantDesc}");
|
|
sb.AppendLine("");
|
|
sb.AppendLine($"Ordine di consegna: {item.OrderCode}");
|
|
sb.AppendLine($"Data ordine: {item.DtOrder}");
|
|
sb.AppendLine($"Data consegna indicativa: {item.DtETA}");
|
|
sb.AppendLine($"Quantità ordinata: {item.OrderQty}");
|
|
emailBody = sb.ToString().Replace(Environment.NewLine, "<br/>");
|
|
|
|
System.Security.Claims.Claim suppClaim = new System.Security.Claims.Claim("SupplierId", $"{item.SupplierId}");
|
|
|
|
// recupero elenco users associati al FORNITORE....
|
|
var userListSupp = supplList
|
|
.Where(x => x.Claims.Where(c => c.Type == "SupplierId" && c.Value == $"{item.SupplierId}").Count() > 0)
|
|
.ToList();
|
|
|
|
foreach (var user in userListSupp)
|
|
{
|
|
// invio email di notifica nuovi ordini inseriti
|
|
emailDest = user.Identity.Email;
|
|
//emailDest = user.Email;
|
|
// invio!
|
|
await _emailSender.SendEmailAsync(emailDest, emailSubj, emailBody);
|
|
_logger.LogInformation($"Email sento to SUPPLIER {emailDest}!");
|
|
}
|
|
|
|
// TRASPORTATORI!
|
|
emailSubj = $"GWMS: Ordine TRASPORTO GAS per {item.Plant.PlantDesc}";
|
|
sb = new StringBuilder();
|
|
sb.AppendLine($"<h3>Trasportatore {item.Transporter.TransporterDesc}</h3>");
|
|
sb.AppendLine($"<b>Fornitore {item.Supplier.SupplierDesc}</b>");
|
|
sb.AppendLine($"Impianto di destinazione {item.Plant.PlantDesc}");
|
|
sb.AppendLine("");
|
|
sb.AppendLine($"Ordine di consegna: {item.OrderCode}");
|
|
sb.AppendLine($"Data ordine: {item.DtOrder}");
|
|
sb.AppendLine($"Data consegna indicativa: {item.DtETA}");
|
|
sb.AppendLine($"Quantità ordinata: {item.OrderQty}");
|
|
sb.AppendLine("");
|
|
sb.AppendLine($"Note: {item.OrderDesc}");
|
|
emailBody = sb.ToString().Replace(Environment.NewLine, "<br/>");
|
|
|
|
// recupero elenco users associati al TRASPORTATORE....
|
|
var userListTransp = transpList
|
|
.Where(x => x.Claims.Where(c => c.Type == "TransporterId" && c.Value == $"{item.TransporterId}").Count() > 0)
|
|
.ToList();
|
|
foreach (var user in userListTransp)
|
|
{
|
|
// invio email di notifica nuovi ordini inseriti
|
|
emailDest = user.Identity.Email;
|
|
//emailDest = user.Email;
|
|
// invio!
|
|
await _emailSender.SendEmailAsync(emailDest, emailSubj, emailBody);
|
|
_logger.LogInformation($"Email sento to TRANSPORTER {emailDest}!");
|
|
}
|
|
}
|
|
}
|
|
fatto = true;
|
|
}
|
|
activity?.SetTag("data.source", source);
|
|
activity?.Stop();
|
|
LogTrace($"CheckCreateOrders | {source} | {activity?.Duration.TotalMilliseconds} ms");
|
|
return fatto;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Esegue flush memoria redis dato pattern
|
|
/// </summary>
|
|
/// <param name="pattern"></param>
|
|
/// <returns></returns>
|
|
protected async Task<bool> ExecFlushRedisPattern(RedisValue pattern)
|
|
{
|
|
bool answ = false;
|
|
/*******************************
|
|
* Recupero elenco dei server da cui cancellare le chaivi:
|
|
* - prendo solo i server connessi
|
|
* - prendo solo NON repliche (= master)
|
|
* - me ne aspetto 1 in uscita cmq
|
|
*******************************/
|
|
var connServ = redisConn.GetEndPoints()
|
|
.Select(endpoint =>
|
|
{
|
|
var server = redisConn.GetServer(endpoint);
|
|
return server;
|
|
})
|
|
.Where(x => x.IsConnected && !x.IsReplica)
|
|
.ToList();
|
|
|
|
// ciclo (anche se me ne aspetto 1 solo)
|
|
foreach (var mServer in connServ)
|
|
{
|
|
try
|
|
{
|
|
var keyList = mServer.Keys(redisDb.Database, pattern);
|
|
if (numPar > 0)
|
|
{
|
|
var options = new ParallelOptions { MaxDegreeOfParallelism = numPar };
|
|
await Parallel.ForEachAsync(keyList, async (item, token) =>
|
|
{
|
|
// cancello
|
|
await redisDb.KeyDeleteAsync(item);
|
|
});
|
|
}
|
|
else
|
|
{
|
|
foreach (var item in keyList)
|
|
{
|
|
await redisDb.KeyDeleteAsync(item);
|
|
}
|
|
}
|
|
answ = true;
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error($"Eccezione durante ExecFlushRedisPattern | pattern: {pattern}{Environment.NewLine}{exc}");
|
|
}
|
|
}
|
|
return answ;
|
|
}
|
|
|
|
protected string getCacheKey(string TableName, SelectOrderData CurrFilter)
|
|
{
|
|
string answ = $"{TableName}:P_{CurrFilter.PlantId:00}:S_{CurrFilter.SupplierId:00}:T_{CurrFilter.TransporterId:00}:D_{CurrFilter.DateStart:yyyyMMddHHmm}_{CurrFilter.DateEnd:yyyyMMddHHmm}";
|
|
return answ;
|
|
}
|
|
|
|
protected async Task<List<PlantLogModel>> PlantLogGetLastByFlux(int PlantId)
|
|
{
|
|
string source = "DB";
|
|
List<PlantLogModel>? lastValues = new List<PlantLogModel>();
|
|
// cerco in cache
|
|
string cacheKey = mHash($"PLANTS:LastFlux:{PlantId}");
|
|
Stopwatch sw = new Stopwatch();
|
|
sw.Start();
|
|
string rawData;
|
|
rawData = await redisDb.StringGetAsync(cacheKey);
|
|
if (!string.IsNullOrEmpty(rawData))
|
|
{
|
|
source = "REDIS";
|
|
var tempResult = JsonConvert.DeserializeObject<List<PlantLogModel>>(rawData);
|
|
if (tempResult == null)
|
|
{
|
|
lastValues = new List<PlantLogModel>();
|
|
}
|
|
else
|
|
{
|
|
lastValues = tempResult;
|
|
}
|
|
}
|
|
// altrimenti DB e salvo...
|
|
else
|
|
{
|
|
lastValues = dbController.PlantLogGetLastByFlux(PlantId);
|
|
rawData = JsonConvert.SerializeObject(lastValues);
|
|
await redisDb.StringSetAsync(cacheKey, rawData, UltraLongCache);
|
|
}
|
|
sw.Stop();
|
|
Log.Debug($"PlantLogGetLastByFlux | {source} | {sw.ElapsedMilliseconds} ms");
|
|
return await Task.FromResult(lastValues);
|
|
}
|
|
|
|
#endregion Protected Methods
|
|
|
|
#region Private Fields
|
|
|
|
/// <summary>
|
|
/// Oggetto per collezione dati Activity (span in Uptrace)
|
|
/// </summary>
|
|
private static readonly ActivitySource ActivitySource = new ActivitySource("GWMS.Data");
|
|
|
|
private static IConfiguration _configuration;
|
|
|
|
private static ILogger<GWMSDataService> _logger;
|
|
|
|
private static JsonSerializerSettings? JSSettings;
|
|
|
|
private static NLog.Logger Log = LogManager.GetCurrentClassLogger();
|
|
|
|
private readonly IEmailSender _emailSender;
|
|
|
|
private readonly UserManager<IdentityUser> _userManager;
|
|
|
|
/// <summary>
|
|
/// Durata cache lunga IN SECONDI
|
|
/// </summary>
|
|
private int cacheTtlLong = 60 * 5;
|
|
|
|
/// <summary>
|
|
/// Durata cache breve IN SECONDI
|
|
/// </summary>
|
|
private int cacheTtlShort = 60 * 1;
|
|
|
|
/// <summary>
|
|
/// Numero di operazioni parallele che si possono svolgere... (se 0 NON usa cicli paralleli)
|
|
/// </summary>
|
|
private int numPar = 0;
|
|
|
|
/// <summary>
|
|
/// Oggetto per connessione a REDIS
|
|
/// </summary>
|
|
private IConnectionMultiplexer redisConn;
|
|
|
|
/// <summary>
|
|
/// Oggetto DB redis da impiegare x chiamate R/W
|
|
/// </summary>
|
|
private IDatabase redisDb = null!;
|
|
|
|
/// <summary>
|
|
/// Generatore random
|
|
/// </summary>
|
|
private Random rnd = new Random();
|
|
|
|
private bool traceEnabled = false;
|
|
|
|
#endregion Private Fields
|
|
|
|
#region Private Properties
|
|
|
|
private string CodApp { get; set; } = "";
|
|
|
|
/// <summary>
|
|
/// Durata cache breve (1 min circa + perturbazione percentuale +/-10%)
|
|
/// </summary>
|
|
private TimeSpan FastCache
|
|
{
|
|
get => TimeSpan.FromSeconds(cacheTtlShort * rnd.Next(900, 1100) / 1000);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Durata cache lunga (+ perturbazione percentuale +/-10%)
|
|
/// </summary>
|
|
private TimeSpan LongCache
|
|
{
|
|
get => TimeSpan.FromSeconds(cacheTtlLong * rnd.Next(900, 1100) / 1000);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Durata cache MOLTO breve (10 sec circa + perturbazione percentuale +/-10%)
|
|
/// </summary>
|
|
private TimeSpan UltraFastCache
|
|
{
|
|
get => TimeSpan.FromSeconds(cacheTtlShort / 6 * rnd.Next(900, 1100) / 1000);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Durata cache MOLTO lunga (+ perturbazione percentuale +/-10%)
|
|
/// </summary>
|
|
private TimeSpan UltraLongCache
|
|
{
|
|
get => TimeSpan.FromSeconds(cacheTtlLong * 10 * rnd.Next(900, 1100) / 1000);
|
|
}
|
|
|
|
#endregion Private Properties
|
|
|
|
#region Private Methods
|
|
|
|
/// <summary>
|
|
/// Hash dati MemoryMap x la macchina specificata
|
|
/// </summary>
|
|
/// <param name="idxMacchina"></param>
|
|
/// <returns></returns>
|
|
private static string currMemMapHash(string idxMacchina)
|
|
{
|
|
return mHash($"MemMap:{idxMacchina}");
|
|
}
|
|
|
|
/// <summary>
|
|
/// Hash dati CurrentParameters x la macchina specificata
|
|
/// </summary>
|
|
/// <param name="idxMacchina"></param>
|
|
/// <returns></returns>
|
|
private static string currParametersHash(string idxMacchina)
|
|
{
|
|
return mHash($"CurrentParameters:{idxMacchina}");
|
|
}
|
|
|
|
/// <summary>
|
|
/// Hash Redis contenente i dati MP di una specifico TYPE (es StatusMacchina,
|
|
/// StateMachineIngressi, ...)
|
|
/// </summary>
|
|
/// <param name="dataType"></param>
|
|
/// <returns></returns>
|
|
private static string mHash(string dataType)
|
|
{
|
|
return $"{Const.rKeyConfig}:DATA:{dataType}";
|
|
}
|
|
|
|
/// <summary>
|
|
/// Hash dati OPT PARAMETERS x la macchina specificata
|
|
/// </summary>
|
|
/// <param name="idxMacchina"></param>
|
|
/// <returns></returns>
|
|
private static string optParHash(string idxMacchina)
|
|
{
|
|
return mHash($"OptPar:{idxMacchina}");
|
|
}
|
|
|
|
/// <summary>
|
|
/// Hash dati SAVED (EXE) TASK x la macchina specificata x poter ripristinare in caso di
|
|
/// perdita valore WRITE
|
|
/// </summary>
|
|
/// <param name="idxMacchina"></param>
|
|
/// <returns></returns>
|
|
private static string savedTaskHash(string idxMacchina)
|
|
{
|
|
return mHash($"SavedTask:{idxMacchina}");
|
|
}
|
|
|
|
/// <summary>
|
|
/// Helper trace messaggio log (SE abilitato)
|
|
/// </summary>
|
|
/// <param name="traceMsg"></param>
|
|
private void LogTrace(string traceMsg, NLog.LogLevel? reqLevel = null)
|
|
{
|
|
if (!traceEnabled)
|
|
return;
|
|
|
|
reqLevel ??= NLog.LogLevel.Debug;
|
|
|
|
// Loggo!
|
|
Log.Log(reqLevel, traceMsg);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Recupera PlantDTO e aggiorno valori attuali (se presente...)
|
|
/// ATTENZIONE: i dati sono sempre ricevuti PER SINGOLO PlantId!!!
|
|
/// </summary>
|
|
/// <param name="newItems"></param>
|
|
private async Task updateCurrDTO(List<PlantLogModel> newItems)
|
|
{
|
|
string source = "DB";
|
|
DateTime adesso = DateTime.Now;
|
|
List<PlantDTO> dbResult = new List<PlantDTO>();
|
|
int PlantId = newItems.FirstOrDefault().PlantId;
|
|
string cacheKey = mHash("PLANTS:ListDTO");
|
|
string rawData;
|
|
rawData = await redisDb.StringGetAsync(cacheKey);
|
|
if (!string.IsNullOrEmpty(rawData))
|
|
{
|
|
source = "REDIS";
|
|
var tempResult = JsonConvert.DeserializeObject<List<PlantDTO>>(rawData);
|
|
if (tempResult == null)
|
|
{
|
|
dbResult = new List<PlantDTO>();
|
|
}
|
|
else
|
|
{
|
|
dbResult = tempResult;
|
|
}
|
|
|
|
// ora ciclo x ogni flusso/macchina l'ultimo record
|
|
List<PlantLogModel> lastByFlux = newItems
|
|
.OrderByDescending(x => x.DtEvent)
|
|
.GroupBy(g => g.FluxType)
|
|
.Select(s => s.First())
|
|
.ToList();
|
|
// aggiorno il DTO x i valori trovati...
|
|
var currDto = dbResult.Where(x => x.PlantId == PlantId).FirstOrDefault();
|
|
|
|
// verifico SE c'è Level
|
|
var lastLev = lastByFlux.Where(x => x.FluxType == "Level").FirstOrDefault();
|
|
if (lastLev != null)
|
|
{
|
|
currDto.LevelAct = lastLev.ValNumber;
|
|
currDto.LastUpdate = adesso;
|
|
}
|
|
|
|
// verifico SE c'è MainPress
|
|
var lastMain = lastByFlux.Where(x => x.FluxType == "MainPress").FirstOrDefault();
|
|
if (lastMain != null)
|
|
{
|
|
if (currDto.PressAct.ContainsKey("Main"))
|
|
{
|
|
currDto.PressAct["Main"] = lastMain.ValNumber;
|
|
}
|
|
else
|
|
{
|
|
currDto.PressAct.Add("Main", lastMain.ValNumber);
|
|
}
|
|
}
|
|
|
|
// verifico SE c'è PressBH
|
|
var lastBH = lastByFlux.Where(x => x.FluxType == "PressBH").FirstOrDefault();
|
|
if (lastBH != null)
|
|
{
|
|
if (currDto.PressAct.ContainsKey("BH"))
|
|
{
|
|
currDto.PressAct["BH"] = lastBH.ValNumber;
|
|
}
|
|
else
|
|
{
|
|
currDto.PressAct.Add("BH", lastBH.ValNumber);
|
|
}
|
|
}
|
|
|
|
// verifico SE c'è PressBL
|
|
var lastBL = lastByFlux.Where(x => x.FluxType == "PressBL").FirstOrDefault();
|
|
if (lastBL != null)
|
|
{
|
|
if (currDto.PressAct.ContainsKey("BL"))
|
|
{
|
|
currDto.PressAct["BL"] = lastBL.ValNumber;
|
|
}
|
|
else
|
|
{
|
|
currDto.PressAct.Add("BL", lastBL.ValNumber);
|
|
}
|
|
}
|
|
|
|
// salvo DTO!
|
|
rawData = JsonConvert.SerializeObject(dbResult);
|
|
await redisDb.StringSetAsync(cacheKey, rawData, UltraLongCache);
|
|
}
|
|
}
|
|
|
|
#endregion Private Methods
|
|
}
|
|
} |