using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Diagnostics.CodeAnalysis; using Modbus.Utility; namespace Modbus.Data { /// /// Event args for read write actions performed on the DataStore. /// public class DataStoreEventArgs : EventArgs { private DataStoreEventArgs(ushort startAddress, ModbusDataType modbusDataType) { this.StartAddress = startAddress; this.ModbusDataType = modbusDataType; } /// /// Type of Modbus data (e.g. Holding register). /// public ModbusDataType ModbusDataType { get; private set; } /// /// Start address of data. /// public ushort StartAddress { get; private set; } /// /// 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, IList data) { if (data == null) throw new ArgumentNullException("data"); if (!(typeof(T) == typeof(bool) || typeof(T) == typeof(ushort))) throw new ArgumentException("Generic type T should be of type bool or ushort"); DataStoreEventArgs eventArgs = new DataStoreEventArgs(startAddress, modbusDataType); if (typeof(T) == typeof(bool)) eventArgs.Data = DiscriminatedUnion, ReadOnlyCollection>.CreateA(new ReadOnlyCollection((IList)data)); else eventArgs.Data = DiscriminatedUnion, ReadOnlyCollection>.CreateB(new ReadOnlyCollection((IList)data)); return eventArgs; } } }