Files
laatzen/Common/Ui/GenesisToolBox/Infrastructure/Appsettings.cs
T
2023-08-02 16:45:17 +02:00

103 lines
3.4 KiB
C#

namespace Xylem.Common.Ui.GenesisToolBox.Infrastructure
{
using System.Configuration;
using System.Runtime.CompilerServices;
public static class Appsettings
{
private static readonly Configuration configuration = ConfigurationManager.OpenExeConfiguration(@"..\..\App.config");
private static KeyValueConfigurationCollection settings => configuration.AppSettings.Settings;
/// <summary>
/// Each, by default: 1min * 60sec * 1000ms = 60_000ms, should be checked has the temperature reached difference from <see cref="TemperatureStepInC"/>°C.
/// When the difference is reached, messurement is started for <see cref="MessurementDauer"/> with tolerance from +/- <see cref="TemperaturToleranceInC"/>°C.
/// </summary>
public static int TemperatureCheckInMs
{
get => GetValue<int>(6_000);
set => value.SetValue(6_000);
}
/// <summary>
/// The temperature step for triggering next messurement. By default(1°C).
/// </summary>
public static double TemperatureStepInC
{
get => GetValue<double>(1);
set => value.SetValue(1);
}
/// <summary>
/// Describes how long should be messured current temperature. By default: 10min * 60sec * 1000ms = 600_000ms.
/// </summary>
public static int MessurementDauer
{
get => GetValue<int>(600_000);
set => value.SetValue(600_000);
}
/// <summary>
/// Describes in how many seconds schuld be messured. By default: 10sec * 1000ms = 10_000ms.
/// </summary>
public static int MessurementStep
{
get => GetValue<int>(10_000);
set => value.SetValue(10_000);
}
/// <summary>
/// Describes the temperature tolerance in whitch the messurement is valid.
/// If the tolerance is oversteped, the messurement is canceled.
/// By default: 0.05.
/// </summary>
public static double TemperaturToleranceInC
{
get => GetValue<double>(0.05);
set => value.SetValue(0.05);
}
private static int GetValue<T>(int @default, [CallerMemberName] string property = "")
{
if (int.TryParse($"{settings[property]?.Value}", out var value))
{
return value;
}
return @default;
}
private static double GetValue<T>(double @default, [CallerMemberName] string property = "")
{
if (double.TryParse($"{settings[property]?.Value}", out var value))
{
return value;
}
return @default;
}
private static void SetValue<T>(this T value, T @default, [CallerMemberName] string property = "")
{
if (value.Equals(default(T)))
{
value = @default;
}
var current = value.ToString();
var saved = settings[property];
if (saved is null)
{
settings.Add(property, current);
}
else
{
saved.Value = current;
}
configuration.Save(ConfigurationSaveMode.Full);
ConfigurationManager.RefreshSection(nameof(configuration.AppSettings));
}
}
}