using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using GcDevicePc.Common;

namespace GcDevicePc.ProThread
{
    class ThreadMonitor
    {
        #region 检查线程时间
        private ushort monitortimer = 1000 * 30;  //线程检查时间
        #endregion

        #region 单实例实现
        private static ThreadMonitor instance = null;  //静态私有成员变量
        private static object _lock = new object();

        public static ThreadMonitor GetInstance()
        {
            if (instance == null)
            {
                lock (_lock)
                {
                    if (instance == null)
                    {
                        instance = new ThreadMonitor();
                    }
                }
            }
            return instance;
        }
        #endregion


        #region 连接中心初始化所有读取线程
        /// <summary>
        /// 显示数据
        /// </summary>
        private DisplayDataTh displayDataTh = DisplayDataTh.GetInstance();

        /// <summary>
        /// 状态数据
        /// </summary>
        private HmiStatueTh hmiStatueTh = HmiStatueTh.GetInstance();

        /// <summary>
        /// 信号数据
        /// </summary>
        private ChannelDataTh channelDataTh = ChannelDataTh.GetInstance();
        #endregion

        #region 通讯中心功能函数

        /// <summary>
        /// 开启所有通讯线程
        /// </summary>
        public void StartALLCommunication()
        {
            if (!displayDataTh.ThreadWorking())
                displayDataTh.DisplayDataThStart();

            if (!hmiStatueTh.ThreadWorking())
                hmiStatueTh.HmiStatueThStart();

            if (!channelDataTh.ThreadWorking())
                channelDataTh.ChannelDataThStart(); 
        }

        /// <summary>
        /// 关闭所有通讯线程
        /// </summary>
        public void StopALLCommunication()
        {
            if (displayDataTh.ThreadWorking())
                displayDataTh.DisplayDataThStop();

            if (hmiStatueTh.ThreadWorking())
                hmiStatueTh.HmiStatueThStop();

            if (channelDataTh.ThreadWorking())
                channelDataTh.ChannelDataThStop();
        }
        #endregion


        #region 通讯中心线程检查进程
        private Thread _tThreadMonitor;    //分析仪读取线程
        private ManualResetEvent monitor_mre = new ManualResetEvent(false);  //信号控制量
        public void MonitorThStart()
        {
            if (monitor_mre != null)
            {
                monitor_mre.Reset();
            }

            if (_tThreadMonitor != null)
            {
                _tThreadMonitor.Abort();
            }

            _tThreadMonitor = new Thread(MonitorThProc);
            // _tThreadMonitor.IsBackground = true;
            _tThreadMonitor.Start();
        }

        public bool ThreadWorking()
        {
            if (_tThreadMonitor != null)
            {
                if (_tThreadMonitor.ThreadState == ThreadState.Running ||
                    (_tThreadMonitor.ThreadState & ThreadState.WaitSleepJoin) != 0)
                    return true;
                else
                    return false;
            }
            return false;
        }

        public void MonitorThStop()
        {
            monitor_mre.Set();
        }

        /// <summary>
        /// 判断是否所有通讯线程已经退出
        /// </summary>
        /// <returns></returns>
        public bool AllIsClosed()
        {
            int ret = 0;
            if (displayDataTh.ThreadWorking())
            {
                ret += 1;
            }

            if (hmiStatueTh.ThreadWorking())
            {
                ret += 1;
            }

            if (channelDataTh.ThreadWorking())
            {
                ret += 1;
            }

            return ret == 0 ? true : false;
        }

        /// <summary>
        /// 每一分钟监测一次 最小做样是2min
        /// </summary>
        private void MonitorThProc()
        {
            while (!monitor_mre.WaitOne(monitortimer))
            {
                if (!displayDataTh.ThreadWorking())
                {
                    Log.Info("数据显示线程 重启");
                    displayDataTh.DisplayDataThStart();
                }

                if (!hmiStatueTh.ThreadWorking())
                {
                    Log.Info("HMI状态线程 重启");
                    hmiStatueTh.HmiStatueThStart();
                }

                if (!channelDataTh.ThreadWorking())
                {
                    Log.Info("信号线程线程 重启");
                    channelDataTh.ChannelDataThStart();
                }
            }
        }
        #endregion
    }
}
