namespace Modbus.Data
{
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using Utility;
///
/// Event args for read write actions performed on the DataStore.
///
public class DataStoreEventArgs : EventArgs
{
private DataStoreEventArgs(ushort startAddress, ModbusDataType modbusDataType)
{
StartAddress = startAddress;
ModbusDataType = modbusDataType;
}
///
/// Type of Modbus data (e.g. Holding register).
///
public ModbusDataType ModbusDataType { get; }
///
/// Start address of data.
///
public ushort StartAddress { get; }
///
/// Data that was read or written.
///
[SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures")]
public DiscriminatedUnion, ReadOnlyCollection> Data { get; private set; }
internal static DataStoreEventArgs CreateDataStoreEventArgs(ushort startAddress, ModbusDataType modbusDataType, IEnumerable data)
{
if (data == null)
{
throw new ArgumentNullException(nameof(data));
}
DataStoreEventArgs eventArgs;
if (typeof(T) == typeof(bool))
{
var a = new ReadOnlyCollection(data.Cast().ToArray());
eventArgs = new DataStoreEventArgs(startAddress, modbusDataType)
{
Data = DiscriminatedUnion, ReadOnlyCollection>.CreateA(a)
};
}
else if (typeof(T) == typeof(ushort))
{
var b = new ReadOnlyCollection(data.Cast().ToArray());
eventArgs = new DataStoreEventArgs(startAddress, modbusDataType)
{
Data = DiscriminatedUnion, ReadOnlyCollection>.CreateB(b)
};
}
else
{
throw new ArgumentException("Generic type T should be of type bool or ushort");
}
return eventArgs;
}
}
}