using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
using System.Drawing;
using CRVM.Entity;

namespace CRVM
{
    /// <summary>
    /// 参数读取与存储
    /// </summary>
    public partial class ConfigHelper
    {
        private static ConfigHelper _instance = null;
        private static Dictionary<int, IList<EntityAlarmSpeed>> config_base = null;
        private static XmlDocument xmldoc = null;
        private string path;
        public Dictionary<int, IList<EntityAlarmSpeed>> GetAlarmParam()
        {
            return config_base;
        }
        private ConfigHelper(string path)
        {
            //Initial();
            //this.path = path;
            //LoadConfigData(path);
        }
        /// <summary>
        /// 创建类
        /// </summary>
        /// <param name="path">设置类中的文件路径</param>
        /// <returns></returns>
        public static ConfigHelper GetInstance(string path)
        {
            if (_instance == null)
            {
                _instance = new ConfigHelper(path);
                _instance.Initial();
                _instance.path = path;
                _instance.LoadConfigData(path);
            }
            return _instance;
        }

        private void Initial()
        {
            config_base = new Dictionary<int, IList<EntityAlarmSpeed>>();//int为机架号
            xmldoc = new XmlDocument();
        }

        private void LoadConfigData(string path)
        {
            if (File.Exists(path))
            {
                try
                {
                    xmldoc.Load(path);
                    //sourceType = xmldoc.DocumentElement.Attributes[0].InnerText;
                    XmlNodeList nodes = xmldoc.SelectNodes("configuration/ALARM_PARAM");
                    int count = nodes.Count;
                    if (count > 0)
                    {
                        for (int i = 0; i < nodes[0].ChildNodes.Count; i++)
                        {
                            SysParam.Instance.channel[i].alarmTimes_l = Convert.ToInt32(nodes[0].ChildNodes[i].Attributes[0].InnerText);
                            SysParam.Instance.channel[i].alarmTimes_m = Convert.ToInt32(nodes[0].ChildNodes[i].Attributes[1].InnerText);
                            SysParam.Instance.channel[i].alarmTimes_h = Convert.ToInt32(nodes[0].ChildNodes[i].Attributes[2].InnerText);
                            IList<EntityAlarmSpeed> itemlist = new List<EntityAlarmSpeed>();
                            int inCount = nodes[0].ChildNodes[i].ChildNodes.Count;
                            for (int j = 0; j < inCount; j++)
                            {
                                EntityAlarmSpeed itemdata = new EntityAlarmSpeed();
                                itemdata.alarmSpeed = Convert.ToInt32(nodes[0].ChildNodes[i].ChildNodes[j].Attributes[0].InnerText);
                                itemdata.lowPassL = Convert.ToInt32(nodes[0].ChildNodes[i].ChildNodes[j].Attributes[1].InnerText);
                                itemdata.lowPassU = Convert.ToInt32(nodes[0].ChildNodes[i].ChildNodes[j].Attributes[2].InnerText);
                                itemdata.lowAlarmValue = Convert.ToSingle(nodes[0].ChildNodes[i].ChildNodes[j].Attributes[3].InnerText);
                                itemdata.midPassL = Convert.ToInt32(nodes[0].ChildNodes[i].ChildNodes[j].Attributes[4].InnerText);
                                itemdata.midPassU = Convert.ToInt32(nodes[0].ChildNodes[i].ChildNodes[j].Attributes[5].InnerText);
                                itemdata.midAlarmValue = Convert.ToSingle(nodes[0].ChildNodes[i].ChildNodes[j].Attributes[6].InnerText);
                                itemdata.highPassL = Convert.ToInt32(nodes[0].ChildNodes[i].ChildNodes[j].Attributes[7].InnerText);
                                itemdata.highPassU = Convert.ToInt32(nodes[0].ChildNodes[i].ChildNodes[j].Attributes[8].InnerText);
                                itemdata.highAlarmValue = Convert.ToSingle(nodes[0].ChildNodes[i].ChildNodes[j].Attributes[9].InnerText);
                                if (nodes[0].ChildNodes[i].ChildNodes[j].Attributes.Count > 10)
                                {
                                    itemdata.lowDefectValue = Convert.ToSingle(nodes[0].ChildNodes[i].ChildNodes[j].Attributes[10].InnerText);
                                    itemdata.midDefectValue = Convert.ToSingle(nodes[0].ChildNodes[i].ChildNodes[j].Attributes[11].InnerText);
                                    itemdata.highDefectValue = Convert.ToSingle(nodes[0].ChildNodes[i].ChildNodes[j].Attributes[12].InnerText);
                                }
                                else
                                {
                                    itemdata.lowDefectValue = 0.3f;
                                    itemdata.midDefectValue = 0.3f;
                                    itemdata.highDefectValue = 0.3f;
                                }
                                itemlist.Add(itemdata);
                            }
                            if (!config_base.ContainsKey(i))
                            {
                                config_base.Add(i, itemlist);
                            }
                        }
                    }

                    nodes = xmldoc.SelectNodes("configuration/SensorConfiguration");
                    count = nodes.Count;
                    if (count > 0)
                    {
                        for (int i = 0; i < SysParam.Instance.channelCount; i++)
                        {
                            SysParam.Instance.channel[i].vibSens = Convert.ToSingle(nodes[0].Attributes[i].InnerText);
                            SysParam.Instance.channel[i].speedSens = Convert.ToSingle(nodes[0].Attributes[8 + i].InnerText);
                        }
                        SysParam.Instance.distanceSens = Convert.ToSingle(nodes[0].Attributes[16].InnerText);
                        SysParam.Instance.samplePoint = Convert.ToInt32(nodes[0].Attributes[17].InnerText);
                        SysParam.Instance.sampleFre = Convert.ToInt32(nodes[0].Attributes[18].InnerText);
                    }

                    nodes = xmldoc.SelectNodes("configuration/LineConfiguration");
                    count = nodes.Count;
                    if (count > 0)
                    {
                        SysParam.Instance.colorTime = Color.FromArgb(Convert.ToInt32(nodes[0].Attributes[0].InnerText));
                        SysParam.Instance.colorFre = Color.FromArgb(Convert.ToInt32(nodes[0].Attributes[1].InnerText));
                        SysParam.Instance.colorEnergy = Color.FromArgb(Convert.ToInt32(nodes[0].Attributes[2].InnerText));
                        SysParam.Instance.colorSpeed = Color.FromArgb(Convert.ToInt32(nodes[0].Attributes[3].InnerText));
                        SysParam.Instance.colorAlarm = Color.FromArgb(Convert.ToInt32(nodes[0].Attributes[4].InnerText));
                        SysParam.Instance.colorDefect = Color.FromArgb(Convert.ToInt32(nodes[0].Attributes[5].InnerText));
                        SysParam.Instance.colorCut = Color.FromArgb(Convert.ToInt32(nodes[0].Attributes[6].InnerText));
                        SysParam.Instance.colorWorkRoll = Color.FromArgb(Convert.ToInt32(nodes[0].Attributes[7].InnerText));
                        SysParam.Instance.colorMidRoll = Color.FromArgb(Convert.ToInt32(nodes[0].Attributes[8].InnerText));
                        SysParam.Instance.colorBackupRoll = Color.FromArgb(Convert.ToInt32(nodes[0].Attributes[9].InnerText));
                        SysParam.Instance.colorFont = Color.FromArgb(Convert.ToInt32(nodes[0].Attributes[10].InnerText));
                        SysParam.Instance.colorBackgroud = Color.FromArgb(Convert.ToInt32(nodes[0].Attributes[11].InnerText));
                        SysParam.Instance.fontSize = Convert.ToInt32(nodes[0].Attributes[12].InnerText);
                        SysParam.Instance.BackgroundGridOn = change(nodes[0].Attributes[13].InnerText);
                        SysParam.Instance.rollLineVisible = change(nodes[0].Attributes[14].InnerText);
                    }

                    nodes = xmldoc.SelectNodes("configuration/BaseConfig");
                    count = nodes.Count;
                    if (count > 0)
                    {
                        SysParam.Instance.IP = nodes[0].Attributes[0].InnerText;
                        SysParam.Instance.Port = Convert.ToInt32(nodes[0].Attributes[1].InnerText);
                        if (nodes[0].Attributes[2].InnerText == "0")
                            SysParam.Instance.checkTXT = false;
                        else
                            SysParam.Instance.checkTXT = true;
                        if (nodes[0].Attributes[3].InnerText == "0")
                            SysParam.Instance.checkBIT = false;
                        else
                            SysParam.Instance.checkBIT = true;
                        //SysParamMag.Instance.path = nodes[0].Attributes[4].InnerText;
                        SysParam.Instance.savePath = nodes[0].Attributes[4].InnerText;
                        SysParam.Instance.saveDaysAlarm = Convert.ToInt32(nodes[0].Attributes[5].InnerText);
                        SysParam.Instance.saveDaysNoAlarm = Convert.ToInt32(nodes[0].Attributes[6].InnerText);
                    }
                    //获取在线钩选及参数设置 20151030  jxp
                    nodes = xmldoc.SelectNodes("configuration/ONLINE");
                    count = nodes.Count;
                    if (count > 0)
                    {
                        for (int i = 0; i < nodes[0].ChildNodes[0].Attributes.Count; i++)
                        {
                            if (nodes[0].ChildNodes[0].Attributes[i].InnerText == "True")
                            {
                                SysParam.Instance.channel[i].ChChecked = true;
                            }
                            else
                            {
                                SysParam.Instance.channel[i].ChChecked = false;
                            }
                        }
                        SysParam.Instance.orderByChannel = change(nodes[0].ChildNodes[3].Attributes[0].InnerText);
                        SysParam.Instance.orderBySingleColumn = change(nodes[0].ChildNodes[3].Attributes[1].InnerText);

                        SysParam.Instance.checkBoxEnergy = change(nodes[0].ChildNodes[2].ChildNodes[0].Attributes[0].InnerText);
                        SysParam.Instance.checkBoxEnergyAll = change(nodes[0].ChildNodes[2].ChildNodes[0].Attributes[1].InnerText);
                        SysParam.Instance.checkBoxEnergyL = change(nodes[0].ChildNodes[2].ChildNodes[0].Attributes[2].InnerText);
                        SysParam.Instance.checkBoxEnergyM = change(nodes[0].ChildNodes[2].ChildNodes[0].Attributes[3].InnerText);
                        SysParam.Instance.checkBoxEnergyH = change(nodes[0].ChildNodes[2].ChildNodes[0].Attributes[4].InnerText);
                        SysParam.Instance.checkBoxEnergySpeed = change(nodes[0].ChildNodes[2].ChildNodes[0].Attributes[5].InnerText);

                        SysParam.Instance.comboBoxEnergyAll = nodes[0].ChildNodes[2].ChildNodes[1].Attributes[0].InnerText;
                        SysParam.Instance.comboBoxEnergyL = nodes[0].ChildNodes[2].ChildNodes[1].Attributes[1].InnerText;
                        SysParam.Instance.comboBoxEnergyM = nodes[0].ChildNodes[2].ChildNodes[1].Attributes[2].InnerText;
                        SysParam.Instance.comboBoxEnergyH = nodes[0].ChildNodes[2].ChildNodes[1].Attributes[3].InnerText;
                        SysParam.Instance.comboBoxEnergySpeed = nodes[0].ChildNodes[2].ChildNodes[1].Attributes[4].InnerText;

                        SysParam.Instance.checkBoxTime = change(nodes[0].ChildNodes[1].ChildNodes[0].Attributes[0].InnerText);
                        SysParam.Instance.checkBoxTimeAll = change(nodes[0].ChildNodes[1].ChildNodes[0].Attributes[1].InnerText);
                        SysParam.Instance.checkBoxTimeL = change(nodes[0].ChildNodes[1].ChildNodes[0].Attributes[2].InnerText);
                        SysParam.Instance.checkBoxTimeM = change(nodes[0].ChildNodes[1].ChildNodes[0].Attributes[3].InnerText);
                        SysParam.Instance.checkBoxTimeH = change(nodes[0].ChildNodes[1].ChildNodes[0].Attributes[4].InnerText);
                        SysParam.Instance.checkBoxTimeFre = change(nodes[0].ChildNodes[1].ChildNodes[0].Attributes[5].InnerText);
                        SysParam.Instance.checkBoxTimeFilt = change(nodes[0].ChildNodes[1].ChildNodes[0].Attributes[6].InnerText);

                        SysParam.Instance.comboBoxTimeAll = nodes[0].ChildNodes[1].ChildNodes[1].Attributes[0].InnerText;
                        SysParam.Instance.comboBoxTimeL = nodes[0].ChildNodes[1].ChildNodes[1].Attributes[1].InnerText;
                        SysParam.Instance.comboBoxTimeM = nodes[0].ChildNodes[1].ChildNodes[1].Attributes[2].InnerText;
                        SysParam.Instance.comboBoxTimeH = nodes[0].ChildNodes[1].ChildNodes[1].Attributes[3].InnerText;
                        SysParam.Instance.comboBoxTimeFre = nodes[0].ChildNodes[1].ChildNodes[1].Attributes[4].InnerText;
                        SysParam.Instance.ComboBoxTimeFilt = nodes[0].ChildNodes[1].ChildNodes[1].Attributes[5].InnerText;
                        SysParam.Instance.textBoxf1 = nodes[0].ChildNodes[1].ChildNodes[1].Attributes[6].InnerText;
                        SysParam.Instance.textBoxf2 = nodes[0].ChildNodes[1].ChildNodes[1].Attributes[7].InnerText;
                        SysParam.Instance.reTimeSpan = nodes[0].ChildNodes[1].ChildNodes[1].Attributes[8].InnerText;

                        SysParam.Instance.orderByStandnoR2L = false;
                        SysParam.Instance.orderByStandnoL2R = true;
                    }
                    else
                    {
                        for (int i = 0; i < SysParam.Instance.channelCount; i++)//初始加载配置参数（在线状态） 20151031 jxp
                        {
                            SysParam.Instance.channel[i].ChChecked = false;
                        }
                        SysParam.Instance.channel[0].ChChecked = true;
                        SysParam.Instance.orderByChannel = true;
                        SysParam.Instance.orderBySingleColumn = true;
                        SysParam.Instance.checkBoxEnergy = true;
                        SysParam.Instance.checkBoxEnergyAll = true;
                        SysParam.Instance.checkBoxEnergyL = true;
                        SysParam.Instance.checkBoxEnergyM = true;
                        SysParam.Instance.checkBoxEnergyH = true;
                        SysParam.Instance.checkBoxEnergySpeed = true;

                        SysParam.Instance.comboBoxEnergyAll = "0.05";
                        SysParam.Instance.comboBoxEnergyL = "0.05";
                        SysParam.Instance.comboBoxEnergyM = "0.05";
                        SysParam.Instance.comboBoxEnergyH = "0.05";
                        SysParam.Instance.comboBoxEnergySpeed = "1500";

                        SysParam.Instance.checkBoxTime = false;
                        SysParam.Instance.checkBoxTimeAll = false;
                        SysParam.Instance.checkBoxTimeL = false;
                        SysParam.Instance.checkBoxTimeM = false;
                        SysParam.Instance.checkBoxTimeH = false;
                        SysParam.Instance.checkBoxTimeFre = false;
                        SysParam.Instance.checkBoxTimeFilt = false;

                        SysParam.Instance.comboBoxTimeAll = "0.05";
                        SysParam.Instance.comboBoxTimeL = "0.05";
                        SysParam.Instance.comboBoxTimeM = "0.05";
                        SysParam.Instance.comboBoxTimeH = "0.05";
                        SysParam.Instance.comboBoxTimeFre = "0.05";
                        //SysParam.Instance.ComboBoxE_r = "1500";//end 20151031 jxp
                        SysParam.Instance.ComboBoxTimeFilt = "0.05";
                    }

                    //获取报警铃声设置
                    nodes = xmldoc.SelectNodes("configuration/ALARMRING");
                    count = nodes.Count;
                    if (count > 0)
                    {
                        SysParam.Instance.isStand1Ring = change(nodes[0].ChildNodes[0].Attributes[0].InnerText);
                        SysParam.Instance.isStand2Ring = change(nodes[0].ChildNodes[1].Attributes[0].InnerText);
                        SysParam.Instance.isStand3Ring = change(nodes[0].ChildNodes[2].Attributes[0].InnerText);
                        SysParam.Instance.isStand4Ring = change(nodes[0].ChildNodes[3].Attributes[0].InnerText);
                        SysParam.Instance.isStand5Ring = change(nodes[0].ChildNodes[4].Attributes[0].InnerText);
                        SysParam.Instance.isStand6Ring = change(nodes[0].ChildNodes[5].Attributes[0].InnerText);
                        SysParam.Instance.ringFre = Convert.ToInt16(nodes[0].ChildNodes[6].Attributes[0].InnerText);
                        SysParam.Instance.ringType = Convert.ToInt16(nodes[0].ChildNodes[7].Attributes[0].InnerText);

                    }
                    else
                    {
                        SysParam.Instance.isStand1Ring = false;
                        SysParam.Instance.isStand2Ring = false;
                        SysParam.Instance.isStand3Ring = false;
                        SysParam.Instance.isStand4Ring = true;
                        SysParam.Instance.isStand5Ring = true;

                        SysParam.Instance.ringFre = 2;
                        SysParam.Instance.ringType = 0;
                    }

                }
                catch (XmlException xmlexp)
                {
                    throw new Exception("打开的项目配置文件错误，请检查配置文件: " + xmlexp.Message);
                }
            }
        }
        public bool change(string str)
        {
            if (str == "True")
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        public void InitialData()
        {

            SysParam.Instance.filePrepared = false;
            SysParam.Instance.fileCreated = false;
            //SysParam.Instance.textBox_f1 = "0";
            //SysParam.Instance.textBox_f2 = "2560";

            for (int i = 0; i < config_base.Keys.Count; i++)
            {
                //EntityAlarmSpeed[] eass = new EntityAlarmSpeed[config_base[i].Count];
                //int k = 0;
                //foreach (EntityAlarmSpeed item in config_base[i])
                //{
                //    eass[k] = item;
                //    k++;
                //}
                //SysParam.Instance.channel[i].alarmConfig = eass;
                SysParam.Instance.channel[i].alarmConfig = config_base[i].ToArray();
            }

        }

        public bool SaveBaseData(int standNo, IList<EntityAlarmSpeed> itemdatas)
        {
            if (config_base.ContainsKey(standNo))
            {

                config_base[standNo] = itemdatas;
            }
            else
            {

                config_base.Add(standNo, itemdatas);
            }
            return true;
        }

        public bool SaveConfig(string path)
        {
            return SaveAllConfigData(config_base, path, "AppSetting");
        }

        private bool SaveAllConfigData(Dictionary<int, IList<EntityAlarmSpeed>> datas, string path, string filename)
        {
            int flag_file = 0;
            if (File.Exists(path))
            {
                File.SetAttributes(path, FileAttributes.Normal);
                flag_file = 1;
            }
            if (flag_file == 0)
            {
                if (!Directory.Exists(path))
                    Directory.CreateDirectory(path);
            }
            else
            {
                int ind = path.LastIndexOf('\\');
                path = path.Substring(0, ind);
            }
            StreamWriter write = new StreamWriter(path + "\\" + filename + ".config", false, Encoding.GetEncoding("gb2312"));
            StringBuilder str = new StringBuilder();
            str = str.AppendLine("<?xml version=\"1.0\" encoding=\"GB2312\" ?> ");
            str = str.AppendLine("<configuration>");

            //senor config
            str = str.Append("  <SensorConfiguration ");
            string str1 = "";
            string str2 = "";
            for (int i = 0; i < SysParam.Instance.channelCount; i++)
            {
                str1 += " Sensor_TextBox" + (i + 1) + "=\"" + SysParam.Instance.channel[i].vibSens + "\" ";
                str2 += " TextBox_dj" + (i + 1) + "=\"" + SysParam.Instance.channel[i].speedSens + "\" ";

            }
            str = str.AppendLine(str1);
            str = str.AppendLine(" " + str2);
            string senorStr = " TextBox_distance=\"" + SysParam.Instance.distanceSens +
                        "\" Point=\"" + SysParam.Instance.samplePoint + "\" Freq=\"" + SysParam.Instance.sampleFre + "\" ";
            str = str.AppendLine(senorStr + "  /> ");

            //display config
            str = str.Append("  <LineConfiguration ");
            str = str.Append(" color_Time=\"" + SysParam.Instance.colorTime.ToArgb() + "\" color_Fre=\"" + SysParam.Instance.colorFre.ToArgb() + "\" color_Energy=\"" + SysParam.Instance.colorEnergy.ToArgb() + "\"");
            str = str.Append(" color_Speed=\"" + SysParam.Instance.colorSpeed.ToArgb() + "\" color_Alarm=\"" + SysParam.Instance.colorAlarm.ToArgb() + "\" color_Closed=\"" + SysParam.Instance.colorDefect.ToArgb() + "\"");
            str = str.Append(" color_Cut=\"" + SysParam.Instance.colorCut.ToArgb() + "\" color_WorkRoll=\"" + SysParam.Instance.colorWorkRoll.ToArgb() + "\" color_MidRoll=\"" + SysParam.Instance.colorMidRoll.ToArgb() + "\"");
            str = str.Append(" BackgroundColor=\"" + SysParam.Instance.colorBackupRoll.ToArgb() + "\" color_Font=\"" + SysParam.Instance.colorFont.ToArgb() + "\" color_Backgroud=\"" + SysParam.Instance.colorBackgroud.ToArgb() + "\"");
            str = str.AppendLine(" font_Size=\"" + SysParam.Instance.fontSize.ToString() + "\" BackgroundGridOn=\"" + SysParam.Instance.BackgroundGridOn + "\" rollLineVisible=\"" + SysParam.Instance.rollLineVisible + "\"/>");

            //senor config
            str = str.Append("  <BaseConfig ");
            string ifile = "";
            ifile += "ip=\"" + SysParam.Instance.IP + "\"";
            if (SysParam.Instance.Port <= 0)
                ifile += " port=\"8888\"";
            else
                ifile += " port=\"" + SysParam.Instance.Port + "\"";
            if (SysParam.Instance.checkTXT)
                ifile += " txt=\"1\" ";
            else
                ifile += " txt=\"0\" ";
            if (SysParam.Instance.checkBIT)
                ifile += " bit=\"1\" ";
            else
                ifile += " bit=\"0\" ";
            //ifile += "path=\"" + SysParamMag.Instance.path + "\"";
            ifile += "path=\"" + SysParam.Instance.savePath + "\" ";
            ifile += "   saveDaysAlarm=\"" + SysParam.Instance.saveDaysAlarm + "\"";
            ifile += "   saveDaysNoAlarm=\"" + SysParam.Instance.saveDaysNoAlarm + "\"";
            str = str.AppendLine(ifile + "  /> ");

            //alarm config
            str = str.AppendLine("  <ALARM_PARAM>");
            foreach (KeyValuePair<int, IList<EntityAlarmSpeed>> items in datas)
            {
                string times = " times_l=\"" + SysParam.Instance.channel[items.Key].alarmTimes_l + "\" times_m=\"" + SysParam.Instance.channel[items.Key].alarmTimes_m + "\" times_h=\"" + SysParam.Instance.channel[items.Key].alarmTimes_h + "\"";
                str = str.AppendLine("      <ALARM_FREQ " + times + ">");
                if (items.Value != null && items.Value.Count > 0)
                {
                    foreach (EntityAlarmSpeed item in items.Value)
                    {
                        str = str.AppendLine("      <AF alarmSpeed=\"" + item.alarmSpeed + "\"");
                        str = str.AppendLine("          lowPassL=\"" + item.lowPassL + "\" lowPassU=\"" + item.lowPassU + "\" lowAlarmValue=\"" + item.lowAlarmValue + "\"");
                        str = str.AppendLine("          midPassL=\"" + item.midPassL + "\" midPassU=\"" + item.midPassU + "\" midAlarmValue=\"" + item.midAlarmValue + "\"");
                        str = str.AppendLine("          highPassL=\"" + item.highPassL + "\" highPassU=\"" + item.highPassU + "\" highAlarmValue=\"" + item.highAlarmValue + "\"");
                        str = str.AppendLine("          lowDefectValue=\"" + item.lowDefectValue + "\" midDefectValue=\"" + item.midDefectValue + "\" highDefectValue=\"" + item.highDefectValue + "\" />");
                    }
                    str = str.AppendLine("      </ALARM_FREQ>");
                }
                else
                {
                    str = str.AppendLine("      </ALARM_FREQ>");
                }
            }

            str = str.AppendLine("  </ALARM_PARAM>");
            str = str.AppendLine("  <ONLINE>");
            str = str.Append("     <CHANNAEL  ");
            for (int i = 0; i < SysParam.Instance.channelCount; i++)
            {
                str = str.Append("channel" + (i + 1) + "State= " + "\"" + SysParam.Instance.channel[i].ChChecked.ToString() + "\"  ");
            }
            str = str.AppendLine(" />");
            str = str.AppendLine("     <TIME> ");
            str = str.AppendLine("          <CHECK  time=\"" + SysParam.Instance.checkBoxTime + "\"  ck_all=\"" + SysParam.Instance.checkBoxTimeAll + "\" ck_l=\"" + SysParam.Instance.checkBoxTimeL + "\"  ck_m=\"" + SysParam.Instance.checkBoxTimeM + "\"");
            str = str.AppendLine("                  ck_h=\"" + SysParam.Instance.checkBoxTimeH + "\"  ck_f=\"" + SysParam.Instance.checkBoxTimeFre + "\"  ck_r=\"" + SysParam.Instance.checkBoxTimeFilt + "\" />");
            str = str.AppendLine("          <Combo  co_all=\"" + SysParam.Instance.comboBoxTimeAll + "\" co_l=\"" + SysParam.Instance.comboBoxTimeL + "\"  co_m=\"" + SysParam.Instance.comboBoxTimeM + "\"");
            str = str.AppendLine("                  co_h=\"" + SysParam.Instance.comboBoxTimeH + "\"  co_f=\"" + SysParam.Instance.comboBoxTimeFre + "\"  co_r=\"" + SysParam.Instance.ComboBoxTimeFilt + "\"");
            str = str.AppendLine("                  f1=\"" + SysParam.Instance.textBoxf1 + "\"  f2=\"" + SysParam.Instance.textBoxf2 + "\"  reTimeSpan=\"" + SysParam.Instance.reTimeSpan + "\" />");
            str = str.AppendLine("     </TIME> ");
            str = str.AppendLine("     <ENERGY> ");
            str = str.AppendLine("          <CHECK  ennergy=\"" + SysParam.Instance.checkBoxEnergy + "\"  ck_all=\"" + SysParam.Instance.checkBoxEnergyAll + "\" ck_l=\"" + SysParam.Instance.checkBoxEnergyL + "\"  ck_m=\"" + SysParam.Instance.checkBoxEnergyM + "\"");
            str = str.AppendLine("                  ck_h=\"" + SysParam.Instance.checkBoxEnergyH + "\"  ck_r=\"" + SysParam.Instance.checkBoxEnergySpeed + "\" />");
            str = str.AppendLine("          <Combo  co_all=\"" + SysParam.Instance.comboBoxEnergyAll + "\" co_l=\"" + SysParam.Instance.comboBoxEnergyL + "\"  co_m=\"" + SysParam.Instance.comboBoxEnergyM + "\"");
            str = str.AppendLine("                  co_h=\"" + SysParam.Instance.comboBoxEnergyH + "\"  co_r=\"" + SysParam.Instance.comboBoxEnergySpeed + "\" />");
            str = str.AppendLine("     </ENERGY> ");
            str = str.AppendLine("     <ORDER       oder=\"" + SysParam.Instance.orderByChannel + "\" showset=\"" + SysParam.Instance.orderBySingleColumn + "\" />");
            str = str.AppendLine("  </ONLINE>");

            str = str.AppendLine("  <ALARMRING>");
            str = str.AppendLine("     <CHANNAEL1RING  channelState= " + "\"" + SysParam.Instance.isStand1Ring + "\"  />");
            str = str.AppendLine("     <CHANNAEL2RING  channelState= " + "\"" + SysParam.Instance.isStand2Ring + "\"  />");
            str = str.AppendLine("     <CHANNAEL3RING  channelState= " + "\"" + SysParam.Instance.isStand3Ring + "\"  />");
            str = str.AppendLine("     <CHANNAEL4RING  channelState= " + "\"" + SysParam.Instance.isStand4Ring + "\"  />");
            str = str.AppendLine("     <CHANNAEL5RING  channelState= " + "\"" + SysParam.Instance.isStand5Ring + "\"  />");
            str = str.AppendLine("     <CHANNAEL6RING  channelState= " + "\"" + SysParam.Instance.isStand6Ring + "\"  />");
            str = str.AppendLine("     <RINGFRE  ringFre= " + "\"" + SysParam.Instance.ringFre + "\"  />");
            str = str.AppendLine("     <RINGTYPE ringType= " + "\"" + SysParam.Instance.ringType + "\"  />");

            str = str.AppendLine("  </ALARMRING>");

            str = str.AppendLine("</configuration>");
            write.WriteLine(str.ToString());
            write.Close();
            return true;

        }
        public bool SaveAlarmConfig(string path)
        {
            return SaveAlarmConfigData(config_base, path);
        }

        private bool SaveAlarmConfigData(Dictionary<int, IList<EntityAlarmSpeed>> datas, string path)
        {
            StreamWriter write = new StreamWriter(path, false, Encoding.GetEncoding("gb2312"));
            StringBuilder str = new StringBuilder();
            str = str.AppendLine("<?xml version=\"1.0\" encoding=\"GB2312\" ?> ");
            str = str.AppendLine("<configuration>");
            str = str.AppendLine("  <ALARM_PARAM>");
            foreach (KeyValuePair<int, IList<EntityAlarmSpeed>> items in datas)
            {
                string times = " times_l=\"" + SysParam.Instance.channel[items.Key].alarmTimes_l + "\" times_m=\"" + SysParam.Instance.channel[items.Key].alarmTimes_m + "\" times_h=\"" + SysParam.Instance.channel[items.Key].alarmTimes_h + "\"";
                str = str.AppendLine("      <ALARM_FREQ " + times + ">");
                if (items.Value != null && items.Value.Count > 0)
                {
                    foreach (EntityAlarmSpeed item in items.Value)
                    {
                        str = str.AppendLine("      <AF alarmSpeed=\"" + item.alarmSpeed + "\"");
                        str = str.AppendLine("          lowPassL=\"" + item.lowPassL + "\" lowPassU=\"" + item.lowPassU + "\" lowAlarmValue=\"" + item.lowAlarmValue + "\"");
                        str = str.AppendLine("          midPassL=\"" + item.midPassL + "\" midPassU=\"" + item.midPassU + "\" midAlarmValue=\"" + item.midAlarmValue + "\"");
                        str = str.AppendLine("          highPassL=\"" + item.highPassL + "\" highPassU=\"" + item.highPassU + "\" highAlarmValue=\"" + item.highAlarmValue + "\"");
                        str = str.AppendLine("          lowDefectValue=\"" + item.lowDefectValue + "\" midDefectValue=\"" + item.midDefectValue + "\" highDefectValue=\"" + item.highDefectValue + "\" />");
                    }
                    str = str.AppendLine("      </ALARM_FREQ>");
                }
                else
                {
                    str = str.AppendLine("      </ALARM_FREQ>");
                }
            }

            str = str.AppendLine("  </ALARM_PARAM>");
            str = str.AppendLine("</configuration>");
            write.WriteLine(str.ToString());
            write.Close();
            return true;
        }
    }
}
