using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Xml; namespace Mesnac.Equips.Connection.GroupNet { public partial class SetConfigForm : Form, Mesnac.Equips.Connection.ConnConfig { private Dictionary dicItem = new Dictionary(); #region 单例模式 private static SetConfigForm _this; public static SetConfigForm Instance { get { if (null == _this) _this = new SetConfigForm(); return _this; } } #endregion public SetConfigForm() { InitializeComponent(); } ConnType thisConnType = new ConnType(); #region SetConfig GetConfig public Connection.ConnType SetConfig(Connection.ConnType connType) { ConnType Result = new ConnType(); if (connType != null) { Result = (ConnType)connType; } try { if (!String.IsNullOrEmpty(Result.IPList)) { string[] strItems = Result.IPList.Split(new char[] { ';' }); if (null != strItems) { foreach (string strItem in strItems) { if (null != strItem) { string[] items = strItem.Split(new char[] { ':' }); this.dicItem.Add(items[0], items[1]); ConnItem item = new ConnItem(items[0], items[1]); this.lstIP.Items.Add(item); } } } } } catch(Exception ex) { MessageBox.Show("nodeDevice.xml设备格式配置错误:" + ex.Message); } this.ShowDialog(); return thisConnType; } 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; } public Connection.ConnType GetConfig(XmlNode node) { ConnType Result = new ConnType(); try { string ss = string.Empty; GetFirstNodeValue(node, ".//IPList[@value]", "value", true, out ss); Result.IPList = ss; } catch(Exception ex) { MessageBox.Show("nodeDevice.xml中设备配置错误:" + ex.Message); } return Result; } #endregion private void btnAdd_Click(object sender, EventArgs e) { if (!this.dicItem.ContainsKey(this.txtEquipCode.Text)) { this.dicItem.Add(this.txtEquipCode.Text, this.txtIP.Text); ConnItem item = new ConnItem(); item.EquipCode = this.txtEquipCode.Text; item.IP = this.txtIP.Text; this.lstIP.Items.Add(item); } else { MessageBox.Show("列表中已存在此机台!"); } } private void btnRemove_Click(object sender, EventArgs e) { ConnItem item = this.lstIP.SelectedItem as ConnItem; this.lstIP.Items.Remove(this.lstIP.SelectedItem); if (this.dicItem.ContainsKey(item.EquipCode)) { this.dicItem.Remove(item.EquipCode); } } private void btnOk_Click(object sender, EventArgs e) { try { StringBuilder sb = new StringBuilder(); foreach(ConnItem item in this.lstIP.Items) { if (String.IsNullOrEmpty(sb.ToString())) { sb.Append(String.Format("{0}:{1}", item.EquipCode, item.IP)); } else { sb.Append(";").Append(String.Format("{0}:{1}", item.EquipCode, item.IP)); } } this.thisConnType.IPList = sb.ToString(); this.Close(); } catch (Exception ex) { MessageBox.Show(ex.Message); } } } public class ConnType : Mesnac.Equips.Connection.ConnType { public ConnType() { this.IPList = String.Empty; } public string IPList { get; set; } } /// /// 连接类型项目类 /// [Serializable] public class ConnItem { public ConnItem() { } public ConnItem(string equipCode, string ip) { this.EquipCode = equipCode; this.IP = ip; } /// /// 机台号 /// public string EquipCode { get; set; } /// /// IP地址 /// public string IP { get; set; } public override string ToString() { return String.Format("{0}:{1}", this.EquipCode, this.IP); } } }