Files
DP44/Common/DTS.Common.Storage/SensorTestHistory.cs

334 lines
12 KiB
C#
Raw Normal View History

2026-04-17 14:55:32 -04:00
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Text;
using System.Xml.Serialization;
namespace DTS.Common.Storage
{
/// <summary>
/// this class handles serializing and retrieving and holding sensor test history
/// 3003 Attach Test History to Sensors
/// </summary>
[Serializable]
public class SensorTestHistory
{
/// <summary>
/// the id from the test history table for the test record
/// </summary>
public long TestHistoryId { get; set; }
/// <summary>
/// the id of the test setup from the test history table
/// </summary>
public int TestSetupId { get; set; } = -1;
/// <summary>
/// the name of the test setup, as stored in the db
/// </summary>
public string TestSetupName { get; set; } = null;
/// <summary>
/// the description of the test setup, as stored in the db
/// </summary>
public string TestSetupDescription { get; set; } = null;
/// <summary>
/// the user provided test id that was stored in the db
/// </summary>
public string TestId { get; set; }
/// <summary>
/// whether the test was marked as destructive or not
/// </summary>
public bool Destructive { get; set; }
/// <summary>
/// the time the unit was armed (or autoarmed)
/// </summary>
public DateTime ArmTime { get; set; }
/// <summary>
/// gzip'd bytes representing the test setup xml
/// </summary>
public byte[] TestSetup { get; set; }
/// <summary>
/// the primary key/id of the record in the sensor history table
/// </summary>
public long SensorTestHistoryId { get; set; }
/// <summary>
/// the id of the sensor in the sensors table
/// </summary>
public int SensorId { get; set; } = -1;
/// <summary>
/// the serial number of the sensor
/// </summary>
public string SerialNumber { get; set; }
/// <summary>
/// the capacity of the sensor when the test was armed
/// </summary>
public double Capacity { get; set; }
/// <summary>
/// the range the sensor was armed with
/// </summary>
public double Range { get; set; }
/// <summary>
/// the Date of the Calibration of the unit
/// </summary>
public DateTime CalibrationDate { get; set; } = DateTime.MinValue;
/// <summary>
/// the hardware channel the sensor was on
/// </summary>
public string HardwareChannelName { get; set; }
/// <summary>
/// the iso channel name
/// </summary>
public string ISOChannelName { get; set; } = null;
/// <summary>
/// the iso code of the sensor
/// </summary>
public string ISOCode { get; set; } = null;
/// <summary>
/// the user channel name the sensor was on
/// </summary>
public string UserChannelName { get; set; } = null;
/// <summary>
/// the user code of the channel the sensor was on
/// </summary>
public string UserCode { get; set; } = null;
/// <summary>
/// the sensitivity of the channel (taken directly as a string, like in the dts file)
/// </summary>
public string Sensitivity { get; set; } = null;
/// <summary>
/// the filter class of the channel (taken as a string, like in the dts file)
/// </summary>
public string FilterClass { get; set; } = null;
/// <summary>
/// whether the sensor was marked as proportional or not
/// </summary>
public bool IsProportional { get; set; }
/// <summary>
/// any linearization formula stored with the sensor
/// </summary>
public string LinearizationFormula { get; set; } = null;
/// <summary>
/// EID on the sensor during the event
/// </summary>
public string EID { get; set; } = null;
/// <summary>
/// measured excitation
/// </summary>
public double MeasuredExcitation { get; set; } = double.NaN;
/// <summary>
/// measurement unit on the channel
/// </summary>
public string MeasurementUnit { get; set; }
/// <summary>
/// samples per second for the channel the sensor was on during the event
/// </summary>
public int SamplesPerSecond { get; set; }
/// <summary>
/// the Anti-alias filter for the channel the sensor was on during the event
/// </summary>
public int AAF { get; set; }
public SensorTestHistory(IDataReader reader)
{
TestHistoryId = GetLong(reader, "TestHistoryId");
TestSetupId = GetInt(reader, "TestSetupId");
TestSetupName = GetString(reader, "TestSetupName");
TestSetupDescription = GetString(reader, "TestSetupDescription");
TestId = GetString(reader, "TestId");
Destructive = GetBool(reader, "Destructive");
ArmTime = GetDateTime(reader, "ArmTime");
TestSetup = GetBytes(reader, "TestSetup");
SensorTestHistoryId = GetLong(reader, "SensorTestHistoryId");
SensorId = GetInt(reader, "SensorId");
SerialNumber = GetString(reader, "SerialNumber");
Capacity = GetDouble(reader, "Capacity");
Range = GetDouble(reader, "Range");
CalibrationDate = GetDateTime(reader, "CalibrationDate");
HardwareChannelName = GetString(reader, "HardwareChannelName");
ISOChannelName = GetString(reader, "ISOChannelName");
ISOCode = GetString(reader, "ISOCode");
UserChannelName = GetString(reader, "UserChannelName");
UserCode = GetString(reader, "UserCode");
Sensitivity = GetString(reader, "Sensitivity");
FilterClass = GetString(reader, "FilterClass");
IsProportional = GetBool(reader, "IsProportional");
LinearizationFormula = GetString(reader, "LinearizationFormula");
EID = GetString(reader, "EID");
MeasuredExcitation = GetDouble(reader, "MeasuredExcitation");
MeasurementUnit = GetString(reader, "MeasurementUnit");
SamplesPerSecond = GetInt(reader, "SamplesPerSecond");
AAF = GetInt(reader, "AAF");
}
public SensorTestHistory()
{
}
/// <summary>
/// helper function, returns a datetime if db value is valid,
/// DateTime.MinValue otherwise
/// </summary>
/// <param name="reader"></param>
/// <param name="field"></param>
/// <returns></returns>
private DateTime GetDateTime(IDataReader reader, string field)
{
var o = reader[field];
if (DBNull.Value.Equals(o)) { return DateTime.MinValue; }
return Convert.ToDateTime(o);
}
/// <summary>
/// helper function, returns a bool if db value is valid,
/// false otherwise
/// </summary>
/// <param name="reader"></param>
/// <param name="field"></param>
/// <returns></returns>
private bool GetBool(IDataReader reader, string field)
{
var o = reader[field];
if (DBNull.Value.Equals(o)) { return false; }
return Convert.ToBoolean(o);
}
/// <summary>
/// helper function, returns a long if db value is valid
/// long.MinValue otherwise
/// </summary>
/// <param name="reader"></param>
/// <param name="field"></param>
/// <returns></returns>
private long GetLong(IDataReader reader, string field)
{
var o = reader[field];
if (DBNull.Value.Equals(o)) { return long.MinValue; }
else
{
return Convert.ToInt64(o);
}
}
/// <summary>
/// helper function, returns int if db value is valid,
/// int.MinValue otherwise
/// </summary>
/// <param name="reader"></param>
/// <param name="field"></param>
/// <returns></returns>
public int GetInt(IDataReader reader, string field)
{
var o = reader[field];
if (DBNull.Value.Equals(o)) { return int.MinValue; }
return Convert.ToInt32(o);
}
/// <summary>
/// returns a double if db value is valid,
/// double.NaN otherwise
/// </summary>
/// <param name="reader"></param>
/// <param name="field"></param>
/// <returns></returns>
public double GetDouble(IDataReader reader, string field)
{
var o = reader[field];
if (DBNull.Value.Equals(o)) { return double.NaN; }
return Convert.ToDouble(o);
}
/// <summary>
/// returns a string if db value is valid,
/// string.Empty otherwise
/// </summary>
/// <param name="reader"></param>
/// <param name="field"></param>
/// <returns></returns>
public string GetString(IDataReader reader, string field)
{
var o = reader[field];
if (DBNull.Value.Equals(o)) { return string.Empty; }
return Convert.ToString(o);
}
/// <summary>
/// returns byte [] if db value is valid, otherwise
/// byte[0]
/// </summary>
/// <param name="reader"></param>
/// <param name="field"></param>
/// <returns></returns>
public byte[] GetBytes(IDataReader reader, string field)
{
var o = reader[field];
if (DBNull.Value.Equals(o))
{
return new byte[0];
}
return (byte[])o;
}
/// <summary>
/// returns all records matching sensor serial number
/// </summary>
/// <param name="sensorSerial"></param>
/// <returns></returns>
public static SensorTestHistory[] GetSensorTestHistory(string sensorSerial)
{
var list = new List<SensorTestHistory>();
using (var cmd = DbOperations.GetSQLCommand(true))
{
try
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "sp_SensorTestHistoryGet";
cmd.Parameters.Add(new SqlParameter("@SerialNumber", SqlDbType.NVarChar)
{ Value = sensorSerial });
var reader = cmd.ExecuteReader();
while (reader.Read())
{
var history = new SensorTestHistory(reader);
list.Add(history);
}
}
finally
{
cmd.Connection.Dispose();
}
}
return list.ToArray();
}
/// <summary>
/// returns an xml document in string for representing all given histories
/// </summary>
/// <param name="histories"></param>
/// <returns></returns>
public static string SerializeToString(SensorTestHistory[] histories)
{
var serializer = new XmlSerializer(typeof(SensorTestHistoryCollection));
using (var stream = new MemoryStream())
{
serializer.Serialize(stream, new SensorTestHistoryCollection(histories));
return Encoding.UTF8.GetString(stream.ToArray());
}
}
/// <summary>
/// this is just a helper class for xml serialization
/// </summary>
[Serializable]
public class SensorTestHistoryCollection
{
public SensorTestHistory[] SensorHistory { get; set; }
public SensorTestHistoryCollection(SensorTestHistory[] history)
{
SensorHistory = history;
}
public SensorTestHistoryCollection()
{
}
}
}
}