using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using GcDevicePc.GCBuffer;
using System.Diagnostics;
//using GcDevicePc.CK_UI;

namespace GcDevicePc.ProThread
{
    class CKvocUpdata3
    {
        ManualResetEvent datamre = new ManualResetEvent(false);
        Thread t_DataSave;
        int isstart = 0;

        //批量更新 
        //用于更新记录数据
        private void Monitor_DataList()
        {
            try
            {
                double time = 0;
                double value = 0;
                UInt32 starttime = 0x7FFFFFFF;
                //float dtime = 0.0f;
                //float dvalue = 0.0f;
                List<double[]> listPoint = new List<double[]>();//临时数据区
                bool drbool = false;

                while (!datamre.WaitOne(500))
                {

                    drbool = false;
                    listPoint.Clear();
                    if ((globaldata.m_hmibuffer.gcinfo.methodstatus == 4 || globaldata.m_hmibuffer.gcinfo.methodstatus == 5) || globaldata.m_pcbuffer.gcpcinfo.pcworkinfo.system_Statue)  //判断HMI工作模式     0-空闲 1-2 准备  3准备就绪  4-5 运行（分保持和程升） 6-7结束后处理
                    {
#if(debug)
                        Stopwatch st = new Stopwatch();  //计时
                        st.Reset();  //清零
#endif

                        lock (globaldata.m_signalbuffer.Draw_Port3)
                        {


                            if (globaldata.m_signalbuffer.Draw_Port3.Count > 1)
                            {

                                if (globaldata.startclear)
                                {

                                    CurveDisPlay3.curdisp3.cleardata();
                                    starttime = ((SignalBuffer.Channel_XY)globaldata.m_signalbuffer.Draw_Port3[0]).ctimer;
                                    time = 0;
                                    //  globaldata.startclear = false;
                                }


                                else
                                {

                                    if (globaldata.startimerIsSet)
                                    {
                                        CurveDisPlay3.curdisp3.cleardata();
                                        starttime = ((SignalBuffer.Channel_XY)globaldata.m_signalbuffer.Draw_Port3[0]).ctimer;
                                        time = 0;
                                        // globaldata.startimerIsSet = false;
                                    }

                                    ushort timeend = globaldata.m_dpbuffer.ShowList.showtime.alltime;
                                    timeend -= 5;
                                    if (starttime == 0)
                                        starttime = ((SignalBuffer.Channel_XY)globaldata.m_signalbuffer.Draw_Port3[0]).ctimer;
#if(debug)
                                    st.Start(); //计时开始
#endif
                                    for (int i = 0; i < globaldata.m_signalbuffer.Draw_Port3.Count; i++)
                                    {
                                        if (globaldata.MethodRun_Statue == 4 || globaldata.MethodRun_Statue == 5)
                                        {
                                            //方法启停判断方法：使用获取HMI时间判断，判断时间会延迟0.03分钟左右
                                            if (globaldata.m_dpbuffer.ShowList.showtime.runtimenow > 0 && isstart == 0 && globaldata.m_dpbuffer.ShowList.showtime.runtimenow < 3)
                                            {
                                                Console.WriteLine("方法开始");
                                                if (CKVocAnalyzer.GlobalCKV.debugwork == 5)
                                                {
                                                    CKVocAnalyzer.GlobalCKV.debugwork = 4;
                                                    isstart = 2;
                                                    time = 0;
                                                    starttime = ((SignalBuffer.Channel_XY)globaldata.m_signalbuffer.Draw_Port3[0]).ctimer;
                                                }
                                                else
                                                {
                                                    isstart = 2;
                                                    CurveDisPlay3.curdisp3.cleardata();
                                                    CurveDisPlay3.curdisp3.SetAxisX((double)globaldata.m_dpbuffer.ShowList.showtime.alltime / 60.0);
                                                    time = 0;
                                                    starttime = ((SignalBuffer.Channel_XY)globaldata.m_signalbuffer.Draw_Port3[0]).ctimer;

                                                }
                                                CKVocAnalyzer.GlobalCKV.dataFtime.FTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");


                                            }
                                        }

                                        if (starttime <= ((SignalBuffer.Channel_XY)globaldata.m_signalbuffer.Draw_Port3[i]).ctimer && (((SignalBuffer.Channel_XY)globaldata.m_signalbuffer.Draw_Port3[i]).ctimer - starttime) < 3000)
                                        {
                                            time += ((((SignalBuffer.Channel_XY)globaldata.m_signalbuffer.Draw_Port3[i]).ctimer - starttime) / 1000.0f / 60.0f);
                                            //time += 0.00083;
                                            starttime = ((SignalBuffer.Channel_XY)globaldata.m_signalbuffer.Draw_Port3[i]).ctimer;
                                            value = (((SignalBuffer.Channel_XY)globaldata.m_signalbuffer.Draw_Port3[i]).cdata * 1000.0f);

                                            if (time < 600)
                                            {
                                                listPoint.Add(new double[] { Math.Round(time, 4), Math.Round(value, 4) });

                                            }



                                            //时间判断终点
                                            if (globaldata.MethodRun_Statue == 4 || globaldata.MethodRun_Statue == 5)
                                            //if(isstart==2)
                                            {
                                                if ((globaldata.m_dpbuffer.ShowList.showtime.runtimenow > timeend && globaldata.m_dpbuffer.ShowList.showtime.runtimenow < globaldata.m_dpbuffer.ShowList.showtime.alltime && isstart == 2))
                                                {
                                                    // CurveDisPlay.curdisp.addPoint(listPoint, true);                                             
                                                    // Thread.Sleep(500);       
                                                    drbool = true;
                                                    // time = 0;
                                                    isstart = 0;
                                                    //Console.WriteLine("方法结束");
                                                    // break;
                                                }
                                            }
                                            else if (isstart == 2 && globaldata.m_pcbuffer.gcpcinfo.pcworkinfo.work_Statue == 2)
                                            {
                                                // CurveDisPlay.curdisp.addPoint(listPoint, true);  
                                                drbool = true;
                                                // time = 0;
                                                isstart = 0;
                                                //break;
                                            }

                                        }
                                        else
                                            starttime = ((SignalBuffer.Channel_XY)globaldata.m_signalbuffer.Draw_Port3[i]).ctimer;

                                    }

                                    //读取数据完毕
                                    globaldata.m_signalbuffer.Draw_Port3.Clear();


                                }
                            }
                        }
#if(debug)
                        st.Stop(); //计时终止
                        Trace.WriteLine("后处理更新时间：" + st.Elapsed.ToString(), "sys");
#endif

                        CurveDisPlay3.curdisp3.addPoint(listPoint, drbool);   //更新绘画区域



                        // Console.WriteLine(string.Format("【{0}】读取个数结束  {1}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"), globaldata.m_signalbuffer.Draw_Port.Count));

                    }

                    if (globaldata.cancelflag)
                    {

                        globaldata.cancelflag = false;
                    }

                    //  Thread.Sleep(500);
                }

                //  Trace.WriteLine("绘图线程退出！","sys");
            }
            catch (Exception ex)
            {
                Trace.Write(ex);
            }
        }

        public void CKvocUpdataStart()
        {
            if (datamre != null)
            {
                datamre.Reset();
            }
            // t_DataSave = new Thread(Monitor_Data);
            t_DataSave = new Thread(Monitor_DataList);
            t_DataSave.IsBackground = true;
            t_DataSave.Start();
        }

        public void CKvocUpdataStop()
        {
            datamre.Set();
        }

    }
}
