Files

589 lines
21 KiB
C#
Raw Permalink Normal View History

2026-04-17 14:55:32 -04:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.ComponentModel;
using System.Data.SqlClient;
namespace DatabaseImport
{
public class Hardware : INotifyPropertyChanged, IISOHardware
{
public event PropertyChangedEventHandler PropertyChanged;
protected bool SetProperty<T>(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));
}
public enum ChannelType
{
Analog,
IEPE,
Squib,
DigitalOutput
}
public static DateTime INVALIDDATE => new DateTime(1970, 1, 1);
private string _serialNumber = "";
public string SerialNumber
{
get => _serialNumber;
set => _serialNumber = value;
}
private int _calInterval = 365;
public int CalInterval
{
get => _calInterval;
set => _calInterval = value;
}
private int _dasType;
public int DASType
{
get => _dasType;
set => SetProperty(ref _dasType, value, "DASType");
}
private int _maxModules;
public int MaxModules
{
get => _maxModules;
set => SetProperty(ref _maxModules, value, "MaxModules");
}
private long _maxMemory;
public long MaxMemory
{
get => _maxMemory;
set => SetProperty(ref _maxMemory, value, "MaxMemory");
}
private double _minSampleRate;
public double MinSampleRate
{
get => _minSampleRate;
set => SetProperty(ref _minSampleRate, value, "MinSampleRate");
}
private double _maxSampleRate = 1000000;
public double MaxSampleRate
{
get => _maxSampleRate;
set => SetProperty(ref _maxSampleRate, value, "MaxSampleRate");
}
private double _maxAAFRate = 200000;
public double MaxAAFRate
{
get => _maxAAFRate;
set => SetProperty(ref _maxAAFRate, value, "MaxAAFRate");
}
private string _firmwareVersion = "";
public string FirmwareVersion
{
get => _firmwareVersion;
set => SetProperty(ref _firmwareVersion, value, "FirmwareVersion");
}
private DateTime _calDate = INVALIDDATE;
public DateTime CalDate
{
get => _calDate < INVALIDDATE ? INVALIDDATE : _calDate;
set => SetProperty(ref _calDate, value, "CalDate");
}
private int _protocolVerison;
public int ProtocolVersion
{
get => _protocolVerison;
set => SetProperty(ref _protocolVerison, value, "ProtocolVersion");
}
private DateTime _lastModified = DateTime.Now;
public DateTime LastModified
{
get => _lastModified;
set => SetProperty(ref _lastModified, value, "LastModified");
}
private string _lastModifiedBy = "";
public string LastModifiedBy
{
get => _lastModifiedBy;
set => SetProperty(ref _lastModifiedBy, value, "LastModifiedBy");
}
private int _version = 1;
public int Version
{
get => _version;
set => SetProperty(ref _version, value, "Version");
}
private bool _bLocalOnly;
public bool LocalOnly
{
get => _bLocalOnly;
set => SetProperty(ref _bLocalOnly, value, "LocalOnly");
}
private DateTime _lastUsed = INVALIDDATE;
public DateTime LastUsed
{
get => _lastUsed;
set => SetProperty(ref _lastUsed, value, "LastUsed");
}
private string _lastUsedBy = "";
public string LastUsedBy
{
get => _lastUsedBy;
set => SetProperty(ref _lastUsedBy, value, "LastUsedBy");
}
private string _ipAddress = "";
public string IPAddress
{
get => _ipAddress;
set => SetProperty(ref _ipAddress, value, "IPAddress");
}
private int _channels;
public int Channels
{
get => _channels;
set => SetProperty(ref _channels, value, "Channels");
}
private string _position = "";
public string Position
{
get => _position;
set => SetProperty(ref _position, value, "Position");
}
private bool _isProgrammable;
public bool IsProgrammable
{
get => _isProgrammable;
set => SetProperty(ref _isProgrammable, value, "IsProgrammable");
}
private bool _isModule;
public bool IsModule
{
get => _isModule;
set => SetProperty(ref _isModule, value, "IsModule");
}
private bool _isReconfigurable;
public bool IsReconfigurable
{
get => _isReconfigurable;
set => SetProperty(ref _isReconfigurable, value, "IsReconfigurable");
}
private int[] _channelTypes;
public int[] ChannelTypes
{
get => _channelTypes ?? (_channelTypes = new int[0]);
set => SetProperty(ref _channelTypes, value, "ChannelTypes");
}
public string ParentDAS { get; set; } = "";
public int Port { get; set; }
public int PositionOnChain { get; set; }
public int PositionOnDistributor { get; set; }
public Hardware()
{
}
public Hardware(Hardware copy)
{
Version = copy.Version;
SerialNumber = copy.SerialNumber;
ProtocolVersion = copy.ProtocolVersion;
Position = copy.Position;
MinSampleRate = copy.MinSampleRate;
MaxSampleRate = copy.MaxSampleRate;
MaxModules = copy.MaxModules;
MaxMemory = copy.MaxMemory;
LocalOnly = copy.LocalOnly;
LastUsedBy = copy.LastUsedBy;
LastUsed = copy.LastUsed;
LastModifiedBy = copy.LastModifiedBy;
LastModified = copy.LastModified;
IsReconfigurable = copy.IsReconfigurable;
IsProgrammable = copy.IsProgrammable;
ISOChannels = copy.ISOChannels.Select(c => new ISO.HardwareChannel(c, this)).ToArray();
IPAddress = copy.IPAddress;
FirmwareVersion = copy.FirmwareVersion;
DASType = copy.DASType;
var channeltypes = new int[copy.ChannelTypes.Length];
Array.Copy(copy.ChannelTypes, channeltypes, copy.ChannelTypes.Length);
ChannelTypes = channeltypes;
Channels = copy.Channels;
CalInterval = copy.CalInterval;
CalDate = copy.CalDate;
IsModule = copy.IsModule;
ParentDAS = copy.ParentDAS;
PositionOnChain = copy.PositionOnChain;
PositionOnDistributor = copy.PositionOnDistributor;
Port = copy.Port;
}
public Hardware(IDataRecord reader)
{
CalDate = Convert.ToDateTime(reader[DbOperations.DAS.Fields.CalDate.ToString()]);
Channels = Convert.ToInt32(reader[DbOperations.DAS.Fields.Channels.ToString()]);
IPAddress = Convert.ToString(reader[DbOperations.DAS.Fields.Connection.ToString()]);
FirmwareVersion = Convert.ToString(reader[DbOperations.DAS.Fields.FirmwareVersion.ToString()]);
LastModified = Convert.ToDateTime(reader[DbOperations.DAS.Fields.LastModified.ToString()]);
LastModifiedBy = Convert.ToString(reader[DbOperations.DAS.Fields.LastModifiedBy.ToString()]);
LastUsed = Convert.ToDateTime(reader[DbOperations.DAS.Fields.LastUsed.ToString()]);
LastUsedBy = Convert.ToString(reader[DbOperations.DAS.Fields.LastUsedBy.ToString()]);
LocalOnly = Convert.ToBoolean(reader[DbOperations.DAS.Fields.LocalOnly.ToString()]);
MaxMemory = Convert.ToInt64(reader[DbOperations.DAS.Fields.MaxMemory.ToString()]);
MaxModules = Convert.ToInt32(reader[DbOperations.DAS.Fields.MaxModules.ToString()]);
MaxSampleRate = Convert.ToDouble(reader[DbOperations.DAS.Fields.MaxSampleRate.ToString()]);
MinSampleRate = Convert.ToDouble(reader[DbOperations.DAS.Fields.MinSampleRate.ToString()]);
Position = Convert.ToString(reader[DbOperations.DAS.Fields.Position.ToString()]);
IsReconfigurable = Convert.ToBoolean(reader[DbOperations.DAS.Fields.Reconfigurable.ToString()]);
IsModule = Convert.ToBoolean(reader[DbOperations.DAS.Fields.IsModule.ToString()]);
IsProgrammable = Convert.ToBoolean(reader[DbOperations.DAS.Fields.Reprogramable.ToString()]);
ProtocolVersion = Convert.ToInt32(reader[DbOperations.DAS.Fields.ProtocolVersion.ToString()]);
SerialNumber = Convert.ToString(reader[DbOperations.DAS.Fields.SerialNumber.ToString()]);
DASType = Convert.ToInt32(reader[DbOperations.DAS.Fields.Type.ToString()]);
if (reader[DbOperations.DAS.Fields.ChannelTypes.ToString()] != null)
{
var tokens =
(reader[DbOperations.DAS.Fields.ChannelTypes.ToString()] as string).Split(',');
var itemp = 0;
ChannelTypes = (from token in tokens where int.TryParse(token, out itemp) select itemp).ToArray();
}
Version = Convert.ToInt32(reader[DbOperations.DAS.Fields.Version.ToString()]);
ParentDAS = Convert.ToString(reader[DbOperations.DAS.Fields.ParentDAS.ToString()]);
Port = reader[DbOperations.DAS.Fields.Port.ToString()] is DBNull
? 0
: Convert.ToInt32(reader[DbOperations.DAS.Fields.Port.ToString()]);
PositionOnChain = Convert.ToInt32(reader[DbOperations.DAS.Fields.PositionOnChain.ToString()]);
PositionOnDistributor = Convert.ToInt32(reader[DbOperations.DAS.Fields.PositionOnDistributor.ToString()]);
}
public static List<ISO.HardwareChannel> GetDASISOChannels(string hardwareId, Hardware das)
{
var channelList = new List<ISO.HardwareChannel>();
using (var cmd = DbOperations.GetSQLCommand(true))
{
try
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = DbOperationsEnum.StoredProcedure.sp_DASChannelsGet.ToString();
#region params
cmd.Parameters.Add(new SqlParameter("@HardwareId", SqlDbType.NVarChar) { Value = hardwareId });
#endregion params
using (var readerISOChannels = cmd.ExecuteReader())
{
while (readerISOChannels.Read())
{
channelList.Add(new ISO.HardwareChannel(readerISOChannels, das));
}
readerISOChannels.Close();
}
}
finally
{
cmd.Connection.Dispose();
}
}
channelList.Sort(ISO.HardwareChannel.PhysicalCompare);
return channelList;
}
private static void GetAllDASISOChannels(Dictionary<string, Hardware> serialNumberToHardware)
{
var channelsByDASKey = new Dictionary<string, List<ISO.HardwareChannel>>();
using (var cmd = DbOperations.GetSQLCommand(true))
{
try
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = DbOperationsEnum.StoredProcedure.sp_DASChannelsGet.ToString();
#region params
cmd.Parameters.Add(new SqlParameter("@HardwareId", SqlDbType.NVarChar) { Value = null });
#endregion params
using (var readerISOChannels = cmd.ExecuteReader())
{
while (readerISOChannels.Read())
{
var serialNumber = Convert.ToString(readerISOChannels["HardwareId"]);
if (!serialNumberToHardware.ContainsKey(serialNumber))
{
continue;
}
var hardware = serialNumberToHardware[serialNumber];
var hc = new ISO.HardwareChannel(readerISOChannels, hardware);
if (!channelsByDASKey.ContainsKey(serialNumber))
{
channelsByDASKey[serialNumber] = new List<ISO.HardwareChannel>();
}
channelsByDASKey[serialNumber].Add(hc);
}
readerISOChannels.Close();
}
using (var enumHardware = serialNumberToHardware.GetEnumerator())
{
while (enumHardware.MoveNext())
{
var hardware = enumHardware.Current.Value;
if (!channelsByDASKey.ContainsKey(enumHardware.Current.Key))
{
continue;
}
var list = channelsByDASKey[enumHardware.Current.Key];
list.Sort(ISO.HardwareChannel.PhysicalCompare);
hardware.ISOChannels = list.ToArray();
}
}
}
finally
{
cmd.Connection.Dispose();
}
}
}
private List<ISO.HardwareChannel> _isoChannels = new List<ISO.HardwareChannel>();
public ISO.HardwareChannel[] ISOChannels
{
get => _isoChannels.ToArray();
set => SetProperty(ref _isoChannels, new List<ISO.HardwareChannel>(value), "ISOChannels");
}
public static Hardware[] GetSingleDAS(string serialNumber, string position)
{
var list = new List<Hardware>();
try
{
using (var cmd = DbOperations.GetSQLCommand(true))
{
try
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = DbOperationsEnum.StoredProcedure.sp_DASGet.ToString();
#region params
cmd.Parameters.Add(
new SqlParameter("@SerialNumber", SqlDbType.NVarChar) { Value = serialNumber });
cmd.Parameters.Add(new SqlParameter("@position", SqlDbType.NVarChar)
{
Value = string.IsNullOrEmpty(position) ? null : position
});
#endregion params
using (var readerDAS = cmd.ExecuteReader())
{
while (readerDAS.Read())
{
list.Add(new Hardware(readerDAS));
}
readerDAS.Close();
}
}
finally
{
cmd.Connection.Dispose();
}
}
foreach (var das in list)
{
var channels = GetDASISOChannels(das.SerialNumber, das);
channels.Sort(ISO.HardwareChannel.PhysicalCompare);
das.ISOChannels = channels.ToArray();
}
}
catch (Exception)
{
/*APILogger.Log("failed to retrieve all das, ", ex); */
}
list.Sort(new HardwareCompare());
return list.ToArray();
}
public static Hardware[] GetAllDAS(string serialNumber, string position)
{
if (!string.IsNullOrWhiteSpace(serialNumber))
{
return GetSingleDAS(serialNumber, position);
}
var list = new List<Hardware>();
try
{
using (var cmd = DbOperations.GetSQLCommand(true))
{
try
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = DbOperationsEnum.StoredProcedure.sp_DASGet.ToString();
#region params
cmd.Parameters.Add(new SqlParameter("@SerialNumber", SqlDbType.NVarChar)
{
Value = string.IsNullOrEmpty(serialNumber) ? null : serialNumber
});
cmd.Parameters.Add(new SqlParameter("@position", SqlDbType.NVarChar)
{
Value = string.IsNullOrEmpty(position) ? null : position
});
#endregion params
using (var readerDAS = cmd.ExecuteReader())
{
while (readerDAS.Read())
{
list.Add(new Hardware(readerDAS));
}
readerDAS.Close();
}
}
finally
{
cmd.Connection.Dispose();
}
}
var serialNumberToDAS = new Dictionary<string, Hardware>();
foreach (var das in list)
{
serialNumberToDAS[$"{das.SerialNumber}_{das.DASType}"] = das;
}
GetAllDASISOChannels(serialNumberToDAS);
}
catch (Exception)
{
/*APILogger.Log("failed to retrieve all das, ", ex);*/
}
list.Sort(new HardwareCompare());
return list.ToArray();
}
public class HardwareCompare : Comparer<Hardware>
{
public override int Compare(Hardware x, Hardware y)
{
var ret = string.Compare(x.SerialNumber, y.SerialNumber, StringComparison.Ordinal);
if (0 == ret)
{
ret = string.Compare(x.IPAddress, y.IPAddress, StringComparison.Ordinal);
}
return ret;
}
}
public void Delete()
{
using (var cmd = DbOperations.GetSQLCommand(true))
{
try
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = DbOperationsEnum.StoredProcedure.sp_DASDelete.ToString();
#region params
cmd.Parameters.Add(new SqlParameter("@SerialNumber", SqlDbType.NVarChar) { Value = SerialNumber });
var errorNumberParam =
new SqlParameter("@errorNumber", SqlDbType.Int) { Direction = ParameterDirection.Output };
cmd.Parameters.Add(errorNumberParam);
var errorMessageParam =
new SqlParameter("@errorMessage", SqlDbType.NVarChar, 250)
{
Direction = ParameterDirection.Output
};
cmd.Parameters.Add(errorMessageParam);
#endregion params
cmd.ExecuteNonQuery();
if (int.Parse(errorNumberParam.Value.ToString()) != 0)
{
//errorMessageParam.Value
}
}
finally
{
cmd.Connection.Dispose();
}
}
}
public string GetId()
{
return GetId(SerialNumber, DASType.ToString(), IPAddress);
}
public static string GetId(string sn, string dastype, string ip)
{
return $"{sn}_{dastype}";
}
}
}