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 Private Fields private Logger Log = LogManager.GetCurrentClassLogger(); #endregion Private Fields #region Protected Fields protected string confPath = ""; #endregion Protected Fields #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 Protected Properties protected IDatabase redisDb { get; set; } = null!; #endregion Protected Properties #region Public Methods public List getAlarmsConf() { List? currConf = null; // leggo e salvo conf stati string fullPath = Path.Combine(confPath, "AlarmList.json"); 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 alarmMap: {item.description} | {item.memAddr} x {item.size} byte | {item.messages.Count} messaggi allarme", true, true); } } // salvo in redis! redisDb.StringSetAsync(Constants.ALARMS_CONF_KEY, JsonConvert.SerializeObject(currConf)); } } if (currConf == null) { currConf = new List(); } return currConf; } public List getMachineModeConf() { List? currConf = null; // leggo e salvo conf stati string fullPath = Path.Combine(confPath, "ModeList.json"); 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() { List? currConf = null; // leggo e salvo conf stati string fullPath = Path.Combine(confPath, "StatusList.json"); if (File.Exists(fullPath)) { var rawData = File.ReadAllText(fullPath); if (!string.IsNullOrEmpty(rawData)) { currConf = JsonConvert.DeserializeObject>(rawData); // salvo in redis! redisDb.StringSetAsync(Constants.STATUS_CONF_KEY, JsonConvert.SerializeObject(currConf)); } } if (currConf == null) { currConf = new List(); } return currConf; } public List getParamsConf() { List? currConf = null; // leggo e salvo conf stati string fullPath = Path.Combine(confPath, "ParamList.json"); 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; } #endregion Public Methods } }