using DTS.Common.Base; using DTS.Common.Enums; using DTS.Common.Interface.Sensors; using DTS.Common.Interface.Sensors.SoftwareFilters; using System; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; using System.Data; using System.Linq; namespace DTS.Common.Classes.Sensors { /// /// Implementation of IDigitalInDbRecord /// represents a record of a digital input setting in the database /// /// public class DigitalInDbRecord : BasePropertyChanged, IDigitalInDbRecord { private int _id = -1; /// /// Database id of record /// [Key] public int Id { get => _id; set => SetProperty(ref _id, value, "Id"); } private string _serialNumber = ""; /// /// serial number or name of setting /// public string SerialNumber { get => _serialNumber; set => SetProperty(ref _serialNumber, value, "SerialNumber"); } private DigitalInputModes _settingMode = DigitalInputModes.TLH; /// /// Input mode for setting /// public DigitalInputModes Mode { get => _settingMode; set => SetProperty(ref _settingMode, value, "Mode"); } private IDigitalInputScaleMultiplier _scaleMultiplier = new DigitalInputScaleMultiplier(); /// /// ScaleMultiplier, defines how to interpret output in terms of /// units or active/default value of input state /// [Required] [StringLength(50)] public IDigitalInputScaleMultiplier ScaleMultiplier { get => _scaleMultiplier; set => SetProperty(ref _scaleMultiplier, value, "ScaleMultiplier"); } private DateTime _lastModified = DateTime.MinValue; [Column(TypeName = "datetime")] /// /// when setting was last modified /// public DateTime LastModified { get => _lastModified; set => SetProperty(ref _lastModified, value, "LastModified"); } private string _lastModifiedBy = ""; /// /// user that last modified setting in db /// [Required] [StringLength(50)] public string LastModifiedBy { get => _lastModifiedBy; set => SetProperty(ref _lastModifiedBy, value, "LastModifiedBy"); } private string _eId = ""; /// /// Electronic ID for digital input setting /// (dallas or TeDS ID value) /// [Required] [Column("eId")] [StringLength(50)] public string EID { get => _eId; set => SetProperty(ref _eId, value, "EID"); } private string _isoCode = ""; /// /// ISO 13499 code for digital input data collected using this setting /// this is the default code when the setting is applied to an input channel /// but can be changed in group or test settings /// [Required] [StringLength(50)] public string ISOCode { get => _isoCode; set => SetProperty(ref _isoCode, value, "ISOCode"); } private string _isoChannelName = ""; /// /// the associated ISO 13499 channel name to apply to a channel when applying /// setting to an input channel /// but can be changed in group or test settings /// [Required] [StringLength(255)] public string ISOChannelName { get => _isoChannelName; set => SetProperty(ref _isoChannelName, value, "ISOChannelName"); } private string _userCode = ""; /// /// the user code to apply to a channel when applying setting to a channel /// can be changed in group or test settings /// [Required] [StringLength(50)] public string UserCode { get => _userCode; set => SetProperty(ref _userCode, value, "UserCode"); } private string _userChannelName = ""; /// /// user channel name to apply to a channel when applying setting to a channel /// can be changed in group or test settings /// [Required] [StringLength(255)] public string UserChannelName { get => _userChannelName; set => SetProperty(ref _userChannelName, value, "UserChannelName"); } private string _userValue1 = ""; /// /// user value to carry through to collected data channel when collecting data with this setting /// [StringLength(255)] public string UserValue1 { get => _userValue1; set => SetProperty(ref _userValue1, value, "UserValue1"); } private string _userValue2 = ""; /// /// user value to carry through to collected data channel when collecting data with this setting /// [StringLength(255)] public string UserValue2 { get => _userValue2; set => SetProperty(ref _userValue2, value, "UserValue2"); } private string _userValue3 = ""; /// /// user value to carry through to collected data channel when collecting data with this setting /// [StringLength(255)] public string UserValue3 { get => _userValue3; set => SetProperty(ref _userValue3, value, "UserValue3"); } private byte[] _userTags; /// /// bytes describing tag ids for tags associated with setting /// see ITagAware for more information /// public byte[] UserTags { get => _userTags; set => SetProperty(ref _userTags, value, "UserTags"); } private string _measurementUnit = "V"; /// /// measurement unit for collected data, for example /// 'V' or Volts /// [Required] [StringLength(50)] public string MeasurementUnit { get => _measurementUnit; set => SetProperty(ref _measurementUnit, value, "MeasurementUnit"); } private IFilterClass _filterClass; /// /// software filter class (applied when viewing data) to apply to collected data by default /// can be changed when viewing or exporting, this is just a default filter /// [Required] [StringLength(50)] public IFilterClass FilterClass { get => _filterClass; set => SetProperty(ref _filterClass, value, "FilterClass"); } private bool _doNotUse = false; /// /// a flag indicating setting should not be used for arbitrary user specified reason /// public bool DoNotUse { get => _doNotUse; set => SetProperty(ref _doNotUse, value, "DoNotUse"); } private bool _broken = false; /// /// a flag indicating setting should not be used because it is currently broken /// public bool Broken { get => _broken; set => SetProperty(ref _broken, value, "Broken"); } public DigitalInDbRecord() { } public DigitalInDbRecord(ISensorData copy, byte[] tagBlockBytes, IDigitalInputScaleMultiplier digitalScaleMultiplier) { Id = copy.DatabaseId; SerialNumber = copy.SerialNumber; ISOCode = copy.ISOCode; ISOChannelName = copy.ISOChannelName; UserCode = copy.UserCode; UserChannelName = copy.UserChannelName; Broken = copy.Broken; DoNotUse = copy.DoNotUse; LastModified = copy.LastModified; LastModifiedBy = copy.LastUpdatedBy; Mode = copy.InputMode; MeasurementUnit = copy.DIUnits; FilterClass = new FilterClass(copy.FilterClass.FClass, copy.FilterClass.Frequency); ISOCode = copy.ISOCode; EID = copy.EID; UserValue1 = copy.UserValue1; UserValue2 = copy.UserValue2; UserValue3 = copy.UserValue3; if (null == tagBlockBytes) { UserTags = null; } else { if (tagBlockBytes.Any()) { UserTags = new byte[tagBlockBytes.Length]; Array.Copy(tagBlockBytes, UserTags, tagBlockBytes.Length); } else { UserTags = new byte[0]; } } ScaleMultiplier.FromDbSerializeString(digitalScaleMultiplier.ToSerializeDbString()); } public DigitalInDbRecord(IDataReader reader) { Id = Utility.GetInt(reader, "Id", -1); SerialNumber = Utility.GetString(reader, "SerialNumber", string.Empty); ISOCode = Utility.GetString(reader, "ISOCode", string.Empty); ISOChannelName = Utility.GetString(reader, "ISOChannelName", string.Empty); UserCode = Utility.GetString(reader, "UserCode", string.Empty); UserChannelName = Utility.GetString(reader, "UserChannelName", string.Empty); Broken = Utility.GetBool(reader, "Broken", false); DoNotUse = Utility.GetBool(reader, "DoNotUse", false); LastModified = Utility.GetDateTime(reader, "LastModified", DateTime.MinValue); LastModifiedBy = Utility.GetString(reader, "LastModifiedBy", string.Empty); Mode = (DigitalInputModes)Utility.GetInt(reader, "SettingMode"); MeasurementUnit = Utility.GetString(reader, "MeasurementUnit"); FilterClass = new FilterClass(Utility.GetString(reader, "FilterClass")); if (ISOCode.Length < 16) { ISOCode = ISOCode.PadRight(16, '?'); } var s = ISOCode.ToCharArray(); s[15] = '0'; ISOCode = new string(s); EID = Utility.GetString(reader, "eId", string.Empty); UserValue1 = Utility.GetString(reader, "UserValue1", string.Empty); UserValue2 = Utility.GetString(reader, "UserValue2", string.Empty); UserValue3 = Utility.GetString(reader, "UserValue3", string.Empty); UserTags = (byte[])reader["UserTags"]; ScaleMultiplier.FromDbSerializeString(Utility.GetString(reader, "ScaleMultiplier")); } public DigitalInDbRecord(IDigitalInDbRecord copy) { Id = copy.Id; SerialNumber = copy.SerialNumber; ISOCode = copy.ISOCode; ISOChannelName = copy.ISOChannelName; UserCode = copy.UserCode; UserChannelName = copy.UserChannelName; Broken = copy.Broken; DoNotUse = copy.DoNotUse; LastModified = copy.LastModified; LastModifiedBy = copy.LastModifiedBy; Mode = copy.Mode; MeasurementUnit = copy.MeasurementUnit; FilterClass = new FilterClass(copy.FilterClass.FClass, copy.FilterClass.Frequency); ISOCode = copy.ISOCode; EID = copy.EID; UserValue1 = copy.UserValue1; UserValue2 = copy.UserValue2; UserValue3 = copy.UserValue3; if (null == copy.UserTags) { UserTags = null; } else { if (copy.UserTags.Any()) { var userTags = new byte[copy.UserTags.Length]; Array.Copy(copy.UserTags, userTags, copy.UserTags.Length); UserTags = userTags; } else { UserTags = new byte[0]; } } ScaleMultiplier.FromDbSerializeString(copy.ScaleMultiplier.ToSerializeDbString()); } } }