Samples Reappend
This commit is contained in:
34
Libraries/h-opc/Common/ClientExtensions.cs
Normal file
34
Libraries/h-opc/Common/ClientExtensions.cs
Normal file
@@ -0,0 +1,34 @@
|
||||
using System;
|
||||
|
||||
namespace Hylasoft.Opc.Common
|
||||
{
|
||||
/// <summary>
|
||||
/// Useful extension methods for OPC Clients
|
||||
/// </summary>
|
||||
public static class ClientExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Reads a tag from the OPC. If for whatever reason the read fails (Tag doesn't exist, server not available) returns a default value
|
||||
/// </summary>
|
||||
/// <param name="client">the opc client to use for the read</param>
|
||||
/// <param name="tag">The fully qualified identifier of the tag</param>
|
||||
/// <param name="defaultValue">the default value to read if the read fails</param>
|
||||
/// <returns></returns>
|
||||
public static ReadEvent<T> ReadOrdefault<T>(this IClient<Node> client, string tag, T defaultValue = default(T))
|
||||
{
|
||||
try
|
||||
{
|
||||
return client.Read<T>(tag);
|
||||
}
|
||||
catch (OpcException)
|
||||
{
|
||||
var readEvent = new ReadEvent<T>();
|
||||
readEvent.Quality = Quality.Good;
|
||||
readEvent.Value = defaultValue;
|
||||
readEvent.SourceTimestamp = DateTime.Now;
|
||||
readEvent.ServerTimestamp = DateTime.Now;
|
||||
return readEvent;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
103
Libraries/h-opc/Common/IClient.cs
Normal file
103
Libraries/h-opc/Common/IClient.cs
Normal file
@@ -0,0 +1,103 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Hylasoft.Opc.Common
|
||||
{
|
||||
/// <summary>
|
||||
/// Client interface to perform basic Opc tasks, like discovery, monitoring, reading/writing tags,
|
||||
/// </summary>
|
||||
public interface IClient<out TNode> : IDisposable
|
||||
where TNode : Node
|
||||
{
|
||||
/// <summary>
|
||||
/// Connect the client to the OPC Server
|
||||
/// </summary>
|
||||
Task Connect();
|
||||
|
||||
/// <summary>
|
||||
/// Gets the current status of the OPC Client
|
||||
/// </summary>
|
||||
OpcStatus Status { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the datatype of an OPC tag
|
||||
/// </summary>
|
||||
/// <param name="tag">Tag to get datatype of</param>
|
||||
/// <returns>System Type</returns>
|
||||
System.Type GetDataType(string tag);
|
||||
|
||||
/// <summary>
|
||||
/// Read a tag
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of tag to read</typeparam>
|
||||
/// <param name="tag">The fully-qualified identifier of the tag. You can specify a subfolder by using a comma delimited name.
|
||||
/// E.g: the tag `foo.bar` reads the tag `bar` on the folder `foo`</param>
|
||||
/// <returns>The value retrieved from the OPC</returns>
|
||||
ReadEvent<T> Read<T>(string tag);
|
||||
|
||||
/// <summary>
|
||||
/// Write a value on the specified opc tag
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of tag to write on</typeparam>
|
||||
/// <param name="tag">The fully-qualified identifier of the tag. You can specify a subfolder by using a comma delimited name.
|
||||
/// E.g: the tag `foo.bar` writes on the tag `bar` on the folder `foo`</param>
|
||||
/// <param name="item"></param>
|
||||
void Write<T>(string tag, T item);
|
||||
|
||||
/// <summary>
|
||||
/// Monitor the specified tag for changes
|
||||
/// </summary>
|
||||
/// <typeparam name="T">the type of tag to monitor</typeparam>
|
||||
/// <param name="tag">The fully-qualified identifier of the tag. You can specify a subfolder by using a comma delimited name.
|
||||
/// E.g: the tag `foo.bar` monitors the tag `bar` on the folder `foo`</param>
|
||||
/// <param name="callback">the callback to execute when the value is changed.
|
||||
/// The first parameter is the new value of the node, the second is an `unsubscribe` function to unsubscribe the callback</param>
|
||||
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures", Justification = "This is an async method.")]
|
||||
void Monitor<T>(string tag, Action<ReadEvent<T>, Action> callback);
|
||||
|
||||
/// <summary>
|
||||
/// Finds a node on the Opc Server
|
||||
/// </summary>
|
||||
/// <param name="tag">The fully-qualified identifier of the tag. You can specify a subfolder by using a comma delimited name.
|
||||
/// E.g: the tag `foo.bar` finds the tag `bar` on the folder `foo`</param>
|
||||
/// <returns>If there is a tag, it returns it, otherwise it throws an </returns>
|
||||
TNode FindNode(string tag);
|
||||
|
||||
/// <summary>
|
||||
/// Gets the root node of the server
|
||||
/// </summary>
|
||||
TNode RootNode { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Explore a folder on the Opc Server
|
||||
/// </summary>
|
||||
/// <param name="tag">The fully-qualified identifier of the tag. You can specify a subfolder by using a comma delimited name.
|
||||
/// E.g: the tag `foo.bar` finds the sub nodes of `bar` on the folder `foo`</param>
|
||||
/// <returns>The list of sub-nodes</returns>
|
||||
IEnumerable<TNode> ExploreFolder(string tag);
|
||||
|
||||
/// <summary>
|
||||
/// Read a tag asynchronusly
|
||||
/// </summary>
|
||||
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures", Justification = "This is an async method.")]
|
||||
Task<ReadEvent<T>> ReadAsync<T>(string tag);
|
||||
|
||||
/// <summary>
|
||||
/// Write a value on the specified opc tag asynchronously
|
||||
/// </summary>
|
||||
Task WriteAsync<T>(string tag, T item);
|
||||
|
||||
/// <summary>
|
||||
/// Finds a node on the Opc Server asynchronously
|
||||
/// </summary>
|
||||
Task<Node> FindNodeAsync(string tag);
|
||||
|
||||
/// <summary>
|
||||
/// Explore a folder on the Opc Server asynchronously
|
||||
/// </summary>
|
||||
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures",
|
||||
Justification = "Task")]
|
||||
Task<IEnumerable<Node>> ExploreFolderAsync(string tag);
|
||||
}
|
||||
}
|
||||
48
Libraries/h-opc/Common/Node.cs
Normal file
48
Libraries/h-opc/Common/Node.cs
Normal file
@@ -0,0 +1,48 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
70
Libraries/h-opc/Common/OpcException.cs
Normal file
70
Libraries/h-opc/Common/OpcException.cs
Normal file
@@ -0,0 +1,70 @@
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
18
Libraries/h-opc/Common/OpcStatus.cs
Normal file
18
Libraries/h-opc/Common/OpcStatus.cs
Normal file
@@ -0,0 +1,18 @@
|
||||
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
|
||||
}
|
||||
}
|
||||
28
Libraries/h-opc/Common/Quality.cs
Normal file
28
Libraries/h-opc/Common/Quality.cs
Normal file
@@ -0,0 +1,28 @@
|
||||
using System.ComponentModel;
|
||||
|
||||
namespace Hylasoft.Opc.Common
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents the quality of the value captured
|
||||
/// </summary>
|
||||
public enum Quality
|
||||
{
|
||||
/// <summary>
|
||||
/// Quality: Unknown, the value of the quality could not be inferred by the library
|
||||
/// </summary>
|
||||
[Description("Unknown")]
|
||||
Unknown,
|
||||
|
||||
/// <summary>
|
||||
/// Quality: Good
|
||||
/// </summary>
|
||||
[Description("Good")]
|
||||
Good,
|
||||
|
||||
/// <summary>
|
||||
/// Quality: Bad
|
||||
/// </summary>
|
||||
[Description("Bad")]
|
||||
Bad
|
||||
}
|
||||
}
|
||||
37
Libraries/h-opc/Common/ReadEvent.cs
Normal file
37
Libraries/h-opc/Common/ReadEvent.cs
Normal file
@@ -0,0 +1,37 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Hylasoft.Opc.Common
|
||||
{
|
||||
/// <summary>
|
||||
/// Base class representing a monitor event on the OPC server
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
public class ReadEvent<T>
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the value that was read from the server
|
||||
/// </summary>
|
||||
public T Value { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the quality of the signal from the server
|
||||
/// </summary>
|
||||
[DefaultValue(Common.Quality.Unknown)]
|
||||
public Quality Quality { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the source timestamp on when the event ocurred
|
||||
/// </summary>
|
||||
public DateTime SourceTimestamp { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the server timestamp on when the event ocurred
|
||||
/// </summary>
|
||||
public DateTime ServerTimestamp { get; set; }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user