using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.IO; using System.Text; using System.Xml; using System.Windows.Forms; using System.Reflection; namespace Mesnac.Equips.SetConfig.DataConfig { public partial class NewEquipForm : Form { private bool isNew = false; private ConfigEquipFactory configEquipFactory = null; public NewEquipForm(ConfigEquipFactory configEquipFactory, bool isNew) { this.isNew = isNew; this.configEquipFactory = configEquipFactory; InitializeComponent(); } private void NewEquip_Load(object sender, EventArgs e) { IniFormComboBox(); } private void NewEquip_Shown(object sender, EventArgs e) { IniDefault(); } private class CanUseEquip { public string BrandName { get; set; } public string BrandCaption { get; set; } public string ModelName { get; set; } public string ModelCaption { get; set; } public string ConnName { get; set; } public string ConnCaption { get; set; } public string Remark { get; set; } public string AssemblyFile { get; set; } } private class Item { public string Key { get; set; } public string Value { get; set; } public CanUseEquip Equip { get; set; } public Item(string key, string value, CanUseEquip equip) { this.Key = key; this.Value = value; this.Equip = equip; } public Item(string key, string value) { this.Key = key; this.Value = value; this.Equip = null; } public override string ToString() { return this.Value; } } private bool GetFirstNodeValue(XmlNode node, string path, string attribute, bool throwException, out string value) { value = string.Empty; try { value = node.SelectNodes(path)[0].Attributes[attribute.ToLower()].Value.ToString(); } catch (Exception ex) { if (throwException) { throw (ex); } return false; } return true; } private bool GetNodeAttributeValue(XmlNode node, string attribute, bool throwException, out string value) { value = string.Empty; try { value = node.Attributes[attribute.ToLower()].Value.ToString(); } catch (Exception ex) { if (throwException) { throw (ex); } return false; } return true; } private List CanUseEquipList = new List(); private bool SameEquipName(string name) { foreach (BaseEquip equip in this.configEquipFactory.AllEquips.Values) { if (equip.Name.Equals(name)) { if (isNew) { return true; } else { if (equip.Name != this.configEquipFactory.CurrentEquip.Name) { return true; } } } } return false; } private void IniCanUseEquipByReflection() { string thisPath = this.GetType().Assembly.Location; List files = new List(); DirectoryInfo dirTop = new DirectoryInfo(Path.GetDirectoryName(thisPath) + "\\Data\\DeviceConfig\\Mesnac.Equips"); if (!dirTop.Exists) { return; } foreach (DirectoryInfo equipDir in dirTop.GetDirectories()) { foreach (FileInfo equipFile in equipDir.GetFiles()) { try { if (equipFile.Extension.ToLower() != ".dll") { continue; } Assembly ass = Assembly.LoadFile(equipFile.FullName); if (ass == null) { continue; } foreach (Type type in ass.GetTypes()) { if ((typeof(BaseEquip).IsAssignableFrom(type)) && (type.Name == "Equip")) { string fullName = type.Namespace; string ss = "Mesnac.Equip."; if (!fullName.StartsWith(ss)) { continue; } fullName = fullName.Substring(ss.Length); int index = 0; CanUseEquip canequip = new CanUseEquip(); canequip.AssemblyFile = equipFile.FullName; #region BrandName index = fullName.IndexOf("."); if (index < 0) { continue; } ss = fullName.Substring(0, index); fullName = fullName.Substring(index + 1); canequip.BrandName = ss; canequip.BrandCaption = ss; #endregion #region ModelName index = fullName.IndexOf("."); if (index < 0) { continue; } ss = fullName.Substring(0, index); fullName = fullName.Substring(index + 1); canequip.ModelName = ss; canequip.ModelCaption = ss; #endregion #region ConnName ss = fullName; canequip.ConnName = ss; canequip.ConnCaption = ss; #endregion CanUseEquipList.Add(canequip); } } } catch (Exception ee) { Console.WriteLine(ee.Message); Console.WriteLine(ee.StackTrace); } } } } private void IniFormComboBox() { int i = 0; while (true) { i++; this.textBox1.Text = "A" + i.ToString(); if (!SameEquipName(this.textBox1.Text)) { break; } } IniCanUseEquipByReflection(); comboBox1.Items.Clear(); comboBox1.Items.Add(new Item(_selectDefault, _selectDefault)); foreach (CanUseEquip equip in CanUseEquipList) { bool isExists = false; foreach (object item in comboBox1.Items) { if (((Item)item).Key == equip.BrandName) { isExists = true; break; } } if (!isExists) { comboBox1.Items.Add(new Item(equip.BrandName, equip.BrandCaption)); } } if (comboBox1.Items.Count > 0) { comboBox1.SelectedIndex = 0; } } private void SetComboBox(ComboBox cb, string key) { for (int i = 0; i < cb.Items.Count; i++) { if (((Item)cb.Items[i]).Key == key) { cb.SelectedIndex = i; return; } } } private void IniDefault() { if (!isNew && this.configEquipFactory.CurrentEquip != null) { this.ConnType = this.configEquipFactory.CurrentEquip.Main.ConnType; this.textBox1.Text = this.configEquipFactory.CurrentEquip.Name; #region 郑立兵于2015-01-19添加,用于设置和显示设备群组的设置 this.cbGroup.Checked = this.configEquipFactory.CurrentEquip.IsEnableGroup; this.txtRange.Text = this.configEquipFactory.CurrentEquip.GroupRange; #endregion this.textBox2.Text = this.configEquipFactory.CurrentEquip.Main.ReadHz.ToString(); SetComboBox(this.comboBox1, this.configEquipFactory.CurrentEquip.Main.Brand); SetComboBox(this.comboBox2, this.configEquipFactory.CurrentEquip.Main.Model); SetComboBox(this.comboBox3, this.configEquipFactory.CurrentEquip.Main.ConnType.Name); if (ConnType is Mesnac.Equips.Connection.Default.ConnType) { var type=ConnType as Mesnac.Equips.Connection.Default.ConnType; this.txt_num.Text = type.IP; } } } private readonly string _selectDefault = "------请选择------"; private Mesnac.Equips.Connection.ConnType ConnType; private void button1_Click(object sender, EventArgs e) { #region 数据校验 string sModel = string.Empty; try { sModel = listViewEquip.SelectedItems[0].Text.Trim().Replace(_selectDefault, string.Empty); } catch { } if (this.isNew) { if (string.IsNullOrWhiteSpace(sModel)) { //MessageBox.Show("请选择模版!"); //return; } } if (SameEquipName(textBox1.Text.Trim())) { MessageBox.Show("设备名称重复!"); return; } if (string.IsNullOrWhiteSpace(textBox1.Text.Trim())) { MessageBox.Show("请输入名称!"); return; } if (this.cbGroup.Checked) { if (!EquipTools.CheckRuleColon(this.txtRange.Text) && !EquipTools.CheckRuleJoin(this.txtRange.Text)) { MessageBox.Show("设备群组范围格式设置不正确!\r\n请参照以下格式:\r\n1、1:2:3:4:5\r\n或者\r\n1..5", "提示"); return; } } if ((comboBox1.SelectedItem == null) || (string.IsNullOrWhiteSpace(((Item)comboBox1.SelectedItem).Key.Replace(_selectDefault, string.Empty)))) { MessageBox.Show("请选择品牌!"); return; } if ((comboBox2.SelectedItem == null) || (string.IsNullOrWhiteSpace(((Item)comboBox2.SelectedItem).Key.Replace(_selectDefault, string.Empty)))) { MessageBox.Show("请选择类型!"); return; } if (ConnType == null) { MessageBox.Show("请选择连接方式!"); return; } if (ConnType is Mesnac.Equips.Connection.Default.ConnType && string.IsNullOrWhiteSpace(txt_num.Text.Trim())) { MessageBox.Show("请输入站号!"); return; } else { var type = (Mesnac.Equips.Connection.Default.ConnType)ConnType; type.IP = txt_num.Text.Trim(); ConnType.IP = txt_num.Text.Trim(); } int iReadHz = 1000; if (!int.TryParse(textBox2.Text, out iReadHz)) { MessageBox.Show("请填写正确的读取频率!"); return; } if (!isNew && this.configEquipFactory.CurrentEquip != null) { this.configEquipFactory.AllEquips.Remove(this.configEquipFactory.CurrentEquip.Name); } #endregion #region 数据复制 #region 使用模版 Dictionary configEquipList = new Dictionary(); if (!string.IsNullOrWhiteSpace(sModel)) //模版 { try { string _xmlPath = Path.Combine(sModel, "Equip.config"); ConfigEquipFactory factory = new ConfigEquipFactory(_xmlPath, "模板"); factory.ReadConfig(); configEquipList = factory.AllEquips; } catch { } } if (configEquipList.Count > 0) //使用模版 { foreach (ConfigEquip equip in configEquipList.Values) { this.configEquipFactory.CurrentEquip = equip; break; } this.configEquipFactory.CurrentEquip.Project = this.configEquipFactory.ProjectWizardName; this.configEquipFactory.CurrentEquip.Name = textBox1.Text.Trim(); this.configEquipFactory.CurrentEquip.IsEnableGroup = this.cbGroup.Checked; if (this.cbGroup.Checked) { this.configEquipFactory.CurrentEquip.GroupRange = this.txtRange.Text.Trim(); } else { this.configEquipFactory.CurrentEquip.GroupRange = String.Empty; } this.configEquipFactory.CurrentEquip.Main.Brand = ((Item)comboBox1.SelectedItem).Key.Replace(_selectDefault, string.Empty).Trim(); this.configEquipFactory.CurrentEquip.Main.Model = ((Item)comboBox2.SelectedItem).Key.Replace(_selectDefault, string.Empty).Trim(); this.configEquipFactory.CurrentEquip.Main.ReadHz = Convert.ToInt32(textBox2.Text.Trim()); this.configEquipFactory.CurrentEquip.Main.ConnType = ConnType; } #endregion #region 不使用模版 else { if (this.isNew) { this.configEquipFactory.CurrentEquip = new ConfigEquip(); } this.configEquipFactory.CurrentEquip.Project = this.configEquipFactory.ProjectWizardName; this.configEquipFactory.CurrentEquip.Name = textBox1.Text.Trim(); this.configEquipFactory.CurrentEquip.IsEnableGroup = this.cbGroup.Checked; if (this.cbGroup.Checked) { this.configEquipFactory.CurrentEquip.GroupRange = this.txtRange.Text.Trim(); } else { this.configEquipFactory.CurrentEquip.GroupRange = String.Empty; } this.configEquipFactory.CurrentEquip.Main.Brand = ((Item)comboBox1.SelectedItem).Key.Replace(_selectDefault, string.Empty).Trim(); this.configEquipFactory.CurrentEquip.Main.Model = ((Item)comboBox2.SelectedItem).Key.Replace(_selectDefault, string.Empty).Trim(); this.configEquipFactory.CurrentEquip.Main.ReadHz = Convert.ToInt32(textBox2.Text.Trim()); this.configEquipFactory.CurrentEquip.Main.ConnType = ConnType; } this.configEquipFactory.AllEquips.Add(this.configEquipFactory.CurrentEquip.Name, this.configEquipFactory.CurrentEquip); #endregion #endregion this.DialogResult = System.Windows.Forms.DialogResult.OK; this.Close(); } private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) { string brand = string.Empty; brand = ((Item)comboBox1.SelectedItem).Key.Replace(_selectDefault, string.Empty); comboBox2.Items.Clear(); comboBox3.Items.Clear(); if (string.IsNullOrWhiteSpace(brand)) { return; } comboBox2.Items.Add(new Item(_selectDefault, _selectDefault)); foreach (CanUseEquip equip in CanUseEquipList) { bool isExists = false; if (equip.BrandName == brand) { foreach (object item in comboBox2.Items) { if (((Item)item).Key == equip.ModelName) { isExists = true; break; } } if (!isExists) { comboBox2.Items.Add(new Item(equip.ModelName, equip.ModelCaption)); } } } if (comboBox2.Items.Count > 0) { comboBox2.SelectedIndex = 0; } } private void comboBox2_SelectedIndexChanged(object sender, EventArgs e) { string brand = string.Empty; brand = ((Item)comboBox1.SelectedItem).Key.Replace(_selectDefault, string.Empty); string type = string.Empty; type = ((Item)comboBox2.SelectedItem).Key.Replace(_selectDefault, string.Empty); comboBox3.Items.Clear(); if (string.IsNullOrWhiteSpace(brand)) { return; } if (string.IsNullOrWhiteSpace(brand)) { return; } comboBox3.Items.Add(new Item(_selectDefault, _selectDefault)); foreach (CanUseEquip equip in CanUseEquipList) { bool isExists = false; if (equip.BrandName == brand && equip.ModelName == type) { foreach (object item in comboBox3.Items) { if (((Item)item).Key == equip.ConnName) { isExists = true; break; } } if (!isExists) { comboBox3.Items.Add(new Item(equip.ConnName, equip.ConnCaption, equip)); } } } if (comboBox3.Items.Count > 0) { comboBox3.SelectedIndex = 0; } ConnType = null; } private void comboBox3_SelectedIndexChanged(object sender, EventArgs e) { ConnType = null; listViewEquip.Items.Clear(); getConnType(false); if (ConnType == null) { return; } string brand = string.Empty; brand = ((Item)comboBox1.SelectedItem).Key.Replace(_selectDefault, string.Empty); string type = string.Empty; type = ((Item)comboBox2.SelectedItem).Key.Replace(_selectDefault, string.Empty); string conn = string.Empty; Item item = (Item)comboBox3.SelectedItem; conn = item.Key.Replace(_selectDefault, string.Empty); ListViewItem listItem = listViewEquip.Items.Add(_selectDefault); listItem.SubItems.Add("0"); listItem.SubItems.Add(_selectDefault); if (item.Equip == null) { return; } if (string.IsNullOrWhiteSpace(this.configEquipFactory.ProjectWizardName)) { return; } //string thisPath = item.Equip.AssemblyFile; string thisPath = this.GetType().Assembly.Location; string path = Path.Combine(Path.GetDirectoryName(thisPath), "Data\\DeviceConfig\\Mesnac.Equips", "Mesnac.Equip." + brand, "EquipConfig." + type, this.configEquipFactory.ProjectWizardName); DirectoryInfo dirTop = new DirectoryInfo(path); if (!dirTop.Exists) { return; } DirectoryInfo[] dirs = (dirTop).GetDirectories(); int index = 0; foreach (DirectoryInfo dir in dirs) { index++; ListViewItem _item = listViewEquip.Items.Add(dir.FullName); _item.SubItems.Add(index.ToString()); _item.SubItems.Add(dir.Name); } } private void getConnType(bool get) { string conn = string.Empty; conn = ((Item)comboBox3.SelectedItem).Key.Replace(_selectDefault, string.Empty); if (string.IsNullOrWhiteSpace(conn)) { ConnType = null; return; } else { try { ConnType = new Mesnac.Equips.Connection.Default.ConnType(); //if (conn == this.configEquipFactory.CurrentEquip.Main.ConnType.Name) //{ // ConnType = this.configEquipFactory.CurrentEquip.Main.ConnType; //} } catch { } } if (ConnType == null || get) { //ConnType = Mesnac.Equips.Connection.Factory.Instance.SetConfig(conn, ConnType); } } private void button2_Click(object sender, EventArgs e) { getConnType(true); } private void textBox2_KeyPress(object sender, KeyPressEventArgs e) { //判断按键是不是要输入的类型。 if (((int)e.KeyChar < 48 || (int)e.KeyChar > 57) && (int)e.KeyChar != 8 && (int)e.KeyChar != 46) { e.Handled = true; } } #region 郑立兵于2015-01-19添加,扩展设备群组控制 /// /// 启用设备群组 /// /// /// private void cbGroup_CheckedChanged(object sender, EventArgs e) { this.lblRange.Visible = this.cbGroup.Checked; this.txtRange.Visible = this.cbGroup.Checked; } #endregion } }