using InnovEnergy.Lib.Protocols.Modbus.Protocol.Frames.Accessors; using InnovEnergy.Lib.Utils; using static InnovEnergy.Lib.Protocols.Modbus.Protocol.FunctionCode; namespace InnovEnergy.Lib.Protocols.Modbus.Protocol.Frames.Replies; using Booleans = IReadOnlyList; using MbFc = MbByte; public class ReadDiscreteInputResponseFrame : ModbusFrame { private new const Int32 MinSize = 3; private MbByte ByteCount => Data.ByteAt(2); public MbBits Inputs => Data.BitsAt(3); public ReadDiscreteInputResponseFrame(Byte slave, Booleans inputs) : base (inputs.Count) { var nBytes = Math.Ceiling(inputs.Count / 8.0).ConvertTo(); SlaveAddress .Set(slave); FunctionCode .Set(ReadDiscreteInputs); ByteCount .Set(nBytes); Inputs .Set(inputs); } private ReadDiscreteInputResponseFrame(Byte[] data) : this(new ArraySegment(data)) { } private ReadDiscreteInputResponseFrame(ArraySegment data) : base(data) { AssertFunctionCode(ReadDiscreteInputs); // TODO // var expectedSize = ByteCount + MinSize; // if (data.Count != expectedSize) // throw new ArgumentException($"Expecting an array of size {expectedSize}", nameof(data)); // if (data.Count < MinSize) // throw new ArgumentException($"Expecting an array of size {MinSize} or more", nameof(data)); } public static ReadDiscreteInputResponseFrame Parse(ModbusFrame rawFrame) { return new ReadDiscreteInputResponseFrame(rawFrame.Data); } public static ReadDiscreteInputResponseFrame Parse(ArraySegment data) { return new ReadDiscreteInputResponseFrame(data); } }