using PlcSiemens.O;
using PlcSiemens.Protocol.Common;
using PlcSiemens.Protocol.Header;

namespace PlcSiemens.Protocol.ReadData
{
    public class ReadRequest : IBuildRequest
    {
        public HeaderPacket Header { get; set; }
        public ReadParamsRequest Params { get; set; }

        public List<DataItem> ListItems { get; set; }

        public ReadRequest()
        {
            Header = new HeaderPacket();
            Params = new ReadParamsRequest();
        }

        public void Build()
        {
            Params.Build(ListItems);
            //因为包的长度不定 所以需要等参数构建好后才能构建Header
            Header.Build(PduType.Request, Params.PacketLength, 0x0000);
        }

        public ByteBuffer GetBuffer()
        {
            ByteBuffer buffer = ByteBuffer.Allocate();
            Header.GetByteBuffer(buffer);
            Params.GetByteBuffer(buffer);
            return buffer;
        }
    }
}
