Change log system

This commit is contained in:
luosheng
2023-03-01 09:36:07 +08:00
parent 46222348be
commit a8c9fefbce
80 changed files with 2227 additions and 1921 deletions

View File

@@ -2,33 +2,33 @@
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
/// Useful extension methods for OPC Clients
/// </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))
public static class ClientExtensions
{
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;
}
/// <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;
}
}
}
}
}

View File

@@ -4,100 +4,100 @@ 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
/// <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>
/// 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 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>
/// 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>
/// 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>
/// 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>
/// 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>
/// 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>
/// 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>
/// 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>
/// 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>
/// 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>
/// 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);
}
/// <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);
}
}

View File

@@ -1,48 +1,46 @@
using System.Collections.Generic;
namespace Hylasoft.Opc.Common
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
/// Base class representing a node on the OPC server
/// </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)
public abstract class Node
{
Name = name;
Parent = parent;
if (parent != null && !string.IsNullOrEmpty(parent.Tag))
Tag = parent.Tag + '.' + name;
else
Tag = name;
}
/// <summary>
/// Gets the displayed name of the node
/// </summary>
public string Name { get; protected set; }
/// <summary>
/// Overrides ToString()
/// </summary>
public override string ToString()
{
return Tag;
/// <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;
}
}
}
}

View File

@@ -4,67 +4,67 @@ 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
/// Identifies an exception occurred during OPC Communication
/// </summary>
public OpcException()
[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);
}
}
/// <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);
}
}
}

View File

@@ -1,18 +1,18 @@
namespace Hylasoft.Opc.Common
{
/// <summary>
/// Identifies the status of an OPC connector
/// </summary>
public enum OpcStatus
{
/// <summary>
/// The client is not connected
/// Identifies the status of an OPC connector
/// </summary>
NotConnected,
public enum OpcStatus
{
/// <summary>
/// The client is not connected
/// </summary>
NotConnected,
/// <summary>
/// The client is connected
/// </summary>
Connected
}
/// <summary>
/// The client is connected
/// </summary>
Connected
}
}

View File

@@ -2,27 +2,27 @@
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
/// Represents the quality of the value captured
/// </summary>
[Description("Unknown")]
Unknown,
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: Good
/// </summary>
[Description("Good")]
Good,
/// <summary>
/// Quality: Bad
/// </summary>
[Description("Bad")]
Bad
}
/// <summary>
/// Quality: Bad
/// </summary>
[Description("Bad")]
Bad
}
}

View File

@@ -1,37 +1,33 @@
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
/// Base class representing a monitor event on the OPC server
/// </summary>
public T Value { get; set; }
/// <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 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 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; }
}
/// <summary>
/// Gets the server timestamp on when the event ocurred
/// </summary>
public DateTime ServerTimestamp { get; set; }
}
}