Files
2022-10-25 12:02:23 +02:00

323 lines
12 KiB
C#

using MP.MONO.Core.CONF;
using MP.MONO.Core.DTO;
using Newtonsoft.Json;
using NLog;
using StackExchange.Redis;
namespace MP.MONO.Core
{
public class ConfigManager
{
#region Public Constructors
public ConfigManager(string redisConf, string confDirPath)
{
confPath = confDirPath;
ConnectionMultiplexer.SetFeatureFlag("preventthreadtheft", true);
ConnectionMultiplexer redis = ConnectionMultiplexer.Connect(redisConf);
redisDb = redis.GetDatabase();
}
#endregion Public Constructors
#region Public Properties
/// <summary>
/// Verifica se alarm log sia attivato (da DECODER; in REDIS)
/// </summary>
public bool AlarmLogActive
{
get
{
bool answ = false;
string rawVal = redisDb.StringGet(Constants.ALARM_LOG_ENABLED);
if (!string.IsNullOrEmpty(rawVal))
{
bool.TryParse(rawVal, out answ);
}
return answ;
}
set => redisDb.StringSet(Constants.ALARM_LOG_ENABLED, $"{value}");
}
#endregion Public Properties
#region Public Methods
public List<DisplayDataDTO> getActLog(string fileName = "ActLog.json")
{
List<DisplayDataDTO>? currConf = null;
string fullPath = Path.Combine(confPath, fileName);
if (File.Exists(fullPath))
{
var rawData = File.ReadAllText(fullPath);
if (!string.IsNullOrEmpty(rawData))
{
currConf = JsonConvert.DeserializeObject<List<DisplayDataDTO>>(rawData);
// salvo in redis!
redisDb.StringSetAsync(Constants.ACTLOG_CONF_KEY, JsonConvert.SerializeObject(currConf));
}
}
if (currConf == null)
{
currConf = new List<DisplayDataDTO>();
}
return currConf;
}
public List<BaseAlarmBankConf> getAlarmsBankConf(string fileName = "AlarmBankList.json")
{
List<BaseAlarmBankConf>? currConf = null;
// leggo e salvo conf stati
string fullPath = Path.Combine(confPath, fileName);
if (File.Exists(fullPath))
{
var rawData = File.ReadAllText(fullPath);
if (!string.IsNullOrEmpty(rawData))
{
currConf = JsonConvert.DeserializeObject<List<BaseAlarmBankConf>>(rawData);
if (currConf != null)
{
// sistemo allarmi
foreach (var item in currConf)
{
item.setupData();
// loggo
Log.Info($"Decodifica aree BankBit: {item.description} | {item.memAddr} x {item.size} byte | {item.messages.Count} messaggi allarme", true, true);
}
}
// salvo in redis!
redisDb.StringSetAsync(Constants.ALARMS_CONF_BBIT_KEY, JsonConvert.SerializeObject(currConf));
}
}
if (currConf == null)
{
currConf = new List<BaseAlarmBankConf>();
}
return currConf;
}
public List<DisplayDataDTO> getAlarmsConf(string fileName = "Alarms.json")
{
List<DisplayDataDTO>? currConf = null;
string fullPath = Path.Combine(confPath, fileName);
if (File.Exists(fullPath))
{
var rawData = File.ReadAllText(fullPath);
if (!string.IsNullOrEmpty(rawData))
{
currConf = JsonConvert.DeserializeObject<List<DisplayDataDTO>>(rawData);
// salvo in redis!
redisDb.StringSet(Constants.ALARMS_ADAP_CONF_KEY, JsonConvert.SerializeObject(currConf));
}
}
if (currConf == null)
{
currConf = new List<DisplayDataDTO>();
}
return currConf;
}
public List<BaseAlarmRawConf> getAlarmsRawConf(string fileName = "AlarmRawList.json")
{
List<BaseAlarmRawConf>? currConf = null;
// leggo e salvo conf stati
string fullPath = Path.Combine(confPath, fileName);
if (File.Exists(fullPath))
{
var rawData = File.ReadAllText(fullPath);
if (!string.IsNullOrEmpty(rawData))
{
currConf = JsonConvert.DeserializeObject<List<BaseAlarmRawConf>>(rawData);
if (currConf != null)
{
// sistemo allarmi
foreach (var item in currConf)
{
// loggo
Log.Info($"Decodifica aree AlarmRaw | source: {item.source} | {item.messages.Count} messaggi allarme", true, true);
}
}
// salvo in redis!
redisDb.StringSetAsync(Constants.ALARMS_CONF_RLIST_KEY, JsonConvert.SerializeObject(currConf));
}
}
if (currConf == null)
{
currConf = new List<BaseAlarmRawConf>();
}
return currConf;
}
public List<DisplayDataDTO> getCountersConf(string fileName = "Counters.json")
{
List<DisplayDataDTO>? currConf = null;
string fullPath = Path.Combine(confPath, fileName);
if (File.Exists(fullPath))
{
var rawData = File.ReadAllText(fullPath);
if (!string.IsNullOrEmpty(rawData))
{
currConf = JsonConvert.DeserializeObject<List<DisplayDataDTO>>(rawData);
// salvo in redis!
redisDb.StringSetAsync(Constants.CNT_STATUS_CONF_KEY, JsonConvert.SerializeObject(currConf));
}
}
if (currConf == null)
{
currConf = new List<DisplayDataDTO>();
}
return currConf;
}
public List<MachineMode> getMachineModeConf(string fileName = "ModeList.json")
{
List<MachineMode>? currConf = null;
// leggo e salvo conf stati
string fullPath = Path.Combine(confPath, fileName);
if (File.Exists(fullPath))
{
var rawData = File.ReadAllText(fullPath);
if (!string.IsNullOrEmpty(rawData))
{
currConf = JsonConvert.DeserializeObject<List<MachineMode>>(rawData);
// salvo in redis!
redisDb.StringSetAsync(Constants.MODE_CONF_KEY, JsonConvert.SerializeObject(currConf));
}
}
if (currConf == null)
{
currConf = new List<MachineMode>();
}
return currConf;
}
public List<MachineStatus> getMachineStatusConf(string fileName = "StatusList.json")
{
List<MachineStatus>? currConf = null;
// leggo e salvo conf stati
string fullPath = Path.Combine(confPath, fileName);
if (File.Exists(fullPath))
{
var rawData = File.ReadAllText(fullPath);
if (!string.IsNullOrEmpty(rawData))
{
currConf = JsonConvert.DeserializeObject<List<MachineStatus>>(rawData);
// salvo in redis!
redisDb.StringSetAsync(Constants.DISPLSTATUS_CONF_KEY, JsonConvert.SerializeObject(currConf));
}
}
if (currConf == null)
{
currConf = new List<MachineStatus>();
}
return currConf;
}
public List<DisplayDataDTO> getMPStatusConf(string fileName = "MPStatus.json")
{
List<DisplayDataDTO>? currConf = null;
string fullPath = Path.Combine(confPath, fileName);
if (File.Exists(fullPath))
{
var rawData = File.ReadAllText(fullPath);
if (!string.IsNullOrEmpty(rawData))
{
currConf = JsonConvert.DeserializeObject<List<DisplayDataDTO>>(rawData);
// salvo in redis!
redisDb.StringSetAsync(Constants.MP_STATUS_CONF_KEY, JsonConvert.SerializeObject(currConf));
}
}
if (currConf == null)
{
currConf = new List<DisplayDataDTO>();
}
return currConf;
}
public List<MachineStatus> getPalletStatusConf(string fileName = "PalletStatusList.json")
{
List<MachineStatus>? currConf = null;
// leggo e salvo conf stati
string fullPath = Path.Combine(confPath, fileName);
if (File.Exists(fullPath))
{
var rawData = File.ReadAllText(fullPath);
if (!string.IsNullOrEmpty(rawData))
{
currConf = JsonConvert.DeserializeObject<List<MachineStatus>>(rawData);
// salvo in redis!
redisDb.StringSetAsync(Constants.PALLSTATUS_CONF_KEY, JsonConvert.SerializeObject(currConf));
}
}
if (currConf == null)
{
currConf = new List<MachineStatus>();
}
return currConf;
}
public List<DisplayDataDTO> getParamsConf(string fileName = "Parameters.json")
{
List<DisplayDataDTO>? currConf = null;
// leggo e salvo conf stati
string fullPath = Path.Combine(confPath, fileName);
if (File.Exists(fullPath))
{
var rawData = File.ReadAllText(fullPath);
if (!string.IsNullOrEmpty(rawData))
{
currConf = JsonConvert.DeserializeObject<List<DisplayDataDTO>>(rawData);
// salvo in redis!
redisDb.StringSetAsync(Constants.PARAMS_CONF_KEY, JsonConvert.SerializeObject(currConf));
}
}
if (currConf == null)
{
currConf = new List<DisplayDataDTO>();
}
return currConf;
}
public List<DisplayDataDTO> getTools(string fileName = "Tools.json")
{
List<DisplayDataDTO>? currConf = null;
string fullPath = Path.Combine(confPath, fileName);
if (File.Exists(fullPath))
{
var rawData = File.ReadAllText(fullPath);
if (!string.IsNullOrEmpty(rawData))
{
currConf = JsonConvert.DeserializeObject<List<DisplayDataDTO>>(rawData);
// salvo in redis!
redisDb.StringSetAsync(Constants.TOOLS_CONF_KEY, JsonConvert.SerializeObject(currConf));
}
}
if (currConf == null)
{
currConf = new List<DisplayDataDTO>();
}
return currConf;
}
#endregion Public Methods
#region Protected Fields
protected string confPath = "";
#endregion Protected Fields
#region Protected Properties
protected IDatabase redisDb { get; set; } = null!;
#endregion Protected Properties
#region Private Fields
private Logger Log = LogManager.GetCurrentClassLogger();
#endregion Private Fields
}
}