276 lines
13 KiB
C#
276 lines
13 KiB
C#
using DTS.Common.Enums.Sensors;
|
|
using DTS.Common.Interface.Sensors.SensorSettingsModule;
|
|
using DTS.Common.Settings;
|
|
using System;
|
|
using System.Xml;
|
|
|
|
namespace DTS.SensorDB
|
|
{
|
|
public class CalibrationPolicy : Common.Base.BasePropertyChanged, ICalibrationPolicy
|
|
{
|
|
private SensorConstants.SensorCalPolicy _selectedCalPolicy;
|
|
/// <summary>
|
|
/// returns the currently selected calibration policy
|
|
/// </summary>
|
|
public SensorConstants.SensorCalPolicy SelectedCalPolicy
|
|
{
|
|
get => _selectedCalPolicy;
|
|
set => SetProperty(ref _selectedCalPolicy, value, "SelectedCalPolicy");
|
|
}
|
|
|
|
private static SensorConstants.SensorCalPolicy[] _availableCalPolicies = new[]
|
|
{SensorConstants.SensorCalPolicy.AllowAlways, SensorConstants.SensorCalPolicy.DONT_ALLOW};
|
|
|
|
/// <summary>
|
|
/// returns all the available calibration policies
|
|
/// </summary>
|
|
public SensorConstants.SensorCalPolicy[] AvailableSensorCalPolicies => _availableCalPolicies;
|
|
|
|
private int _warningPeriod;
|
|
/// <summary>
|
|
/// returns the period in days before the calibration is due for a sensor to warn
|
|
/// </summary>
|
|
public int WarningPeriod
|
|
{
|
|
get => _warningPeriod;
|
|
set => SetProperty(ref _warningPeriod, value, "WarningPeriod");
|
|
}
|
|
|
|
private bool _useSensorFirstUseDate = true;
|
|
/// <summary>
|
|
/// indicates whether calibration interval starts after calibration or first use
|
|
/// 13065 Sensor "First Use" Date
|
|
/// </summary>
|
|
public bool UseSensorFirstUseDate
|
|
{
|
|
get => _useSensorFirstUseDate;
|
|
set => SetProperty(ref _useSensorFirstUseDate, value, "UseSensorFirstUseDate");
|
|
}
|
|
public bool SensorAssemblyEnabled
|
|
{
|
|
get => DTS.Common.Storage.DbOperations.GetConnectionDbVersion() >= Common.Constants.SENSOR_ASSEMBLY_DB_VERSION;
|
|
}
|
|
|
|
private bool _dontAllowDataCollectionIfOverused = false;
|
|
/// <summary>
|
|
/// Indicates whether or not to keep track of, and validate Test Setups based on, sensor overuse.
|
|
/// </summary>
|
|
public bool DontAllowDataCollectionIfOverused
|
|
{
|
|
get => SensorAssemblyEnabled && _dontAllowDataCollectionIfOverused;
|
|
set
|
|
{
|
|
SetProperty(ref _dontAllowDataCollectionIfOverused, value, "DontAllowDataCollectionIfOverused");
|
|
}
|
|
}
|
|
|
|
public bool AllowInspectBeforeUseEnabled
|
|
{
|
|
get => DTS.Common.Storage.DbOperations.GetConnectionDbVersion() >= Common.Constants.ALLOW_INSPECT_BEFORE_USE_DB_VERSION;
|
|
}
|
|
|
|
private bool _allowInspectBeforeUse = false;
|
|
//FB 43142
|
|
public bool AllowInspectBeforeUse
|
|
{
|
|
get => AllowInspectBeforeUseEnabled && _allowInspectBeforeUse;
|
|
set
|
|
{
|
|
SetProperty(ref _allowInspectBeforeUse, value, "AllowInspectBeforeUse");
|
|
}
|
|
}
|
|
|
|
private int _usageRemainingForWarning;
|
|
/// <summary>
|
|
/// A warning will be displayed if a sensor's usage is within this amount of its maximum.
|
|
/// </summary>
|
|
public int UsageRemainingForWarning
|
|
{
|
|
get => _usageRemainingForWarning;
|
|
set => SetProperty(ref _usageRemainingForWarning, value, "UsageRemainingForWarning");
|
|
}
|
|
private int _defaultMaxUsageAllowed;
|
|
/// <summary>
|
|
/// The default maximum number of uses for sensors
|
|
/// </summary>
|
|
public int DefaultMaxUsageAllowed
|
|
{
|
|
get => _defaultMaxUsageAllowed;
|
|
set => SetProperty(ref _defaultMaxUsageAllowed, value, "DefaultMaxUsageAllowed");
|
|
}
|
|
|
|
protected CalibrationPolicy(SensorConstants.SensorCalPolicy calPolicy,
|
|
int warningPeriod,
|
|
bool useFirstUseDate,
|
|
bool dontAllowDataCollectionIfOverused,
|
|
int usageRemainingForWarning,
|
|
int defaultMaxUsageAllowed,
|
|
bool allowInspectBeforeUse)
|
|
{
|
|
WarningPeriod = warningPeriod;
|
|
SelectedCalPolicy = calPolicy;
|
|
UseSensorFirstUseDate = useFirstUseDate;
|
|
DontAllowDataCollectionIfOverused = dontAllowDataCollectionIfOverused;
|
|
UsageRemainingForWarning = usageRemainingForWarning;
|
|
DefaultMaxUsageAllowed = defaultMaxUsageAllowed;
|
|
AllowInspectBeforeUse = allowInspectBeforeUse;
|
|
}
|
|
/// <summary>
|
|
/// the key for the setting in the db for calibration policy
|
|
/// </summary>
|
|
private const string SENSOR_CAL_POLICY_KEY = "SensorCalPolicy";
|
|
/// <summary>
|
|
/// the key for the setting in the db for the warning period
|
|
/// </summary>
|
|
private const string SENSOR_WARNING_PERIOD_KEY = "SensorCalWarningPeriodDays";
|
|
|
|
/// <summmary>
|
|
/// the key for sensor first use setting in db
|
|
/// </summary>
|
|
private const string SENSOR_FIRST_USE_KEY = "UseSensorFirstUseDate";
|
|
|
|
/// <summary>
|
|
/// the key for inspect before use setting in db
|
|
/// </summary>
|
|
private const string ALLOW_INSPECT_BEFORE_USE = "AllowInspectBeforeUse";
|
|
|
|
/// <summary>
|
|
/// The key for sensor overuse setting in db
|
|
/// </summary>
|
|
private const string SENSOR_OVERUSE_KEY = "DontAllowDataCollectionIfOverused";
|
|
|
|
/// <summary>
|
|
/// The key for sensor overuse setting in db
|
|
/// </summary>
|
|
private const string SENSOR_USAGE_REMAINING_WARNING_KEY = "UsageRemainingForWarning";
|
|
/// <summary>
|
|
/// The default maximum number of uses for sensors
|
|
/// </summary>
|
|
private const string SENSOR_DEFAULT_MAX_USAGE_KEY = "DefaultMaxUsageAllowed";
|
|
/// <summary>
|
|
/// retrieves calibration policies for sensors from the db and returns it
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public static ICalibrationPolicy GetCalibrationPolicy()
|
|
{
|
|
var s = SettingsDB.GetGlobalValue(SENSOR_CAL_POLICY_KEY, SensorConstants.CAL_SENSOR_POLICY_DEFAULT.ToString());
|
|
SensorConstants.SensorCalPolicy policy;
|
|
Enum.TryParse(s, out policy);
|
|
var period = SettingsDB.GetGlobalValueInt(SENSOR_WARNING_PERIOD_KEY,
|
|
SensorConstants.CAL_SENSOR_POLICY_WARNING_DAYS_DEFAULT);
|
|
var firstUse = SettingsDB.GetGlobalValueBool(SENSOR_FIRST_USE_KEY, SensorConstants.SENSOR_FIRST_USE_DEFAULT);
|
|
var allowInspectBeforeUse = SettingsDB.GetGlobalValueBool(ALLOW_INSPECT_BEFORE_USE, SensorConstants.AllowInspectBeforeUse);
|
|
var overuse = SettingsDB.GetGlobalValueBool(SENSOR_OVERUSE_KEY, SensorConstants.SENSOR_OVERUSE_DEFAULT);
|
|
var usageRemainingForWarning = SettingsDB.GetGlobalValueInt(SENSOR_USAGE_REMAINING_WARNING_KEY, SensorConstants.SENSOR_USAGE_REMAINING_FOR_WARNING_DEFAULT);
|
|
var defaultMaxUsage = SettingsDB.GetGlobalValueInt(SENSOR_DEFAULT_MAX_USAGE_KEY, SensorConstants.SENSOR_DEFAULT_MAX_USAGE_DEFAULT);
|
|
return new CalibrationPolicy(policy, period, firstUse, overuse, usageRemainingForWarning, defaultMaxUsage, allowInspectBeforeUse);
|
|
}
|
|
/// <summary>
|
|
/// writes calibration policies to the db
|
|
/// </summary>
|
|
/// <param name="policy"></param>
|
|
public static void WriteCalibrationPolicyToDb(ICalibrationPolicy policy)
|
|
{
|
|
SettingsDB.SetGlobalValue(SENSOR_CAL_POLICY_KEY, policy.SelectedCalPolicy.ToString());
|
|
SettingsDB.SetGlobalValueInt(SENSOR_WARNING_PERIOD_KEY, policy.WarningPeriod);
|
|
SensorConstants.SensorCalPolicyCurrent = policy.SelectedCalPolicy;
|
|
SensorConstants.SensorCalOutOfDateWarningPeriodDays = policy.WarningPeriod;
|
|
SettingsDB.SetGlobalValueBoolean(SENSOR_FIRST_USE_KEY, policy.UseSensorFirstUseDate);
|
|
SensorConstants.UseSensorFirstUseDate = policy.UseSensorFirstUseDate;
|
|
SettingsDB.SetGlobalValueBoolean(SENSOR_OVERUSE_KEY, policy.DontAllowDataCollectionIfOverused);
|
|
SensorConstants.DontAllowDataCollectionIfOverused = policy.DontAllowDataCollectionIfOverused;
|
|
SettingsDB.SetGlobalValueInt(SENSOR_USAGE_REMAINING_WARNING_KEY, policy.UsageRemainingForWarning);
|
|
SensorConstants.UsageRemainingForWarning = policy.UsageRemainingForWarning;
|
|
SettingsDB.SetGlobalValueInt(SENSOR_DEFAULT_MAX_USAGE_KEY, policy.DefaultMaxUsageAllowed);
|
|
SensorConstants.DefaultMaxUsageAllowed = policy.DefaultMaxUsageAllowed;
|
|
//FB 43142
|
|
SettingsDB.SetGlobalValueBoolean(ALLOW_INSPECT_BEFORE_USE, policy.AllowInspectBeforeUse);
|
|
SensorConstants.AllowInspectBeforeUse = policy.AllowInspectBeforeUse;
|
|
}
|
|
/// <summary>
|
|
/// restores the defaults in the db of calibration policies
|
|
/// </summary>
|
|
public static void RestoreDefaults()
|
|
{
|
|
SettingsDB.SetGlobalValue(SENSOR_CAL_POLICY_KEY, SensorConstants.CAL_SENSOR_POLICY_DEFAULT.ToString());
|
|
SettingsDB.SetGlobalValueInt(SENSOR_WARNING_PERIOD_KEY, SensorConstants.CAL_SENSOR_POLICY_WARNING_DAYS_DEFAULT);
|
|
SettingsDB.SetGlobalValueBoolean(SENSOR_FIRST_USE_KEY, SensorConstants.SENSOR_FIRST_USE_DEFAULT);
|
|
SettingsDB.SetGlobalValueBoolean(SENSOR_OVERUSE_KEY, SensorConstants.SENSOR_OVERUSE_DEFAULT);
|
|
SettingsDB.SetGlobalValueInt(SENSOR_USAGE_REMAINING_WARNING_KEY, SensorConstants.SENSOR_USAGE_REMAINING_FOR_WARNING_DEFAULT);
|
|
SettingsDB.SetGlobalValueInt(SENSOR_DEFAULT_MAX_USAGE_KEY, SensorConstants.SENSOR_DEFAULT_MAX_USAGE_DEFAULT);
|
|
//FB 43142
|
|
SettingsDB.SetGlobalValueBoolean(ALLOW_INSPECT_BEFORE_USE, SensorConstants.ALLOW_INSPECT_BEFORE_USE_DEFAULT);
|
|
}
|
|
|
|
public void ReadXML(System.Xml.XmlElement root)
|
|
{
|
|
foreach (var node in root.ChildNodes)
|
|
{
|
|
if (node is System.Xml.XmlElement) { ProcessXMLElement(node as System.Xml.XmlElement); }
|
|
}
|
|
}
|
|
private void ProcessXMLElement(System.Xml.XmlElement node)
|
|
{
|
|
if (Enum.TryParse(node.Name, out CalPolicyXMLFields field))
|
|
{
|
|
switch (field)
|
|
{
|
|
case CalPolicyXMLFields.SelectedCalPolicy:
|
|
if (Enum.TryParse(node.InnerText, out SensorConstants.SensorCalPolicy calPolicy))
|
|
{
|
|
SelectedCalPolicy = calPolicy;
|
|
}
|
|
break;
|
|
case CalPolicyXMLFields.UseSensorFirstUseDate: UseSensorFirstUseDate = Convert.ToBoolean(node.InnerText, System.Globalization.CultureInfo.InvariantCulture); break;
|
|
case CalPolicyXMLFields.WarningPeriod: WarningPeriod = Convert.ToInt32(node.InnerText, System.Globalization.CultureInfo.InvariantCulture); break;
|
|
case CalPolicyXMLFields.DontAllowDataCollectionIfOverused: DontAllowDataCollectionIfOverused = Convert.ToBoolean(node.InnerText, System.Globalization.CultureInfo.InvariantCulture); break;
|
|
case CalPolicyXMLFields.UsageRemainingForWarning: UsageRemainingForWarning = Convert.ToInt32(node.InnerText, System.Globalization.CultureInfo.InvariantCulture); break;
|
|
case CalPolicyXMLFields.DefaultMaxUsageAllowed: DefaultMaxUsageAllowed = Convert.ToInt32(node.InnerText, System.Globalization.CultureInfo.InvariantCulture); break;
|
|
default: throw new NotSupportedException("CalibrationPolicy::ProcessXMLElement unsupported field: " + field);
|
|
}
|
|
}
|
|
}
|
|
|
|
public void WriteXML(ref XmlWriter writer)
|
|
{
|
|
writer.WriteStartElement("CalibrationPolicy");
|
|
|
|
writer.WriteStartElement(CalPolicyXMLFields.SelectedCalPolicy.ToString());
|
|
writer.WriteString(SelectedCalPolicy.ToString());
|
|
writer.WriteEndElement();
|
|
|
|
writer.WriteStartElement(CalPolicyXMLFields.UseSensorFirstUseDate.ToString());
|
|
writer.WriteString(UseSensorFirstUseDate.ToString());
|
|
writer.WriteEndElement();
|
|
|
|
writer.WriteStartElement(CalPolicyXMLFields.WarningPeriod.ToString());
|
|
writer.WriteString(WarningPeriod.ToString(System.Globalization.CultureInfo.InvariantCulture));
|
|
writer.WriteEndElement();
|
|
|
|
writer.WriteStartElement(CalPolicyXMLFields.DontAllowDataCollectionIfOverused.ToString());
|
|
writer.WriteString(DontAllowDataCollectionIfOverused.ToString());
|
|
writer.WriteEndElement();
|
|
|
|
writer.WriteStartElement(CalPolicyXMLFields.UsageRemainingForWarning.ToString());
|
|
writer.WriteString(UsageRemainingForWarning.ToString());
|
|
writer.WriteEndElement();
|
|
|
|
writer.WriteStartElement(CalPolicyXMLFields.DefaultMaxUsageAllowed.ToString());
|
|
writer.WriteString(DefaultMaxUsageAllowed.ToString());
|
|
writer.WriteEndElement();
|
|
|
|
writer.WriteEndElement();
|
|
}
|
|
}
|
|
|
|
public enum CalPolicyXMLFields
|
|
{
|
|
SelectedCalPolicy,
|
|
UseSensorFirstUseDate,
|
|
WarningPeriod,
|
|
DontAllowDataCollectionIfOverused,
|
|
UsageRemainingForWarning,
|
|
DefaultMaxUsageAllowed
|
|
}
|
|
}
|