<|start_filename|>h-opc/Common/Node.cs<|end_filename|>
﻿using System.Collections.Generic;

namespace Hylasoft.Opc.Common
{
  /// <summary>
  /// Base class representing a node on the OPC server
  /// </summary>
  public abstract class Node
  {
    /// <summary>
    /// Gets the displayed name of the node
    /// </summary>
    public string Name { get; protected set; }

    /// <summary>
    /// Gets the dot-separated fully qualified tag of the node
    /// </summary>
    public string Tag { get; protected set; }

    /// <summary>
    /// Gets the parent node. If the node is root, returns null
    /// </summary>
    public Node Parent { get; private set; }

    /// <summary>
    /// Creates a new node
    /// </summary>
    /// <param name="name">the name of the node</param>
    /// <param name="parent">The parent node</param>
    protected Node(string name, Node parent = null)
    {
      Name = name;
      Parent = parent;
      if (parent != null && !string.IsNullOrEmpty(parent.Tag))
        Tag = parent.Tag + '.' + name;
      else
        Tag = name;
    }

    /// <summary>
    /// Overrides ToString()
    /// </summary>
    public override string ToString()
    {
      return Tag;
    }
  }
}


<|start_filename|>h-opc/Ua/ClientUtils.cs<|end_filename|>
﻿using System.Linq;
using Opc.Ua;
using System;
using Opc.Ua.Client;

namespace Hylasoft.Opc.Ua
{
  /// <summary>
  /// List of static utility methods
  /// </summary>
  internal static class ClientUtils
  {
    // TODO I didn't write these methods. I should rewrite it once I understand whtat it does, beacuse it looks crazy

    public static EndpointDescription SelectEndpoint(Uri discoveryUrl, bool useSecurity)
    {
      var configuration = EndpointConfiguration.Create();
      configuration.OperationTimeout = 5000;
      EndpointDescription endpointDescription1 = null;
      using (var discoveryClient = DiscoveryClient.Create(discoveryUrl, configuration))
      {
        var endpoints = discoveryClient.GetEndpoints(null);
        foreach (var endpointDescription2 in endpoints.Where(endpointDescription2 => endpointDescription2.EndpointUrl.StartsWith(discoveryUrl.Scheme)))
        {
          if (useSecurity)
          {
            if (endpointDescription2.SecurityMode == MessageSecurityMode.None)
              continue;
          }
          else if (endpointDescription2.SecurityMode != MessageSecurityMode.None)
            continue;
          if (endpointDescription1 == null)
            endpointDescription1 = endpointDescription2;
          if (endpointDescription2.SecurityLevel > endpointDescription1.SecurityLevel)
            endpointDescription1 = endpointDescription2;
        }
        if (endpointDescription1 == null)
        {
          if (endpoints.Count > 0)
            endpointDescription1 = endpoints[0];
        }
      }
      var uri = Utils.ParseUri(endpointDescription1.EndpointUrl);
      if (uri != null && uri.Scheme == discoveryUrl.Scheme)
        endpointDescription1.EndpointUrl = new UriBuilder(uri)
        {
          Host = discoveryUrl.DnsSafeHost,
          Port = discoveryUrl.Port
        }.ToString();
      return endpointDescription1;
    }

    public static ReferenceDescriptionCollection Browse(Session session, NodeId nodeId)
    {
      var desc = new BrowseDescription
      {
        NodeId = nodeId,
        BrowseDirection = BrowseDirection.Forward,
        IncludeSubtypes = true,
        NodeClassMask = 0U,
        ResultMask = 63U,
      };
      return Browse(session, desc, true);
    }

    public static ReferenceDescriptionCollection Browse(Session session, BrowseDescription nodeToBrowse, bool throwOnError)
    {
      try
      {
        var descriptionCollection = new ReferenceDescriptionCollection();
        var nodesToBrowse = new BrowseDescriptionCollection { nodeToBrowse };
        BrowseResultCollection results;
        DiagnosticInfoCollection diagnosticInfos;
        session.Browse(null, null, 0U, nodesToBrowse, out results, out diagnosticInfos);
        ClientBase.ValidateResponse(results, nodesToBrowse);
        ClientBase.ValidateDiagnosticInfos(diagnosticInfos, nodesToBrowse);
        while (!StatusCode.IsBad(results[0].StatusCode))
        {
          for (var index = 0; index < results[0].References.Count; ++index)
            descriptionCollection.Add(results[0].References[index]);
          if (results[0].References.Count == 0 || results[0].ContinuationPoint == null)
            return descriptionCollection;
          var continuationPoints = new ByteStringCollection();
          continuationPoints.Add(results[0].ContinuationPoint);
          session.BrowseNext(null, false, continuationPoints, out results, out diagnosticInfos);
          ClientBase.ValidateResponse(results, continuationPoints);
          ClientBase.ValidateDiagnosticInfos(diagnosticInfos, continuationPoints);
        }
        throw new ServiceResultException(results[0].StatusCode);
      }
      catch (Exception ex)
      {
        if (throwOnError)
          throw new ServiceResultException(ex, 2147549184U);
        return null;
      }
    }
  }
}



<|start_filename|>tests/TestExtendUaClient.cs<|end_filename|>
﻿using Hylasoft.Opc.Ua;
using Opc.Ua.Client;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Hylasoft.Opc.Tests
{
  public class TestExtendUaClient : UaClient
  {
    public TestExtendUaClient(Uri server)
      : base(server)
    {
    }

    public Session SessionExtended
    {
      get
      {
        return this.Session;
      }
    }
  }
}


<|start_filename|>h-opc-cli/Command.cs<|end_filename|>
using System.Collections.Generic;

namespace Hylasoft.Opc.Cli
{
  /// <summary>
  /// Represent a command
  /// </summary>
  public class Command
  {
    public Command(SupportedCommands cmd, IList<string> args = null)
    {
      Cmd = cmd;
      Args = args ?? new string[0];
    }

    public SupportedCommands Cmd { get; private set; }

    public IList<string> Args { get; private set; }
  }
}

<|start_filename|>h-opc/Common/OpcException.cs<|end_filename|>
﻿using Opc.Ua;
using System;
using System.Runtime.Serialization;

namespace Hylasoft.Opc.Common
{
  /// <summary>
  /// Identifies an exception occurred during OPC Communication
  /// </summary>
  [Serializable]
  public class OpcException : Exception
  {
    /// <summary>
    /// Initialize a new instance of the OpcException class
    /// </summary>
    public OpcException()
    {
    }

    /// <summary>
    /// Initialize a new instance of the OpcException class
    /// </summary>
    public OpcException(string message)
      : base(message)
    {
    }

    /// <summary>
    /// Returns an (optional) associated OPC UA StatusCode for the exception.
    /// </summary>
    public StatusCode? Status { get; private set; }

    /// <summary>
    /// Initialize a new instance of the OpcException class
    /// </summary>
    public OpcException(string message, StatusCode status)
      : base(message)
    {
      Status = status;
    }

    /// <summary>
    /// Initialize a new instance of the OpcException class
    /// </summary>
    public OpcException(string message, Exception inner)
      : base(message, inner)
    {
    }

    /// <summary>
    /// Initialize a new instance of the OpcException class
    /// </summary>
    protected OpcException(SerializationInfo info, StreamingContext context)
      : base(info, context)
    {
    }

    /// <summary>
    /// Sets the System.Runtime.Serialization.SerializationInfo with information about the exception.
    /// </summary>
    /// <param name="info">The System.Runtime.Serialization.SerializationInfo that holds the serialized object data about the exception being thrown.</param>
    /// <param name="context">The System.Runtime.Serialization.StreamingContext that contains contextual information about the source or destination.</param>
    public override void GetObjectData(SerializationInfo info, StreamingContext context)
    {
      base.GetObjectData(info, context);
    }

  }

}

<|start_filename|>h-opc/Common/OpcStatus.cs<|end_filename|>
﻿namespace Hylasoft.Opc.Common
{
  /// <summary>
  /// Identifies the status of an OPC connector
  /// </summary>
  public enum OpcStatus
  {
    /// <summary>
    /// The client is not connected
    /// </summary>
    NotConnected,

    /// <summary>
    /// The client is connected
    /// </summary>
    Connected
  }
}

<|start_filename|>h-opc-cli/CliUtils.cs<|end_filename|>
﻿using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;

namespace Hylasoft.Opc.Cli
{
  internal static class CliUtils
  {
    /// <summary>
    /// Split arguments in a string taking in consideration quotes
    /// E.g: 'aaa bbb "cc cc"' => ['aaa', 'bbb', 'cc cc']
    /// </summary>
    public static IList<string> SplitArguments(string input)
    {
      return Regex.Split(input, @"(?:([^\s""]+)|""([^""]*)"")+")
        .Where(s => s.Any(c => c != ' '))
        .ToList();
    }
  }
}


<|start_filename|>tests/TestExtendDaClient.cs<|end_filename|>
﻿using Hylasoft.Opc.Common;
using Hylasoft.Opc.Da;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpcDa = Opc.Da;

namespace Hylasoft.Opc.Tests
{
  public class TestExtendDaClient : DaClient
  {
    public TestExtendDaClient(Uri server) : base(server)
    {
    }

    public OpcDa.Server ExposedServer
    {
      get
      {
        return this.Server;
      }
    }
  }
}


<|start_filename|>h-opc-cli/BadCommandException.cs<|end_filename|>
using System;
using System.Runtime.Serialization;

namespace Hylasoft.Opc.Cli
{
  /// <summary>
  /// Thrown when the provided command is bad
  /// </summary>
  [Serializable]
  public class BadCommandException : Exception
  {
    public BadCommandException()
    {
    }

    public BadCommandException(string message)
      : base(message)
    {
    }

    public BadCommandException(string message, Exception inner)
      : base(message, inner)
    {
    }

    protected BadCommandException(SerializationInfo info, StreamingContext context)
      : base(info, context)
    {
    }
  }
}