Files
mapo-mono/MP.MONO.Core/ConfigManager.cs
T
2022-03-21 18:50:22 +01:00

143 lines
4.8 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 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<BaseAlarmConf> getAlarmsConf()
{
List<BaseAlarmConf>? 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<List<BaseAlarmConf>>(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<BaseAlarmConf>();
}
return currConf;
}
public List<MachineMode> getMachineModeConf()
{
List<MachineMode>? 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<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()
{
List<MachineStatus>? 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<List<MachineStatus>>(rawData);
// salvo in redis!
redisDb.StringSetAsync(Constants.STATUS_CONF_KEY, JsonConvert.SerializeObject(currConf));
}
}
if (currConf == null)
{
currConf = new List<MachineStatus>();
}
return currConf;
}
public List<DisplayDataDTO> getParamsConf()
{
List<DisplayDataDTO>? 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<List<DisplayDataDTO>>(rawData);
// salvo in redis!
redisDb.StringSetAsync(Constants.PARAMS_CONF_KEY, JsonConvert.SerializeObject(currConf));
}
}
if (currConf == null)
{
currConf = new List<DisplayDataDTO>();
}
return currConf;
}
#endregion Public Methods
}
}