using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using System.IO.Ports;
using IOBoardControl;
using Common;

namespace QTIOBoardControl
{
    public class QTIOBoard : IOBoard
    {
        public QTIOBoard(string COM)
            : this(COM, 115200, Parity.None, 8, StopBits.One)
        { }

        public QTIOBoard(string port, int baudRate, Parity parity, int dataBits, StopBits stopBits)
            : base(port, 115200, Parity.None, 8, StopBits.One)
        { }
        //public bool IsInThePosition(ushort tovalue,bool wait,ushort timeout)
        //{
        //    return false;
        //}

        public bool StatusCk(Func<bool> f, double timeout)
        {          
            DateTime end = System.DateTime.Now.AddSeconds(timeout);
            while (DateTime.Now <= end)
            {
                if (f())
                {
                    FileWriter.WriteLog(f.Method.Name.ToString() + " status:true");
                    return true;
                }
                else
                {
                    Thread.Sleep(100);
                }
            }

            //            Console.WriteLine("%s run timeout",f.ToString());
            FileWriter.WriteLog(f.Method.Name.ToString() + " run timeout");
            return false;
        }
        #region 写io

        #region IO板一

        /// <summary>
        /// 启动按钮指示灯
        /// </summary>
        public bool StartBtLight(bool On)
        {
            return WriteDO(ConfigHelper.Instance.OUT_StartBtLight, On);
        }

        //// 
        //public bool LightCardReset(bool On)
        //{
        //    return WriteDO(3, On);
        //}

        /// <summary>
        /// 复位按钮指示灯
        /// </summary>
        /// <param name="On"></param>
        /// <returns></returns>
        public bool RsetBtLight(bool On)
        {
            return WriteDO(ConfigHelper.Instance.OUT_RsetBtLight, On);
        }

        /// <summary>
        /// 三色灯-蓝
        /// </summary>
        /// <param name="On"></param>
        /// <returns></returns>
        public bool TriColorBlue(bool On)
        {
            return WriteDO(ConfigHelper.Instance.OUT_TriColorBlue, On);
        }
        /// <summary>
        /// 三色灯-红
        /// </summary>
        /// <param name="On"></param>
        /// <returns></returns>
        public bool TriColorRed(bool On)
        {
            return WriteDO(ConfigHelper.Instance.OUT_TriColorRed, On);
        }
        /// <summary>
        /// 三色灯-绿
        /// </summary>
        /// <param name="On"></param>
        /// <returns></returns>
        public bool TriColorGreen(bool On)
        {
            return WriteDO(ConfigHelper.Instance.OUT_TriColorGreen, On);
        }

        /// <summary>
        /// 三色灯-Close 
        /// </summary>
        /// <param name="On"></param>
        /// <returns></returns>
        public bool TriColorClose(bool On)
        {
            return WriteDO(ConfigHelper.Instance.OUT_TriColorGreen, On) && WriteDO(ConfigHelper.Instance.OUT_TriColorRed, On) && WriteDO(ConfigHelper.Instance.OUT_TriColorBlue, On);
        }

        /// <summary>
        /// 三色灯-蜂鸣器
        /// </summary>
        /// <param name="On"></param>
        /// <returns></returns>
        public bool TriColorBuzzer(bool On)
        {
            return WriteDO(ConfigHelper.Instance.OUT_TriColorBuzzer, On);
        }

        /// <summary>
        /// 状态指示灯红
        /// </summary>
        /// <param name="On"></param>
        /// <returns></returns>
        public bool StatusLightRed(bool On)
        {
            return WriteDO(ConfigHelper.Instance.OUT_StatusLightRed, On) && WriteDO(ConfigHelper.Instance.OUT_StatusLightGreen, !On);
        }

        /// <summary>
        /// 状态指示灯黄
        /// </summary>
        /// <param name="On"></param>
        /// <returns></returns>
        public bool StatusLightYellow(bool On)
        {
            return WriteDO(ConfigHelper.Instance.OUT_StatusLightRed, On) && WriteDO(ConfigHelper.Instance.OUT_StatusLightGreen, On);
        }
        /// <summary>
        /// 状态指示灯绿
        /// </summary>
        /// <param name="On"></param>
        /// <returns></returns>
        public bool StatusLightGreen(bool On)
        {
            return WriteDO(ConfigHelper.Instance.OUT_StatusLightGreen, On) && WriteDO(ConfigHelper.Instance.OUT_StatusLightRed, !On);
        }

        /// <summary>
        /// 状态指示灯关
        /// </summary>
        /// <param name="On"></param>
        /// <returns></returns>
        public bool StatusLightOff(bool On)
        {
            return WriteDO(ConfigHelper.Instance.OUT_StatusLightRed, On) && WriteDO(ConfigHelper.Instance.OUT_StatusLightGreen, On);
        }

        /// <summary>
        /// Power推进气缸
        /// </summary>
        /// <param name="On"></param>
        /// <returns></returns>
        public bool PowerOutCylinder(bool On)
        {
            return WriteDO(ConfigHelper.Instance.OUT_PowerOutCylinder, On);
        }

        /// <summary>
        /// 电源键气缸
        /// </summary>
        /// <param name="On"></param>
        /// <returns></returns>
        public bool PowerCylinder(bool On)
        {
            return WriteDO(ConfigHelper.Instance.OUT_PowerCylinder, On);
        }

        /// <summary>
        /// 音量键+气缸
        /// </summary>
        /// <param name="On"></param>
        /// <returns></returns>
        public bool VolAddCylinder(bool On)
        {
            return WriteDO(ConfigHelper.Instance.OUT_VolAddCylinder, On);
        }

        /// <summary>
        /// 音量键-气缸
        /// </summary>
        /// <param name="On"></param>
        /// <returns></returns>
        public bool VolReduceCylinder(bool On)
        {
            return WriteDO(ConfigHelper.Instance.OUT_VolReduceCylinder, On);
        }

        #endregion

        #region IO板二

        /// <summary>
        /// PowerRGB气缸
        /// </summary>
        /// <param name="On"></param>
        /// <returns></returns>
        public bool PowerRGBCylinder(bool On)
        {
            return WriteDO(ConfigHelper.Instance.OUT_PowerRGBCylinder, On);
        }

        /// <summary>
        /// Pogo气缸
        /// </summary>
        /// <param name="On"></param>
        /// <returns></returns>
        public bool PogoCylinder(bool On)
        {
            //return WriteDO(14, On);
            return WriteDO(ConfigHelper.Instance.OUT_PogoCylinder, On);
        }

        /// <summary>
        /// 光强板复位输出
        /// </summary>
        /// <param name="On"></param>
        /// <returns></returns>
        public bool LBreset(bool On)
        {
            //return WriteDO(13, On);
            return true;
        }

        ////SD卡水平气缸
        //public bool SDHorizCylinder(bool On)
        //{
        //    return WriteDO(14, On);
        //}

        /// <summary>
        /// 灰卡气缸
        /// </summary>
        /// <param name="On"></param>
        /// <returns></returns>
        public bool GrayCardCylinder(bool On)
        {
            return WriteDO(ConfigHelper.Instance.OUT_GrayCardCylinder, On);
        }

        /// <summary>
        /// TypeC气缸
        /// </summary>
        /// <param name="On"></param>
        /// <returns></returns>
        public bool TypeCCylinder(bool On)
        {
            //return WriteDO(1, On);
            return true;
        }

        /// <summary>
        /// IPD气缸
        /// </summary>
        /// <param name="On"></param>
        /// <returns></returns>
        public bool IPDCCylinder(bool On)
        {
            return WriteDO(ConfigHelper.Instance.OUT_IPDCCylinder, On);
        }

        /// <summary>
        /// 电磁铁通电
        /// </summary>
        /// <param name="On"></param>
        public bool ElectromagnetPower(bool On)
        {
            return WriteDO(ConfigHelper.Instance.OUT_ElectromagnetPower, On);
        }

        /// <summary>
        /// IPD通电
        /// </summary>
        /// <param name="On"></param>
        /// <returns></returns>
        public bool PogoPower(bool On)
        {
            return WriteDO(ConfigHelper.Instance.OUT_PogoPower, On);
        }

        /// <summary>
        /// Holder盖子伸出电磁阀
        /// </summary>
        /// <param name="On"></param>
        /// <returns></returns>
        public bool HolderSensorOut(bool On)
        {
            return WriteDO(ConfigHelper.Instance.OUT_HolderSensorOut, On);
        }

        /// <summary>
        /// Holder盖子缩回电磁阀
        /// </summary>
        /// <param name="On"></param>
        /// <returns></returns>
        public bool HolderSensorIn(bool On)
        {
            return WriteDO(ConfigHelper.Instance.OUT_HolderSensorIn, On);
        }

        /// <summary>
        /// VBUS继电器
        /// </summary>
        /// <param name="On"></param>
        /// <returns></returns>
        public bool Ping_VBUS(bool On)
        {
            return WriteDO(ConfigHelper.Instance.OUT_Ping_VBUS, On);
        }

        /// <summary>
        /// CC继电器
        /// </summary>
        /// <param name="On"></param>
        /// <returns></returns>
        public bool Ping_CC(bool On)
        {
            return WriteDO(ConfigHelper.Instance.OUT_Ping_CC, On);
        }

        /// <summary>
        /// GND继电器
        /// </summary>
        /// <param name="On"></param>
        /// <returns></returns>
        public bool Ping_GND(bool On)
        {
            return WriteDO(ConfigHelper.Instance.OUT_Ping_GND, On);
        }

        #endregion

        #endregion

        #region 读io

        #region IO板一
        /// <summary>
        /// 急停按钮
        /// </summary>
        /// <returns></returns>
        public bool E_Stop()
        {
            if (GetIN(ConfigHelper.Instance.IN_E_Stop))
                return false;
            else
                return true;
        }

        /// <summary>
        /// 启动按钮
        /// </summary>
        /// <returns></returns>
        public bool Start()
        {
            if (GetIN(ConfigHelper.Instance.IN_Start))
                return true;
            else
                return false;
        }

        /// <summary>
        /// 复位按钮
        /// </summary>
        /// <returns></returns>
        public bool Reset()
        {
            if (GetIN(ConfigHelper.Instance.IN_Reset))
                return true;
            else
                return false;
        }

        /// <summary>
        /// PowrOut气缸原点
        /// </summary>
        /// <returns></returns>
        public bool PoweroutOrigin()
        {
            if (GetIN(ConfigHelper.Instance.IN_PoweroutOrigin))
                return true;
            else
                return false;
        }

        /// <summary>
        /// PowerOut气缸到位
        /// </summary>
        /// <returns></returns>
        public bool PowerOutTest()
        {
            if (GetIN(ConfigHelper.Instance.IN_PowerOutTest))
                return true;
            else
                return false;
        }

        /// <summary>
        /// 电源键气缸原点
        /// </summary>
        /// <returns></returns>
        public bool PowerOrigin()
        {
            if (GetIN(ConfigHelper.Instance.IN_PowerOrigin))
                return true;
            else
                return false;
        }

        /// <summary>
        /// 电源键气缸到位
        /// </summary>
        /// <returns></returns>
        public bool PowerTest()
        {
            if (GetIN(ConfigHelper.Instance.IN_PowerTest))
                return true;
            else
                return false;
        }

        /// <summary>
        /// 音量键+气缸原点
        /// </summary>
        /// <returns></returns>
        public bool VolAddOrigin()
        {
            if (GetIN(ConfigHelper.Instance.IN_VolAddOrigin))
                return true;
            else
                return false;
        }

        /// <summary>
        /// 音量键+气缸到位
        /// </summary>
        /// <returns></returns>
        public bool VolAddTest()
        {
            if (GetIN(ConfigHelper.Instance.IN_VolAddTest))
                return true;
            else
                return false;
        }

        /// <summary>
        /// 音量键-气缸原点
        /// </summary>
        /// <returns></returns>
        public bool VolReduOrigin()
        {
            if (GetIN(ConfigHelper.Instance.IN_VolReduOrigin))
                return true;
            else
                return false;
        }

        /// <summary>
        /// 音量键-气缸到位
        /// </summary>
        /// <returns></returns>
        public bool VolReduTest()
        {
            if (GetIN(ConfigHelper.Instance.IN_VolReduTest))
                return true;
            else
                return false;
        }

        /// <summary>
        /// Pogo滑台气缸原点
        /// </summary>
        /// <returns></returns>
        public bool PogoOrigin()
        {
            if (GetIN(ConfigHelper.Instance.IN_PogoOrigin))
                return true;
            else
                return false;
        }

        /// <summary>
        /// Pogo滑台气缸到位
        /// </summary>
        /// <returns></returns>
        public bool PogoTest()
        {
            if (GetIN(ConfigHelper.Instance.IN_PogoTest))
                return true;
            else
                return false;
        }

        /// <summary>
        /// Holder盖子sensor
        /// </summary>
        /// <returns></returns>
        public bool CoverOpen()
        {
            if (GetIN(ConfigHelper.Instance.IN_CoverOpen))
                return true;
            else
                return false;
        }

        /// <summary>
        /// Holder盖子气缸到位
        /// </summary>
        /// <returns></returns>
        public bool CoverClose()
        {
            if (GetIN(ConfigHelper.Instance.IN_CoverClose))
                return true;
            else
                return false;
        }

        /// <summary>
        /// 检测左夹紧气缸伸出
        /// </summary>
        /// <returns></returns>
        public bool LeftCover_ON()
        {
            if (!GetIN(ConfigHelper.Instance.IN_LeftCover_ON))
                return false;
            else
                return true;
        }

        /// <summary>
        /// 检测DUT到位感应1
        /// </summary>
        /// <returns></returns>
        public bool CheckDUT_1()
        {
            if (!GetIN(ConfigHelper.Instance.IN_CheckDUT_1))
                return false;
            else
                return true;
        }

        #region 取消使用的感应器
        ////安全门
        //public bool SafeDoor()
        //{
        //    if (GetIN(5))
        //        return true;
        //    else
        //        return false;
        //}

        ////电源键气缸原点
        //public bool PowerOrigin()
        //{
        //    if (GetIN(6))
        //        return true;
        //    else
        //        return false;
        //}

        ////电源键气缸到位
        //public bool PowerTest()
        //{
        //    if (GetIN(7))
        //        return true;
        //    else
        //        return false;
        //}

        ////音量键+气缸原点
        //public bool VolAddOrigin()
        //{
        //    if (GetIN(8))
        //        return true;
        //    else
        //        return false;
        //}

        ////音量键+气缸到位
        //public bool VolAddTest()
        //{
        //    if (GetIN(9))
        //        return true;
        //    else
        //        return false;
        //}

        ////音量键-气缸原点
        //public bool VolReduOrigin()
        //{
        //    if (GetIN(10))
        //        return true;
        //    else
        //        return false;
        //}

        ////音量键-气缸到位
        //public bool VolReduTest()
        //{
        //    if (GetIN(11))
        //        return true;
        //    else
        //        return false;
        //}

        ////SD卡竖直气缸原点
        //public bool SDVertOrigin()
        //{
        //    if (GetIN(12))
        //        return true;
        //    else
        //        return false;
        //}

        ////SD卡竖直气缸到位
        //public bool SDVertTest()
        //{
        //    if (GetIN(13))
        //        return true;
        //    else
        //        return false;
        //}

        ////SD卡水平气缸原点
        //public bool SDHorizOrig()
        //{
        //    if (GetIN(14))
        //        return true;
        //    else
        //        return false;
        //}

        ////SD卡水平气缸到位
        //public bool SDHorizTest()
        //{
        //    if (GetIN(15))
        //        return true;
        //    else
        //        return false;
        //}

        ///// <summary>
        ///// 盖板sensor ,盖板到位
        ///// </summary>
        ///// <returns></returns>
        //public bool CoverClose()
        //{
        //    if (GetIN(15))
        //        return false;
        //    else
        //        return true;
        //}
        #endregion

        #endregion

        #region IO板二

        /// <summary>
        /// RGB下压气缸伸出
        /// </summary>
        /// <returns></returns>
        public bool RGBTest()
        {
            if (GetIN(ConfigHelper.Instance.IN_RGBTest))
                return true;
            else
                return false;
        }

        /// <summary>
        /// IPD气缸原点
        /// </summary>
        /// <returns></returns>
        public bool IPDOrigin()
        {
            return true;
            if (GetIN(ConfigHelper.Instance.IN_IPDOrigin))
                return true;
            else
                return false;
        }
        /// <summary>
        /// IPD气缸到位
        /// </summary>
        /// <returns></returns>
        public bool IPDTest()
        {
            if (GetIN(ConfigHelper.Instance.IN_IPDTest))
                return true;
            else
                return false;
        }

        /// <summary>
        /// 灰卡气缸原点
        /// </summary>
        /// <returns></returns>
        public bool GrayCardOrigin()
        {
            if (GetIN(ConfigHelper.Instance.IN_GrayCardOrigin))
                return true;
            else
                return false;
        }
        /// <summary>
        /// 灰卡气缸到位
        /// </summary>
        /// <returns></returns>
        public bool GrayCardTest()
        {
            if (GetIN(ConfigHelper.Instance.IN_GrayCardTest))
                return true;
            else
                return false;
        }
        /// <summary>
        /// TypeC气缸原点,结构取消
        /// </summary>
        /// <returns></returns>
        public bool TypeCOrigin()
        {
            return true;
            if (GetIN(2))
                return true;
            else
                return false;
        }

        /// <summary>
        /// TypeC气缸到位,结构取消
        /// </summary>
        /// <returns></returns>
        public bool TypeCTest()
        {
            return true;
            if (GetIN(1))
                return true;
            else
                return false;
        }

        /// <summary>
        /// 检查光栅
        /// </summary>
        /// <returns></returns>
        public bool Check_safe_raster()
        {
            if (GetIN(ConfigHelper.Instance.IN_Check_safe_raster))
                return true;
            else
                return false;
        }

        /// <summary>
        /// 检测左夹紧气缸松开
        /// </summary>
        /// <returns></returns>
        public bool LeftCover_OFF()
        {
            if (!GetIN(ConfigHelper.Instance.IN_LeftCover_OFF))
                return false;
            else
                return true;
        }

        /// <summary>
        /// 检测右夹紧气缸夹紧
        /// </summary>
        /// <returns></returns>
        public bool RightCover_ON()
        {
            if (!GetIN(ConfigHelper.Instance.IN_RightCover_ON))
                return false;
            else
                return true;
        }

        /// <summary>
        /// 检测右夹紧气缸松开
        /// </summary>
        /// <returns></returns>
        public bool RightCover_OFF()
        {
            if (!GetIN(ConfigHelper.Instance.IN_RightCover_OFF))
                return false;
            else
                return true;
        }

        /// <summary>
        /// 检测DUT到位感应2
        /// </summary>
        /// <returns></returns>
        public bool CheckDUT_2()
        {
            if (!GetIN(ConfigHelper.Instance.IN_CheckDUT_2))
                return false;
            else
                return true;
        }

        #endregion

        #endregion
    }
}
