Files
DP44/Common/DTS.Common.Serialization/Test.Module.CalculatedChannel.cs

386 lines
17 KiB
C#
Raw Normal View History

2026-04-17 14:55:32 -04:00
/*
Test.Module.AnalogInputChannel.cs
Copyright © 2008
Diversified Technical Systems, Inc.
All Rights Reserved
*/
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Serialization;
using DTS.DAS.Concepts;
using DTS.Utilities;
using DTS.Utilities.DotNetProgrammingConstructs;
using DTS.Utilities.Xml;
using System.ComponentModel;
namespace DTS.Serialization
{
// *** see Test.cs ***
public partial class Test
{
// *** see Test.Module.cs ***
public partial class Module
{
/// <summary>
/// Representation of a calculated channel.
/// </summary>
[XmlSerializationTag("CalculatedChannel")]
public class CalculatedChannel : DTS.Serialization.Test.Module.AnalogInputChannel
{
public CalculatedChannel(Test.Module parentModule)
: base(parentModule)
{
}
/// <summary>
/// Get/set the Source Channel for this channel.
/// </summary>
[XmlSerializationTag("SourceChannelNumber")]
public int [] SourceChannelNumber
{
get { return _SourceChannelNumber.Value; }
set { _SourceChannelNumber.Value = value; }
}
private Property<int []> _SourceChannelNumber
= new Property<int[]>(
typeof(CalculatedChannel).Namespace + ".Test.Module.CalculatedChannel.SourceChannelNumber",
null,
false
);
/// <summary>
/// Get/set the Source Channel for this channel.
/// </summary>
[XmlSerializationTag("SourceModuleNumber")]
public int [] SourceModuleNumber
{
get { return _SourceModuleNumber.Value; }
set { _SourceModuleNumber.Value = value; }
}
private Property<int[]> _SourceModuleNumber
= new Property<int[]>(
typeof(CalculatedChannel).Namespace + ".Test.Module.CalculatedChannel.SourceModuleNumber",
null,
false
);
/// <summary>
/// Get/set the Source Channel for this channel.
/// </summary>
[XmlSerializationTag("SourceModuleSerialNumber")]
public string [] SourceModuleSerialNumber
{
get { return _SourceModuleSerialNumber.Value; }
set { _SourceModuleSerialNumber.Value = value; }
}
private Property<string [] > _SourceModuleSerialNumber
= new Property<string []>(
typeof(CalculatedChannel).Namespace + ".Test.Module.CalculatedChannel.SourceModuleSerialNumber",
null,
false
);
/// <summary>
/// the calculation for the channel(s) involved
/// </summary>
[XmlSerializationTag("Calculation")]
public string Calculation
{
get { return _Calculation.Value; }
set { _Calculation.Value = value; }
}
private Property<string> _Calculation
= new Property<string>(
typeof(CalculatedChannel).Namespace + ".Test.Module.CalculatedChannel.Calculation",
"NONE",
false
);
/// <summary>
/// Get/set the sample rate for this test module.
/// </summary>
[XmlSerializationTag("SampleRateHz")]
public double SampleRateHz
{
get { return _SampleRateHz.Value; }
set { _SampleRateHz.Value = value; }
}
private Property<double> _SampleRateHz
= new Property<double>(
typeof(CalculatedChannel).Namespace + ".Test.Module.CalculatedChannel.SampleRateHz",
0,
false
);
private const string BeginTagModifier = "Begin";
private const string EndTagModifier = "End";
/// <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 override void WriteXml(XmlWriter writer)
{
try
{
DTS.Utilities.Logging.APILogger.Log("writing CalculatedChannel::WriteXML");
var cult = new System.Globalization.CultureInfo("");
AttributeExtractor<XmlSerializationTagAttribute> attributeExtractor = new AttributeExtractor<XmlSerializationTagAttribute>();
writer.WriteStartElement(attributeExtractor.ExtractAttachedAttributeFromObject(this).Value);
base.writeXmlAttributes(writer);
writer.WriteAttributeString(attributeExtractor.ExtractAttachedAttributeFromProperty(this,
"SourceChannelNumber").Value, IntArrayToString(this.SourceChannelNumber));
writer.WriteAttributeString(attributeExtractor.ExtractAttachedAttributeFromProperty(this,
"SourceModuleNumber").Value, IntArrayToString(this.SourceModuleNumber));
writer.WriteAttributeString(
attributeExtractor.ExtractAttachedAttributeFromProperty(this, "SourceModuleSerialNumber")
.Value, StringArrayToString(this.SourceModuleSerialNumber));
writer.WriteAttributeString(attributeExtractor.ExtractAttachedAttributeFromProperty(this,
"Calculation").Value, this.Calculation.ToString(cult));
writer.WriteAttributeString(attributeExtractor.ExtractAttachedAttributeFromProperty(this,
"SampleRateHz").Value, this.SampleRateHz.ToString(cult));
writer.WriteEndElement();
}
catch (System.Exception ex)
{
throw new Exception("encountered problem converting " + this.GetType().FullName + " 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 override void ReadXml(XmlReader reader)
{
try
{
var cult = new System.Globalization.CultureInfo("");
AttributeExtractor<XmlSerializationTagAttribute> attributeExtractor = new AttributeExtractor<XmlSerializationTagAttribute>();
DTS.Utilities.Xml.PropertyAttributeDecoder<Test.Module.CalculatedChannel> xmlAttributeDecoder
= new PropertyAttributeDecoder<CalculatedChannel>(this);
base.ReadXml(reader);
this.SourceChannelNumber =
StringToIntArray(xmlAttributeDecoder.ExtractStringProperty("SourceChannelNumber", reader));
this.SourceModuleNumber =
StringToIntArray(xmlAttributeDecoder.ExtractStringProperty("SourceModuleNumber", reader));
this.SourceModuleSerialNumber =
StringToStringArray(xmlAttributeDecoder.ExtractStringProperty("SourceModuleSerialNumber",
reader));
this.Calculation = xmlAttributeDecoder.ExtractStringProperty("Calculation", reader);
this.SampleRateHz = xmlAttributeDecoder.ExtractDoubleProperty("SampleRateHz", reader);
}
catch (System.Exception ex)
{
throw new Exception("encountered problem converting XML to " + this.GetType().FullName + " object", ex);
}
}
private int[] StringToIntArray(string s)
{
List<int> ints = new List<int>();
string[] tokens = s.Split(new char[] {','});
foreach (var token in tokens)
{
int i;
if (int.TryParse(token, System.Globalization.NumberStyles.Any,
System.Globalization.CultureInfo.InvariantCulture, out i))
{
ints.Add(i);
}
}
return ints.ToArray();
}
private string[] StringToStringArray(string s)
{
string[] tokens = s.Split(new string[] {"_.-._"}, StringSplitOptions.None);
return tokens;
}
private string IntArrayToString(int[] array)
{
StringBuilder sb = new StringBuilder();
foreach (var i in array)
{
if (sb.Length > 0)
{
sb.Append(",");
}
sb.Append(i.ToString(System.Globalization.CultureInfo.InvariantCulture));
}
return sb.ToString();
}
private string StringArrayToString(string[] array)
{
return string.Join("_.-._", array);
}
/// <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)
{
try
{
Test.Module.CalculatedChannel that = obj as Test.Module.CalculatedChannel;
return (null != obj)
// Must-be-initialized properties.
&& this.ChannelId.Equals(that.ChannelId)
&& this.SourceChannelNumber.Equals(that.SourceChannelNumber)
&& this.ChannelDescriptionString.Equals(that.ChannelDescriptionString)
&& this.EngineeringUnits.Equals(that.EngineeringUnits, StringComparison.OrdinalIgnoreCase)
&& this.Calculation.Equals(that.Calculation)
&& this.IsoCode.Equals(that.IsoCode);
}
catch (System.Exception ex)
{
throw new Exception("encountered problem equality-testing the object " + (null != obj ? "\"" + obj.ToString() + "\"" : "<<NULL>>"), 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();
}
public override string ToString()
{
return ChannelDescriptionString;
}
/// <summary>
/// creates a new calculated channel, with the modulenumbers, channelnumbers, and moduleserialnumbers composed of all the inputs
///
/// </summary>
/// <param name="channels"></param>
/// <returns></returns>
public static CalculatedChannel CreateInstance(DTS.Serialization.Test.Module.Channel[] channels)
{
//Dammit, need to manually clone properties from souce channel
var cc = new DTS.Serialization.Test.Module.CalculatedChannel(channels.First().ParentModule);
//CC only values
List<int> channelNumbers = new List<int>();
List<int> moduleNumbers = new List<int>();
List<string> moduleSerialNumbers = new List<string>();
float sps = channels.First().ParentModule.SampleRateHz;
foreach (var ch in channels)
{
channelNumbers.Add(ch.Number);
moduleSerialNumbers.Add(ch.ParentModule.SerialNumber);
if (ch.ParentModule.SampleRateHz != sps)
{
throw new InvalidOperationException(String.Format("Sample rates don't match, ({0} != {1})",
sps, ch.ParentModule.SampleRateHz));
}
moduleNumbers.Add(ch.ParentModule.Number);
}
cc.SourceChannelNumber = channelNumbers.ToArray();
cc.SourceModuleNumber = moduleNumbers.ToArray();
cc.SourceModuleSerialNumber = moduleSerialNumbers.ToArray();
cc.SampleRateHz = sps;
//Rip a copy. Can't use cloning
foreach (
PropertyDescriptor item in
TypeDescriptor.GetProperties(
channels.First() as DTS.Serialization.Test.Module.AnalogInputChannel))
{
try
{
item.SetValue(cc, item.GetValue(channels.First()));
}
catch( System.Exception ex)
{
DTS.Utilities.Logging.APILogger.Log(ex);
}
}
//need a copy not the original for linearization formula...
cc.LinearizationFormula =
new LinearizationFormula(
(channels.First() as DTS.Serialization.Test.Module.AnalogInputChannel).LinearizationFormula);
return cc;
}
/// <summary>
/// creates a calculated channel using a single source channel
/// </summary>
/// <param name="sourceChannel"></param>
/// <returns></returns>
public static CalculatedChannel CreateInstance(DTS.Serialization.Test.Module.Channel sourceChannel)
{
//Dammit, need to manually clone properties from souce channel
var cc = new DTS.Serialization.Test.Module.CalculatedChannel(sourceChannel.ParentModule);
//CC only values
cc.SourceChannelNumber = new int[] {sourceChannel.Number};
cc.SourceModuleNumber = new int[] {sourceChannel.ParentModule.Number};
cc.SourceModuleSerialNumber = new string[] {sourceChannel.ParentModule.SerialNumber};
cc.SampleRateHz = sourceChannel.ParentModule.SampleRateHz;
//Rip a copy. Can't use cloning
foreach (PropertyDescriptor item in TypeDescriptor.GetProperties(sourceChannel as DTS.Serialization.Test.Module.AnalogInputChannel))
{
try
{
item.SetValue(cc, item.GetValue(sourceChannel));
}
catch { }
}
return cc;
}
}
}
}
}