// Copyright Keysight Technologies 2012-2019 // This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this // file, you can obtain one at http://mozilla.org/MPL/2.0/. using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Xml.Serialization; namespace OpenTap { /// /// Base class for resources. Specializations include Dut, Instrument and ResultListener. /// public abstract class Resource : ValidatingObject, IResource, INotifyActivity { /// /// Default log that the resource object can write to. Typically used by instances and extensions of the Resource object. /// [XmlIgnore] public TraceSource Log { get; private set; } /// /// Instantiate a new instance of Resource class and creates logging source. /// public Resource() { Name = "N/A"; } string _name = ""; /// /// A short name displayed in the user interface where space is limited. /// [Display(nameof(Name), Group: "Common", Order: -3)] [Browsable(false)] public string Name { get { return _name; } set { if (_name != value) { if (value == null) throw new ArgumentNullException(nameof(Name)); _name = value; if (Log != null) { OpenTap.Log.RemoveSource(Log); } Log = OpenTap.Log.CreateSource(_name, this); OnPropertyChanged(nameof(Name)); } } } /// /// Overrides ToString() to return the Name of the resource. Can be overridden by derived classes to provider a more descriptive name. Note the overrider should include the Name in the output. /// public override string ToString() { return Name; } /// /// When overridden in a derived class, should contain implementation to open a connection to the resource represented by this class. /// Any one time initialization should be done here as well. /// public virtual void Open() { IsConnected = true; } /// /// When overridden in a derived class, should contain implementation to close the connection made to the resource represented by this class. /// public virtual void Close() { IsConnected = false; } /// /// Invoked on activity. /// public event EventHandler Activity; /// /// Triggers the ActivityStateChanged event. /// public void OnActivity() { if (Activity != null) { Activity.Invoke(this, new EventArgs()); } } private bool isConnected = false; /// /// Indicates whether this resource is currently connected. /// This value should be set by Open() and Close(). /// [XmlIgnore] [Browsable(false)] public bool IsConnected { get { return isConnected; } set { if(value == isConnected) return; isConnected = value; OnPropertyChanged(nameof(IsConnected)); } } } /// /// Indicates that a property contains metadata which may be saved into results. /// /// /// ResultListeners can use this attribute to determine whether to save a property. /// [AttributeUsage(AttributeTargets.Property)] public class MetaDataAttribute : Attribute { /// /// Contains metadata for one property. /// public struct MetaDataParameter { /// /// Metadata name. Always property name. /// public readonly string Name; /// /// Metadata value. Property value. /// public readonly object Value; /// Constructor for MetaDataParameter. /// /// public MetaDataParameter(string name, object value) { Name = name; Value = value; } } /// Constructor for MetaDataAttribute. /// The options for use with MetaData. /// The text for use as macroname if the Macro option is selected. public MetaDataAttribute(bool promptUser = false, string macroName = null) { this.PromptUser = promptUser; this.MacroName = macroName; } /// Which options are enabled for this attribute. public bool PromptUser { get; private set; } /// Name of the macro. public string MacroName { get; private set; } /// These properties does not change during test plan run. public bool Frozen { get; set; } /// The group of this metadata. Generally this does not need to be set. /// Common values for this is "Plan", "Step", "DUT". public string Group { get; set; } /// The name of this metadata. Mostly this can be set to null to inherit Display Name. public string Name { get; set; } /// /// Gets the name and value for each metadata property in this object that is not null. /// /// Name and value for each metadata property in this /// object that is not null as a MetaDataParameter object. public static List GetMetaDataParameters(object obj) { if (obj == null) throw new ArgumentNullException(nameof(obj)); var metadata = ResultParameters.GetMetadataFromObject(obj); return metadata.Select(meta => new MetaDataParameter(meta.Name, meta.Value)).ToList(); } } }