using RRQMCore.ByteManager;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ProjectManagementSystem.Device.CasunBox
{
    public class BoxDataHandlingAdapterCom : BoxDataHandlingAdapter
    {
        public Action<byte[]> OnReceived;
        public void PreviewReceivedData(byte[] data)
        {
            var byteBlock = BytePoolEx.GetByteBlock(data.Length);
            byteBlock.SetBuffer(data);
            byteBlock.SetLength(data.Length);
            PreviewReceived(byteBlock);
        }

        protected override void PreviewHandle(ByteBlock byteBlock)
        {
            try
            {
                if ((byteBlock.Buffer[0] == 0x10 && byteBlock.Length == 14 && byteBlock.Buffer[byteBlock.Length - 1] == 0x03)
                    || (byteBlock.Buffer[0] == 0x20 && byteBlock.Length == 16))
                {
                    OnReceived?.Invoke(byteBlock.ToArray());
                }
            }
            finally
            {
                byteBlock.Dispose();
            }
        }
    }
}

