using System.Threading;
using System.Threading.Tasks;
using LLAirtightApi;

namespace SLZ_4
{
    public class DeviceObject
    {
        public Airtight airtightObj;
        public bool bExit;
        public bool bStatus;
        public TEST_PARAM testInfo;
        public DEVICE_PARAM pDeviceInfo;
        public int groupId = -1;                 //测试组号

        public CONFIG_PARAM configParam;   //设备测试参数
        public VERSION_PARAM versionInfo;   //版本信息
        /// <summary>
        /// 创建设备对象
        /// </summary>
        /// <param name="type">仪器类型</param>
        /// <param name="protocol">协议类型</param>
        public DeviceObject(ref DEVICE_PARAM pDev)
        {
            pDeviceInfo = pDev;
            bExit = false;
            airtightObj = new Airtight(pDev.iDevType, pDev.iProtocol);

            testInfo = new TEST_PARAM();
            testInfo.chTest = new TEST_CHANNEL[4];
            configParam = new CONFIG_PARAM();

            Task.Run(() =>
            {
                while (!bExit)
                {
                    if (!bStatus)
                    {
                        airtightObj.AirtightInit(pDeviceInfo.sDevAddr, pDeviceInfo.iPort, pDeviceInfo.iSlaveId);
                        if (airtightObj.Open())
                        {
                            Thread.Sleep(100);
                            //获取测试参数
                            if (airtightObj.GetConfig(ref configParam) > 0)
                            {
                                bStatus = true;
                                //airtightObj.GetVersion(ref versionInfo);
                            }
                            else
                            {
                                airtightObj.Close();
                                bStatus = false;
                            }
                        }
                    }
                    else
                    {
                        //if (groupId == -1)
                        //{
                        //    groupId = airtightObj.GetGroupId();
                        //}
                        if (airtightObj.GetTestInfo(ref testInfo) > 0)
                        {
                            bStatus = true;
                        }
                        else
                        {
                            airtightObj.Close();
                            bStatus = false;
                        }
                    }
                    Thread.Sleep(100);
                }

                airtightObj.Close();
                airtightObj = null;
            });
        }
        
        public bool Start(bool bStart)
        {
            if (bStatus)
            {
                return airtightObj.Start(bStart);
            }
            return false;
        }
        public int GetGroupId()
        {
            if (bStatus)
            {
                return airtightObj.GetGroupId();
            }
            return -1;
        }

        public bool SetGroupId(int iId)
        {
            if (bStatus)
            {
                return airtightObj.SetGroupId(iId);
            }
            return false;
        }

        public int GetConfig(ref CONFIG_PARAM pInfo)
        {
            if (bStatus)
            {
                int nRet = airtightObj.GetConfig(ref pInfo);
                if (nRet > 0)
                {
                    configParam = pInfo;
                    return nRet;
                }
            }
            return 0;
        }

        public bool SetConfig(ref CONFIG_PARAM configInfo)
        {
            if (bStatus)
            {
                return airtightObj.SetConfig(ref configInfo);
            }
            return false;
        }
        public int GetConfig(ref CONFIG_PARAM_LL30 configInfo)
        {
            if (bStatus)
            {
                return airtightObj.GetConfig(ref configInfo);
            }
            return 0;
        }

        public bool SetConfig(ref CONFIG_PARAM_LL30 configInfo)
        {
            if (bStatus)
            {
                return airtightObj.SetConfig(ref configInfo);
            }
            return false;
        }

        public int GetFunction(ref FUNCTION_PARAM functionInfo)
        {
            if (bStatus)
            {
                return airtightObj.GetFunction(ref functionInfo);
            }
            return 0;
        }
        public bool SetFunction(ref FUNCTION_PARAM functionInfo)
        {
            if (bStatus)
            {
                return airtightObj.SetFunction(ref functionInfo);
            }
            return false;
        }

        public int GetVersion(ref VERSION_PARAM version)
        {
            if (bStatus)
            {
                return airtightObj.GetVersion(ref version);
            }
            return 0;
        }

        public int GetLanguage()
        {
            if (bStatus)
            {
                return airtightObj.GetLanguage();
            }
            return 0;
        }

        public bool SetLanguage(int iLanguage)
        {
            if (bStatus)
            {
                return airtightObj.SetLanguage(iLanguage);
            }
            return false;
        }

        public int GetPassword(ref PASSWORD_PARAM password)
        {
            if (bStatus)
            {
                return airtightObj.GetPassword(ref password);
            }
            return 0;
        }
        public bool SetPassword(ref PASSWORD_PARAM password)
        {
            if (bStatus)
            {
                return airtightObj.SetPassword(ref password);
            }
            return false;
        }
        public int GetGroup(ref GROUP_PARAM group)
        {
            if (bStatus)
            {
                return airtightObj.GetGroup(ref group);
            }
            return 0;
        }
        public bool SetGroup(ref GROUP_PARAM group)
        {
            if (bStatus)
            {
                return airtightObj.SetGroup(ref group);
            }
            return false;
        }
    }
}
