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
{
///
/// 参数读取与存储
///
public partial class ConfigHelper
{
private static ConfigHelper _instance = null;
private static Dictionary> config_base = null;
private static XmlDocument xmldoc = null;
private string path;
public Dictionary> GetAlarmParam()
{
return config_base;
}
private ConfigHelper(string path)
{
//Initial();
//this.path = path;
//LoadConfigData(path);
}
///
/// 创建类
///
/// 设置类中的文件路径
///
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为机架号
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 itemlist = new List();
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.orderByStandnoR2L = false;
SysParam.Instance.orderByStandnoL2R = true;
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;
}
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";
}
}
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 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> 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(" ");
str = str.AppendLine("");
//senor config
str = str.Append(" ");
//display config
str = str.Append(" ");
//senor config
str = str.Append(" ");
//alarm config
str = str.AppendLine(" ");
foreach (KeyValuePair> 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(" ");
if (items.Value != null && items.Value.Count > 0)
{
foreach (EntityAlarmSpeed item in items.Value)
{
str = str.AppendLine(" ");
}
str = str.AppendLine(" ");
}
else
{
str = str.AppendLine(" ");
}
}
str = str.AppendLine(" ");
str = str.AppendLine(" ");
str = str.Append(" ");
str = str.AppendLine(" ");
str = str.AppendLine(" ");
str = str.AppendLine(" ");
str = str.AppendLine(" ");
str = str.AppendLine(" ");
str = str.AppendLine(" ");
str = str.AppendLine(" ");
str = str.AppendLine("");
write.WriteLine(str.ToString());
write.Close();
return true;
}
public bool SaveAlarmConfig(string path)
{
return SaveAlarmConfigData(config_base, path);
}
private bool SaveAlarmConfigData(Dictionary> datas, string path)
{
StreamWriter write = new StreamWriter(path, false, Encoding.GetEncoding("gb2312"));
StringBuilder str = new StringBuilder();
str = str.AppendLine(" ");
str = str.AppendLine("");
str = str.AppendLine(" ");
foreach (KeyValuePair> 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(" ");
if (items.Value != null && items.Value.Count > 0)
{
foreach (EntityAlarmSpeed item in items.Value)
{
str = str.AppendLine(" ");
}
str = str.AppendLine(" ");
}
else
{
str = str.AppendLine(" ");
}
}
str = str.AppendLine(" ");
str = str.AppendLine("");
write.WriteLine(str.ToString());
write.Close();
return true;
}
}
}