using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using GcDevicePc.Module;

namespace GcDevicePc.ProThread
{
    class DataOutput
    {
        ManualResetEvent outputmre = new ManualResetEvent(false);
        Thread t_DataOut;

        private WTD624X wtd624x;

        private bool c6h6 = false;
        private bool c7h8 = false;
        private bool c8h10 = false;

        //批量更新 
        //用于更新记录数据
        private void OutPut_DataList()
        {
            try
            {
                string file = System.Windows.Forms.Application.StartupPath+ "\\..\\GC_Config\\GC_DataPro" + "\\CarLine" + "\\Ingredient.ini";

                int[] datano = new int[6] {1, 1, 1, 1, 1, 1};
                if (File.Exists(file))
                {
                    INIOperation test = new INIOperation(file);
                    string scount = test.INIGetStringValue("SYSINFO", "ECOUNT", null);

                    int count = Convert.ToInt32(scount);

                    for (int i = 0; i < count; i++)
                    {
                        string sid = test.INIGetStringValue("ENTITY" + i.ToString(), "ID", null);
                        string sname = test.INIGetStringValue("ENTITY" + i.ToString(), "NAME", null);

                        if (!String.IsNullOrEmpty(sname))
                        {
                            try
                            {
                                if (sname == "NMHC")
                                {
                                    datano[0] = Convert.ToInt32(sid);
                                }

                                if (sname == "苯" || sname == "C6H6")
                                {
                                    c6h6 = true;
                                    datano[1] = Convert.ToInt32(sid);
                                }

                                if (sname == "甲苯" || sname == "C7H8")
                                {
                                    c7h8 = true;
                                    datano[2] = Convert.ToInt32(sid);
                                }

                                if (sname == "二甲苯" || sname == "C8H10")
                                {
                                    c8h10 = true;
                                    datano[3] = Convert.ToInt32(sid);
                                }

                                if (sname == "THC")
                                {
                                    datano[4] = Convert.ToInt32(sid);
                                }

                                if (sname == "CH4")
                                {
                                    datano[5] = Convert.ToInt32(sid);
                                }
                            }
                            catch
                            {

                            }
                        }
                    }
                }

                while (!outputmre.WaitOne(500))
                {
                    if (SharedSpace.Data.change)
                    {
                        float[] data = new float[4] {0, 0, 0, 0};
                        double[] ratio = new double[4] {1, 3.487053571, 4.113392857, 4.739285714};
                        for (int count = 0; count < 4; count++)
                        {

                            switch (count)
                            {
                                case 0:
                                {
                                    double nmhc_data =
                                        (SharedSpace.Data.listValue[datano[4] - 1].vale -
                                         SharedSpace.Data.listValue[datano[5] - 1].vale) * ratio[count] * 12.0f / 22.4f;
                                    data[count] = float.Parse(nmhc_data.ToString());
                                }
                                    break;
                                case 1:
                                {
                                    data[count] = c6h6
                                        ? float.Parse(
                                            (SharedSpace.Data.listValue[datano[count] - 1].vale * ratio[count])
                                            .ToString())
                                        : 0.0f;

                                }
                                    break;
                                case 2:
                                {
                                    data[count] = c7h8
                                        ? float.Parse(
                                            (SharedSpace.Data.listValue[datano[count] - 1].vale * ratio[count])
                                            .ToString())
                                        : 0.0f;
                                }
                                    break;
                                case 3:
                                {
                                    data[count] = c8h10
                                        ? float.Parse(
                                            (SharedSpace.Data.listValue[datano[count] - 1].vale * ratio[count])
                                            .ToString())
                                        : 0.0f;
                                }
                                    break;
                                default:
                                    data[count] = 0;
                                    break;
                            }

                           // Trace.WriteLine("周期结束" + count.ToString() + ":" + data[count].ToString(), "sys");
                        }

                        if (wtd624x.GetWorkFlag())
                        {
                            wtd624x.UpdateData(data);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Trace.WriteLine("WTD624X模块输出 Crash 由于:" + ex.Message, "sys");
            }
        }

        public void DataOutStart()
        {
            if (!String.IsNullOrEmpty(globaldata.m_pcbuffer.gcpcinfo.moduleinfo.ip))
            {
                wtd624x = new WTD624X(globaldata.m_pcbuffer.gcpcinfo.moduleinfo.ip, 502);
            }

            if (wtd624x!= null)
            {
                if (outputmre != null)
                {
                    outputmre.Reset();
                }

                t_DataOut = new Thread(OutPut_DataList);
                t_DataOut.IsBackground = true;
                t_DataOut.Start();

                if (!wtd624x.GetWorkFlag())
                {
                    wtd624x.WTD624X_Open();
                }
            }
            
        }

        public void DataOutStop()
        {
            if (wtd624x != null)
            {
                outputmre.Set();
                if (wtd624x.GetWorkFlag())
                {
                    wtd624x.WTD624X_Open();
                }
            }
                

        }
    }
}
