Files
DP44/Common/DTS.Common.ISO/TestSetting.cs
2026-04-17 14:55:32 -04:00

188 lines
6.6 KiB
C#

using System;
using System.Collections.Generic;
using System.Text;
namespace ISO
{
/// <summary>
/// a simple setting in a test
/// can have a default value, a value, and an id
/// default value is used by TestSettingsDictionary for when
/// the setting doesn't currently exist or have a value
/// </summary>
public class TestSetting
{
//public string Id { get; }
public int Id { get; }
public string Value { get; set; }
public string DefaultValue { get; }
private const string SEPARATOR = "_x_";
public TestSetting(TestSetting copy, string value)
{
Id = copy.Id;
DefaultValue = copy.DefaultValue;
Value = value;
}
public TestSetting(TestSetting copy)
{
Id = copy.Id;
DefaultValue = copy.DefaultValue;
Value = copy.Value;
}
public TestSetting(int id, string value, string defaultValue)
{
Id = id;
Value = value;
DefaultValue = defaultValue;
}
public TestSetting(TestSettingsEnum id, string value, string defaultValue)
{
Id = (int)id;
Value = value;
DefaultValue = defaultValue;
}
public string ToSerializeString()
{
return $"{Id}={Value.Replace("=", SEPARATOR)}";
}
public static bool TryParse(string s, out TestSetting ts)
{
ts = null;
var tokens = s.Split(new[] { "=" }, StringSplitOptions.None);
if (tokens.Length < 2) { return false; }
var id = tokens[0].Replace(SEPARATOR, "=");
var val = tokens[1].Replace(SEPARATOR, "=");
if (Enum.TryParse(id, out TestSettingsEnum enumValue))
{
ts = new TestSetting(enumValue, val, val);
return true;
}
if (int.TryParse(id, out var iTemp))
{
ts = new TestSetting(iTemp, val, val);
return true;
}
return false;
}
}
/// <summary>
/// list of all non columned test settings
/// </summary>
public enum TestSettingsEnum
{
ArmCheckListStep,
CheckListInputVoltageCheck,
CheckListBatteryVoltageCheck,
CheckListSquibResistanceCheck,
CheckListSensorIDCheck,
CheckListTriggerStartCheck,
CheckListTiltSensorCheck,
CheckListTemperatureCheck,
CheckListClockSyncCheck,
EW,//ExcitationWarmup
CheckListMustPass
}
/// <summary>
/// holds all possible settings for a test
/// </summary>
public class TestSettingDictionary
{
public TestSettingDictionary()
{
}
public TestSettingDictionary(TestSettingDictionary copy)
{
using (var e = copy._lookup.GetEnumerator())
{
while (e.MoveNext())
{
_lookup[e.Current.Key] = new TestSetting(e.Current.Value);
}
}
}
private const string SEPARATOR = "_X_";
private readonly Dictionary<int, TestSetting> _lookup = new Dictionary<int, TestSetting>();
public string GetValue(int id, string defaultValue)
{
if (!_lookup.ContainsKey(id)) { return defaultValue; }
return _lookup[id].Value ?? _lookup[id].DefaultValue;
}
public string GetValue(TestSettingsEnum id, string defaultValue)
{
return GetValue((int)id, defaultValue);
}
public void UnLoad()
{
_lookup.Clear();
}
/// <summary>
/// used to change the value in the dictionary (just the value, leave everything else the same)
/// </summary>
/// <param name="setting"></param>
/// <param name="value"></param>
public void SetValue(TestSetting setting, string value)
{
//we do it this way to avoid Add() and also to avoid accidentally reusing the input setting inappropriately
_lookup[setting.Id] = new TestSetting(setting, value);
}
/// <summary>
/// used to initialize a value in the dictionary
/// </summary>
/// <param name="setting"></param>
public void SetValue(TestSetting setting)
{
_lookup[setting.Id] = setting;
}
public void SetValue(TestSettingsEnum id, string value)
{
SetValue((int)id, value);
}
public void SetValue(int id, string value)
{
if (!_lookup.ContainsKey(id))
{
_lookup[id] = new TestSetting(id, value, value);
}
else { _lookup[id].Value = value; }
}
public string ToSerializeString()
{
var sb = new StringBuilder();
foreach (var s in _lookup.Values)
{
var sVal = s.ToSerializeString();
// ReSharper disable once StringIndexOfIsCultureSpecific.1
System.Diagnostics.Trace.Assert(sVal.IndexOf(SEPARATOR) < 0);
sVal = sVal.Replace(System.Globalization.CultureInfo.InvariantCulture.TextInfo.ListSeparator, SEPARATOR);
if (sb.Length > 0) { sb.Append(System.Globalization.CultureInfo.InvariantCulture.TextInfo.ListSeparator); }
sb.Append(sVal);
}
return sb.ToString();
}
public void LoadSettings(string s)
{
if (string.IsNullOrWhiteSpace(s)) { return; } //nothing to deserialize
var tokens = s.Split(new[] { System.Globalization.CultureInfo.InvariantCulture.TextInfo.ListSeparator }, StringSplitOptions.None);
foreach (var token in tokens)
{
var tok = token.Replace(SEPARATOR, System.Globalization.CultureInfo.InvariantCulture.TextInfo.ListSeparator);
//we prefer the default settings from the application
//if for some reason this key is no longer used, we can still stick it in the storage and use it as is
if (!TestSetting.TryParse(tok, out var ts)) continue;
if (!_lookup.ContainsKey(ts.Id))//no longer has a default setting, just use as is
{
_lookup[ts.Id] = ts;
}
else { _lookup[ts.Id].Value = ts.Value; }//default setting exists, just set the value
}
}
}
}