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;
using GcDevicePc.IniParam;
//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;

                //float dtime = 0.0f;
                //float dvalue = 0.0f;
                List<double[]> listPoint = new List<double[]>();//临时数据区
                List<double[]> listRaw = new List<double[]>();//原始数据
                List<double[]> listRawTem = new List<double[]>();//原始数据缓存
                bool drbool = false;
                uint NowTimeOld = 0;//上一周期开始
                UInt32 starttimer = 0x7FFFFFFF;
                while (!datamre.WaitOne(500))
                {

                    drbool = false;
                    listPoint.Clear();
                    listRaw.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(true)
                    {
#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.startclear3)
                                {
                                    CurveDisPlay3.curdisp3.cleardata();
                                    starttimer = ((SignalBuffer.Channel_XY)globaldata.m_signalbuffer.Draw_Port3[0]).ctimer;


                                    time = 0;
                                    globaldata.startclear3 = false;
                                }
                                else
                                {

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

                                    ushort timeend = globaldata.m_dpbuffer.ShowList.showtime.alltime;
                                    timeend -= 5;
                                    if (starttimer == 0)
                                        starttimer = ((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)
                                        //  if (true)
                                        {

                                            if ((globaldata.m_hmibuffer.gcinfo.NowStartTime <= ((SignalBuffer.Channel_XY)globaldata.m_signalbuffer.Draw_Port3[0]).ctimer) && isstart == 0 && (NowTimeOld != globaldata.m_hmibuffer.gcinfo.NowStartTime))
                                            {

                                                if (CKVocAnalyzer.GlobalCKV.debugwork == 5)
                                                {
                                                    CKVocAnalyzer.GlobalCKV.debugwork = 4;
                                                    isstart = 2;
                                                    time = 0;
                                                    drbool = false;
                                                    starttimer = globaldata.m_hmibuffer.gcinfo.NowStartTime;
                                                    NowTimeOld = globaldata.m_hmibuffer.gcinfo.NowStartTime;
                                                    CurveDisPlay3.curdisp3.cleardata();


                                                }
                                                else
                                                {
                                                    isstart = 2;

                                                    CurveDisPlay3.curdisp3.SetAxisX((double)globaldata.m_dpbuffer.ShowList.showtime.alltime / 60.0);
                                                    time = 0;
                                                    drbool = false;
                                                    starttimer = globaldata.m_hmibuffer.gcinfo.NowStartTime;
                                                    NowTimeOld = globaldata.m_hmibuffer.gcinfo.NowStartTime;
                                                    CurveDisPlay3.curdisp3.cleardata();

                                                    Trace.WriteLine(globaldata.m_hmibuffer.gcinfo.NowStartTime.ToString(), "sys");

                                                }
                                                CKVocAnalyzer.GlobalCKV.dataFtime.FTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                                                listPoint.Clear();  //清除记录
                                                listRaw.Clear();


                                            }
                                        }

                                        //数据回填
                                        if (isstart == 0)  //方法结束，缓存原始数据
                                        {
                                            if (listRawTem.Count < 3000)
                                            {
                                                listRawTem.Add(new double[] { ((SignalBuffer.Channel_XY)globaldata.m_signalbuffer.Draw_Port3[i]).ctimer, Math.Round((((SignalBuffer.Channel_XY)globaldata.m_signalbuffer.Draw_Port3[i]).cdata * 1000.0f), 6) });
                                            }
                                            else
                                            {
                                                listRawTem.Clear();//缓存3000点，大于3000，重新开始
                                            }

                                            //TCP数据回传

                                            lock (globaldata.m_signalbuffer.TCP_Port)
                                            {
                                                if (globaldata.m_signalbuffer.TCP_Port.Count > 0)
                                                {
                                                    Trace.WriteLine("TCP数据回传", "sys");
                                                    List<double[]> tcpraw = new List<double[]>();
                                                    for (int tcpi = 0; tcpi < globaldata.m_signalbuffer.TCP_Port.Count; tcpi++)
                                                    {
                                                        tcpraw.Add(new double[] { ((SignalBuffer.TCP_Channel)globaldata.m_signalbuffer.TCP_Port[tcpi]).dettimer, Math.Round(((SignalBuffer.TCP_Channel)globaldata.m_signalbuffer.TCP_Port[tcpi]).detdata1 * 1000.0f, 6) });
                                                    }
                                                    if (tcpraw.Count > 0)
                                                    {
                                                        CurveDisPlay3.curdisp3.WriteTcp(tcpraw);
                                                    }
                                                }
                                                globaldata.m_signalbuffer.TCP_Port.Clear();

                                            }


                                        }
                                        if (isstart == 2 && listRawTem.Count > 0)  //回填数据
                                        {
                                            int rami = listRawTem.FindIndex(s => s[0] >= starttimer);
                                            if (rami > -1) //有需要回填数据
                                            {
                                                for (; rami < listRawTem.Count; rami++)
                                                {
                                                    time += (((UInt32)listRawTem[rami][0] - starttimer) / 1000.0f / 60.0f);

                                                    starttimer = (UInt32)listRawTem[rami][0];
                                                    value = listRawTem[rami][1];
                                                    listPoint.Add(new double[] { Math.Round(time, 4), Math.Round(value, 6) });
                                                    listRaw.Add(new double[] { listRawTem[rami][0], Math.Round(value, 6) });

                                                }
                                            }
                                            listRawTem.Clear();
                                        }

                                        if (starttimer <= ((SignalBuffer.Channel_XY)globaldata.m_signalbuffer.Draw_Port3[i]).ctimer && (((SignalBuffer.Channel_XY)globaldata.m_signalbuffer.Draw_Port3[i]).ctimer - starttimer) > 0)
                                        {

                                            time += ((((SignalBuffer.Channel_XY)globaldata.m_signalbuffer.Draw_Port3[i]).ctimer - starttimer) / 1000.0f / 60.0f);
                                            //time += 0.00083;
                                            starttimer = ((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, 6) });
                                                listRaw.Add(new double[] { ((SignalBuffer.Channel_XY)globaldata.m_signalbuffer.Draw_Port3[i]).ctimer, Math.Round(value, 6) });



                                            }



                                            //时间判断终点
                                            //  if ((globaldata.MethodRun_Statue == 4 || globaldata.MethodRun_Statue == 5))
                                            if (globaldata.m_hmibuffer.gcinfo.methodstatus < 4 || globaldata.m_hmibuffer.gcinfo.methodstatus > 5)
                                            {

                                                if ((globaldata.m_hmibuffer.gcinfo.NowStartTime < globaldata.m_hmibuffer.gcinfo.NowEndTime) && (globaldata.m_hmibuffer.gcinfo.NowEndTime <= ((SignalBuffer.Channel_XY)globaldata.m_signalbuffer.Draw_Port3[i]).ctimer) && isstart == 2)
                                                {
                                                    drbool = true;
                                                    isstart = 0;
                                                    break;
                                                }
                                            }


                                        }
                                        else
                                            starttimer = ((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
                        //if (CProfileDevice.m_DevParam.syspara.u16DetBehind == 1)
                        //{
                        //    for (int i = (listRaw.Count / CProfileDevice.m_DevParam.syspara.u16DetNum)*2; i < listRaw.Count; i++)
                        //    {
                        //        lr.Add(listRaw[i]);
                        //    }
                        //    for (int i = (listPoint.Count / CProfileDevice.m_DevParam.syspara.u16DetNum)*2; i < listPoint.Count; i++)
                        //    {
                        //        lp.Add(listPoint[i]);
                        //    }
                        //}
                        CurveDisPlay3.curdisp3.addDataRaw(listRaw); //记录原始数据
                        CurveDisPlay3.curdisp3.addPoint(listPoint, drbool);   //更新绘画区域
                        //lr.Clear();
                        //lp.Clear();
                        if (drbool)
                        {
                            time = 0;
                            CurveDisPlay3.curdisp3.cleardata();
                        }


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

                    }

                    if (globaldata.cancelflag3)
                    {

                        globaldata.cancelflag3 = false;
                    }

                    //  Thread.Sleep(500);
                }

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

        //List<double[]> lr = new List<double[]>();
        //List<double[]> lp = new List<double[]>();
        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();
        }

    }
}
