using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using GcDevicePc.Common;
using Ini.Net;

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

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

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

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

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


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

        public HWConfigInfo hwconfiginfo;

        private IniFile HwConfigFile;
        private String _filename;

        public bool fileflag = false;

        public void HConfigInit()
        {
            hwconfiginfo.GCNum = "GC001";                  
            hwconfiginfo.GCName = "VocHMI";
            hwconfiginfo.u16Col = 0;
            hwconfiginfo.u16ColType = 0;
            hwconfiginfo.u16ColPTemp = 0;
            hwconfiginfo.u16ColCtl = 0;
            hwconfiginfo.u16InjPortNum = 0;
            hwconfiginfo.u16InjPortFront = 0;
            hwconfiginfo.u16InjPortFPTemp = 0;
            hwconfiginfo.u16InjPortBehind = 0;
            hwconfiginfo.u16InjPortBPTemp = 0;
            hwconfiginfo.u16InjPortType_Front = 0;   
            hwconfiginfo.u16InjPortCtlMode_Front = 0;
            hwconfiginfo.u16InjPortLoadGas_Front = 0;
            hwconfiginfo.u16InjPortType_Behind = 0;
            hwconfiginfo.u16InjPortCtlMode_Behind = 0;
            hwconfiginfo.u16InjPortLoadGas_Behind = 0;
            hwconfiginfo.u16ValveBox = 0;
            hwconfiginfo.u16ValveOne = 0;
            hwconfiginfo.u16ValveTwo = 0;
            hwconfiginfo.u16ValveThree = 0;
            hwconfiginfo.u16ValveFour = 0;
            hwconfiginfo.u16ValveFive = 0;
            hwconfiginfo.u16ValveSix = 0;
            hwconfiginfo.u16DetNum = 0;
            hwconfiginfo.u16DetFront = 0;
            hwconfiginfo.u16DetFrontType = 0;
            hwconfiginfo.u16DetFrontPTemp = 0;
            hwconfiginfo.u16DetFrontAisle = 0;   
            hwconfiginfo.u16DetInter = 0;
            hwconfiginfo.u16DetInterType = 0;
            hwconfiginfo.u16DetInterPTemp = 0;
            hwconfiginfo.u16DetInterAisle = 0;
            hwconfiginfo.u16DetBehind = 0;
            hwconfiginfo.u16DetBehindType = 0;   
            hwconfiginfo.u16DetBehindPTemp = 0;  
            hwconfiginfo.u16DetBehindAisle = 0;
            hwconfiginfo.u16SampleNum = 0;
            hwconfiginfo.u16SampleFront = 0;
            hwconfiginfo.u16SampleFrontType = 0;
            hwconfiginfo.u16SampleBehind = 0;
            hwconfiginfo.u16SampleBehindType = 0;
            hwconfiginfo.u16AuxHeater = 0;
            hwconfiginfo.u16AuxHeaterNum = 0;
            hwconfiginfo.u16AuxHeater1 = 0;
            hwconfiginfo.u16AuxHeaterName1 = "辅助1";
            hwconfiginfo.u16AuxHeaterPTemp1 = 0;
            hwconfiginfo.u16AuxHeater2 = 0;      
            hwconfiginfo.u16AuxHeaterName2 = "辅助2";
            hwconfiginfo.u16AuxHeaterPTemp2 = 0;
            hwconfiginfo.u16ChromNum = 0;
            hwconfiginfo.u16ChromFront = 0;
            hwconfiginfo.u16ChromFrontType = 0;
            hwconfiginfo.u16ChromBehindType = 0;
            hwconfiginfo.u16ChromBehind = 0;
            hwconfiginfo.u16StartType = 0;
        }

        public HWConfig(string folder, string filename)
        {
            _filename = folder + "\\" + filename;

            try
            {
                if (!File.Exists(_filename))
                {
                    StreamWriter sw = new StreamWriter(_filename, false, Encoding.Unicode);
                    sw.WriteLine("[仪器编号]");
                    sw.Close();
                    fileflag = true;
                    HConfigInit();
                }
                else
                {
                    fileflag = true;
                }
            }
            catch (Exception e)
            {
                Log.Error(e.Message);
            }

            if (fileflag)
                LoadStatus();
        }

        public HWConfig()
        {

        }

        public bool setFileName(string filename)
        {
            _filename = filename;

            if (File.Exists(_filename))
            {
                fileflag = true;
                if (fileflag)
                    LoadStatus();

                return true;
            }
            else
            {
                return false;
            }
        }


        public void LoadStatus()
        {
            HwConfigFile = new IniFile(_filename);
            GetHWConfig();
        }

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

            sAppName = "仪器编号";
            hwconfiginfo.GCNum = HwConfigFile.ReadString(sAppName, "仪器编码");
            hwconfiginfo.GCName = HwConfigFile.ReadString(sAppName, "仪器名称");

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

            hwconfiginfo.u16InjPortBehind = HwConfigFile.ReadInteger(sAppName, "后进样口");

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

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

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

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


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

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

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

            return true;
        }

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

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

            HwConfigFile.WriteInteger("进样单元进样器", "进样器数量", hwconfiginfo.u16SampleNum);
            HwConfigFile.WriteInteger("进样单元进样器", "前进样器", hwconfiginfo.u16SampleFront);
            HwConfigFile.WriteInteger("进样单元进样器", "前进样器启动方式", hwconfiginfo.u16SampleFrontType);
            HwConfigFile.WriteInteger("进样单元进样器", "后进样器", hwconfiginfo.u16SampleBehind);
            HwConfigFile.WriteInteger("进样单元进样器", "后进样器启动方式", hwconfiginfo.u16SampleBehindType);

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

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

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

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

            HwConfigFile.WriteInteger("辅助设置", "辅助加热", hwconfiginfo.u16AuxHeater);
            HwConfigFile.WriteInteger("辅助设置", "辅助加热器数量", hwconfiginfo.u16AuxHeaterNum);

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

            HwConfigFile.WriteInteger("仪器档次", "仪器档次", hwconfiginfo.u16StartType);

            return true;
        }

    }
}
