diff --git a/SMGen.Data/Controllers/SMGenController.cs b/SMGen.Data/Controllers/SMGenController.cs new file mode 100644 index 0000000..25ed44a --- /dev/null +++ b/SMGen.Data/Controllers/SMGenController.cs @@ -0,0 +1,47 @@ +using Microsoft.Extensions.Configuration; +using NLog; +using SMGen.Data.DbModels; + +namespace SMGen.Data.Controllers +{ + public class SMGenController : IDisposable + { + private static IConfiguration _configuration; + + private static NLog.Logger Log = LogManager.GetCurrentClassLogger(); + + public SMGenController(IConfiguration configuration) + { + _configuration = configuration; + } + + public void Dispose() + { } + + /// + /// Aggiunta in bulk delle transizioni/ingressi + /// + /// + /// + public async Task TranInInsert(List newTIRecs) + { + bool fatto = false; + using (SMGDataContext localDbCtx = new SMGDataContext(_configuration)) + { + try + { + localDbCtx + .DbSetTranIngTemp + .AddRange(newTIRecs); + await localDbCtx.SaveChangesAsync(); + fatto = true; + } + catch (Exception exc) + { + Log.Error($"Eccezione durante TranInInsert: {Environment.NewLine}{exc}"); + } + } + return fatto; + } + } +} \ No newline at end of file diff --git a/SMGen.Data/SMGDataContext.cs b/SMGen.Data/SMGDataContext.cs index dd1bee3..4e57827 100644 --- a/SMGen.Data/SMGDataContext.cs +++ b/SMGen.Data/SMGDataContext.cs @@ -36,7 +36,7 @@ namespace SMGen.Data public SMGDataContext(DbContextOptions options, IConfiguration configuration) { _configuration = configuration; - bool disableMigrate = _configuration.GetValue("SetupOpt:DisableMigrate"); + bool disableMigrate = _configuration.GetValue("SetupOpt:DisableSMGMigrate"); if (!disableMigrate) { try @@ -56,7 +56,7 @@ namespace SMGen.Data public void DbForceMigrate() { // verifico SE devo eseguire la migration del DB IDENT... - bool disableMigrate = _configuration.GetValue("SetupOpt:DisableMigrate"); + bool disableMigrate = _configuration.GetValue("SetupOpt:DisableSMGMigrate"); if (!disableMigrate) { try @@ -76,7 +76,7 @@ namespace SMGen.Data { if (!optionsBuilder.IsConfigured) { - string connString = _configuration.GetConnectionString("WDC.DB"); + string connString = _configuration.GetConnectionString("SMGen.DB"); if (!string.IsNullOrEmpty(connString)) { optionsBuilder.UseSqlServer(connString); diff --git a/SMGen.Data/SMGen.Data.csproj b/SMGen.Data/SMGen.Data.csproj index 88c10e8..6ce1af5 100644 --- a/SMGen.Data/SMGen.Data.csproj +++ b/SMGen.Data/SMGen.Data.csproj @@ -21,6 +21,7 @@ + diff --git a/SMGen.Data/Services/SMGDataService.cs b/SMGen.Data/Services/SMGDataService.cs new file mode 100644 index 0000000..6da31d6 --- /dev/null +++ b/SMGen.Data/Services/SMGDataService.cs @@ -0,0 +1,118 @@ +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Configuration; +using Newtonsoft.Json; +using NLog; +using SMGen.Data.Controllers; +using SMGen.Data.DbModels; +using StackExchange.Redis; + +namespace SMGen.Data.Services +{ + public class SMGDataService + { + public static SMGenController dbController = null!; + + private static IConfiguration _configuration = null!; + + private static JsonSerializerSettings? JSSettings; + + private static Logger Log = LogManager.GetCurrentClassLogger(); + + /// + /// Durata cache lunga IN SECONDI + /// + private int cacheTtlLong = 60 * 5; + + /// + /// Durata cache breve IN SECONDI + /// + private int cacheTtlShort = 60 * 1; + + /// + /// Oggetto per connessione a REDIS + /// + private IConnectionMultiplexer redisConn; + + /// + /// Oggetto DB redis da impiegare x chiamate R/W + /// + private IDatabase redisDb = null!; + + private Random rnd = new Random(); + + public SMGDataService(IConfiguration configuration, IConnectionMultiplexer redisConnMult) + { + _configuration = configuration; + // 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["CodApp"]; + // Conf DB + string connStr = _configuration.GetConnectionString("WDC.DB"); + if (string.IsNullOrEmpty(connStr)) + { + Log.Info("ConnString empty!"); + } + else + { + dbController = new SMGenController(configuration); + } + + // chiudo log + Log.Info("Avviata classe WebDoorCreatorService"); + } + public string CodApp { get; set; } = ""; + + + public async Task TranInInsert(List newTIList) + { + var dbResult = await dbController.TranInInsert(newTIList); + return dbResult; + } + + + #region Private Properties + + /// + /// Durata cache breve (1 min circa + perturbazione percentuale +/-10%) + /// + private TimeSpan FastCache + { + get => TimeSpan.FromSeconds(cacheTtlShort * rnd.Next(900, 1100) / 1000); + } + + /// + /// Durata cache lunga (+ perturbazione percentuale +/-10%) + /// + private TimeSpan LongCache + { + get => TimeSpan.FromSeconds(cacheTtlLong * rnd.Next(900, 1100) / 1000); + } + + /// + /// Durata cache molto breve (10 sec circa + perturbazione percentuale +/-10%) + /// + private TimeSpan UltraFastCache + { + get => TimeSpan.FromSeconds(cacheTtlShort / 6 * rnd.Next(900, 1100) / 1000); + } + + /// + /// Durata cache lunga (+ perturbazione percentuale +/-10%) + /// + private TimeSpan UltraLongCache + { + get => TimeSpan.FromSeconds(cacheTtlLong * 10 * rnd.Next(900, 1100) / 1000); + } + + #endregion Private Properties + } +} \ No newline at end of file diff --git a/SMGen/Pages/SMIn2Event.razor.cs b/SMGen/Pages/SMIn2Event.razor.cs index aa6b831..b9b4d19 100644 --- a/SMGen/Pages/SMIn2Event.razor.cs +++ b/SMGen/Pages/SMIn2Event.razor.cs @@ -157,10 +157,6 @@ namespace SMGen.Pages sw.WriteLine(sz_line); } - //else - //{ - // sz_line = $"----{n_state_machine_index};{i};{n_input};{Events_to_send.IndexOf(act_rule.event_to_send)};{States.IndexOf(act_rule.next_state)}"; - //} exitFor = true; } } @@ -170,12 +166,6 @@ namespace SMGen.Pages } filesStatus[origFileName] = true; files2Download.Add(origFileName, sz_file2Write); - //var files2Del = Directory.GetFiles(sz_file2Read); - - //foreach (var item in files2Del) - //{ - // File.Delete(item); - //} } catch { diff --git a/SMGen/appsettings.json b/SMGen/appsettings.json index 6552f87..dd07529 100644 --- a/SMGen/appsettings.json +++ b/SMGen/appsettings.json @@ -10,7 +10,8 @@ "SMGen.DB": "Server=SQL2016DEV;Database=MoonPro; User ID=sa;Password=keyhammer16; integrated security=False; MultipleActiveResultSets=True; App=SMGen.UI;" }, "ServerConf": { - "ProcCsvRootPath": "Temp\\Rules\\PROCESSED" + "ProcCsvRootPath": "Temp\\Rules\\PROCESSED", + "DisableSMGMigrate": true }, "AllowedHosts": "*" }