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

namespace GcDevicePc.GCBuffer
{
    class HWConfig
    {
        public struct HWConfigInfo
        {
            public string GCNum;                    //仪器编号
            public string GCName;                   //仪器名字
            public ushort u16Col;			        //柱箱
            public ushort u16ColType;               //柱箱类型
            public ushort u16ColPTemp;              //柱箱保护温度
            public ushort u16ColCtl;		        //柱箱控制方式
            public ushort u16InjPortNum;            //进样口数量
            public ushort u16InjPortFront;	        //前进样口
            public ushort u16InjPortFPTemp;         //前进样口保护温度
            public ushort u16InjPortBehind;	        //后进样口
            public ushort u16InjPortBPTemp;	        //后进样口保护温度
            public ushort u16InjPortType_Front;		//前进样口类型
            public ushort u16InjPortCtlMode_Front;  //前进样口控制方式
            public ushort u16InjPortLoadGas_Front;  //前进样口载气
            public ushort u16InjPortType_Behind;    //前进样口类型
            public ushort u16InjPortCtlMode_Behind; //前进样口控制方式
            public ushort u16InjPortLoadGas_Behind; //前进样口载气

            public ushort u16ValveBox;      //阀箱
            public ushort u16ValveOne;		//阀1
            public ushort u16ValveTwo;		//阀2
            public ushort u16ValveThree;	//阀3
            public ushort u16ValveFour;		//阀4
            public ushort u16ValveFive;		//阀5
            public ushort u16ValveSix;		//阀6

            public ushort u16DetNum;            //检测器数量
            public ushort u16DetFront;		    //前检测器
            public ushort u16DetFrontType;	    //前检测器类型
            public ushort u16DetFrontPTemp;	    //前检测器保护温度
            public ushort u16DetFrontAisle;	    //前检测器通道
            public ushort u16DetInter;		    //中检测器
            public ushort u16DetInterType;	    //中检测器类型
            public ushort u16DetInterPTemp; 	//中检测器保护温度
            public ushort u16DetInterAisle;	    //中检测器通道
            public ushort u16DetBehind;		    //后检测器
            public ushort u16DetBehindType;	    //后检测器类型
            public ushort u16DetBehindPTemp;	//后检测器保护温度
            public ushort u16DetBehindAisle;	//后检测器通道

            public ushort u16SampleNum;         //进样器数量
            public ushort u16SampleFront;	    //前进样器
            public ushort u16SampleFrontType;	//前进样器方式
            public ushort u16SampleBehind;	    //后进样器
            public ushort u16SampleBehindType;	//后进样器方式

            public ushort u16AuxHeater;		    //辅助加热器
            public ushort u16AuxHeaterNum;	    //辅助加热数量
            public ushort u16AuxHeater1;	    //辅助1
            public string u16AuxHeaterName1;	//辅助名字1
            public ushort u16AuxHeaterPTemp1;	//辅助1保护温度
            public ushort u16AuxHeater2;	    //辅助2
            public string u16AuxHeaterName2;	//辅助名字2
            public ushort u16AuxHeaterPTemp2;   //辅助2保护温度


            public ushort u16ChromNum;	        //色谱柱数量
            public ushort u16ChromFront;	    //前色谱柱
            public ushort u16ChromFrontType;    //前色谱柱类型
            public ushort u16ChromBehindType;   //后色谱柱类型
            public ushort u16ChromBehind;	    //后色谱柱
            public ushort u16StartType;		    //启动方式
        }

        FileHelper hwconfigini;
        HWConfigInfo hwconfiginfo;
        FileHelper savehwconfig;

        public HWConfig(string path)
        {
            hwconfigini = new FileHelper(path);
            hwconfiginfo = new HWConfigInfo();
        }

        public bool GetHWConfig()
        {
            const int iDefault = 0;
  
            string sAppName = "柱箱单元柱箱";
            hwconfiginfo.u16Col = hwconfigini.GetProfileInt(sAppName, "柱箱", iDefault);
            hwconfiginfo.u16ColType = hwconfigini.GetProfileInt(sAppName, "柱箱类型", iDefault);
            try
            {
                hwconfiginfo.u16ColPTemp = hwconfigini.GetProfileInt(sAppName, "柱箱保护温度", iDefault);
            }
            catch
            {
                hwconfiginfo.u16ColPTemp = 0;
            }

            sAppName = "仪器编号";
            hwconfiginfo.GCNum = hwconfigini.GetProfileString(sAppName, "仪器号");
            hwconfiginfo.GCName = hwconfigini.GetProfileString(sAppName, "仪器名称");

            sAppName = "进样单元进样口";
            hwconfiginfo.u16InjPortNum = hwconfigini.GetProfileInt(sAppName, "进样口数量", iDefault);
            hwconfiginfo.u16InjPortFront = hwconfigini.GetProfileInt(sAppName, "前进样口", iDefault);
            try
            {
                hwconfiginfo.u16InjPortFPTemp = hwconfigini.GetProfileInt(sAppName, "前进样口保护温度", iDefault);
            }
            catch
            {
                hwconfiginfo.u16InjPortFPTemp = 0;
            }


            hwconfiginfo.u16InjPortBehind = hwconfigini.GetProfileInt(sAppName, "后进样口", iDefault);

            hwconfiginfo.u16InjPortType_Front = hwconfigini.GetProfileInt(sAppName, "前进样口类型", iDefault);
            hwconfiginfo.u16InjPortLoadGas_Front = hwconfigini.GetProfileInt(sAppName, "前进样口载气", iDefault);
            hwconfiginfo.u16InjPortType_Behind = hwconfigini.GetProfileInt(sAppName, "后进样口类型", iDefault);
            try
            {
                hwconfiginfo.u16InjPortBPTemp = hwconfigini.GetProfileInt(sAppName, "后进样口保护温度", iDefault);
            }
            catch
            {
                hwconfiginfo.u16InjPortBPTemp = 0;
            }

            hwconfiginfo.u16InjPortLoadGas_Behind = hwconfigini.GetProfileInt(sAppName, "后进样口载气", iDefault);
            sAppName = "仪器档次";
            hwconfiginfo.u16InjPortCtlMode_Behind = hwconfigini.GetProfileInt(sAppName, "仪器档次", iDefault);
            hwconfiginfo.u16InjPortCtlMode_Front = hwconfigini.GetProfileInt(sAppName, "仪器档次", iDefault);
            sAppName = "进样单元切换阀";
            hwconfiginfo.u16ValveBox = hwconfigini.GetProfileInt(sAppName, "阀数量", iDefault);
            hwconfiginfo.u16ValveOne = hwconfigini.GetProfileInt(sAppName, "阀1", iDefault);
            hwconfiginfo.u16ValveTwo = hwconfigini.GetProfileInt(sAppName, "阀2", iDefault);
            hwconfiginfo.u16ValveThree = hwconfigini.GetProfileInt(sAppName, "阀3", iDefault);
            hwconfiginfo.u16ValveFour = hwconfigini.GetProfileInt(sAppName, "阀4", iDefault);
            hwconfiginfo.u16ValveFive = hwconfigini.GetProfileInt(sAppName, "阀5", iDefault);
            hwconfiginfo.u16ValveSix = hwconfigini.GetProfileInt(sAppName, "阀6", iDefault);

            sAppName = "检测单元";
            hwconfiginfo.u16DetNum = hwconfigini.GetProfileInt(sAppName, "检测器数量", iDefault);
            hwconfiginfo.u16DetFront = hwconfigini.GetProfileInt(sAppName, "前检测器", iDefault);
            hwconfiginfo.u16DetFrontType = hwconfigini.GetProfileInt(sAppName, "前检测器类型", iDefault);
            try
            {
                hwconfiginfo.u16DetFrontPTemp = hwconfigini.GetProfileInt(sAppName, "前检测器保护温度", iDefault);
            }
            catch
            {
                hwconfiginfo.u16DetFrontPTemp = 0;
            }
            hwconfiginfo.u16DetFrontAisle = hwconfigini.GetProfileInt(sAppName, "前检测器通道", iDefault);

            hwconfiginfo.u16DetInter = hwconfigini.GetProfileInt(sAppName, "中检测器", iDefault);
            hwconfiginfo.u16DetInterType = hwconfigini.GetProfileInt(sAppName, "中检测器类型", iDefault);
            try
            {
                hwconfiginfo.u16DetInterPTemp = hwconfigini.GetProfileInt(sAppName, "中检测器保护温度", iDefault);
            }
            catch
            {
                hwconfiginfo.u16DetInterPTemp = 0;
            }


            hwconfiginfo.u16DetInterAisle = hwconfigini.GetProfileInt(sAppName, "中检测器通道", iDefault);
            hwconfiginfo.u16DetBehind = hwconfigini.GetProfileInt(sAppName, "后检测器", iDefault);
            hwconfiginfo.u16DetBehindType = hwconfigini.GetProfileInt(sAppName, "后检测器类型", iDefault);
            try
            {
                hwconfiginfo.u16DetBehindPTemp = hwconfigini.GetProfileInt(sAppName, "后检测器保护温度", iDefault);
            }
            catch
            {
                hwconfiginfo.u16DetBehindPTemp = 0;
            }

            hwconfiginfo.u16DetBehindAisle = hwconfigini.GetProfileInt(sAppName, "后检测器通道", iDefault);
            sAppName = "进样单元进样器";
            hwconfiginfo.u16SampleNum = hwconfigini.GetProfileInt(sAppName, "进样器数量", iDefault);
            hwconfiginfo.u16SampleFront = hwconfigini.GetProfileInt(sAppName, "前进样器", iDefault);
            hwconfiginfo.u16SampleBehind = hwconfigini.GetProfileInt(sAppName, "后进样器", iDefault);
            sAppName = "辅助设置";
            hwconfiginfo.u16AuxHeater = hwconfigini.GetProfileInt(sAppName, "辅助加热", iDefault);
            hwconfiginfo.u16AuxHeaterNum = hwconfigini.GetProfileInt(sAppName, "辅助加热器数量", iDefault);
            hwconfiginfo.u16AuxHeater1 = hwconfigini.GetProfileInt(sAppName, "辅助1", iDefault);
            hwconfiginfo.u16AuxHeaterName1 = hwconfigini.GetProfileString(sAppName, "辅助名字1");
            hwconfiginfo.u16AuxHeater2 = hwconfigini.GetProfileInt(sAppName, "辅助2", iDefault);
            hwconfiginfo.u16AuxHeaterName2 = hwconfigini.GetProfileString(sAppName, "辅助名字2");

            sAppName = "柱箱单元色谱柱";
            hwconfiginfo.u16ChromNum = hwconfigini.GetProfileInt(sAppName, "色谱柱数量", iDefault);
            hwconfiginfo.u16ChromFront = hwconfigini.GetProfileInt(sAppName, "前色谱柱", iDefault);
            hwconfiginfo.u16ChromBehind = hwconfigini.GetProfileInt(sAppName, "后色谱柱", iDefault);
            hwconfiginfo.u16ChromFrontType = hwconfigini.GetProfileInt(sAppName, "前色谱柱类型", iDefault);
            hwconfiginfo.u16ChromBehindType = hwconfigini.GetProfileInt(sAppName, "后色谱柱类型", iDefault);
            sAppName = "仪器档次";
            hwconfiginfo.u16StartType = hwconfigini.GetProfileInt(sAppName, "仪器档次", iDefault);

            return true;
        }

        public bool SavaHWConfig(string filename)
        {
            savehwconfig = 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();
            }

            //写入仪器编号和编码
           
            savehwconfig.WriteValue("仪器编号", "仪器编码", this.hwconfiginfo.GCNum);
            savehwconfig.WriteValue("仪器编号", "仪器名称", this.hwconfiginfo.GCName);

            savehwconfig.EmptySection("进样单元进样器");
            savehwconfig.WriteValue("进样单元进样器", "进样器数量", hwconfiginfo.u16SampleNum.ToString());
            savehwconfig.WriteValue("进样单元进样器", "前进样器", hwconfiginfo.u16SampleFront.ToString());
            savehwconfig.WriteValue("进样单元进样器", "前进样器启动方式", hwconfiginfo.u16SampleBehindType.ToString());
            savehwconfig.WriteValue("进样单元进样器", "后进样器", hwconfiginfo.u16SampleBehind.ToString());
            savehwconfig.WriteValue("进样单元进样器", "后进样器启动方式", hwconfiginfo.u16SampleBehindType.ToString());

            savehwconfig.EmptySection("进样单元切换阀");
            savehwconfig.WriteValue("进样单元切换阀", "阀数量", hwconfiginfo.u16ValveBox.ToString());
            savehwconfig.WriteValue("进样单元切换阀", "阀1", hwconfiginfo.u16ValveOne.ToString());
            savehwconfig.WriteValue("进样单元切换阀", "阀2", hwconfiginfo.u16ValveTwo.ToString());
            savehwconfig.WriteValue("进样单元切换阀", "阀3", hwconfiginfo.u16ValveThree.ToString());
            savehwconfig.WriteValue("进样单元切换阀", "阀4", hwconfiginfo.u16ValveFour.ToString());
            savehwconfig.WriteValue("进样单元切换阀", "阀5", hwconfiginfo.u16ValveFive.ToString());
            savehwconfig.WriteValue("进样单元切换阀", "阀6", hwconfiginfo.u16ValveSix.ToString());

            savehwconfig.EmptySection("进样单元进样口");
            savehwconfig.WriteValue("进样单元进样口", "进样口数量", hwconfiginfo.u16InjPortNum.ToString());
            savehwconfig.WriteValue("进样单元进样口", "前进样口", hwconfiginfo.u16InjPortFront.ToString());
            savehwconfig.WriteValue("进样单元进样口", "前进样口保护温度", hwconfiginfo.u16InjPortFPTemp.ToString());
            savehwconfig.WriteValue("进样单元进样口", "前进样口类型", hwconfiginfo.u16InjPortType_Front.ToString());
            savehwconfig.WriteValue("进样单元进样口", "前进样口载气", hwconfiginfo.u16InjPortLoadGas_Front.ToString());
            savehwconfig.WriteValue("进样单元进样口", "后进样口", hwconfiginfo.u16InjPortBehind.ToString());
            savehwconfig.WriteValue("进样单元进样口", "后进样口保护温度", hwconfiginfo.u16InjPortBPTemp.ToString());
            savehwconfig.WriteValue("进样单元进样口", "后进样口类型", hwconfiginfo.u16InjPortType_Behind.ToString());
            savehwconfig.WriteValue("进样单元进样口", "后进样口载气", hwconfiginfo.u16InjPortLoadGas_Behind.ToString());

            savehwconfig.EmptySection("柱箱单元柱箱");
            savehwconfig.WriteValue("柱箱单元柱箱", "柱箱", hwconfiginfo.u16Col.ToString());
            savehwconfig.WriteValue("柱箱单元柱箱", "柱箱类型", hwconfiginfo.u16ColType.ToString());
            savehwconfig.WriteValue("柱箱单元柱箱", "柱箱保护温度", hwconfiginfo.u16ColPTemp.ToString());
            
            savehwconfig.EmptySection("柱箱单元色谱柱");
            savehwconfig.WriteValue("柱箱单元色谱柱", "色谱柱数量", hwconfiginfo.u16ChromNum.ToString());
            savehwconfig.WriteValue("柱箱单元色谱柱", "前色谱柱", hwconfiginfo.u16ChromFront.ToString());
            savehwconfig.WriteValue("柱箱单元色谱柱", "前色谱柱类型", hwconfiginfo.u16ChromFrontType.ToString());
            savehwconfig.WriteValue("柱箱单元色谱柱", "后色谱柱", hwconfiginfo.u16ChromBehind.ToString());
            savehwconfig.WriteValue("柱箱单元色谱柱", "后色谱柱类型", hwconfiginfo.u16ChromBehindType.ToString());

            savehwconfig.EmptySection("检测单元");
            savehwconfig.WriteValue("检测单元", "检测器数量", hwconfiginfo.u16DetNum.ToString());
            savehwconfig.WriteValue("检测单元", "前检测器", hwconfiginfo.u16DetFront.ToString());
            savehwconfig.WriteValue("检测单元", "前检测器保护温度", hwconfiginfo.u16DetFrontPTemp.ToString());
            savehwconfig.WriteValue("检测单元", "前检测器类型", hwconfiginfo.u16DetFrontType.ToString());
            savehwconfig.WriteValue("检测单元", "前检测器通道", hwconfiginfo.u16DetFrontAisle.ToString());
            savehwconfig.WriteValue("检测单元", "中检测器", hwconfiginfo.u16DetInter.ToString());
            savehwconfig.WriteValue("检测单元", "中检测器保护温度", hwconfiginfo.u16DetInterPTemp.ToString());
            savehwconfig.WriteValue("检测单元", "中检测器类型", hwconfiginfo.u16DetInterType.ToString());
            savehwconfig.WriteValue("检测单元", "中检测器通道", hwconfiginfo.u16DetInterAisle.ToString());
            savehwconfig.WriteValue("检测单元", "后检测器", hwconfiginfo.u16DetBehind.ToString());
            savehwconfig.WriteValue("检测单元", "后检测器保护温度", hwconfiginfo.u16DetBehindPTemp.ToString());
            savehwconfig.WriteValue("检测单元", "后检测器类型", hwconfiginfo.u16DetBehindType.ToString());
            savehwconfig.WriteValue("检测单元", "后检测器通道", hwconfiginfo.u16DetBehindAisle.ToString());

            savehwconfig.EmptySection("辅助设置");
            savehwconfig.WriteValue("辅助设置", "辅助加热", hwconfiginfo.u16AuxHeater.ToString());
            savehwconfig.WriteValue("辅助设置", "辅助加热器数量", hwconfiginfo.u16AuxHeaterNum.ToString());

            savehwconfig.WriteValue("辅助设置", "辅助1", hwconfiginfo.u16AuxHeater1.ToString());
            savehwconfig.WriteValue("辅助设置", "辅助名字1", hwconfiginfo.u16AuxHeaterName1);
            savehwconfig.WriteValue("检测单元", "中检测器保护温度", hwconfiginfo.u16AuxHeaterPTemp1.ToString());
            savehwconfig.WriteValue("辅助设置", "辅助2", hwconfiginfo.u16AuxHeater2.ToString());
            savehwconfig.WriteValue("辅助设置", "辅助名字2", hwconfiginfo.u16AuxHeaterName2);
            savehwconfig.WriteValue("检测单元", "后检测器保护温度", hwconfiginfo.u16AuxHeaterPTemp2.ToString());

            savehwconfig.EmptySection("仪器档次");
            savehwconfig.WriteValue("仪器档次", "仪器档次", hwconfiginfo.u16StartType.ToString());

            return true;
        }

    }
}
