using System;
namespace Hylasoft.Opc.Common
{
///
/// Useful extension methods for OPC Clients
///
public static class ClientExtensions
{
///
/// Reads a tag from the OPC. If for whatever reason the read fails (Tag doesn't exist, server not available) returns a default value
///
/// the opc client to use for the read
/// The fully qualified identifier of the tag
/// the default value to read if the read fails
///
public static ReadEvent ReadOrdefault(this IClient client, string tag, T defaultValue = default(T))
{
try
{
return client.Read(tag);
}
catch (OpcException)
{
var readEvent = new ReadEvent();
readEvent.Quality = Quality.Good;
readEvent.Value = defaultValue;
readEvent.SourceTimestamp = DateTime.Now;
readEvent.ServerTimestamp = DateTime.Now;
return readEvent;
}
}
}
}