95 lines
3.2 KiB
C#
95 lines
3.2 KiB
C#
using IniParser;
|
|
using IniParser.Model;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using static System.Collections.Specialized.BitVector32;
|
|
|
|
namespace SoraV2Utils_Service
|
|
{
|
|
public class ServiceSettings
|
|
{
|
|
public static ServiceSettings Instance { get; private set; }
|
|
|
|
private IniData data = null;
|
|
private FileIniDataParser parser = null;
|
|
|
|
public int intervall = 60000;
|
|
public int warningThreshold = 30;
|
|
public int criticalThreshold = 10;
|
|
|
|
public ServiceSettings()
|
|
{
|
|
string serviceSettingsFile = AppDomain.CurrentDomain.BaseDirectory + "\\SoraV2Utils.ini";
|
|
|
|
this.parser = new FileIniDataParser();
|
|
|
|
this.data = ReadOrCreateConfigFile(serviceSettingsFile);
|
|
|
|
TryParseConfig<int>("SoraV2Utils", "interval", ref this.intervall);
|
|
TryParseConfig<int>("SoraV2Utils", "warningThreshold", ref this.warningThreshold);
|
|
TryParseConfig<int>("SoraV2Utils", "criticalThreshold", ref this.criticalThreshold);
|
|
|
|
Instance = this;
|
|
}
|
|
|
|
public bool TryParseConfig<T>(string section, string key, ref T variable)
|
|
{
|
|
bool parseResult = false;
|
|
|
|
string value = this.data[section][key];
|
|
if (!string.IsNullOrEmpty(value))
|
|
{
|
|
try
|
|
{
|
|
variable = (T)Convert.ChangeType(value, typeof(T)); // Convert the value to the appropriate type.
|
|
parseResult = true;
|
|
}
|
|
catch (FormatException)
|
|
{
|
|
ServiceLogger.Instance.Log($"{key} setting could not be read from config! (Using default: {variable})");
|
|
}
|
|
}
|
|
|
|
return parseResult;
|
|
}
|
|
|
|
private IniData ReadOrCreateConfigFile(string filePath)
|
|
{
|
|
try
|
|
{
|
|
var data = parser.ReadFile(filePath);
|
|
if (data.Sections.Count == 0)
|
|
{
|
|
CreateServiceFile(data, filePath);
|
|
}
|
|
return data;
|
|
}
|
|
catch (IniParser.Exceptions.ParsingException)
|
|
{
|
|
var newData = new IniParser.Parser.IniDataParser().Parse("");
|
|
CreateServiceFile(newData,filePath);
|
|
return newData;
|
|
}
|
|
}
|
|
|
|
private IniData CreateServiceFile(IniData _data, string path)
|
|
{
|
|
_data.Sections.AddSection("SoraV2Utils");
|
|
_data["SoraV2Utils"].AddKey("interval", "60000");
|
|
_data["SoraV2Utils"].GetKeyData("interval").Comments.Add("Interval to check the mouse battery");
|
|
|
|
_data["SoraV2Utils"].AddKey("warningThreshold", "30");
|
|
_data["SoraV2Utils"].GetKeyData("warningThreshold").Comments.Add("Threshold for first warning");
|
|
|
|
_data["SoraV2Utils"].AddKey("criticalThreshold", "10");
|
|
_data["SoraV2Utils"].GetKeyData("criticalThreshold").Comments.Add("Threshold for second (critial) warning");
|
|
this.parser.WriteFile("SoraV2Utils.ini", _data);
|
|
return _data;
|
|
}
|
|
}
|
|
}
|