-
Notifications
You must be signed in to change notification settings - Fork 9
DataValue
The DataValue library provides a set of helper classes to facilitate the implementation of data values updated by typically using a remote invocation (TCP, HTTP etc.). An application data class is derived from the DataValue class and can use the Status property to maintain information about the quality of the data value.
The IDataValue interface provides a standard set of properties.
public interface IDataValue
{
DataStatus Status { get; set; }
DateTimeOffset Timestamp { get; }
bool IsGood { get; }
bool IsBad { get; }
bool IsUncertain { get; }
}
An application class is derived from the DataValue class already providing the IDataValue interface implementation. In addtion to the IDataValue the DataValue class also implements an INotifyPropertyChanged interface.
Everytime the Status property is changed, the timestamp value is updated, and the PropertyChanged event is raised.
The DataValue class also defines a set of predefined (static) DataStatus instances:
- Good: The operation completed successfully.
- Uncertain: The status is uncertain and the value is not necessarily set.
- Bad: The operation failed.
- BadUnexpectedError: An unexpected error occurred.
- BadInternalError: An internal error occurred as a result of a programming or configuration error.
- BadOutOfMemory: Not enough memory to complete the operation.
- BadResourceUnavailable: An operating system resource is not available.
- BadCommunicationError: A low level communication error occurred.
- BadEncodingError: Encoding halted because of invalid data in the objects being serialized.
- BadDecodingError: Decoding halted because of invalid data in the stream.
- BadEncodingLimitsExceeded: The message encoding/decoding limits imposed by the stack have been exceeded.
- BadRequestTooLarge: The request message size exceeds limits set by the server.
- BadResponseTooLarge: The response message size exceeds limits set by the client.
- BadUnknownResponse: An unrecognized response was received from the server.
- BadTimeout: The operation timed out.
- BadNoCommunication: Communication with the data source is defined, but not established, and there is no last known value available.
- BadWaitingForInitialData: Waiting for the server to obtain values from the underlying data source.
- BadNotReadable: The access level does not allow reading or subscribing to the Node.
- BadNotWritable: The access level does not allow writing to the Node.
- BadOutOfRange: The value was out of range.
- BadNotSupported: The requested operation is not supported.
- BadNotFound: A requested item was not found or a search operation ended without success.
- BadTcpServerTooBusy: The server cannot process the request because it is too busy.
- BadTcpMessageTooLarge: The size of the message specified in the header is too large.
- BadTcpNotEnoughResources: There are not enough resources to process the request.
- BadTcpInternalError: An internal error occurred.
- BadTcpEndpointUrlInvalid: The Server does not recognize the QueryString specified.
- BadRequestInterrupted: The request could not be sent because of a network interruption.
- BadRequestTimeout: Timeout occurred while processing the request.
- BadNotConnected: The variable should receive its value from another variable, but has never been configured to do so.
- BadDeviceFailure: There has been a failure in the device/data source that generates the value that has affected the value.
The PropertyValue class provides static helper methods for simple class properties.
public static PropertyInfo GetPropertyInfo(Type type, string property)
public static object GetPropertyValue(object obj, string property)
The property name can be a point delimited property name allowing nested properties to be resolved. Additionally simple arrays and lists are supported using the typical index notation. The example of a simple TestClass can be used as follows:
public class IPAddress
{
public string Address { get; set; }
public byte[] Bytes { get; set; }
public List<string> Operations { get; set; }
}
public class TestData
{
public string Name { get; set; }
public IPAddress Url { get; set; } = new IPAddress();
}
The allowed property names to get the property info or the property value are:
- "Test"
- "Test.Name"
- "Test.Url"
- "Test.Url.Address"
- "Test.Url.Address.Length"
- "Test.Url.Bytes"
- "Test.Url.Bytes.Length"
- "Test.Url.Operations"
- "Test.Url.Operations.Count"
- "Test.Url.Operations[0].Length"
The generic PropertyHelper class can be used to simplify the implementation of property related methods for a typical class.
public class PropertyHelper<T> where T : class
{
public static bool IsProperty(string property) => (PropertyValue.GetPropertyInfo(typeof(T), property) != null) ? true : false;
public static PropertyInfo GetPropertyInfo(string property) => PropertyValue.GetPropertyInfo(typeof(T), property);
public object GetPropertyValue(string property) => PropertyValue.GetPropertyValue(this, property);
public void SetPropertyValue(string property, object value) => PropertyValue.SetPropertyValue(this, property, value);
}
Those methods are the blueprint for an implementation (e.g. if the class cannot be derived from the PropertyHelper class) and can easily implemented in any class.