/* 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 { /// /// Representation of a calculated channel. /// [XmlSerializationTag("CalculatedChannel")] public class CalculatedChannel : DTS.Serialization.Test.Module.AnalogInputChannel { public CalculatedChannel(Test.Module parentModule) : base(parentModule) { } /// /// Get/set the Source Channel for this channel. /// [XmlSerializationTag("SourceChannelNumber")] public int [] SourceChannelNumber { get { return _SourceChannelNumber.Value; } set { _SourceChannelNumber.Value = value; } } private Property _SourceChannelNumber = new Property( typeof(CalculatedChannel).Namespace + ".Test.Module.CalculatedChannel.SourceChannelNumber", null, false ); /// /// Get/set the Source Channel for this channel. /// [XmlSerializationTag("SourceModuleNumber")] public int [] SourceModuleNumber { get { return _SourceModuleNumber.Value; } set { _SourceModuleNumber.Value = value; } } private Property _SourceModuleNumber = new Property( typeof(CalculatedChannel).Namespace + ".Test.Module.CalculatedChannel.SourceModuleNumber", null, false ); /// /// Get/set the Source Channel for this channel. /// [XmlSerializationTag("SourceModuleSerialNumber")] public string [] SourceModuleSerialNumber { get { return _SourceModuleSerialNumber.Value; } set { _SourceModuleSerialNumber.Value = value; } } private Property _SourceModuleSerialNumber = new Property( typeof(CalculatedChannel).Namespace + ".Test.Module.CalculatedChannel.SourceModuleSerialNumber", null, false ); /// /// the calculation for the channel(s) involved /// [XmlSerializationTag("Calculation")] public string Calculation { get { return _Calculation.Value; } set { _Calculation.Value = value; } } private Property _Calculation = new Property( typeof(CalculatedChannel).Namespace + ".Test.Module.CalculatedChannel.Calculation", "NONE", false ); /// /// Get/set the sample rate for this test module. /// [XmlSerializationTag("SampleRateHz")] public double SampleRateHz { get { return _SampleRateHz.Value; } set { _SampleRateHz.Value = value; } } private Property _SampleRateHz = new Property( typeof(CalculatedChannel).Namespace + ".Test.Module.CalculatedChannel.SampleRateHz", 0, false ); private const string BeginTagModifier = "Begin"; private const string EndTagModifier = "End"; /// /// Write XML serialization for this object to the specified writer. /// /// /// /// The to which this object's XML serialization /// will be written. /// /// public override void WriteXml(XmlWriter writer) { try { DTS.Utilities.Logging.APILogger.Log("writing CalculatedChannel::WriteXML"); var cult = new System.Globalization.CultureInfo(""); AttributeExtractor attributeExtractor = new AttributeExtractor(); 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); } } /// /// Read XML serialization for this object from the specified reader. /// /// /// /// The from which this object's XML serialization /// will be read. /// /// public override void ReadXml(XmlReader reader) { try { var cult = new System.Globalization.CultureInfo(""); AttributeExtractor attributeExtractor = new AttributeExtractor(); DTS.Utilities.Xml.PropertyAttributeDecoder xmlAttributeDecoder = new PropertyAttributeDecoder(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 ints = new List(); 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); } /// /// Test the specified object for equality with this object. /// /// /// /// The to be tested for equality. /// /// /// /// true if the specified object has memeberwise equality with /// this object; false otherwise. /// /// 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() + "\"" : "<>"), ex); } } /// /// Return the hash code for this object. /// /// /// /// The hash code for this object. /// /// public override int GetHashCode() { return base.GetHashCode(); } public override string ToString() { return ChannelDescriptionString; } /// /// creates a new calculated channel, with the modulenumbers, channelnumbers, and moduleserialnumbers composed of all the inputs /// /// /// /// 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 channelNumbers = new List(); List moduleNumbers = new List(); List moduleSerialNumbers = new List(); 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; } /// /// creates a calculated channel using a single source channel /// /// /// 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; } } } } }