using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Serialization; using System.Data; using System.ComponentModel; namespace DatabaseImport { public class TestObjectTemplateChannel : INotifyPropertyChanged { #region INotifyPropertyChanged public event PropertyChangedEventHandler PropertyChanged; protected bool SetProperty(ref T storage, T value, string propertyName = null) { if (Equals(storage, value)) return false; storage = value; OnPropertyChanged(propertyName); return true; } protected void OnPropertyChanged(string propertyName = null) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } #endregion public const string NONISOCHANNELTYPE = "NONISO"; public const string SEPARATOR = "_X_"; public const string TEST_SPECIFIC_DOUT = "TSD_"; public enum DataStatusTypes { OK, ChannelFailed, MeaninglessData, NoData, QuestionableData, ScalingFactorApplied, SystemFailed, LinearisedData, NOVALUE } public enum StandardChannelProperties { NameOfTheChannel, DisplayOrder } public int DisplayOrder { get => Convert.ToInt32(_channelProperties[StandardChannelProperties.DisplayOrder.ToString()].Value, System.Globalization.CultureInfo.InvariantCulture); set => _channelProperties[StandardChannelProperties.DisplayOrder.ToString()].Value = value.ToString(System.Globalization.CultureInfo.InvariantCulture); } public int TemplateChannelId { get; set; } public string NameOfTheChannel { get { var s = _channelProperties[StandardChannelProperties.NameOfTheChannel.ToString()].Value as string; if (!string.IsNullOrWhiteSpace(s) && s != DataStatusTypes.NOVALUE.ToString()) return s; if (Name.StartsWith(TEST_SPECIFIC_DOUT)) { return $"(Digital Output Setting){Name}"; } return Name; } set { _channelProperties[StandardChannelProperties.NameOfTheChannel.ToString()].Value = value; OnPropertyChanged("NameOfTheChannel"); } } public string Name { get { var tokens = Channel.Text_L1.Split(new[] { SEPARATOR }, StringSplitOptions.None); return tokens.Last(); } } public ISO.TestObjectTemplate Template { get; private set; } private readonly List _channelPropertyNames = new List(); private Dictionary _channelProperties = new Dictionary(); public MMEPossibleChannels Channel { get; private set; } public bool LocalOnly { get; set; } // #endregion // #region methods protected object GetProperty(string name, object defaultValue) { if (!_channelProperties.ContainsKey(name)) { _channelProperties[name] = new ChannelProperty(name, defaultValue); } return _channelProperties[name].Value; } protected void SetProperty(string name, object value) { if (!_channelProperties.ContainsKey(name)) { _channelProperties[name] = new ChannelProperty(name, value); } else { _channelProperties[name].Value = value; } OnPropertyChanged("name"); // ? variable name instead of "name" } private void AddStandardProperties() { if (null != _channelProperties) { _channelProperties.Clear(); _channelProperties = null; } _channelProperties = new Dictionary(); var scp = Enum.GetValues(typeof(StandardChannelProperties)).Cast().ToArray(); foreach (var p in scp) { ChannelProperty cp; switch (p) { case StandardChannelProperties.DisplayOrder: cp = new ChannelProperty(p.ToString(), "0"); break; case StandardChannelProperties.NameOfTheChannel: cp = new ChannelProperty("Name of the channel", "NOVALUE"); break; default: cp = new ChannelProperty(p.ToString(), "NOVALUE"); break; } _channelProperties.Add(p.ToString(), cp); } } public void SetTemplate(ISO.TestObjectTemplate template) { Template = template; } private bool _bRequired; /// /// Required channel property raises the OnRequiredChanged event /// public bool Required { get => _bRequired; set { _bRequired = value; //OnRequiredChanged(new RequiredChangedEventArgs { NewValue = value }); _bRequired = value; } } public TestObjectTemplateChannel(DataRow dr, ISO.TestObjectTemplate template, ref ISO13499FileDb db) { Template = template; AddStandardProperties(); TemplateChannelId = Convert.ToInt32(dr["TemplateChannelId"]); _channelProperties[StandardChannelProperties.NameOfTheChannel.ToString()].Value = (string)dr["NameOfTheChannel"]; _channelProperties[StandardChannelProperties.DisplayOrder.ToString()].Value = 0; if (!DBNull.Value.Equals(dr["DisplayOrder"])) { _channelProperties[StandardChannelProperties.DisplayOrder.ToString()].Value = Convert.ToInt32(dr["DisplayOrder"]).ToString(System.Globalization.CultureInfo.InvariantCulture); } _bRequired = Convert.ToBoolean(dr["Required"]); LocalOnly = Convert.ToBoolean(dr["LocalOnly"]); var channelId = Convert.ToInt64(dr["MMEChannelId"]); var channelType = Convert.ToInt32(dr["MMEChannelType"]); Channel = db.GetPossibleChannel(channelId, channelType); } public TestObjectTemplateChannel(TestObjectTemplateChannel copy, ISO.TestObjectTemplate template) { TemplateChannelId = copy.TemplateChannelId; LocalOnly = copy.LocalOnly; _bRequired = copy.Required; Channel = new MMEPossibleChannels(copy.Channel); _channelProperties = new Dictionary(); using (var e = copy._channelProperties.GetEnumerator()) { while (e.MoveNext()) { _channelProperties[e.Current.Key] = new ChannelProperty(e.Current.Value); } } _channelPropertyNames = new List(copy._channelPropertyNames.ToArray()); Template = template; } public TestObjectTemplateChannel(MMEPossibleChannels channel) { Channel = channel; AddStandardProperties(); var scp = Enum.GetValues(typeof(StandardChannelProperties)).Cast().ToArray(); foreach (var p in scp) { switch (p) { case StandardChannelProperties.DisplayOrder: _channelProperties[StandardChannelProperties.DisplayOrder.ToString()].Value = Convert.ToInt32(Channel.Id).ToString(System.Globalization.CultureInfo.InvariantCulture); break; case StandardChannelProperties.NameOfTheChannel: break; } } } //TODO: move to separate class public class ChannelProperty : ISerializable { public string Name { get; } public object Value { get; set; } = "NOVALUE"; public ChannelProperty(ChannelProperty copy) { Name = copy.Name; Value = copy.Value; } public ChannelProperty(string name, object value) { Name = name; Value = value; } public void GetObjectData(SerializationInfo info, StreamingContext context) { info.AddValue("PropertyName", Name); info.AddValue("PropertyValue", Value, Value.GetType()); } } } }