192 lines
7.8 KiB
C#
192 lines
7.8 KiB
C#
using Microsoft.AspNetCore.Mvc;
|
|
using MP.Data.Conf;
|
|
using MP.Data.DatabaseModels;
|
|
using MP.WASM.Mon.Data;
|
|
using Newtonsoft.Json;
|
|
using NLog;
|
|
using StackExchange.Redis;
|
|
|
|
namespace MP.WASM.Mon.Server.Controllers
|
|
{
|
|
[ApiController]
|
|
[Route("api/[controller]")]
|
|
public class MSEController : ControllerBase
|
|
{
|
|
#region Public Constructors
|
|
|
|
|
|
/// <summary>
|
|
/// Dataservice x accesso DB
|
|
/// </summary>
|
|
protected MpDataService _DataService { get; set; }
|
|
|
|
/// <summary>
|
|
/// Avvio controller dei dati MSE
|
|
/// </summary>
|
|
/// <param name="DataService"></param>
|
|
public MSEController(MpDataService DataService, IConfiguration configuration, ILogger<MSEController> logger)
|
|
{
|
|
_logger = logger;
|
|
_configuration = configuration;
|
|
_DataService = DataService;
|
|
Log.Info("Avviata classe MSEController");
|
|
// setup conf IOB da dizionario
|
|
tryLoadIobTags();
|
|
}
|
|
|
|
#endregion Public Constructors
|
|
|
|
#region Public Properties
|
|
|
|
/// <summary>
|
|
/// Dizionario dei tag configurati per IOB
|
|
/// </summary>
|
|
public Dictionary<string, List<TagData>> currTagConf { get; set; } = new Dictionary<string, List<TagData>>();
|
|
|
|
#endregion Public Properties
|
|
|
|
#region Public Methods
|
|
|
|
[HttpGet("getConfig")]
|
|
public Task<List<ConfigModel>> ConfigGetAll()
|
|
{
|
|
return _DataService.ConfigGetAll();
|
|
}
|
|
|
|
/// <summary>
|
|
/// restituisce il valore da REDIS associato al tag richeisto
|
|
/// </summary>
|
|
/// <param name="redKey">Chiave in cui cercare il valore</param>
|
|
/// <returns></returns>
|
|
[HttpGet("getTagConf/{redKey}")]
|
|
public string getTagConf(string redKey)
|
|
{
|
|
return _DataService.getTagConf(redKey);
|
|
}
|
|
|
|
[HttpGet("getMachines")]
|
|
public Task<List<Macchine>> MacchineGetAll()
|
|
{
|
|
return _DataService.MacchineGetAll();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Metodo principale x ricevere tutte i dati MSE di tutti gli impianti
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
[HttpGet()]
|
|
public Task<List<MappaStatoExpl>> MseGetAll()
|
|
{
|
|
return _DataService.MseGetAll();
|
|
}
|
|
|
|
#endregion Public Methods
|
|
|
|
#region Private Fields
|
|
|
|
private static IConfiguration _configuration = null!;
|
|
private static NLog.Logger Log = LogManager.GetCurrentClassLogger();
|
|
|
|
//private static ILogger<MSEController> _logger = null!;
|
|
private readonly ILogger<MSEController> _logger;
|
|
|
|
/// <summary>
|
|
/// Oggetto per connessione a REDIS
|
|
/// </summary>
|
|
private ConnectionMultiplexer redisConn = null!;
|
|
|
|
/// <summary>
|
|
/// Oggetto DB redis da impiegare x chiamate R/W
|
|
/// </summary>
|
|
private IDatabase redisDb = null!;
|
|
|
|
#endregion Private Fields
|
|
|
|
#region Private Methods
|
|
|
|
/// <summary>
|
|
/// Prova a caricare da file la conf degli IOB se presente
|
|
/// </summary>
|
|
private void tryLoadIobTags()
|
|
{
|
|
Dictionary<string, List<TagData>> currConf = new Dictionary<string, List<TagData>>();
|
|
string strExeFilePath = System.Reflection.Assembly.GetExecutingAssembly().Location;
|
|
if (!string.IsNullOrEmpty(strExeFilePath))
|
|
{
|
|
string? strWorkPath = Path.GetDirectoryName(strExeFilePath);
|
|
if (!string.IsNullOrEmpty(strWorkPath))
|
|
{
|
|
string filePath = $"{strWorkPath}/Conf/iobTagsConf.json";
|
|
if (System.IO.File.Exists(filePath))
|
|
{
|
|
string rawData = System.IO.File.ReadAllText(filePath);
|
|
if (!string.IsNullOrEmpty(rawData))
|
|
{
|
|
var fileConfData = JsonConvert.DeserializeObject<IobTags>(rawData);
|
|
if (fileConfData != null)
|
|
{
|
|
// effettuo esplosione conf SE contenesse il valore "***" = tutti
|
|
// gli IOB
|
|
if (fileConfData.IobSetup.ContainsKey("***"))
|
|
{
|
|
// recupero elenco macchine...
|
|
var elencoMacc = _DataService.MacchineGetAll().Result;
|
|
// x ogni macchina creo le righe standard da conf...
|
|
var baseConf = fileConfData.IobSetup.Where(x => x.Key == "***").FirstOrDefault();
|
|
foreach (var item in elencoMacc)
|
|
{
|
|
if (!string.IsNullOrEmpty(item.IdxMacchina))
|
|
{
|
|
// converto i valori x la macchina corrente... clono in
|
|
// nuovo oggetto
|
|
var specVal = baseConf.Value.Select(i => i.Clone()).ToList();
|
|
// sostituisco segnaposto
|
|
foreach (var singleVal in specVal)
|
|
{
|
|
singleVal.TagLocation = singleVal.TagLocation.Replace("***", item.IdxMacchina);
|
|
}
|
|
// ora aggiungo eventuali valori in override...
|
|
if (fileConfData.IobSetup.ContainsKey(item.IdxMacchina))
|
|
{
|
|
var otConf = fileConfData.IobSetup.Where(x => x.Key == item.IdxMacchina).FirstOrDefault();
|
|
//verifico x ogni valore other...
|
|
foreach (var otTag in otConf.Value)
|
|
{
|
|
var ovrTag = specVal.Where(x => x.ColNum == otTag.ColNum && x.RowNum == otTag.RowNum).FirstOrDefault();
|
|
// se contiene --> sovrascrivo
|
|
if (ovrTag != null)
|
|
{
|
|
//ovrTag = otTag.Clone();
|
|
specVal.Remove(ovrTag);
|
|
specVal.Add(otTag.Clone());
|
|
}
|
|
// se non contiene --> aggiungo
|
|
else
|
|
{
|
|
specVal.Add(otTag);
|
|
}
|
|
}
|
|
}
|
|
currConf.Add(item.IdxMacchina, specVal);
|
|
}
|
|
}
|
|
}
|
|
// altrimenti copio ed ho finito
|
|
else
|
|
{
|
|
currConf = fileConfData.IobSetup;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
if (currConf != null)
|
|
{
|
|
currTagConf = currConf;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
#endregion Private Methods
|
|
}
|
|
} |