138 lines
4.3 KiB
C#
138 lines
4.3 KiB
C#
using Microsoft.VisualBasic;
|
|
using Newtonsoft.Json;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using StackExchange.Redis;
|
|
using NLog;
|
|
using Maat.Core.CONF;
|
|
|
|
namespace Maat.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
|
|
|
|
#if false
|
|
/// <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}");
|
|
}
|
|
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;
|
|
}
|
|
#endif
|
|
|
|
#region Public Methods
|
|
|
|
public JobConfigConf getJobConfig(string JobConfigFile = "JobConfig.json")
|
|
{
|
|
JobConfigConf? currConf = null;
|
|
// leggo la JobConfig.conf
|
|
Log.Info($"Reading JobConfigFile: {JobConfigFile}");
|
|
string jcfPath = Path.Combine(confPath, JobConfigFile);
|
|
if (!File.Exists(jcfPath))
|
|
{
|
|
Log.Error($"Conf file not found: {jcfPath}");
|
|
}
|
|
else
|
|
{
|
|
string rawData = File.ReadAllText(jcfPath);
|
|
if (string.IsNullOrEmpty(rawData))
|
|
{
|
|
Log.Error($"Conf file empty: {jcfPath}");
|
|
}
|
|
else
|
|
{
|
|
try
|
|
{
|
|
JobConfigConf? jcData = JsonConvert.DeserializeObject<JobConfigConf>(rawData);
|
|
if (jcData != null)
|
|
{
|
|
currConf = jcData;
|
|
// salvo in redis!
|
|
redisDb.StringSetAsync(Const.JOB_CONF_KEY, JsonConvert.SerializeObject(currConf));
|
|
Log.Info("JobConfigFile deserialization complete!");
|
|
}
|
|
else
|
|
{
|
|
Log.Error($"JobConfigFile empty: {jcfPath}");
|
|
}
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Fatal($"Error during JobCOnfigFile parsing{Environment.NewLine}{exc}");
|
|
}
|
|
}
|
|
}
|
|
// verifica finale null...
|
|
if (currConf == null)
|
|
{
|
|
currConf = new JobConfigConf();
|
|
}
|
|
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
|
|
}
|
|
} |