using PLC.Siemens.Protocol.ReadData;
using PlcSiemens.O;
using PlcSiemens.Protocol.Common;
using PlcSiemens.Protocol.ReadData;

namespace PlcSiemens.ProtocolHandle
{
    public class ReadMultiHandle : IPacketHandle
    {
        public ushort PduLength { get; set; }
        public List<DataItem> DataItem { get; set; }
        public ResultCode ResultCode { get; private set; }

        public void Handle(IIsoSender sender)
        {
            ReadRequest read = new ReadRequest();
            read.ListItems = DataItem;
            ReadResponse res = sender.IsoSend<ReadRequest, ReadResponse>(read);
            ResultCode = res.Header.Error;
            if (ResultCode == ResultCode.OK)
            {
                for (int i = 0; i < res.DataItems.Count; i++)
                {
                    var reqItem = DataItem[i];
                    var resItem = res.DataItems[i];
                    reqItem.Err = (ResultCode)resItem.ReturnCode;
                    reqItem.Data = resItem.Data;
                    if (reqItem.Err == ResultCode.OKFF)
                        reqItem.Err = ResultCode.OK;
                }
            }
        }
    }
}
