using InnovEnergy.Lib.Protocols.Modbus.Protocol; using static System.AttributeTargets; namespace InnovEnergy.Lib.Protocols.Modbus.Reflection.Attributes; [AttributeUsage(Field | Property)] public abstract class ModbusAttribute : Attribute { public UInt16 Address { get; } public UInt16 Size { get; } public Type ModbusType { get; } public ModbusKind Kind { get; } protected ModbusAttribute(UInt16 address, UInt16 size, Type modbusType, ModbusKind kind) { Address = address; Size = size; ModbusType = modbusType; Kind = kind; } protected ModbusAttribute(UInt16 address, Type modbusType, ModbusKind kind) : this(address, GetSize(modbusType), modbusType, kind) { } private static UInt16 GetSize(Type modbusType) { if (!TypeToSize.TryGetValue(modbusType, out var size)) throw new ArgumentException("cannot infer size of" + nameof(modbusType), nameof(modbusType)); return size; } private static readonly Dictionary TypeToSize = new() { [typeof(Boolean)] = 1, [typeof(Int16)] = 1, [typeof(UInt16)] = 1, [typeof(Int32)] = 2, [typeof(UInt32)] = 2, [typeof(Single)] = 2, [typeof(Int64)] = 4, [typeof(UInt64)] = 4, [typeof(Double)] = 4, }; }