Predisposti controller e data service
This commit is contained in:
@@ -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()
|
||||
{ }
|
||||
|
||||
/// <summary>
|
||||
/// Aggiunta in bulk delle transizioni/ingressi
|
||||
/// </summary>
|
||||
/// <param name="newTIRecs"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<bool> TranInInsert(List<TransizioneIngressiTempModel> 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -36,7 +36,7 @@ namespace SMGen.Data
|
||||
public SMGDataContext(DbContextOptions<SMGDataContext> options, IConfiguration configuration)
|
||||
{
|
||||
_configuration = configuration;
|
||||
bool disableMigrate = _configuration.GetValue<bool>("SetupOpt:DisableMigrate");
|
||||
bool disableMigrate = _configuration.GetValue<bool>("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<bool>("SetupOpt:DisableMigrate");
|
||||
bool disableMigrate = _configuration.GetValue<bool>("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);
|
||||
|
||||
@@ -21,6 +21,7 @@
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration" Version="6.0.1" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="6.0.0" />
|
||||
<PackageReference Include="NLog" Version="5.2.2" />
|
||||
<PackageReference Include="StackExchange.Redis" Version="2.6.122" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
@@ -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();
|
||||
|
||||
/// <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>
|
||||
/// Oggetto per connessione a REDIS
|
||||
/// </summary>
|
||||
private IConnectionMultiplexer redisConn;
|
||||
|
||||
/// <summary>
|
||||
/// Oggetto DB redis da impiegare x chiamate R/W
|
||||
/// </summary>
|
||||
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<bool> TranInInsert(List<TransizioneIngressiTempModel> newTIList)
|
||||
{
|
||||
var dbResult = await dbController.TranInInsert(newTIList);
|
||||
return dbResult;
|
||||
}
|
||||
|
||||
|
||||
#region Private Properties
|
||||
|
||||
/// <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 lunga (+ perturbazione percentuale +/-10%)
|
||||
/// </summary>
|
||||
private TimeSpan UltraLongCache
|
||||
{
|
||||
get => TimeSpan.FromSeconds(cacheTtlLong * 10 * rnd.Next(900, 1100) / 1000);
|
||||
}
|
||||
|
||||
#endregion Private Properties
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
{
|
||||
|
||||
@@ -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": "*"
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user