using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using GcDevicePc.Common;

namespace GcDevicePc.GCBuffer
{
    public struct stTemp
    {
        public ushort usCurTemp_Word1;
        public ushort usCurTemp_Word2;
    }

    [StructLayout(LayoutKind.Explicit, Size = 8)]
    public struct UNION_TEMP
    {
        [FieldOffset(0)]
        public float fCurTemp;

        [FieldOffset(0)]
        public stTemp t;
    }

    /// <summary>
    /// 辅助加热区
    /// </summary>
    public struct DevAux
    {
        public ushort u16AuxHeatArea1;		//辅助加热区1
        public bool fTemp_Enable1;          //加热器1升温开关
        public float fSetTemp_HeatArea1;	//加热器1温度设定值
        public float fActualTemp_HeatArea1; //加热器1温室实际值
        public ushort u16AuxHeatArea2;		//辅助加热区2
        public bool fTemp_Enable2;          //加热器2升温开关
        public float fSetTemp_HeatArea2;	//加热器2温度设定值
        public float fActualTemp_HeatArea2; //加热器2温室实际值
    }

    /// <summary>
    /// 色谱柱
    /// </summary>
    public struct DevChromato
    {
        public ushort sChromSelect;		//选择的色谱柱
        public ushort u16ChromType;			//色谱柱模式
        public float fHigestTempSet;		//最高温度
        public float fLengthSet;			//长度设置
        public float fCalSet;					//口径设置
        public float fFilmThicknessSet;		//膜厚设置
        public string sDescribe;				//描述
        public float fPreSetValue;		        //压力设定值
        public float fPreActualValue;	//压力实际值
        public float fFlowSetValue;		//流量设定值
        public float fFlowActualValue;	//流量实际值
        public float fSetAverageLineSpeed;	//平均线速度
        public float fActualAverageLineSpeed;	//实际线速度
    }

    /// <summary>
    /// 柱箱
    /// </summary>
    public struct DevCol
    {
        public ushort u16KeyCol;			//柱箱开关
        public ushort u16ColProNum;         //程升阶段数
        public float fColTempSet;			//柱箱设定温度
        public float fColTempActual;		//柱箱实际温度
        public float fHigestTemp;			//柱箱最高温度
        public float fBalanceTime;			//平衡时间
        public float fSumProgramTime;		//总程序时间
        public float fBehindRunTemp;		//程升结束之后的运行温度
        public float fBehindRunTime;		//后运行时间
        public ushort u16FastColdTrap;		//快速冷阱
        public ushort u16StartTarp_Type;    //启动冷阱（风扇）//  制冷剂
        public UNION_TEMP fCurTemp;			//实际温度
        public ushort uTcColStage;			//注箱进行程序升温的不同阶段
    }

    /// <summary>
    /// FID
    /// </summary>
    public struct DevFid
    {
        public string sFid;		//Fid的位置
        public ushort u16SignalValve;	//信号值
        public ushort u16KeyFire;	//自动点火
        public ushort u16IsFire;    //是否已经点火
        public ushort u16Zero;		//调零
        public ushort u16Sensitive;	//灵敏度
        public ushort u16Polarity;	//极性
        public ushort u16KeyHeater;		//加热器开关
        public float fHeaterTempSet;  //加热器设定温度
        public float fHeaterActualTemp;//加热器实际温度
        public ushort u16KeyH2Flow;		//氢气流量开关
        public float fSetH2Flow;		//氢气流量设定值
        public float fActualH2Flow;	//氢气流量实际值
        public ushort u16KeyAirFlow;		//空气流量开关
        public float fAirFlowSet;			//空气流量设定值
        public float fAirFlowActual;		//空气流量实际值
        public ushort u16KeyTailGas;			//尾吹气开关
        public float fSetTailGas;			//尾吹气设定值
        public float fActualGasFlow;		//尾吹气实际
        public float fColumnFlowReference;//柱流量参考值
        public ushort u16SigDeduct;		//信号扣除

        //  ushort u16FilterDepth;		//滤波深度
    }

    /// <summary>
    /// TCD
    /// </summary>
    public struct DevTcd
    {
        public string sTcdLoc;		        //TCD位置
        public float fSigValve;	        //信号值
        public ushort u16Electric;	        //桥流
        public ushort u16Polarity;	        //极性
        public ushort u16AutoZeroSet;      //自动调零
        public float fHeaterTempSet;		//加热器设定值
        public float fHeaterTempActual;	//加热器实际值
        public float fTaiGasSet;			//尾吹气设定值
        public float fTaiGasActual;		//尾吹气实际值
        public float fColumnFlowReference; //柱流量参考
        public ushort u16SigDeduct;		//信号扣除
        public ushort u16KeyBridgeFlow;    //桥流开关
        public ushort u16FilterDepth;	    //滤波深度
        public ushort u16HeaterSel;	    //加热器
        public ushort u16TaiGasSel;	    //尾吹气
    };

    /// <summary>
    /// 进样口
    /// </summary>
    public struct DevPort
    {
        public string sInjPortLoc;	//进样口位置
        public bool bHeater;			//加热器
        public float fHeaterSetTemp;	//加热器设定温度值
        public float fHeaterSetTempm;	//加热器modbus设定温度值
        public float fHeaterTempActual;//加热器实际温度值
        public float fSetPre;			//设定压力
        public float fPreActual;		//实际压力
        public float fSetSumFlow;		//总流量设定值
        public float fSumFlowActual;	//总流量实际值
        public float fSetPurgeFlow;	//隔垫吹扫流量设定值
        public float fPurgeFlowActual;	//隔垫吹扫流量实际值
        public float fSetColumnFlow;	//柱流量设定值
        public float fColumnFlowActual;	//柱流量实际值
        public string sWeatherShuntModel; //分流不分流模式
        public ushort u16SplitRatio;			//分流比
        public float fSplitFlow;			//分流流量
        public bool bCarrierGasSave;	    //载气节省开启
        public ushort u16CarrierGasSave;		//载气节省20ml/min
        public float fSaveGasTime;			//开始等待时间
    }

    /// <summary>
    /// 进样器
    /// </summary>
    public struct DevInj
    {
        public string sSample;			//配备的进样器
        public float fSampleQuantity;	//进样量
        public float fInjectorSize;		//注射器尺寸
        public string sFrontSample;		//进样前样品
        public string sBSSolventA;		//进样前溶剂A
        public string sBSSolventB;		//进样前溶剂B
        public string sASSolventA;		//进样后溶剂A
        public string sASSolventB;		//进样后溶剂B
        public ushort u16PushPullTimes;		//推拉针芯次数
        //其他参数
       // UNION_TEMP fCurTemp;			//进样室当前温度
        public float fSetTemp;			//进样室设定温度
    }

    /// <summary>
    /// 阀
    /// </summary>
    public struct DevValve
    {
        public bool bValveKey;            //是否有阀存在
        public bool bHeaterKey;           //是否有加热器
        public float fSetTemp;            //设定值
        public float fActualTemp;         //当前值
        public ushort u16ValveSetTime1;
        public ushort u16ValveSetTime2;
        public ushort u16ValveSetTime3;
        public ushort u16ValveSetTime4;
        public ushort u16ValveSetTime5;
        public ushort u16ValveSetTime6;
        public ushort u16ValveSetTime7;
        public ushort u16ValveSetTime8;
        public char ValveStatus;
    }

    public class DevSignal
    {
        public ushort u16Channel;	//A/B通道开关
        public string sDet1;	//A/B通道第一时间段的检测器
        public string sDet2;	//A/B通道第二时间段的检测器
        public string sDet3;	//A/B通道第三时间段的检测器
        public ushort u16DetTimeLength1;	//A/B第一时间段的检测时间
        public ushort u16DetTimeLength2;	//A/B第二时间段的检测时间
        public ushort u16DetTimeLength3;	//A/B第三时间段的检测时间
        public ushort u16Zero1;				//A/B第一时间段归零操作
        public ushort u16Zero2;				//A/B第二时间段归零操作
        public ushort u16Zero3;				//A/B第三时间段归零操作
        public ushort u16Save1;				//A/B第一时间段保存操作
        public ushort u16Save2;				//A/B第二时间段保存操作
        public ushort u16Save3;				//A/B第三时间段保存操作
    }

    class MethodConfig
    {
        public const int MAX_VALVE_NUM = 6;
        public const int MAX_CHANNEL_NUM = 2;
        public const int MAX_SAMPLE_NUM = 2;
        public const int MAX_PORT_NUM = 2;
        public const int MAX_FID_NUM = 3;
        public const int MAX_TCD_NUM = 3;

        public DevAux aux = new DevAux();
        public DevChromato chrom = new DevChromato();
        public DevCol col = new DevCol();
        public DevFid[] fid = new DevFid[MAX_FID_NUM];
        public DevTcd[] tcd = new DevTcd[MAX_TCD_NUM];
        public DevPort[] port = new DevPort[MAX_PORT_NUM];
        public DevValve[] valve = new DevValve[MAX_VALVE_NUM];
        public DevSignal[] signal = new DevSignal[MAX_CHANNEL_NUM];
        public DevInj[] sample = new DevInj[MAX_SAMPLE_NUM];

        FileHelper methodconfigini;
        FileHelper savemethodconfig;

        public MethodConfig()
        {
            InitMethodConfig();
        }

        /// <summary>
        /// 初始化辅助加热器
        /// </summary>
        private void InitDevAux()
        {
            aux.u16AuxHeatArea1 = 0;
            aux.fTemp_Enable1 = false;
            aux.fSetTemp_HeatArea1=0.0f;
            aux.fActualTemp_HeatArea1 =0.0f;
            aux.u16AuxHeatArea2 =0;
            aux.fTemp_Enable2 = false;
            aux.fSetTemp_HeatArea2 = 0.0f;
            aux.fActualTemp_HeatArea2 = 0.0f;
        }

        /// <summary>
        ///  色谱柱
        /// </summary>
        private void InitDevChrom()
        {
            chrom.sChromSelect= 0;
            chrom.u16ChromType = 0;
            chrom.fHigestTempSet = 0.0f;		
            chrom.fLengthSet = 0.0f;		
            chrom.fCalSet = 0.0f;				
            chrom.fFilmThicknessSet = 0.0f;
            chrom.sDescribe = "色谱柱";			
            chrom.fPreSetValue = 0.0f;		     
            chrom.fPreActualValue = 0.0f;	
            chrom.fFlowSetValue = 0.0f;		
            chrom.fFlowActualValue = 0.0f;	
            chrom.fSetAverageLineSpeed = 0.0f;	
            chrom.fActualAverageLineSpeed = 0.0f;
        }

        /// <summary>
        /// 柱箱
        /// </summary>
        private void InitDevCol()
        {
            col.u16KeyCol = 0; ;			//柱箱开关
            col.u16ColProNum = 0; ;         //程升阶段数
            col.fColTempSet = 0.0f;			//柱箱设定温度
            col.fColTempActual = 0.0f;		//柱箱实际温度
            col.fHigestTemp = 0.0f;			//柱箱最高温度
            col.fBalanceTime = 0.0f;		//平衡时间
            col.fSumProgramTime = 0.0f;		//总程序时间
            col.fBehindRunTemp = 0.0f;		//程升结束之后的运行温度
            col.fBehindRunTime = 0.0f;		//后运行时间
            col.u16FastColdTrap = 0;		//快速冷阱
            col.u16StartTarp_Type = 0;      //启动冷阱（风扇）//  制冷剂
            col.uTcColStage = 0;			//注箱进行程序升温的不同阶段
        }

        /// <summary>
        /// FID
        /// </summary>
        private void InitDevFid()
        {

            for(int i = 0; i < 3; i++)
            {
                switch (i)
                {
                    case 0:
                        fid[i].sFid = "前";		//Fid的位置
                        break;
                    case 1:
                        fid[i].sFid = "中";		//Fid的位置
                        break;
                    case 2:
                        fid[i].sFid = "后";		//Fid的位置
                        break;
                    default:
                        fid[i].sFid = "前";		//Fid的位置
                        break;
                }
                fid[i].u16SignalValve =0;	//信号值
                fid[i].u16KeyFire = 0;	//自动点火
                fid[i].u16IsFire = 0;    //是否已经点火
                fid[i].u16Zero = 0; ;		//调零
                fid[i].u16Sensitive = 0;	//灵敏度
                fid[i].u16Polarity = 0;	//极性
                fid[i].u16KeyHeater = 0;		//加热器开关
                fid[i].fHeaterTempSet =0.0f;  //加热器设定温度
                fid[i].fHeaterActualTemp = 0.0f;//加热器实际温度
                fid[i].u16KeyH2Flow = 0;		//氢气流量开关
                fid[i].fSetH2Flow = 0.0f;		//氢气流量设定值
                fid[i].fActualH2Flow = 0.0f;	//氢气流量实际值
                fid[i].u16KeyAirFlow = 0;		//空气流量开关
                fid[i].fAirFlowSet = 0.0f;			//空气流量设定值
                fid[i].fAirFlowActual = 0.0f;		//空气流量实际值
                fid[i].u16KeyTailGas = 0;			//尾吹气开关
                fid[i].fSetTailGas = 0.0f;			//尾吹气设定值
                fid[i].fActualGasFlow = 0.0f;		//尾吹气实际
                fid[i].fColumnFlowReference = 0.0f;//柱流量参考值
                fid[i].u16SigDeduct = 0;		//信号扣除
            }
        }

        /// <summary>
        /// TCD
        /// </summary>
        private void InitDevTcd()
        {
            for(int i =0; i < 3; i++)
            {
                switch (i)
                {
                    case 0:
                        tcd[i].sTcdLoc = "前";
                        break;
                    case 1:
                        tcd[i].sTcdLoc = "中";		//Fid的位置
                        break;
                    case 2:
                        tcd[i].sTcdLoc = "后";		//Fid的位置
                        break;
                    default:
                        tcd[i].sTcdLoc = "前";		//Fid的位置
                        break;
                }

                tcd[i].fSigValve = 0.0f;	         //信号值
                tcd[i].u16Electric = 0;	     //桥流
                tcd[i].u16Polarity = 0;	     //极性
                tcd[i].u16AutoZeroSet = 0;      //自动调零
                tcd[i].fHeaterTempSet = 0.0f;		 //加热器设定值
                tcd[i].fHeaterTempActual = 0.0f;	 //加热器实际值
                tcd[i].fTaiGasSet = 0.0f;			 //尾吹气设定值
                tcd[i].fTaiGasActual = 0.0f;		 //尾吹气实际值
                tcd[i].fColumnFlowReference = 0.0f; //柱流量参考
                tcd[i].u16SigDeduct = 0;		 //信号扣除
                tcd[i].u16KeyBridgeFlow = 0;    //桥流开关
                tcd[i].u16FilterDepth = 0;	     //滤波深度
                tcd[i].u16HeaterSel = 0;	     //加热器
                tcd[i].u16TaiGasSel = 0;	     //尾吹气
            }
        }

        /// <summary>
        /// 进样口
        /// </summary>
        private void InitDevPort()
        {
            for(int i = 0; i < 2; i++)
            {
                switch(i)
                {
                    case 0:
                        port[i].sInjPortLoc = "前";
                        break;
                    case 1:
                        port[i].sInjPortLoc = "后";
                        break;
                    default:
                        port[i].sInjPortLoc = "前";
                        break;
                }

                port[i].bHeater = false;			//加热器
                port[i].fHeaterSetTemp = 0.0f;	//加热器设定温度值
                port[i].fHeaterSetTempm = 0.0f;	//加热器modbus设定温度值
                port[i].fHeaterTempActual = 0.0f ;//加热器实际温度值
                port[i].fSetPre = 0.0f;			//设定压力
                port[i].fPreActual = 0.0f;		//实际压力
                port[i].fSetSumFlow = 0.0f;		//总流量设定值
                port[i].fSumFlowActual = 0.0f;	//总流量实际值
                port[i].fSetPurgeFlow = 0.0f;	//隔垫吹扫流量设定值
                port[i].fPurgeFlowActual = 0.0f;	//隔垫吹扫流量实际值
                port[i].fSetColumnFlow = 0.0f;	//柱流量设定值
                port[i].fColumnFlowActual = 0.0f;	//柱流量实际值
                port[i].sWeatherShuntModel = "不分流"; //分流不分流模式
                port[i].u16SplitRatio = 0;			//分流比
                port[i].fSplitFlow = 0.0f;			//分流流量
                port[i].bCarrierGasSave = false;	    //载气节省开启
                port[i].u16CarrierGasSave = 0;		//载气节省20ml/min
                port[i].fSaveGasTime = 0.0f;			//开始等待时间
            }
      
        }

        /// <summary>
        /// 阀
        /// </summary>
        private void InitDevValue()
        {
            for(int i = 0; i< MAX_VALVE_NUM; i++)
            {
                 valve[i].bValveKey = false;            //是否有阀存在
                 valve[i].bHeaterKey = false;           //是否有加热器
                 valve[i].fSetTemp = 0.0f;            //设定值
                 valve[i].fActualTemp = 0.0f;         //当前值
                 valve[i].u16ValveSetTime1 = 0;
                 valve[i].u16ValveSetTime2 = 0;
                 valve[i].u16ValveSetTime3 = 0;
                 valve[i].u16ValveSetTime4 = 0;
                 valve[i].u16ValveSetTime5 = 0;
                 valve[i].u16ValveSetTime6 = 0;
                 valve[i].u16ValveSetTime7 = 0;
                 valve[i].u16ValveSetTime8 = 0;
                 valve[i].ValveStatus = '0';
            }
        }

        /// <summary>
        /// 信号通道
        /// </summary>
        private void InitDevSignal()
        {
            for(int i = 0; i < MAX_VALVE_NUM; i++)
            {
                signal[i].u16Channel = 0;	        //A/B通道开关
                signal[i].sDet1 ="fid";	            //A/B通道第一时间段的检测器
                signal[i].sDet2 ="tcd";	            //A/B通道第二时间段的检测器
                signal[i].sDet3 ="fid";	            //A/B通道第三时间段的检测器
                signal[i].u16DetTimeLength1 = 0;	//A/B第一时间段的检测时间
                signal[i].u16DetTimeLength2 = 0;	//A/B第二时间段的检测时间
                signal[i].u16DetTimeLength3 = 0;	//A/B第三时间段的检测时间
                signal[i].u16Zero1 = 0;				//A/B第一时间段归零操作
                signal[i].u16Zero2 = 0;				//A/B第二时间段归零操作
                signal[i].u16Zero3 = 0;				//A/B第三时间段归零操作
                signal[i].u16Save1 = 0;				//A/B第一时间段保存操作
                signal[i].u16Save2 = 0;				//A/B第二时间段保存操作
                signal[i].u16Save3 = 0;				//A/B第三时间段保存操作
            }
        }
        
        /// <summary>
        /// 进样器
        /// </summary>
        private void InitDevSample()
        {
            for(int i =0; i < MAX_SAMPLE_NUM; i++)
            {
                switch(i)
                {
                    case 0:
                        sample[i].sSample ="前";			//配备的进样器
                        break;
                    case 1:
                        sample[i].sSample ="后";			//配备的进样器
                        break;
                    default:
                        sample[i].sSample ="前";			//配备的进样器
                        break;
                }
                sample[i].fSampleQuantity = 0.0f;	//进样量
                sample[i].fInjectorSize = 0.0f;		//注射器尺寸
                sample[i].sFrontSample = "酒精";		//进样前样品
                sample[i].sBSSolventA = "XX";		//进样前溶剂A
                sample[i].sBSSolventB = "XX";		//进样前溶剂B
                sample[i].sASSolventA = "XX";		//进样后溶剂A
                sample[i].sASSolventB = "XX";		//进样后溶剂B
                sample[i].u16PushPullTimes = 0;		//推拉针芯次数
               // sample[i].fCurTemp = 0.0f;			//进样室当前温度
                sample[i].fSetTemp = 0.0f;			//进样室设定温度
            }
        }

        public void InitMethodConfig()
        {
            InitDevAux();
            InitDevChrom();
            InitDevCol();
            InitDevFid();
            InitDevTcd();
            InitDevPort();
            InitDevValue();
            InitDevSignal();
            InitDevSample();
        }

        public void GetMethodConfig(string filename)
        {
            methodconfigini = new FileHelper(filename);
        }

        public void SaveMethodConfig(string filename)
        {
            savemethodconfig = new FileHelper(filename);

            if (System.IO.File.Exists(filename) == false)
            {
                System.IO.StreamWriter sw = new System.IO.StreamWriter(filename, false, System.Text.Encoding.Unicode);
                sw.WriteLine("[方法名称]");
                sw.Close();
            }

        }

    }
}
