using System; using System.ComponentModel; using DTS.Common.Events.RegionOfInterest; using System.Linq; using DTS.Common.Interface.RegionOfInterest; using Prism.Events; using Prism.Ioc; using DTS.Common.Enums.Sensors; namespace DTS.Common.Classes.TestSetups { [Serializable] public class RegionOfInterest : IRegionOfInterest { /// /// holds whether a test setup is currently deserializing ROIs or not /// this was necessary to prevent deserialization from firing property changed notifications on construction /// 27034 ROI channel assignments are not saved when modifying and clicking save in test setup /// public static bool Deserializing { get; set; } = false; public RegionOfInterest() { Suffix = string.Empty; _start = double.NegativeInfinity; _end = double.PositiveInfinity; IsEnabled = true; IsDefault = true; _channelNames = null; _channelIds = null; } public RegionOfInterest(bool isDefault = false) : this() { IsDefault = isDefault; } public RegionOfInterest(string suffix = "", bool isDefault = false, double start = -1D, double end = 1D) : this(isDefault) { Suffix = suffix; _start = start; _end = end; _channelNames = new string[0]; _channelIds = new long[0]; } private string _suffix = string.Empty; public string Suffix { get => _suffix; set { if (!string.IsNullOrWhiteSpace(value) && !(value.StartsWith("_") && string.IsNullOrWhiteSpace(value.Substring(1)))) { _suffix = value.StartsWith("_") ? value : "_" + value; } OnPropertyChanged("Suffix"); } } private double _start = double.MinValue; public double Start { get => _start; set { if (value >= End) { value = End - 0.01; } if (value != _start) { //FB 43462 Infinity is not a valid value don't notify var notify = !double.IsInfinity(_start); _start = value; OnPropertyChanged("Start"); if (notify) { NotifyChanged(); } } } } private double _end = double.MaxValue; public double End { get => _end; set { if (value <= Start) { value = Start + 0.01; } if (value != _end) { //FB 43462 Infinity is not a valid value don't notify var notify = !double.IsInfinity(_end); _end = value; OnPropertyChanged("End"); if (notify) { NotifyChanged(); } } } } /// /// notifies any consumers that ROI has changed (if it should notify) /// 27034 ROI channel assignments are not saved when modifying and clicking save in test setup /// private void NotifyChanged() { try { if (Deserializing) { return; } if (null == ContainerLocator.Container) { return; } var eventAggregator = ContainerLocator.Container.Resolve(); eventAggregator.GetEvent().Publish(this); } catch (Exception) { } } private bool _isEnabled = true; public bool IsEnabled { get => _isEnabled; set { _isEnabled = value; OnPropertyChanged("IsEnabled"); } } //17954 Modify ROI does not show in UI //deserialize wasn't setting this, seems like it should public bool IsDefault { get; set; } public event PropertyChangedEventHandler PropertyChanged; private void OnPropertyChanged(string propertyName = null) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } public void ResetSuffix() { _suffix = string.Empty; } private string[] _channelNames = new string[0]; public string[] ChannelNames { get => _channelNames; set { //FB 43462 if the value is null first initialize it to empty array if (_channelNames == null) { _channelNames = value ?? new string[] { }; OnPropertyChanged("ChannelNames"); } //FB 43462 check all elements to determeine equal if (!_channelNames.SequenceEqual(value)) { _channelNames = value ?? new string[] { }; OnPropertyChanged("ChannelNames"); NotifyChanged(); } } } private long[] _channelIds; public long[] ChannelIds { get => _channelIds; set { //FB 43462 if the value is null first initialize it to empty array if (_channelIds == null) { _channelIds = value ?? new long[] { }; OnPropertyChanged("ChannelIds"); } //FB 43462 check all elements to determeine equal if (!_channelIds.SequenceEqual(value)) { _channelIds = value ?? new long[] { }; OnPropertyChanged("ChannelIds"); NotifyChanged(); } } } /// /// return a TSA_Embedded channel on a TSR AIR differently than a TSA_Embedded channel /// on other DAS (it's a Voltage input channel if on non-TSR AIR DAS). /// /// /// /// /// /// public static string GetAnalogChanName(string serialNumber, string hardwareChannelName, string startOfHardware, string originalChannelName = "") { var chanName = string.Empty; if ((serialNumber == SensorConstants.TEST_SPECIFIC_ANALOG_SERIAL) && !hardwareChannelName.StartsWith($"[{startOfHardware}") || (serialNumber == SensorConstants.VOLTAGE_INPUT)) { //34350 This is a Voltage input channel, so parse it differently chanName = GetChanName(SensorConstants.VOLTAGE_INPUT, hardwareChannelName, originalChannelName); } else { chanName = GetChanName(serialNumber, hardwareChannelName, originalChannelName); } return chanName; } /// /// Takes a Serial Number, Hardware Channel Name, and Original Channel Name and prepends the hardware channel /// based on whether or not it's an embedded (TSR AIR channel), or has a parent DAS. /// /// /// public static string GetChanName(string serialNumber, string hardwareChannelName, string originalChannelName = "") { var chanName = string.Empty; if (serialNumber == SensorConstants.TEST_SPECIFIC_ANALOG_SERIAL) { chanName = hardwareChannelName + "\\" + originalChannelName; } else { var indexOfColon = hardwareChannelName.IndexOf(":"); if (indexOfColon == -1) { chanName = hardwareChannelName + "\\" + serialNumber; } else { var dasSerialNumber = hardwareChannelName.Substring(indexOfColon).Replace(':', '['); chanName = dasSerialNumber + "\\" + serialNumber; } } return chanName; } public static string RemoveParentDASName(string entireChannelName) { var chanName = entireChannelName; var indexOfColon = entireChannelName.IndexOf(":"); if (indexOfColon > -1) { chanName = entireChannelName.Substring(indexOfColon).Replace(':', '['); } return chanName; } /// /// sets the channel names without notifying of a change /// allows a constructor to bypass notifications /// 27034 ROI channel assignments are not saved when modifying and clicking save in test setup /// /// public void SetChannelNamesNoNotify(string[] names) { _channelNames = names; } /// /// sets the channel ids without notifying of a change /// allows a constructor to bypass notifications /// /// public void SetChannelIdsNoNotify(long[] ids) { _channelIds = ids; } /// /// returns string after "Assigned by ID" /// /// /// public static string RemoveAssignedByIDFromHardwareString(string chHardware) { var newChHardware = string.Empty; if (!string.IsNullOrWhiteSpace(chHardware)) { newChHardware = chHardware.TrimStart(); if (chHardware.StartsWith("Assigned by ID")) { newChHardware = chHardware.Replace("Assigned by ID", ""); newChHardware = newChHardware.TrimStart(); } if (newChHardware.Contains("[")) { newChHardware = newChHardware.TrimStart(); newChHardware = newChHardware.TrimStart('\\').Trim(); } } return newChHardware; } } }