using Pms.Models;
using ProjectManagementSystem.Common.Extenions;
using ProjectManagementSystem.Common.WebApi;
using ProjectManagementSystem.Device.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ProjectManagementSystem.Common.Core
{
    public class CommunicationVcu : CommunicationBase, ICommunication
    {
        private static CommunicationVcu m_instance;
        private string addressName = "VCU";
        public static CommunicationVcu Instance
        {
            get
            {
                if (m_instance == null)
                {
                    m_instance = new CommunicationVcu();
                }
                return m_instance;
            }
        }

        private CommunicationVcu()
        {
        }
        

        public void SendCommand(byte[] data, string address)
        {
            if (address == addressName)
            {
                int agvId = BitConverter.ToUInt16(data, 1);
                PmsApi.SendTransparentData(agvId, data);
            }
        }

        public void Received(byte[] data)
        {
            try
            {
                LogMsg($"{addressName}: Recv: {data.ToHexString()}");
                DataReceived?.Invoke(addressName, data);
            }
            catch (Exception ex)
            {
                LogMsg("接收VCU数据异常", ex);
            }

        }
    }
}
