init
This commit is contained in:
503
Common/DTS.Common.Serialization/Test.Module.Channel.DataArray.cs
Normal file
503
Common/DTS.Common.Serialization/Test.Module.Channel.DataArray.cs
Normal file
@@ -0,0 +1,503 @@
|
||||
/*
|
||||
Test.Module.Channel.DataArray.cs
|
||||
|
||||
Copyright © 2009
|
||||
Diversified Technical Systems, Inc.
|
||||
All Rights Reserved
|
||||
*/
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using System.Xml;
|
||||
using System.Xml.Schema;
|
||||
using System.Xml.Serialization;
|
||||
using DTS.Utilities;
|
||||
using DTS.Utilities.DotNetProgrammingConstructs;
|
||||
using DTS.Utilities.Xml;
|
||||
|
||||
namespace DTS.Serialization
|
||||
{
|
||||
// *** see Test.cs ***
|
||||
public partial class Test
|
||||
{
|
||||
// *** see Test.Module.cs ***
|
||||
public partial class Module
|
||||
{
|
||||
// *** see Test.Module.Channel.cs ***
|
||||
public partial class Channel
|
||||
{
|
||||
/// <summary>
|
||||
/// Representation of serializable channel data.
|
||||
/// </summary>
|
||||
///
|
||||
/// <typeparam name="DataType">
|
||||
/// The actual type of this channel data's datum.
|
||||
/// </typeparam>
|
||||
///
|
||||
public class DataArray<DataType> : Exceptional, IXmlSerializable
|
||||
{
|
||||
private readonly string DataXmlTag = "Data";
|
||||
private readonly string DataLengthXmlTag = "Length";
|
||||
private readonly string DataTypeXmlTag = "Type";
|
||||
private readonly string DatumXmlTag = "Datum";
|
||||
private readonly string DatumValueXmlTag = "Value";
|
||||
private readonly string DataAdcToMvScaleFactorTag = "ScaleFactorMv";
|
||||
private readonly string DataMvPerEuTag = "SensitivityMvEu";
|
||||
|
||||
/// <summary>
|
||||
/// Method for converting a <see cref="List"/> of the generic data type to
|
||||
/// a DataArray.
|
||||
/// </summary>
|
||||
///
|
||||
/// <param name="dataList">
|
||||
/// The <see cref="List"/> of the generic data type to be converted into a
|
||||
/// DataArray.
|
||||
/// </param>
|
||||
///
|
||||
/// <returns>
|
||||
/// A DataArray containing the specified values.
|
||||
/// </returns>
|
||||
///
|
||||
public static implicit operator DataArray<DataType>( List<DataType> dataList )
|
||||
{
|
||||
return new DataArray<DataType>( dataList.ToArray( ) );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Method for converting a DataArray of the generic data type to a <see cref="List"/>
|
||||
/// of the generic data type.
|
||||
/// </summary>
|
||||
///
|
||||
/// <param name="dataArray">
|
||||
/// The DataArray of the specified generic type to be converted into a <see cref="List"/>.
|
||||
/// </param>
|
||||
///
|
||||
/// <returns>
|
||||
/// A <see cref="List"/> of the specified generic type.
|
||||
/// </returns>
|
||||
///
|
||||
public static implicit operator List<DataType>( DataArray<DataType> dataArray )
|
||||
{
|
||||
return new List<DataType>( dataArray.Values );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initialize an instance of the Data class.
|
||||
/// </summary>
|
||||
public DataArray( )
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initialize an instance of the Data class.
|
||||
/// </summary>
|
||||
///
|
||||
/// <param name="values">
|
||||
/// The values with which to initialize this data collection.
|
||||
/// </param>
|
||||
///
|
||||
public DataArray( DataType[] values )
|
||||
{
|
||||
try
|
||||
{
|
||||
this.Values = values;
|
||||
}
|
||||
|
||||
catch ( System.Exception ex )
|
||||
{
|
||||
throw new Exception( "encountered problem constructing Test.Module.Channel.DataArray", ex );
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get all of the data values in an array.
|
||||
/// </summary>
|
||||
public DataType[] Values
|
||||
{
|
||||
//get { return _Values.Value; }
|
||||
get
|
||||
{
|
||||
if (!_Values.IsInitialized || null == _Values.Value)
|
||||
{
|
||||
return new DataType[0];
|
||||
}
|
||||
else { return _Values.Value; }
|
||||
}
|
||||
set { _Values.Value = value; }
|
||||
}
|
||||
private Property<DataType[]> _Values
|
||||
= new Property<DataType[]>(
|
||||
typeof( DataArray<DataType> ).Namespace + ".Test.Module.Channel.DataArray.Values",
|
||||
null,
|
||||
false
|
||||
);
|
||||
|
||||
/// <summary>
|
||||
/// Get/set the ADC->MV scale factor for this data.
|
||||
/// </summary>
|
||||
public double ScaleFactorMv
|
||||
{
|
||||
get { return _ScaleFactorMv.Value; }
|
||||
set { _ScaleFactorMv.Value = value; }
|
||||
}
|
||||
private Property<double> _ScaleFactorMv
|
||||
= new Property<double>(
|
||||
typeof( DataArray<DataType> ).Namespace + ".Test.Module.Channel.DataArray.ScaleFactorMv",
|
||||
0.0,
|
||||
false
|
||||
);
|
||||
public double ScaleFactorEU
|
||||
{
|
||||
get { return _ScaleFactorEU.Value; }
|
||||
set { _ScaleFactorEU.Value = value; }
|
||||
}
|
||||
private Property<double> _ScaleFactorEU
|
||||
= new Property<double>(
|
||||
typeof(DataArray<DataType>).Namespace + ".Test.Module.Channel.DataArray.ScaleFactorEU",
|
||||
0.0,
|
||||
false
|
||||
);
|
||||
public bool UseTDASScaleFactors
|
||||
{
|
||||
get { return _UseTDASScaleFactors.Value; }
|
||||
set { _UseTDASScaleFactors.Value = value; }
|
||||
}
|
||||
private Property<bool> _UseTDASScaleFactors
|
||||
= new Property<bool>(
|
||||
typeof(DataArray<DataType>).Namespace + ".Test.Module.Channel.DataArray.UseTDASScaleFactors",
|
||||
false,
|
||||
true
|
||||
);
|
||||
public short DataZeroLevel
|
||||
{
|
||||
get { return _DataZeroLevel.Value; }
|
||||
set { _DataZeroLevel.Value = value; }
|
||||
}
|
||||
private Property<short> _DataZeroLevel
|
||||
= new Property<short>(
|
||||
typeof(DataArray<DataType>).Namespace + ".Test.Module.Channel.DataArray.DataZeroLevel",
|
||||
0,
|
||||
false
|
||||
);
|
||||
/// <summary>
|
||||
/// Get/set the Unit Conversion for this data.
|
||||
/// </summary>
|
||||
public double UnitConversion
|
||||
{
|
||||
get { return _UnitConversion.Value; }
|
||||
set { _UnitConversion.Value = value; }
|
||||
}
|
||||
private Property<double> _UnitConversion
|
||||
= new Property<double>(
|
||||
typeof(DataArray<DataType>).Namespace + ".Test.Module.Channel.DataArray.UnitConversion",
|
||||
1.0,
|
||||
true
|
||||
);
|
||||
|
||||
/// <summary>
|
||||
/// Get/set the Multiplier for this data.
|
||||
/// </summary>
|
||||
public double Multiplier
|
||||
{
|
||||
get { return _Multiplier.Value; }
|
||||
set { _Multiplier.Value = value; }
|
||||
}
|
||||
private Property<double> _Multiplier
|
||||
= new Property<double>(
|
||||
typeof(DataArray<DataType>).Namespace + ".Test.Module.Channel.DataArray.Multiplier",
|
||||
1.0,
|
||||
true
|
||||
);
|
||||
|
||||
/// <summary>
|
||||
/// Get/set the Modify Offset for this data.
|
||||
/// </summary>
|
||||
public double UserOffsetEU
|
||||
{
|
||||
get { return _UserOffsetEU.Value; }
|
||||
set { _UserOffsetEU.Value = value; }
|
||||
}
|
||||
private Property<double> _UserOffsetEU
|
||||
= new Property<double>(
|
||||
typeof(DataArray<DataType>).Namespace + ".Test.Module.Channel.DataArray.UserOffsetEU",
|
||||
0.0,
|
||||
true
|
||||
);
|
||||
|
||||
/// <summary>
|
||||
/// Get/set the ADC->EU scale factor for this data.
|
||||
/// </summary>
|
||||
public double MvPerEu
|
||||
{
|
||||
get { return _MvPerEu.Value; }
|
||||
set { _MvPerEu.Value = value; }
|
||||
}
|
||||
private Property<double> _MvPerEu
|
||||
= new Property<double>(
|
||||
typeof( DataArray<DataType> ).Namespace + ".Test.Module.Channel.DataArray.MvPerEu",
|
||||
0.0,
|
||||
false
|
||||
);
|
||||
|
||||
/// <summary>
|
||||
/// Write XML serialization for this object to the specified writer.
|
||||
/// </summary>
|
||||
///
|
||||
/// <param name="writer">
|
||||
/// The <see cref="XmlWriter"/> to which this object's XML serialization
|
||||
/// will be written.
|
||||
/// </param>
|
||||
///
|
||||
public void WriteXml( XmlWriter writer )
|
||||
{
|
||||
try
|
||||
{
|
||||
var cult = new System.Globalization.CultureInfo("");
|
||||
int dataLength = Values.Length;
|
||||
|
||||
writer.WriteStartElement( DataXmlTag );
|
||||
writer.WriteAttributeString( DataLengthXmlTag, dataLength.ToString(cult) );
|
||||
writer.WriteAttributeString( DataTypeXmlTag, typeof( DataType ).FullName );
|
||||
writer.WriteAttributeString( DataAdcToMvScaleFactorTag, ScaleFactorMv.ToString(cult) );
|
||||
writer.WriteAttributeString( DataMvPerEuTag, MvPerEu.ToString(cult) );
|
||||
|
||||
for ( int i=0; i<dataLength; i++ )
|
||||
{
|
||||
writer.WriteStartElement( DatumXmlTag );
|
||||
writer.WriteAttributeString( DatumValueXmlTag, Values[ i ].ToString() );
|
||||
writer.WriteEndElement( );
|
||||
}
|
||||
|
||||
writer.WriteEndElement( );
|
||||
}
|
||||
|
||||
catch ( System.Exception ex )
|
||||
{
|
||||
throw new Exception( "encountered problem converting DTS.Serialization.Test.Module.Channel.Data object to XML", ex );
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Read XML serialization for this object from the specified reader.
|
||||
/// </summary>
|
||||
///
|
||||
/// <param name="reader">
|
||||
/// The <see cref="XmlReader"/> from which this object's XML serialization
|
||||
/// will be read.
|
||||
/// </param>
|
||||
///
|
||||
public void ReadXml( XmlReader reader )
|
||||
{
|
||||
try
|
||||
{
|
||||
var cult = new System.Globalization.CultureInfo("");
|
||||
reader.MoveToContent();
|
||||
|
||||
Type dataType = null;
|
||||
int dataLength;
|
||||
try
|
||||
{
|
||||
string scaleFactorMvString = null;
|
||||
try { scaleFactorMvString = reader.GetAttribute( DataAdcToMvScaleFactorTag ); }
|
||||
catch ( System.Exception ex ) { throw new Exception( "encountered problem extracting ADC->MV scaling factor from XML", ex ); }
|
||||
|
||||
string mvPerEuString = null;
|
||||
try { mvPerEuString = reader.GetAttribute( DataMvPerEuTag ); }
|
||||
catch ( System.Exception ex ) { throw new Exception( "encountered problem extracting sensitivity MV/EU factor from XML", ex ); }
|
||||
|
||||
string dataTypeString = null;
|
||||
try { dataTypeString = reader.GetAttribute( DataTypeXmlTag ); }
|
||||
catch ( System.Exception ex ) { throw new Exception( "encountered problem extracting data type name from XML", ex ); }
|
||||
|
||||
dataType = null;
|
||||
try { dataType = Type.GetType( dataTypeString ); }
|
||||
catch ( System.Exception ex ) { throw new Exception( "encountered problem instantiating type " + ( null != dataTypeString ? "\"" + dataTypeString + "\"" : "<<NULL>>" ), ex ); }
|
||||
|
||||
string dataLengthString = null;
|
||||
try { dataLengthString = reader.GetAttribute( DataLengthXmlTag ); }
|
||||
catch ( System.Exception ex ) { throw new Exception( "encountered problem extracting data length string representation from XML", ex ); }
|
||||
|
||||
dataLength = 0;
|
||||
try { dataLength = int.Parse( dataLengthString, cult ); }
|
||||
catch ( System.Exception ex ) { throw new Exception( "encountered problem parsing data length from string representation " + ( null != dataLengthString ? "\"" + dataLengthString + "\"" : "<<NULL>>" ), ex ); }
|
||||
|
||||
Array dataArray = null;
|
||||
try { dataArray = Array.CreateInstance( dataType, dataLength ); }
|
||||
catch ( System.Exception ex ) { throw new Exception( "encountered problem creating data array of type " + ( null != dataType ? "\"" + dataType.FullName + "\"" : "<<NULL>>" ) + " and length " + dataLength.ToString( ) , ex ); }
|
||||
|
||||
try { this.Values = (DataType[])dataArray; }
|
||||
catch ( System.Exception ex ) { throw new Exception( "encountered problem assigning data array dynamically created from XML to Data.Values", ex ); }
|
||||
}
|
||||
catch ( System.Exception ex )
|
||||
{
|
||||
throw new Exception( "encountered problem creating data array from XML", ex );
|
||||
}
|
||||
|
||||
try { this.ScaleFactorMv = double.Parse( reader.GetAttribute( DataAdcToMvScaleFactorTag ), cult ); }
|
||||
catch ( System.Exception ex )
|
||||
{
|
||||
throw new Exception( "encountered problem parsing ADC->MV scaling factor property", ex );
|
||||
}
|
||||
|
||||
try { this.MvPerEu = double.Parse( reader.GetAttribute( DataMvPerEuTag ), cult ); }
|
||||
catch ( System.Exception ex )
|
||||
{
|
||||
throw new Exception( "encountered problem parsing MV/EU sensitivity factor property", ex );
|
||||
}
|
||||
|
||||
MemberInfo[] dataTypeMembers = typeof( DataType ).GetMember( "Parse" );
|
||||
if ( null == dataTypeMembers || dataTypeMembers.Length < 1 )
|
||||
throw new Exception( "data type does not contain a \"Parse\" method" );
|
||||
else if ( dataLength > 0 )
|
||||
{
|
||||
Object dataTypeObject = null;
|
||||
try { dataTypeObject = Activator.CreateInstance( dataType ); }
|
||||
catch ( System.Exception ex ) { throw new Exception( "encountered problem creating an instance of type " + ( null != dataType ? typeof( DataType ).FullName : "<<NULL>>" ), ex ); }
|
||||
|
||||
Type[] paramTypes = new Type[ 1 ];
|
||||
paramTypes[ 0 ] = Type.GetType( "System.String" );
|
||||
|
||||
MethodInfo parseMethodInfo = null;
|
||||
try { parseMethodInfo = dataType.GetMethod( "Parse", paramTypes ); }
|
||||
catch ( System.Exception ex ) { throw new Exception( "encountered problem getting \"Parse\" method information from data type " + dataType.FullName, ex ); }
|
||||
|
||||
for ( int i=0; i < this.Values.Length; i++ )
|
||||
{
|
||||
reader.Read( );
|
||||
reader.MoveToContent( );
|
||||
|
||||
string datumValueString = null;
|
||||
try { datumValueString = reader.GetAttribute( DatumValueXmlTag ); }
|
||||
catch ( System.Exception ex ) { throw new Exception( "encountered problem extracting datum value string representation from XML", ex ); }
|
||||
|
||||
Object[] parameters = new Object[ 1 ];
|
||||
parameters[ 0 ] = datumValueString;
|
||||
DataType parseResult;
|
||||
try { parseResult = (DataType)parseMethodInfo.Invoke( dataTypeObject, parameters ); }
|
||||
catch ( System.Exception ex ) { throw new Exception( "encountered problem invoking \"Parse\" method on string representation " + ( null != datumValueString ? datumValueString : "<<NULL>>" ), ex ); }
|
||||
|
||||
try { this.Values[ i ] = parseResult; }
|
||||
catch ( System.Exception ex ) { throw new Exception( "encountered problem inserting parse result " + ( null != parseResult ? parseResult.ToString( ) : "<<NULL>>" ) + " into collection of data values", ex ); }
|
||||
}
|
||||
}
|
||||
|
||||
reader.Read( );
|
||||
reader.MoveToContent( );
|
||||
|
||||
if ( reader.Name.Equals( DataXmlTag, StringComparison.OrdinalIgnoreCase ) )
|
||||
{ //
|
||||
// This needs to be conditional in case we get inline data of length
|
||||
// zero. Though this probably will only ever happen under unit test.
|
||||
//
|
||||
reader.ReadEndElement( ); // consume attribute end tag.
|
||||
reader.MoveToContent( );
|
||||
}
|
||||
}
|
||||
|
||||
catch ( System.Exception ex )
|
||||
{
|
||||
throw new Exception( "encountered problem converting XML to DTS.Serialization.Test.Module.Channel.Data object", ex );
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Should normally return a schema representing the form of the XML
|
||||
/// generated/consumed by WriteXml/ReadXml, but it never called during
|
||||
/// the serialization process so ours just returns null.
|
||||
/// </summary>
|
||||
///
|
||||
/// <returns>
|
||||
/// Null <see cref="XmlSchema"/> reference, always.
|
||||
/// </returns>
|
||||
///
|
||||
public XmlSchema GetSchema( )
|
||||
{
|
||||
// This method is never invoked during XML object serialization.
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test the specified object for equality with this object.
|
||||
/// </summary>
|
||||
///
|
||||
/// <param name="obj">
|
||||
/// The <see cref="object"/> to be tested for equality.
|
||||
/// </param>
|
||||
///
|
||||
/// <returns>
|
||||
/// <see cref="bool"/> true if the specified object has memeberwise equality with
|
||||
/// this object; false otherwise.
|
||||
/// </returns>
|
||||
///
|
||||
public override bool Equals( object obj )
|
||||
{
|
||||
DataArray<DataType> that = obj as DataArray<DataType>;
|
||||
try
|
||||
{
|
||||
return (null != that)
|
||||
&& this.ScaleFactorMv.Equals( that.ScaleFactorMv )
|
||||
&& this.MvPerEu.Equals( that.MvPerEu )
|
||||
&& (this.Values.Length == that.Values.Length)
|
||||
&& this.ValuesEquals( that.Values );
|
||||
}
|
||||
|
||||
catch ( System.Exception ex )
|
||||
{
|
||||
throw new Exception( "encountered problem equality-testing object " + ( null != obj ? "\"" + obj.ToString( )+ "\"" : "<<NULL>>" ), ex );
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test the specified object's module list for equality with this object's
|
||||
/// data list.
|
||||
/// </summary>
|
||||
///
|
||||
/// <param name="thoseChannels">
|
||||
/// The <see cref="List"/> of <see cref="Dts.Serialization.Test"/> object to be
|
||||
/// compared for equality with this test's equivalent.
|
||||
/// </param>
|
||||
///
|
||||
/// <returns>
|
||||
/// <see cref="bool"/> true if the two lists contain equivalent-valued members;
|
||||
/// false otherwise.
|
||||
/// </returns>
|
||||
///
|
||||
private bool ValuesEquals( DataType[] thoseValues )
|
||||
{
|
||||
try
|
||||
{
|
||||
if ( null == thoseValues || this.Values.Length != thoseValues.Length )
|
||||
return false;
|
||||
else for ( int i=0; i < this.Values.Length; i++ )
|
||||
if ( !this.Values[ i ].Equals( thoseValues[ i ] ) )
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
catch ( System.Exception ex )
|
||||
{
|
||||
throw new Exception( "encountered problem equality testing value array", ex );
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Return the hash code for this object.
|
||||
/// </summary>
|
||||
///
|
||||
/// <returns>
|
||||
/// The <see cref="int"/> hash code for this object.
|
||||
/// </returns>
|
||||
///
|
||||
public override int GetHashCode( )
|
||||
{
|
||||
return base.GetHashCode( );
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user