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
{
///
/// this class handles serializing and retrieving and holding sensor test history
/// 3003 Attach Test History to Sensors
///
[Serializable]
public class SensorTestHistory
{
///
/// the id from the test history table for the test record
///
public long TestHistoryId { get; set; }
///
/// the id of the test setup from the test history table
///
public int TestSetupId { get; set; } = -1;
///
/// the name of the test setup, as stored in the db
///
public string TestSetupName { get; set; } = null;
///
/// the description of the test setup, as stored in the db
///
public string TestSetupDescription { get; set; } = null;
///
/// the user provided test id that was stored in the db
///
public string TestId { get; set; }
///
/// whether the test was marked as destructive or not
///
public bool Destructive { get; set; }
///
/// the time the unit was armed (or autoarmed)
///
public DateTime ArmTime { get; set; }
///
/// gzip'd bytes representing the test setup xml
///
public byte[] TestSetup { get; set; }
///
/// the primary key/id of the record in the sensor history table
///
public long SensorTestHistoryId { get; set; }
///
/// the id of the sensor in the sensors table
///
public int SensorId { get; set; } = -1;
///
/// the serial number of the sensor
///
public string SerialNumber { get; set; }
///
/// the capacity of the sensor when the test was armed
///
public double Capacity { get; set; }
///
/// the range the sensor was armed with
///
public double Range { get; set; }
///
/// the Date of the Calibration of the unit
///
public DateTime CalibrationDate { get; set; } = DateTime.MinValue;
///
/// the hardware channel the sensor was on
///
public string HardwareChannelName { get; set; }
///
/// the iso channel name
///
public string ISOChannelName { get; set; } = null;
///
/// the iso code of the sensor
///
public string ISOCode { get; set; } = null;
///
/// the user channel name the sensor was on
///
public string UserChannelName { get; set; } = null;
///
/// the user code of the channel the sensor was on
///
public string UserCode { get; set; } = null;
///
/// the sensitivity of the channel (taken directly as a string, like in the dts file)
///
public string Sensitivity { get; set; } = null;
///
/// the filter class of the channel (taken as a string, like in the dts file)
///
public string FilterClass { get; set; } = null;
///
/// whether the sensor was marked as proportional or not
///
public bool IsProportional { get; set; }
///
/// any linearization formula stored with the sensor
///
public string LinearizationFormula { get; set; } = null;
///
/// EID on the sensor during the event
///
public string EID { get; set; } = null;
///
/// measured excitation
///
public double MeasuredExcitation { get; set; } = double.NaN;
///
/// measurement unit on the channel
///
public string MeasurementUnit { get; set; }
///
/// samples per second for the channel the sensor was on during the event
///
public int SamplesPerSecond { get; set; }
///
/// the Anti-alias filter for the channel the sensor was on during the event
///
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()
{
}
///
/// helper function, returns a datetime if db value is valid,
/// DateTime.MinValue otherwise
///
///
///
///
private DateTime GetDateTime(IDataReader reader, string field)
{
var o = reader[field];
if (DBNull.Value.Equals(o)) { return DateTime.MinValue; }
return Convert.ToDateTime(o);
}
///
/// helper function, returns a bool if db value is valid,
/// false otherwise
///
///
///
///
private bool GetBool(IDataReader reader, string field)
{
var o = reader[field];
if (DBNull.Value.Equals(o)) { return false; }
return Convert.ToBoolean(o);
}
///
/// helper function, returns a long if db value is valid
/// long.MinValue otherwise
///
///
///
///
private long GetLong(IDataReader reader, string field)
{
var o = reader[field];
if (DBNull.Value.Equals(o)) { return long.MinValue; }
else
{
return Convert.ToInt64(o);
}
}
///
/// helper function, returns int if db value is valid,
/// int.MinValue otherwise
///
///
///
///
public int GetInt(IDataReader reader, string field)
{
var o = reader[field];
if (DBNull.Value.Equals(o)) { return int.MinValue; }
return Convert.ToInt32(o);
}
///
/// returns a double if db value is valid,
/// double.NaN otherwise
///
///
///
///
public double GetDouble(IDataReader reader, string field)
{
var o = reader[field];
if (DBNull.Value.Equals(o)) { return double.NaN; }
return Convert.ToDouble(o);
}
///
/// returns a string if db value is valid,
/// string.Empty otherwise
///
///
///
///
public string GetString(IDataReader reader, string field)
{
var o = reader[field];
if (DBNull.Value.Equals(o)) { return string.Empty; }
return Convert.ToString(o);
}
///
/// returns byte [] if db value is valid, otherwise
/// byte[0]
///
///
///
///
public byte[] GetBytes(IDataReader reader, string field)
{
var o = reader[field];
if (DBNull.Value.Equals(o))
{
return new byte[0];
}
return (byte[])o;
}
///
/// returns all records matching sensor serial number
///
///
///
public static SensorTestHistory[] GetSensorTestHistory(string sensorSerial)
{
var list = new List();
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();
}
///
/// returns an xml document in string for representing all given histories
///
///
///
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());
}
}
///
/// this is just a helper class for xml serialization
///
[Serializable]
public class SensorTestHistoryCollection
{
public SensorTestHistory[] SensorHistory { get; set; }
public SensorTestHistoryCollection(SensorTestHistory[] history)
{
SensorHistory = history;
}
public SensorTestHistoryCollection()
{
}
}
}
}