using EtherCAT_DLL;
using EtherCAT_DLL_Err;
using LogClass;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace CCDCount.DLL
{
    public class MechanicalControlClass
    {
        ushort g_uRet = 0;
        // 当前选择卡的卡号
        ushort g_uESCCardNo = 99;
        ushort g_uESCNodeID = 0;
        ushort g_uESCSlotID;
        // 卡片初始化状态
        bool g_bInitialFlag = false;
        //全部卡号
        ushort[] g_uESCCardNoList = new ushort[32];
        List<ushort> LsNodeID = new List<ushort>();
        List<short> LsSlotID = new List<short>();
        ushort g_nESCExistCards = 0;
        bool IsCanInit = false;

        public MechanicalControlClass()
        {
            OpenTaidaCard();
            InitControl();
            FindSlave();
        }
        /// <summary>
        /// 初始化
        /// </summary>
        public void InitControl()
        {
            ushort nSeq = 0, nCardNo = 0;

            g_uRet = 0;
            for (nSeq = 0; nSeq < 32; nSeq++)
            {
                // 清空卡号记录
                g_uESCCardNoList[nSeq] = 0;
            }
            g_bInitialFlag = false;
            for (nSeq = 0; nSeq < g_nESCExistCards; nSeq++)
            {
                // 获取卡号
                g_uRet = CEtherCAT_DLL.CS_ECAT_Master_Get_CardSeq(nSeq, ref nCardNo);
                // 初始化控制卡
                g_uRet = CEtherCAT_DLL.CS_ECAT_Master_Initial(nCardNo);
                if (g_uRet != CEtherCAT_DLL_Err.ERR_ECAT_NO_ERROR)
                {
                    LOG.error("_ECAT_Master_Initial, ErrorCode = " + g_uRet.ToString());
                    g_uESCCardNoList[nSeq] = 99;
                }
                else
                {
                    //记录全部的卡号
                    g_uESCCardNoList[nSeq] = nCardNo;
                    //标记卡是否已经初始化
                    g_bInitialFlag = true;
                }
            }
            g_uESCCardNo = g_uESCCardNoList[0];
        }

        /// <summary>
        /// 释放资源
        /// </summary>
        public void Dispose()
        {
            
        }

        /// <summary>
        /// 电机旋转
        /// </summary>
        public void MotorRotation(float Angle)
        {
            //开启电机
            g_uRet = CEtherCAT_DLL.CS_ECAT_Slave_Motion_Set_Svon(g_uESCCardNo, g_uESCNodeID, g_uESCSlotID, 1);

            if (g_uRet != CEtherCAT_DLL_Err.ERR_ECAT_NO_ERROR)
            {
                LOG.error("_ECAT_Slave_Motion_Set_Svon, Error Code = " + g_uRet.ToString());
            }
        }

        /// <summary>
        /// 推出气动阀
        /// </summary>
        public void OpenAirValve(int ChannelNO)
        {
            Console.WriteLine("{0}气缸阀门关闭", ChannelNO);
        }

        /// <summary>
        /// 收回气动阀
        /// </summary>
        public void CloseAirValve(int ChannelNO)
        {

        }

        /// <summary>
        /// 收回所有气动阀
        /// </summary>
        public void CloseAllAirValve()
        {

            Console.WriteLine("所有阀门打开");
        }

        // <summary>
        /// 启动震动盘
        /// </summary>
        public void OpenShockPlate()
        {

        }

        /// <summary>
        /// 修改震动盘速度
        /// </summary>
        public void ChangeShokSpeed()
        {
            Console.WriteLine("震盘速度修改");
        }

        // <summary>
        /// 停止震动盘
        /// </summary>
        public void CloseShockPlate()
        {

            Console.WriteLine("震盘停止");
        }

        private void OpenTaidaCard()
        {
            g_uRet = 0;
            g_bInitialFlag = false;
            // 获取轴卡数量
            g_uRet = CEtherCAT_DLL.CS_ECAT_Master_Open(ref g_nESCExistCards);

            if (g_uRet != CEtherCAT_DLL_Err.ERR_ECAT_NO_ERROR)
            {
                LOG.error("CS_ECAT_Master_Open, rt =" + g_uRet.ToString());
            }
            else
            {
                if (g_nESCExistCards == 0)
                {
                    LOG.error("No EtherCat can be found!");
                }
                else
                {
                    IsCanInit = true;
                }
            }
        }

        // 查找全部从站
        private void FindSlave()
        {
            short nSID = 0, Cnt = 0;
            ushort uNID = 0, uSlaveNum = 0, uReMapNodeID = 0;
            uint uVendorID = 0, uProductCode = 0, uRevisionNo = 0, uSlaveDCTime = 0;

            g_uRet = CEtherCAT_DLL.CS_ECAT_Master_Get_SlaveNum(g_uESCCardNo, ref uSlaveNum);

            if (g_uRet != CEtherCAT_DLL_Err.ERR_ECAT_NO_ERROR)
            {
                LOG.error("_ECAT_Master_Get_SlaveNum, ErrorCode = " + g_uRet.ToString());
            }
            else
            {
                if (uSlaveNum == 0)
                {
                    LOG.error("Card NO: " + g_uESCCardNo.ToString() + " No slave found!");
                }
                else
                {
                    for (uNID = 0; uNID < uSlaveNum; uNID++)
                    {
                        g_uRet = CEtherCAT_DLL.CS_ECAT_Master_Get_Slave_Info(g_uESCCardNo, uNID, ref uReMapNodeID, ref uVendorID, ref uProductCode, ref uRevisionNo, ref uSlaveDCTime);

                        if (uVendorID == 0x1DD && uProductCode == 0x10305070) // A2E
                        {
                            nSID = 0;
                            LsNodeID.Add(uNID);
                            LsSlotID.Add(nSID);
                            //CmbNode.Items.Add("NodeID:" + uNID + " - SlotID:" + nSID + "-A2E");
                            Cnt++;
                        }
                        else if (uVendorID == 0x539 && uProductCode == 0x2200001) //Yaskawa
                        {
                            nSID = 0;
                            LsNodeID.Add(uNID);
                            LsSlotID.Add(nSID);
                            //CmbNode.Items.Add("NodeID:" + uNID + " - SlotID:" + nSID + "-Yaskawa");
                            Cnt++;
                        }
                        else if ((uVendorID == 0x1A05 || uVendorID == 0x1DD) && uProductCode == 0x0624) //Ec4Axis
                        {
                            for (nSID = 0; nSID < 4; nSID++)
                            {
                                LsNodeID.Add(uNID);
                                LsSlotID.Add(nSID);
                                //CmbNode.Items.Add("NodeID:" + uNID + " - SlotID:" + nSID + "-Ec4Axis");
                                Cnt++;
                            }
                        }
                        else if ((uVendorID == 0x1A05 || uVendorID == 0x1DD) && uProductCode == 0x5621) //EcAxis
                        {
                            nSID = 0;
                            LsNodeID.Add(uNID);
                            LsSlotID.Add(nSID);
                            // CmbNode.Items.Add("NodeID:" + uNID + " - SlotID:" + nSID + "EcAxis");
                            Cnt++;
                        }
                    }
                }
            }
        }
    }
}
