chr
2026-04-05 fe750b791d5b517cc4e9bc8e99a9a75139a0cfba
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
//            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
{
    /// <summary>
    /// Base class for resources. Specializations include Dut, Instrument and ResultListener.
    /// </summary>
    public abstract class Resource : ValidatingObject, IResource, INotifyActivity
    {
 
        /// <summary>
        /// Default log that the resource object can write to.  Typically used by instances and extensions of the Resource object.
        /// </summary>
        [XmlIgnore]
        public TraceSource Log
        {
            get;
            private set;
        }
 
        /// <summary>
        /// Instantiate a new instance of <see cref="Resource">Resource</see> class and creates logging source.
        /// </summary>
        public Resource()
        {
            Name = "N/A";
        }
        
        string _name = "";
 
        /// <summary>
        /// A short name displayed in the user interface where space is limited.
        /// </summary>
        [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));
                }
            }
        }
 
        /// <summary>
        /// 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.
        /// </summary>
        public override string ToString()
        {
            return Name;
        }
 
        /// <summary>
        /// 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. 
        /// </summary>
        public virtual void Open()
        {
            IsConnected = true;
        }
 
        /// <summary>
        /// When overridden in a derived class, should contain implementation to close the connection made to the resource represented by this class.
        /// </summary>
        public virtual void Close()
        {
            IsConnected = false;
        }
 
        /// <summary>
        /// Invoked on activity.
        /// </summary>
        public event EventHandler<EventArgs> Activity;
        /// <summary>
        /// Triggers the ActivityStateChanged event.
        /// </summary>
        public void OnActivity()
        {
            if (Activity != null)
            {
                Activity.Invoke(this, new EventArgs());
            }
        }
        
        private bool isConnected = false;
        /// <summary>
        /// Indicates whether this resource is currently connected.
        /// This value should be set by Open() and Close().
        /// </summary>
        [XmlIgnore]
        [Browsable(false)]
        public bool IsConnected
        {
            get { return isConnected; }
            set
            {
                if(value == isConnected) return;
                isConnected = value;
                OnPropertyChanged(nameof(IsConnected));
            }
        }
 
    }
 
    /// <summary>
    /// Indicates that a property contains metadata which may be saved into results.
    /// </summary>
    /// <remarks>
    /// ResultListeners can use this attribute to determine whether to save a property.
    /// </remarks>
    [AttributeUsage(AttributeTargets.Property)]
    public class MetaDataAttribute : Attribute
    {
        /// <summary>
        /// Contains metadata for one property.
        /// </summary>
        public struct MetaDataParameter
        {
            /// <summary>
            /// Metadata name. Always property name.
            /// </summary>
            public readonly string Name;
            /// <summary>
            /// Metadata value. Property value.
            /// </summary>
            public readonly object Value;
            /// <summary>Constructor for MetaDataParameter.</summary>
            /// <param name="name"></param>
            /// <param name="value"></param>
            public MetaDataParameter(string name, object value)
            {
                Name = name;
                Value = value;
            }
        }
 
        /// <summary> Constructor for MetaDataAttribute. </summary>
        /// <param name="promptUser">The options for use with MetaData.</param>
        /// <param name="macroName">The text for use as macroname if the Macro option is selected.</param>
        public MetaDataAttribute(bool promptUser = false, string macroName = null)
        {
            this.PromptUser = promptUser;
            this.MacroName = macroName;
        }
        
        /// <summary> Which options are enabled for this attribute. </summary>
        public bool PromptUser { get; private set; }
 
        /// <summary> Name of the macro. </summary>
        public string MacroName { get; private set; }
 
        /// <summary> These properties does not change during test plan run. </summary>
        public bool Frozen { get; set; }
        
        /// <summary> The group of this metadata. Generally this does not need to be set.
        /// Common values for this is "Plan", "Step", "DUT". </summary>
        public string Group { get; set; }
        
        /// <summary> The name of this metadata. Mostly this can be set to null to inherit Display Name.</summary>
        public string Name { get; set; }
 
        /// <summary>
        /// Gets the name and value for each metadata property in this object that is not null.
        /// </summary>
        /// <returns>Name and value for each metadata property in this
        /// object that is not null as a MetaDataParameter object.</returns>
        public static List<MetaDataParameter> 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();
        }
    }
}