using DTS.Common.Base; using DTS.Common.Enums.Sensors; using DTS.Common.Interface.Sensors; using DTS.Common.Utilities.Logging; using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; using System.Data; using System.Globalization; using System.Linq; namespace DTS.Common.Classes.Sensors { public class SensorCalDbRecord : BasePropertyChanged, ISensorCalDbRecord { public bool LinearAdded { get => NonLinear && Records.Records[0].Poly.NonLinearStyle == NonLinearStyles.Polynomial && Records.Records.Length > 1 && ZeroMethods.Methods.Length > 1; } private int? _calibrationId = null; /// /// database id, if known, for calibration, null indicates not known /// 13065 Sensor "First Use" Date /// [Key] public int? CalibrationId { get => _calibrationId; set => SetProperty(ref _calibrationId, value, "CalibrationId"); } protected string _serialNumber; public string SerialNumber { get => _serialNumber; set => SetProperty(ref _serialNumber, value, "SerialNumber"); } protected DateTime _calibrationDate; [Column(TypeName = "datetime")] public DateTime CalibrationDate { get => _calibrationDate; set => SetProperty(ref _calibrationDate, value, "CalibrationDate"); } protected string _userName = ""; [Required] [StringLength(50)] public string Username { get => _userName; set => SetProperty(ref _userName, value, "Username"); } private bool _localOnly; public bool LocalOnly { get => _localOnly; set => SetProperty(ref _localOnly, value, "LocalOnly"); } private bool _nonLinear; public bool NonLinear { get => _nonLinear; set { SetProperty(ref _nonLinear, value, "NonLinear"); if (!value) return; Records.Records.First().Sensitivity = 1D; //FB 29728 don't make IsProportinal false //IsProportional = false; RemoveOffset = false; } } private ICalibrationRecords _records = new CalibrationRecords(); [Required] [StringLength(255)] public ICalibrationRecords Records { get => _records; set => SetProperty(ref _records, value, "Records"); } private DateTime _modifyDate; [Column(TypeName = "datetime")] public DateTime ModifyDate { get => _modifyDate; set => SetProperty(ref _modifyDate, value, "ModifyDate"); } private bool _isProportional; public bool IsProportional { get => _isProportional; set => SetProperty(ref _isProportional, value, "IsProportional"); } private bool _removeOffset; public bool RemoveOffset { get => _removeOffset; set => SetProperty(ref _removeOffset, value, "RemoveOffset"); } private ZeroMethods _zeroMethods = new ZeroMethods(); [Required] [StringLength(255)] public ZeroMethods ZeroMethods { get => _zeroMethods; set => SetProperty(ref _zeroMethods, value, "ZeroMethods"); } private string[] _certificationDocuments = new string[0]; [Required] [StringLength(2048)] public string[] CertificationDocuments { get => _certificationDocuments; set => SetProperty(ref _certificationDocuments, value, "CertificationDocuments"); } //FB18158 It's None the default now not EU private InitialOffsets _initialOffsets = new InitialOffsets(new InitialOffset()); public InitialOffsets InitialOffsets { get => _initialOffsets; set => SetProperty(ref _initialOffsets, value, "InitialOffsets"); } public SensorCalDbRecord() { } public SensorCalDbRecord(ISensorCalDbRecord copy) { CalibrationDate = copy.CalibrationDate; LocalOnly = copy.LocalOnly; SerialNumber = copy.SerialNumber; Username = copy.Username; Records = new CalibrationRecords(copy.Records); NonLinear = copy.NonLinear; IsProportional = copy.IsProportional; ModifyDate = copy.ModifyDate; var list = new List(copy.CertificationDocuments); CertificationDocuments = list.ToArray(); RemoveOffset = copy.RemoveOffset; ZeroMethods = new ZeroMethods(copy.ZeroMethods); InitialOffsets = new InitialOffsets(copy.InitialOffsets); CalibrationId = copy.CalibrationId; //this is downright silly, but because the linearization formula marks itself valid when it deserializes with data in it, we go and correct it here. Records.Records[0].Poly.MarkValid(NonLinear); } public SensorCalDbRecord(IDataReader reader) { try { CalibrationDate = Utility.GetDateTime(reader, "CalibrationDate", DateTime.MinValue); LocalOnly = Utility.GetBool(reader, "LocalOnly", false); SerialNumber = Utility.GetString(reader, "SerialNumber"); Username = Utility.GetString(reader, "Username"); Records = new CalibrationRecords(Utility.GetString(reader, "CalibrationRecords")); NonLinear = Utility.GetBool(reader, "NonLinear"); IsProportional = Utility.GetBool(reader, "IsProportional"); ModifyDate = Utility.GetDateTime(reader, "ModifyDate", DateTime.MinValue); CertificationDocuments = Utility.GetString(reader, "CertificationDocuments").Split(new[] { CultureInfo.InvariantCulture.TextInfo.ListSeparator }, StringSplitOptions.None).ToArray(); RemoveOffset = Utility.GetBool(reader, "RemoveOffset"); ZeroMethods = new ZeroMethods(Utility.GetString(reader, "ZeroMethod")); InitialOffsets = new InitialOffsets(Utility.GetString(reader, "InitialOffset")); CalibrationId = Utility.GetNullableInt(reader, "SensorCalibrationId"); //this is downright silly, but because the linearization formula marks itself valid when it deserializes with data in it, we go and correct it here. Records.Records[0].Poly.MarkValid(NonLinear); } catch (Exception ex) { APILogger.Log("Failed to process Sensor Calibration record", ex); } } } }