Files
DP44/Common/DTS.CommonCore/.svn/pristine/35/35010c11ce932212c81896040adc5c0942625321.svn-base
2026-04-17 14:55:32 -04:00

321 lines
12 KiB
Plaintext

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
{
/// <summary>
/// Implementation of IDigitalInDbRecord
/// represents a record of a digital input setting in the database
/// <inheritdoc cref="IDigitalInDbRecord"/>
/// </summary>
public class DigitalInDbRecord : BasePropertyChanged, IDigitalInDbRecord
{
private int _id = -1;
/// <summary>
/// Database id of record
/// </summary>
[Key]
public int Id
{
get => _id;
set => SetProperty(ref _id, value, "Id");
}
private string _serialNumber = "";
/// <summary>
/// serial number or name of setting
/// </summary>
public string SerialNumber
{
get => _serialNumber;
set => SetProperty(ref _serialNumber, value, "SerialNumber");
}
private DigitalInputModes _settingMode = DigitalInputModes.TLH;
/// <summary>
/// Input mode for setting
/// </summary>
public DigitalInputModes Mode
{
get => _settingMode;
set => SetProperty(ref _settingMode, value, "Mode");
}
private IDigitalInputScaleMultiplier _scaleMultiplier = new DigitalInputScaleMultiplier();
/// <summary>
/// ScaleMultiplier, defines how to interpret output in terms of
/// units or active/default value of input state
/// </summary>
[Required]
[StringLength(50)]
public IDigitalInputScaleMultiplier ScaleMultiplier
{
get => _scaleMultiplier;
set => SetProperty(ref _scaleMultiplier, value, "ScaleMultiplier");
}
private DateTime _lastModified = DateTime.MinValue;
[Column(TypeName = "datetime")]
/// <summary>
/// when setting was last modified
/// </summary>
public DateTime LastModified
{
get => _lastModified;
set => SetProperty(ref _lastModified, value, "LastModified");
}
private string _lastModifiedBy = "";
/// <summary>
/// user that last modified setting in db
/// </summary>
[Required]
[StringLength(50)]
public string LastModifiedBy
{
get => _lastModifiedBy;
set => SetProperty(ref _lastModifiedBy, value, "LastModifiedBy");
}
private string _eId = "";
/// <summary>
/// Electronic ID for digital input setting
/// (dallas or TeDS ID value)
/// </summary>
[Required]
[Column("eId")]
[StringLength(50)]
public string EID
{
get => _eId;
set => SetProperty(ref _eId, value, "EID");
}
private string _isoCode = "";
/// <summary>
/// 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
/// </summary>
[Required]
[StringLength(50)]
public string ISOCode
{
get => _isoCode;
set => SetProperty(ref _isoCode, value, "ISOCode");
}
private string _isoChannelName = "";
/// <summary>
/// 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
/// </summary>
[Required]
[StringLength(255)]
public string ISOChannelName
{
get => _isoChannelName;
set => SetProperty(ref _isoChannelName, value, "ISOChannelName");
}
private string _userCode = "";
/// <summary>
/// the user code to apply to a channel when applying setting to a channel
/// can be changed in group or test settings
/// </summary>
[Required]
[StringLength(50)]
public string UserCode
{
get => _userCode;
set => SetProperty(ref _userCode, value, "UserCode");
}
private string _userChannelName = "";
/// <summary>
/// user channel name to apply to a channel when applying setting to a channel
/// can be changed in group or test settings
/// </summary>
[Required]
[StringLength(255)]
public string UserChannelName
{
get => _userChannelName;
set => SetProperty(ref _userChannelName, value, "UserChannelName");
}
private string _userValue1 = "";
/// <summary>
/// user value to carry through to collected data channel when collecting data with this setting
/// </summary>
[StringLength(255)]
public string UserValue1
{
get => _userValue1;
set => SetProperty(ref _userValue1, value, "UserValue1");
}
private string _userValue2 = "";
/// <summary>
/// user value to carry through to collected data channel when collecting data with this setting
/// </summary>
[StringLength(255)]
public string UserValue2
{
get => _userValue2;
set => SetProperty(ref _userValue2, value, "UserValue2");
}
private string _userValue3 = "";
/// <summary>
/// user value to carry through to collected data channel when collecting data with this setting
/// </summary>
[StringLength(255)]
public string UserValue3
{
get => _userValue3;
set => SetProperty(ref _userValue3, value, "UserValue3");
}
private byte[] _userTags;
/// <summary>
/// bytes describing tag ids for tags associated with setting
/// see ITagAware for more information
/// </summary>
public byte[] UserTags
{
get => _userTags;
set => SetProperty(ref _userTags, value, "UserTags");
}
private string _measurementUnit = "V";
/// <summary>
/// measurement unit for collected data, for example
/// 'V' or Volts
/// </summary>
[Required]
[StringLength(50)]
public string MeasurementUnit
{
get => _measurementUnit;
set => SetProperty(ref _measurementUnit, value, "MeasurementUnit");
}
private IFilterClass _filterClass;
/// <summary>
/// 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
/// </summary>
[Required]
[StringLength(50)]
public IFilterClass FilterClass
{
get => _filterClass;
set => SetProperty(ref _filterClass, value, "FilterClass");
}
private bool _doNotUse = false;
/// <summary>
/// a flag indicating setting should not be used for arbitrary user specified reason
/// </summary>
public bool DoNotUse
{
get => _doNotUse;
set => SetProperty(ref _doNotUse, value, "DoNotUse");
}
private bool _broken = false;
/// <summary>
/// a flag indicating setting should not be used because it is currently broken
/// </summary>
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());
}
}
}