using DTS.Common; using DTS.Common.Classes.Sensors; using DTS.Common.Enums.Sensors; using DTS.Common.Interface; using DTS.Common.Interface.Sensors; using DTS.Common.Interface.Sensors.SoftwareFilters; using DTS.SensorDB; using Prism.Commands; using System; using System.ComponentModel; // ReSharper disable InconsistentNaming // ReSharper disable RedundantDefaultMemberInitializer // ReSharper disable CheckNamespace // ReSharper disable UnassignedGetOnlyAutoProperty namespace DTS.Viewer.ChartOptions.Model { public class TestModificationModel : ITestModificationModel { #region Properties /// /// the calibration date for cal on channel (or empty if not applicable) /// public string CalDate { get { if (null == Cal) { return string.Empty; } return Cal.CalibrationDate.ToShortDateString(); } } /// /// the modify date for cal on channel (or empty if not applicable) /// public string ModifyDate { get { if (null == Cal) { return string.Empty; } return $"{Cal.ModifyDate.ToShortDateString()} {Cal.ModifyDate.ToShortTimeString()}"; } } /// /// sensitivity in calibration record for latest cal for sensor on channel /// public double CalSensitivity { get { if (null == Cal) { return double.NaN; } if (null == Cal.Records || null == Cal.Records.Records || 0 == Cal.Records.Records.Length) { return double.NaN; } return Cal.Records.Records[0].Sensitivity; } set { if (null == Cal) { return; } if (null == Cal.Records || null == Cal.Records.Records || 0 == Cal.Records.Records.Length) { return; } Cal.Records.Records[0].Sensitivity = value; } } private void RebindCalProperties() { OnPropertyChanged("CalSensitivity"); OnPropertyChanged("NonLinear"); OnPropertyChanged("ProportionalToExcitation"); OnPropertyChanged("ShowSensorCal"); OnPropertyChanged("CalDate"); OnPropertyChanged("ModifyDate"); } /// /// whether latest calibration for channel is proportional to excitation or not /// public bool ProportionalToExcitation { get { if (null == Cal) { return false; } return Cal.IsProportional; } } /// /// whether you should show sensor cal or not /// public bool ShowSensorCal { get { return null != Cal && null != Sensor && !NonLinear; } } /// /// whether calibration is non linear or not /// public bool NonLinear { get { if (null == Cal) { return false; } return Cal.NonLinear; } } private ISensorCalDbRecord _cal; /// /// the latest calibration for channel /// public ISensorCalDbRecord Cal { get => _cal; set { _cal = value; RebindCalProperties(); } } private ISensorDbRecord _sensor; /// /// sensor corresponding to channel /// public ISensorDbRecord Sensor { get => _sensor; set { _sensor = value; RebindCalProperties(); } } private ITestChannel _selectedChannel; public ITestChannel SelectedChannel { get => _selectedChannel; set { if (value != _selectedChannel) { _selectedChannel = value; OnPropertyChanged("SelectedChannel"); } } } public bool IsModifiedDescription => !Description.Equals(SelectedChannel?.ChannelDescriptionString); private string _description = ""; public string Description { get => _description; set { if (value != _description) { _description = value; OnPropertyChanged("Description"); OnPropertyChanged("IsModifiedDescription"); } } } public bool IsModifiedEuMultiplier => !EuMultiplier.Equals(SelectedChannel?.Multiplier); private double _euMultiplier = 0D; public double EuMultiplier { get => _euMultiplier; set { if (value != _euMultiplier) { _euMultiplier = value; OnPropertyChanged("EuMultiplier"); OnPropertyChanged("IsModifiedEuMultiplier"); } } } public bool IsModifiedEuOffset => !EuOffset.Equals(SelectedChannel?.UserOffsetEu); private double _euOffset = 0D; public double EuOffset { get => _euOffset; set { if (value != _euOffset) { _euOffset = value; OnPropertyChanged("EuOffset"); OnPropertyChanged("IsModifiedEuOffset"); } } } public bool IsModifiedT0 => !T0.Equals(0D); private double _t0 = 0D; public double T0 { get => _t0; set { if (value != _t0) { _t0 = value; OnPropertyChanged("T0"); OnPropertyChanged("IsModifiedT0"); } } } private bool _isModified; /// /// indicates whether any values have changed from default for the selected channel /// public bool IsModified { get => _isModified; private set { if (value != _isModified) { _isModified = value; OnPropertyChanged("IsModified"); } } } private bool _isModifiedLineFit; public bool IsModifiedLineFit { get { _isModifiedLineFit = !T1.Equals(0D) || !T2.Equals(0D); return _isModifiedLineFit; } set { if (value != _isModifiedLineFit) { _isModifiedLineFit = value; OnPropertyChanged("IsModifiedLineFit"); } } } /// /// Line fit start point (ms) /// private double _t1 = 0D; public double T1 { get => _t1; set { if (!_t1.Equals(value)) { _t1 = value; OnPropertyChanged("T1"); OnPropertyChanged("IsModifiedLineFit"); } } } /// /// Line fit end point (ms) /// private double _t2 = 0D; public double T2 { get => _t2; set { if (!_t2.Equals(value)) { _t2 = value; OnPropertyChanged("T2"); OnPropertyChanged("IsModifiedLineFit"); } } } public bool IsModifiedSensitivity => !Sensitivity.Equals(SelectedChannel?.Sensitivity); private double _sensitivity = 0D; public double Sensitivity { get => _sensitivity; set { if (!_sensitivity.Equals(value)) { _sensitivity = value; OnPropertyChanged("Sensitivity"); OnPropertyChanged("IsModifiedSensitivity"); } } } //FB 13120 updated the IsModifiedFilterto use filter class public bool IsModifiedFilter { get { var sf = FilterClass.GetFilterClassFromString(SelectedChannel?.SoftwareFilter); return !SelectedFilter.Equals(sf); } } //FB 13120 selected filter private IFilterClass _selectedFilter = new FilterClass(FilterClassType.Unfiltered); public IFilterClass SelectedFilter { get => _selectedFilter; set { if (!_selectedFilter.Equals(value)) { _selectedFilter = value; OnPropertyChanged("SelectedFilter"); OnPropertyChanged("IsModifiedFilter"); } } } /// /// T0 Mode indicates whether T0 changes will be applied to DAS or test /// private T0Mode _t0Mode = T0Mode.Test; public T0Mode T0Mode { get => _t0Mode; set { var newValue = !IsT0ModeTestOnly ? value : T0Mode.Test; if (newValue != _t0Mode) { _t0Mode = newValue; OnPropertyChanged("T0Mode"); } } } private bool _isT0ModeTestOnly = false; public bool IsT0ModeTestOnly { get => _isT0ModeTestOnly; internal set { if (value != _isT0ModeTestOnly) { _isT0ModeTestOnly = value; OnPropertyChanged("IsT0ModeTestOnly"); } } } private bool _enableSensitivityControl = true; public bool EnableSensitivityControl { get => _enableSensitivityControl; internal set { if (value != _enableSensitivityControl) { _enableSensitivityControl = value; OnPropertyChanged("EnableSensitivityControl"); } } } private bool _enableLineFitControl = true; public bool EnableLineFitControl { get => _enableLineFitControl; internal set { if (value != _enableLineFitControl) { _enableLineFitControl = value; OnPropertyChanged("EnableLineFitControl"); } } } private bool _enableEUOffsetControl = true; public bool EnableEUOffsetControl { get => _enableEUOffsetControl; internal set { if (value != _enableEUOffsetControl) { _enableEUOffsetControl = value; OnPropertyChanged("EnableEUOffsetControl"); } } } private bool _enableEUMultiplierControl = true; public bool EnableEUMultiplierControl { get => _enableEUMultiplierControl; internal set { if (value != _enableEUMultiplierControl) { _enableEUMultiplierControl = value; OnPropertyChanged("EnableEUMultiplierControl"); } } } private bool _enableFilterControl = true; public bool EnableFilterControl { get => _enableFilterControl; internal set { if (value != _enableFilterControl) { _enableFilterControl = value; OnPropertyChanged("EnableFilterControl"); } } } private bool _enableDescriptionControl = true; public bool EnableDescriptionControl { get => _enableDescriptionControl; internal set { if (value != _enableDescriptionControl) { _enableDescriptionControl = value; OnPropertyChanged("EnableDescriptionControl"); } } } private bool _IsDataFlagEnabled = true; public bool IsDataFlagEnabled { get => _IsDataFlagEnabled; internal set { if (value != _IsDataFlagEnabled) { _IsDataFlagEnabled = value; OnPropertyChanged("IsDataFlagEnabled"); } } } private bool _IsT0Enabled = true; public bool IsT0Enabled { get => _IsT0Enabled; internal set { if (value != _IsT0Enabled) { _IsT0Enabled = value; OnPropertyChanged("IsT0Enabled"); } } } public bool IsModifiedDataFlag => null != SelectedChannel && !SelectedDataFlag.Equals((DataFlag)SelectedChannel.DataFlag); private DataFlag _dataFlag = DataFlag.None; public DataFlag SelectedDataFlag { get => _dataFlag; set { if (value != _dataFlag) { _dataFlag = value; OnPropertyChanged("SelectedDataFlag"); OnPropertyChanged("IsModifiedDataFlag"); } } } private ITestModificationViewModel _parent; public ITestModificationViewModel Parent { get => _parent; set { if (value != _parent) { _parent = value; OnPropertyChanged("Parent"); } } } /// ///Occurs when a property value changes. /// public event PropertyChangedEventHandler PropertyChanged; public void OnPropertyChanged(string propertyName) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); if (propertyName != "IsModified" && propertyName != "LineFitModified" && propertyName != "SelectedDataFlag") { IsModified = (IsModifiedDescription || IsModifiedEuMultiplier || IsModifiedEuOffset || IsModifiedFilter || IsModifiedLineFit || IsModifiedSensitivity || IsModifiedT0) && SelectedChannel != null; //can't modify nothing } if (propertyName == "IsT0ModeTestOnly") { T0Mode = _t0Mode; //if we've changed the global setting, run a check of the current mode } Parent?.PublishChanges(); } public bool IsSaved { get; } #endregion Properties private DelegateCommand _UpdateDatabaseCommand; public DelegateCommand UpdateDatabaseCommand => _UpdateDatabaseCommand ?? (_UpdateDatabaseCommand = new DelegateCommand(UpdateDatabaseMethod)); private void UpdateDatabaseMethod() { Parent.UpdateDatabaseMethod(); } #region Functions /// /// returns true if T0 is valid /// right now this just means T0 falls between the start and end of the current channel's dataset /// /// public bool ValidateT0() { var valid = true; if (null == SelectedChannel) { return true; } if (null == SelectedChannel.ParentModule) { return true; } var startRecordSampleNumber = Convert.ToDecimal(SelectedChannel.ParentModule.StartRecordSampleNumber); var triggerSampleNumber = Convert.ToDecimal(SelectedChannel.ParentModule.TriggerSampleNumbers[0]); var totalSamples = Convert.ToDecimal(SelectedChannel.ParentModule.NumberOfSamples); var sampleRate = Convert.ToDecimal(SelectedChannel.ParentModule.SampleRateHz); //note that T0 is always in ms, so we just deal in ms here (sampleRate is hz[sps]) var startTime = -1000 * (triggerSampleNumber - startRecordSampleNumber) / sampleRate; var endTime = 1000 * (totalSamples - triggerSampleNumber + startRecordSampleNumber) / sampleRate; var t0 = Convert.ToDecimal(T0); if (t0 < startTime || t0 > endTime) { return false; } return valid; } #endregion Functions } }