using DTS.Common.Enums; using DTS.Common.Enums.Hardware; using System; using System.Collections.Generic; using System.Configuration; using System.Linq; namespace DataPROWin7.DataModel.BatteryAndInputVoltageDefaults { public class DasBatteryInputSettings { /// /// all possible battery settings /// note that the order is important since serialization assumes this order, /// so don't change order without addressing that. /// public enum Settings { BatteryLowDiagnosticsThreshold, BatteryHighDiagnosticsThreshold, BatteryLowArmedThreshold, BatteryHighArmedThreshold, InputLowDiagnosticsThreshold, InputHighDiagnosticsThreshold, InputLowArmedThreshold, InputHighArmedThreshold, MinimumValidBatteryThreshold, MinimumValidInputThreshold, MaximumValidBatteryThreshold, MaximumValidInputThreshold, BatteryMediumDiagnosticsThreshold, BatteryMediumArmedThreshold, InputMediumDiagnosticsThreshold, InputMediumArmedThreshold, } public double BatteryLowDiagnosticsThreshold { get => GetValue(Settings.BatteryLowDiagnosticsThreshold); set => SetValue(Settings.BatteryLowDiagnosticsThreshold, value); } public double BatteryHighDiagnosticsThreshold { get => GetValue(Settings.BatteryHighDiagnosticsThreshold); set => SetValue(Settings.BatteryHighDiagnosticsThreshold, value); } public double BatteryLowArmedThreshold { get => GetValue(Settings.BatteryLowArmedThreshold); set => SetValue(Settings.BatteryLowArmedThreshold, value); } public double BatteryHighArmedThreshold { get => GetValue(Settings.BatteryHighArmedThreshold); set => SetValue(Settings.BatteryHighArmedThreshold, value); } public double InputLowDiagnosticsThreshold { get => GetValue(Settings.InputLowDiagnosticsThreshold); set => SetValue(Settings.InputLowDiagnosticsThreshold, value); } public double InputHighDiagnosticsThreshold { get => GetValue(Settings.InputHighDiagnosticsThreshold); set => SetValue(Settings.InputHighDiagnosticsThreshold, value); } public double InputLowArmedThreshold { get => GetValue(Settings.InputLowArmedThreshold); set => SetValue(Settings.InputLowArmedThreshold, value); } public double InputHighArmedThreshold { get => GetValue(Settings.InputHighArmedThreshold); set => SetValue(Settings.InputHighArmedThreshold, value); } public double MinimumValidBatteryThreshold { get => GetValue(Settings.MinimumValidBatteryThreshold); set => SetValue(Settings.MinimumValidBatteryThreshold, value); } public double MinimumValidInputThreshold { get => GetValue(Settings.MinimumValidInputThreshold); set => SetValue(Settings.MinimumValidInputThreshold, value); } public double MaximumValidBatteryThreshold { get => GetValue(Settings.MaximumValidBatteryThreshold); set => SetValue(Settings.MaximumValidBatteryThreshold, value); } public double MaximumValidInputThreshold { get => GetValue(Settings.MaximumValidInputThreshold); set => SetValue(Settings.MaximumValidInputThreshold, value); } public double BatteryMediumDiagnosticsThreshold { get => GetValue(Settings.BatteryMediumDiagnosticsThreshold); set => SetValue(Settings.BatteryMediumDiagnosticsThreshold, value); } public double BatteryMediumArmedThreshold { get => GetValue(Settings.BatteryMediumArmedThreshold); set => SetValue(Settings.BatteryMediumArmedThreshold, value); } public double InputMediumDiagnosticsThreshold { get => GetValue(Settings.InputMediumDiagnosticsThreshold); set => SetValue(Settings.InputMediumDiagnosticsThreshold, value); } public double InputMediumArmedThreshold { get => GetValue(Settings.InputMediumArmedThreshold); set => SetValue(Settings.InputMediumArmedThreshold, value); } public Dictionary SettingsProperty { get; } = new Dictionary(); public DasBatteryInputSettings(string s) { var tokens = s.Split(new char[] { ',' }); for (int i = 0; i < tokens.Length; i++) { var setting = (Settings)i; if (double.TryParse(tokens[i], System.Globalization.NumberStyles.Any, System.Globalization.CultureInfo.InvariantCulture, out double d)) { SettingsProperty[setting] = d; } } } public DasBatteryInputSettings(DasBatteryInputSettings copy) { using (var e = copy.SettingsProperty.GetEnumerator()) { while (e.MoveNext()) { SettingsProperty[e.Current.Key] = e.Current.Value; } } } public DasBatteryInputSettings() { var settings = Enum.GetValues(typeof(Settings)).Cast().ToArray(); foreach (var s in settings) { double d = double.NaN; switch (s) { case Settings.BatteryHighArmedThreshold: d = 9D; break; case Settings.BatteryHighDiagnosticsThreshold: d = 9D; break; case Settings.BatteryLowArmedThreshold: d = 6.8D; break; case Settings.BatteryLowDiagnosticsThreshold: d = 7.8D; break; case Settings.InputHighArmedThreshold: d = 15.3; break; case Settings.InputHighDiagnosticsThreshold: d = 15.3; break; case Settings.InputLowArmedThreshold: d = 6.5D; break; case Settings.InputLowDiagnosticsThreshold: d = 10D; break; case Settings.MinimumValidBatteryThreshold: d = 4D; break; case Settings.MinimumValidInputThreshold: d = 4D; break; case Settings.MaximumValidBatteryThreshold: d = 9D; break; case Settings.MaximumValidInputThreshold: d = 19D; break; case Settings.BatteryMediumArmedThreshold: d = 7.9D; break; //7.9 is halfway between low and high case Settings.BatteryMediumDiagnosticsThreshold: d = 8.4D; break; //8.4 is halfway between low and high case Settings.InputMediumArmedThreshold: d = 10.9D; break; //10.9 is halfway between low and high case Settings.InputMediumDiagnosticsThreshold: d = 12.7D; break; //12.7 is halfway between low and high default: throw new NotSupportedException("Unknown Battery setting: " + s.ToString()); } SettingsProperty[s] = d; } } public string ToSerializedString() { var settings = Enum.GetValues(typeof(Settings)).Cast().ToArray(); var sb = new string[settings.Length]; foreach (var setting in settings) { sb[(int)setting] = GetValue(setting).ToString(System.Globalization.CultureInfo.InvariantCulture); } return string.Join(",", sb); } public double GetValue(Settings setting) { if (SettingsProperty.ContainsKey(setting)) { return SettingsProperty[setting]; } else { throw new NotSupportedException("unknown setting: " + setting.ToString()); } } public void SetValue(Settings setting, double d) { SettingsProperty[setting] = d; } } public static class InputAndBatterySettings { /// /// holds the battery/input settings in a cache so they don't have to be requeried from db /// 17615 BatteryAndInputVoltageDefaults improvements 3.2 /// public static Dictionary _cache = new Dictionary(); /// /// clears the cache. This serves the purpose of ensuring /// that the values will be re-queried between runs, so if the settings are changed /// between runs the new values will be used /// public static void ClearCache() { _cache.Clear(); } public static double GetValue(string DasType, DasBatteryInputSettings.Settings setting) { if (Enum.TryParse(DasType, out HardwareTypes type)) { return GetValue(type, setting); } else { return double.NaN; } } public static DasBatteryInputSettings GetDefaultSettingForHWType(HardwareTypes type) { if (RunTestVariables.InRunTest && RunTestVariables.CacheVoltageSettingsInRunTest) { if (_cache.ContainsKey(type)) { return _cache[type]; } } DasBatteryInputSettings setting = null; switch (type) { case HardwareTypes.G5INDUMMY: setting = Common.SerializedSettings.G5INDUMMY_PowerSetting_Default; break; case HardwareTypes.G5VDS: setting = Common.SerializedSettings.G5VDS_PowerSetting_Default; break; case HardwareTypes.SLICE_Distributor: setting = Common.SerializedSettings.SLICE_Distributor_PowerSetting_Default; break; case HardwareTypes.SLICE_Mini_Distributor: setting = Common.SerializedSettings.SLICE_Mini_Distributor_PowerSetting_Default; break; case HardwareTypes.SLICE_EthernetController: setting = Common.SerializedSettings.SLICE_EthernetController_PowerSetting_Default; break; case HardwareTypes.SLICE_LabEthernet: setting = Common.SerializedSettings.SLICE_LabEthernet_PowerSetting_Default; break; case HardwareTypes.SLICE_Micro_Base: setting = Common.SerializedSettings.SLICE_Micro_Base_PowerSetting_Default; break; case HardwareTypes.SLICE_NANO_Base: setting = Common.SerializedSettings.SLICE_NANO_Base_PowerSetting_Default; break; case HardwareTypes.SLICE1_5_Micro_Base: setting = Common.SerializedSettings.SLICE1_5_Micro_Base_PowerSetting_Default; break; case HardwareTypes.SLICE1_5_Nano_Base: setting = Common.SerializedSettings.SLICE1_5_Nano_Base_PowerSetting_Default; break; case HardwareTypes.SLICE1_G5Stack: setting = Common.SerializedSettings.SLICE1_G5Stack_PowerSetting_Default; break; case HardwareTypes.SLICE2_DIM: setting = Common.SerializedSettings.SLICE2_DIM_PowerSetting_Default; break; case HardwareTypes.SLICE2_SIM: case HardwareTypes.SLICE2_Base: setting = Common.SerializedSettings.SLICE2_SIM_PowerSetting_Default; break; case HardwareTypes.SLICE2_TOM: setting = Common.SerializedSettings.SLICE2_TOM_PowerSetting_Default; break; case HardwareTypes.SLICE2_SLD: setting = Common.SerializedSettings.SLICE2_SLD_PowerSetting_Default; break; case HardwareTypes.SLICE2_SLS: setting = Common.SerializedSettings.SLICE2_SLS_PowerSetting_Default; break; case HardwareTypes.SLICE2_SLT: setting = Common.SerializedSettings.SLICE2_SLT_PowerSetting_Default; break; case HardwareTypes.TDAS_Pro_Rack: case HardwareTypes.TDAS_LabRack: setting = Common.SerializedSettings.TDAS_Pro_Rack_PowerSetting_Default; break; case HardwareTypes.SLICE6_Base: setting = Common.SerializedSettings.SLICE6_PowerSetting_Default; break; case HardwareTypes.SLICE6_AIR: setting = Common.SerializedSettings.SLICE6_AIR_PowerSetting_Default; break; case HardwareTypes.SLICE6_AIR_BR: setting = Common.SerializedSettings.SLICE6_AIR_BR_PowerSetting_Default; break; case HardwareTypes.SLICE6DB: setting = Common.SerializedSettings.SLICE6Db_PowerSetting_Default; break; case HardwareTypes.SLICE_Pro_Distributor: setting = Common.SerializedSettings.SLICEPRODistributor_PowerSetting_Default; break; case HardwareTypes.SLICE6DB3: setting = Common.SerializedSettings.SLICE6Db3_PowerSetting_Default; break; case HardwareTypes.SLICE6DB_InDummy: setting = Common.SerializedSettings.SLICE6Db_InDummy_PowerSetting_Default; break; case HardwareTypes.PowerPro: setting = Common.SerializedSettings.PowerPRO_PowerSetting_Default; break; case HardwareTypes.TSR_AIR: case HardwareTypes.TSR_AIR_RevB: setting = Common.SerializedSettings.TSRAIR_PowerSetting_Default; break; //case HardwareTypes.SLICE_Pro_Distributor: setting = Common.SerializedSettings.SLICEPRODistributor_PowerSetting_Default; break; //case HardwareTypes.Falcon: setting = Common.SerializedSettings.Falcon_PowerSetting_Default; break; } if (null != setting) { if (RunTestVariables.InRunTest && RunTestVariables.CacheVoltageSettingsInRunTest) { _cache[type] = setting; } return setting; } throw new NotSupportedException($"unknown type: {type.ToString()}"); } public static void SetSettingForHWType(HardwareTypes type, DasBatteryInputSettings setting) { switch (type) { case HardwareTypes.G5INDUMMY: Common.SerializedSettings.G5INDUMMY_PowerSetting = setting; break; case HardwareTypes.G5VDS: Common.SerializedSettings.G5VDS_PowerSetting = setting; break; case HardwareTypes.SLICE_Distributor: Common.SerializedSettings.SLICE_Distributor_PowerSetting = setting; break; case HardwareTypes.SLICE6DB: Common.SerializedSettings.SLICE6Db_PowerSetting = setting; break; case HardwareTypes.SLICE6DB3: Common.SerializedSettings.SLICE6Db3_PowerSetting = setting; break; case HardwareTypes.SLICE_Pro_Distributor: Common.SerializedSettings.SLICEPRODistributor_PowerSetting = setting; break; //case HardwareTypes.SLICE6DB_AIR: Common.SerializedSettings.SLICE6Db_AIR_PowerSetting = setting; break; case HardwareTypes.SLICE6DB_InDummy: Common.SerializedSettings.SLICE6Db_InDummy_PowerSetting = setting; break; case HardwareTypes.SLICE_Mini_Distributor: Common.SerializedSettings.SLICE_Mini_Distributor_PowerSetting = setting; break; case HardwareTypes.SLICE_EthernetController: Common.SerializedSettings.SLICE_EthernetController_PowerSetting = setting; break; case HardwareTypes.SLICE_LabEthernet: Common.SerializedSettings.SLICE_LabEthernet_PowerSetting = setting; break; case HardwareTypes.SLICE_Micro_Base: Common.SerializedSettings.SLICE_Micro_Base_PowerSetting = setting; break; case HardwareTypes.SLICE_NANO_Base: Common.SerializedSettings.SLICE_NANO_Base_PowerSetting = setting; break; case HardwareTypes.SLICE1_5_Micro_Base: Common.SerializedSettings.SLICE1_5_Micro_Base_PowerSetting = setting; break; case HardwareTypes.SLICE1_5_Nano_Base: Common.SerializedSettings.SLICE1_5_Nano_Base_PowerSetting = setting; break; case HardwareTypes.SLICE1_G5Stack: Common.SerializedSettings.SLICE1_G5Stack_PowerSetting = setting; break; case HardwareTypes.SLICE2_DIM: Common.SerializedSettings.SLICE2_DIM_PowerSetting = setting; break; case HardwareTypes.SLICE2_SIM: case HardwareTypes.SLICE2_Base: Common.SerializedSettings.SLICE2_SIM_PowerSetting = setting; break; case HardwareTypes.SLICE2_SLD: Common.SerializedSettings.SLICE2_SLD_PowerSetting = setting; break; case HardwareTypes.SLICE2_SLS: Common.SerializedSettings.SLICE2_SLS_PowerSetting = setting; break; case HardwareTypes.SLICE2_SLT: Common.SerializedSettings.SLICE2_SLT_PowerSetting = setting; break; case HardwareTypes.SLICE2_TOM: Common.SerializedSettings.SLICE2_TOM_PowerSetting = setting; break; case HardwareTypes.TDAS_Pro_Rack: case HardwareTypes.TDAS_LabRack: Common.SerializedSettings.TDAS_Pro_Rack_PowerSetting = setting; break; case HardwareTypes.SLICE6_Base: Common.SerializedSettings.SLICE6_PowerSetting = setting; break; case HardwareTypes.SLICE6_AIR: Common.SerializedSettings.SLICE6_AIR_PowerSetting = setting; break; case HardwareTypes.SLICE6_AIR_BR: Common.SerializedSettings.SLICE6_AIR_BR_PowerSetting = setting; break; case HardwareTypes.PowerPro: Common.SerializedSettings.PowerPRO_PowerSetting = setting; break; case HardwareTypes.TSR_AIR: case HardwareTypes.TSR_AIR_RevB: Common.SerializedSettings.TSRAIR_PowerSetting = setting; break; } } public static DasBatteryInputSettings GetSettingForHWType(HardwareTypes type) { if (RunTestVariables.InRunTest && RunTestVariables.CacheVoltageSettingsInRunTest) { if (_cache.ContainsKey(type)) { return _cache[type]; } } DasBatteryInputSettings setting = null; switch (type) { case HardwareTypes.G5INDUMMY: setting = Common.SerializedSettings.G5INDUMMY_PowerSetting; break; case HardwareTypes.G5VDS: setting = Common.SerializedSettings.G5VDS_PowerSetting; break; case HardwareTypes.SLICE_Distributor: setting = Common.SerializedSettings.SLICE_Distributor_PowerSetting; break; case HardwareTypes.SLICE_Mini_Distributor: setting = Common.SerializedSettings.SLICE_Mini_Distributor_PowerSetting; break; case HardwareTypes.SLICE_Pro_Distributor: setting = Common.SerializedSettings.SLICEPRODistributor_PowerSetting; break; case HardwareTypes.SLICE6DB: setting = Common.SerializedSettings.SLICE6Db_PowerSetting; break; case HardwareTypes.SLICE6DB3: setting = Common.SerializedSettings.SLICE6Db3_PowerSetting; break; case HardwareTypes.SLICE6DB_InDummy: setting = Common.SerializedSettings.SLICE6Db_InDummy_PowerSetting; break; case HardwareTypes.SLICE_EthernetController: setting = Common.SerializedSettings.SLICE_EthernetController_PowerSetting; break; case HardwareTypes.SLICE_LabEthernet: setting = Common.SerializedSettings.SLICE_LabEthernet_PowerSetting; break; case HardwareTypes.SLICE_Micro_Base: setting = Common.SerializedSettings.SLICE_Micro_Base_PowerSetting; break; case HardwareTypes.SLICE_NANO_Base: setting = Common.SerializedSettings.SLICE_NANO_Base_PowerSetting; break; case HardwareTypes.SLICE1_5_Micro_Base: setting = Common.SerializedSettings.SLICE1_5_Micro_Base_PowerSetting; break; case HardwareTypes.SLICE1_5_Nano_Base: setting = Common.SerializedSettings.SLICE1_5_Nano_Base_PowerSetting; break; case HardwareTypes.SLICE1_G5Stack: setting = Common.SerializedSettings.SLICE1_G5Stack_PowerSetting; break; case HardwareTypes.SLICE2_DIM: setting = Common.SerializedSettings.SLICE2_DIM_PowerSetting; break; case HardwareTypes.SLICE2_SIM: case HardwareTypes.SLICE2_Base: setting = Common.SerializedSettings.SLICE2_SIM_PowerSetting; break; case HardwareTypes.SLICE2_SLD: setting = Common.SerializedSettings.SLICE2_SLD_PowerSetting; break; case HardwareTypes.SLICE2_SLS: setting = Common.SerializedSettings.SLICE2_SLS_PowerSetting; break; case HardwareTypes.SLICE2_SLT: setting = Common.SerializedSettings.SLICE2_SLT_PowerSetting; break; case HardwareTypes.SLICE2_TOM: setting = Common.SerializedSettings.SLICE2_TOM_PowerSetting; break; case HardwareTypes.TDAS_Pro_Rack: case HardwareTypes.TDAS_LabRack: setting = Common.SerializedSettings.TDAS_Pro_Rack_PowerSetting; break; case HardwareTypes.DIM: case HardwareTypes.SIM: case HardwareTypes.TOM: //we are not expected to get here but if we do, just use the tdas default setting = Common.SerializedSettings.TDAS_Pro_Rack_PowerSetting; break; case HardwareTypes.Ribeye: case HardwareTypes.RibeyeLED: case HardwareTypes.SLICE_Base: case HardwareTypes.SLICE_Bridge: case HardwareTypes.SLICE_IEPE: case HardwareTypes.SLICE2_IEPE_Hi: case HardwareTypes.SLICE2_IEPE_Lo: //these are all never expected to get here, but are all slice types setting = Common.SerializedSettings.SLICE_NANO_Base_PowerSetting; break; case HardwareTypes.SLICE6_Base: setting = Common.SerializedSettings.SLICE6_PowerSetting; break; case HardwareTypes.SLICE6_AIR: case HardwareTypes.S6A_EthernetRecorder: setting = Common.SerializedSettings.SLICE6_AIR_PowerSetting; break; case HardwareTypes.SLICE6_AIR_BR: setting = Common.SerializedSettings.SLICE6_AIR_BR_PowerSetting; break; case HardwareTypes.PowerPro: setting = Common.SerializedSettings.PowerPRO_PowerSetting; break; case HardwareTypes.TSR_AIR: case HardwareTypes.TSR_AIR_RevB: setting = Common.SerializedSettings.TSRAIR_PowerSetting; break; case HardwareTypes.SLICE6_AIR_TC: setting = Common.SerializedSettings.SLICE6_AIR_TC_PowerSetting; break; case HardwareTypes.SLICE_PRO_CAN_FD: setting = Common.SerializedSettings.SPFD_PowerSetting; break; default: throw new NotSupportedException("unknown das type: " + type.ToString()); } if (null != setting) { if (RunTestVariables.InRunTest && RunTestVariables.CacheVoltageSettingsInRunTest) { _cache[type] = setting; } } return setting; } public static double GetValue(HardwareTypes type, DasBatteryInputSettings.Settings setting) { return GetSettingForHWType(type).GetValue(setting); } } }