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
///
/// Verifica se alarm log sia attivato (da DECODER; in REDIS)
///
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 getActLog(string fileName = "ActLog.json")
{
List? currConf = null;
string fullPath = Path.Combine(confPath, fileName);
if (File.Exists(fullPath))
{
var rawData = File.ReadAllText(fullPath);
if (!string.IsNullOrEmpty(rawData))
{
currConf = JsonConvert.DeserializeObject>(rawData);
// salvo in redis!
redisDb.StringSetAsync(Constants.ACTLOG_CONF_KEY, JsonConvert.SerializeObject(currConf));
}
}
if (currConf == null)
{
currConf = new List();
}
return currConf;
}
public List getAlarmsBankConf(string fileName = "AlarmBankList.json")
{
List? 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>(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();
}
return currConf;
}
public List getAlarmsConf(string fileName = "Alarms.json")
{
List? currConf = null;
string fullPath = Path.Combine(confPath, fileName);
if (File.Exists(fullPath))
{
var rawData = File.ReadAllText(fullPath);
if (!string.IsNullOrEmpty(rawData))
{
currConf = JsonConvert.DeserializeObject>(rawData);
// salvo in redis!
redisDb.StringSet(Constants.ALARMS_ADAP_CONF_KEY, JsonConvert.SerializeObject(currConf));
}
}
if (currConf == null)
{
currConf = new List();
}
return currConf;
}
public List getAlarmsRawConf(string fileName = "AlarmRawList.json")
{
List? 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>(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();
}
return currConf;
}
public List getCountersConf(string fileName = "Counters.json")
{
List? currConf = null;
string fullPath = Path.Combine(confPath, fileName);
if (File.Exists(fullPath))
{
var rawData = File.ReadAllText(fullPath);
if (!string.IsNullOrEmpty(rawData))
{
currConf = JsonConvert.DeserializeObject>(rawData);
// salvo in redis!
redisDb.StringSetAsync(Constants.CNT_STATUS_CONF_KEY, JsonConvert.SerializeObject(currConf));
}
}
if (currConf == null)
{
currConf = new List();
}
return currConf;
}
public List getMachineModeConf(string fileName = "ModeList.json")
{
List? 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>(rawData);
// salvo in redis!
redisDb.StringSetAsync(Constants.MODE_CONF_KEY, JsonConvert.SerializeObject(currConf));
}
}
if (currConf == null)
{
currConf = new List();
}
return currConf;
}
public List getMachineStatusConf(string fileName = "StatusList.json")
{
List? 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>(rawData);
// salvo in redis!
redisDb.StringSetAsync(Constants.DISPLSTATUS_CONF_KEY, JsonConvert.SerializeObject(currConf));
}
}
if (currConf == null)
{
currConf = new List();
}
return currConf;
}
public List getMPStatusConf(string fileName = "MPStatus.json")
{
List? currConf = null;
string fullPath = Path.Combine(confPath, fileName);
if (File.Exists(fullPath))
{
var rawData = File.ReadAllText(fullPath);
if (!string.IsNullOrEmpty(rawData))
{
currConf = JsonConvert.DeserializeObject>(rawData);
// salvo in redis!
redisDb.StringSetAsync(Constants.MP_STATUS_CONF_KEY, JsonConvert.SerializeObject(currConf));
}
}
if (currConf == null)
{
currConf = new List();
}
return currConf;
}
public List getPalletStatusConf(string fileName = "PalletStatusList.json")
{
List? 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>(rawData);
// salvo in redis!
redisDb.StringSetAsync(Constants.PALLSTATUS_CONF_KEY, JsonConvert.SerializeObject(currConf));
}
}
if (currConf == null)
{
currConf = new List();
}
return currConf;
}
public List getParamsConf(string fileName = "Parameters.json")
{
List? 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>(rawData);
// salvo in redis!
redisDb.StringSetAsync(Constants.PARAMS_CONF_KEY, JsonConvert.SerializeObject(currConf));
}
}
if (currConf == null)
{
currConf = new List();
}
return currConf;
}
public List getTools(string fileName = "Tools.json")
{
List? currConf = null;
string fullPath = Path.Combine(confPath, fileName);
if (File.Exists(fullPath))
{
var rawData = File.ReadAllText(fullPath);
if (!string.IsNullOrEmpty(rawData))
{
currConf = JsonConvert.DeserializeObject>(rawData);
// salvo in redis!
redisDb.StringSetAsync(Constants.TOOLS_CONF_KEY, JsonConvert.SerializeObject(currConf));
}
}
if (currConf == null)
{
currConf = new List();
}
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
}
}