// 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.Linq; using System.Xml.Serialization; namespace OpenTap { /// /// Object representing a port on an instrument or DUT. Ports are connected with objects. /// public class Port : IConstResourceProperty { /// /// The device (usually an or ) on which this port exists. /// [XmlIgnore] // avoid potential cycle in XML. public IResource Device { get; private set; } /// /// List of s connected to this port. /// public IEnumerable Connections => ConnectionSettings.Current.Where(con => con.HasPort(this)); IEnumerable ActiveConnections => Connections.Where(con => con.IsActive); /// /// The name of this port. (Should be unique among objects on the same device/resource). /// public string Name { get; internal set; } /// /// Initializes a new instance of the class. /// public Port(IResource device, string name) { Device = device; Name = name; } /// /// Returns a list of the ports that this port has connections to. /// public IEnumerable GetConnectedPorts() { return Connections.SelectMany(c => new[] {c.Port1, c.Port2}).Where(p => p != this && p != null); } /// /// Returns a list of the ports that this port has connections to and that are active. /// public IEnumerable GetActiveConnectedPorts() { return ActiveConnections .SelectMany(c => new[] {c.Port1, c.Port2}) .Where(p => p != this && p != null); } /// /// Returns a list of connections from this port to a specified device. /// public IEnumerable GetConnectionsTo(IResource device) { if (device == null) throw new ArgumentNullException(nameof(device)); return Connections.Where(x => x?.Port1?.Device == device || x?.Port2?.Device == device || (x?.Via?.Any(v => v.Device == device) ?? false)); } /// /// Returns a list of connections from this port to a specified port. /// public IEnumerable GetConnectionsTo(Port otherPort) { return Connections.Where(c => c.HasPort(otherPort)); } /// /// Returns a string describing this port. /// /// public override string ToString() { return $"{Name} on {(Device != null ? Device.Name : "")}"; } } /// /// An unidirectional port that is always an input. /// public class InputPort : Port { /// /// Initializes a new instance of the class. /// public InputPort(IResource device, string name) : base(device, name) { } } /// /// An unidirectional port that is always an output. /// public class OutputPort : Port { /// /// Initializes a new instance of the class. /// public OutputPort(IResource device, string name) : base(device, name) { } } }