NodeManager.cs 7.6 KB
using Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DeviceLibrary.manager;
using System.Windows.Forms;
using System.Drawing;
using static DeviceLibrary.Node;

namespace DeviceLibrary.manager
{
    public class NodeManager
    {
        /// <summary>
        /// 节点信息
        /// </summary>
        public static List<Node> nodeInfo;

        public static void InitNodesInfos()
        {
            nodeInfo = new List<Node>();
            XmlConfigOperation.LoadNodeInfos(nodeInfo);
            WriteAllNodeInfos();
        }
        public static void InitView(DataGridView DgvNode)
        {
            BindingSource bindingSource = new BindingSource();
            foreach (Node node in NodeManager.nodeInfo)
            {
                //if (!node.Type.Equals(NodeType.Node))
                //    continue;
                bindingSource.Add(node);
            }
            DgvNode.AutoGenerateColumns = false;
            DgvNode.AutoSize = true;
            DgvNode.DataSource = bindingSource;
            DgvNode.RowPostPaint += delegate {
                DgvNode.RowsDefaultCellStyle.BackColor = Color.White;
                DgvNode.AlternatingRowsDefaultCellStyle.BackColor = Color.LightBlue;
            };
            DgvNode.Columns.Add(new DataGridViewCheckBoxColumn() { HeaderText = "启用", DataPropertyName = "IsUse" });
            //DgvNode.Columns.Add(new DataGridViewTextBoxColumn() { HeaderText = "编号", DataPropertyName = "Id" });
            DgvNode.Columns.Add(new DataGridViewTextBoxColumn() { HeaderText = "名称", DataPropertyName = "Name" });
            DgvNode.Columns.Add(new DataGridViewTextBoxColumn() { HeaderText = "别名", DataPropertyName = "AliceName" });
            DgvNode.Columns.Add(new DataGridViewTextBoxColumn() { HeaderText = "车间", DataPropertyName = "Workshop" });
            DgvNode.Columns.Add(new DataGridViewTextBoxColumn() { HeaderText = "IP", DataPropertyName = "IP" });
            DgvNode.Columns.Add(new DataGridViewTextBoxColumn() { HeaderText = "动作", DataPropertyName = "nodeStatus" });
            DgvNode.Columns.Add(new DataGridViewTextBoxColumn() { HeaderText = "等级", DataPropertyName = "ClientLevel" });
            DgvNode.Columns.Add(new DataGridViewTextBoxColumn() { HeaderText = "RFID", DataPropertyName = "RFID" });
            DgvNode.Columns.Add(new DataGridViewTextBoxColumn() { HeaderText = "IO", DataPropertyName = "IOState" });
            DgvNode.Columns.Add(new DataGridViewTextBoxColumn() { HeaderText = "状态", DataPropertyName = "Online" });
            DgvNode.RowsDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            //int n;
            //DgvNode.Columns.Add(new DataGridViewTextBoxColumn() { HeaderText = "名称"});
            //DgvNode.Columns.Add(new DataGridViewTextBoxColumn() { HeaderText = "IP" });
            //DgvNode.Columns.Add(new DataGridViewTextBoxColumn() { HeaderText = "动作" });
            //DgvNode.Columns.Add(new DataGridViewTextBoxColumn() { HeaderText = "等级" });
            //DgvNode.Columns.Add(new DataGridViewTextBoxColumn() { HeaderText = "RFID" });
            //DgvNode.Columns.Add(new DataGridViewTextBoxColumn() { HeaderText = "连接" });
            //DgvNode.Columns.Add(new DataGridViewTextBoxColumn() { HeaderText = "IO" });
            //DgvNode.Columns.Add(new DataGridViewButtonColumn() { HeaderText = "启用" });
            //DgvNode.RowsDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            //for (int i = 0; i < NodeManager.nodeInfo.Count; i++)
            //{
            //    //if (!NodeManager.nodeInfo[i].Type.Equals(NodeType.Node))
            //    //    continue;
            //    n = DgvNode.Rows.Add(NodeManager.nodeInfo[i].ToRow());
            //    DgvNode.Rows[n].HeaderCell.Value = (n + 1).ToString();
            //    if (i % 2 == 0)
            //        DgvNode.Rows[n].DefaultCellStyle.BackColor = Color.LightBlue;
            //    if (!NodeManager.nodeInfo[i].Online)
            //        DgvNode.Rows[n].DefaultCellStyle.ForeColor = Color.Red;
            //}
        }

        public static void UpdateDataSource(DataGridView dataGridView,int curRow=0,int curColum=0)
        {
            BindingSource bindingSource = new BindingSource();
            foreach (Node node in NodeManager.nodeInfo)
            {
                //if (!node.Type.Equals(NodeType.Node))
                //    continue;
                bindingSource.Add(node);
            }
            dataGridView.DataSource = bindingSource;
            if(dataGridView.Rows.Count>=curRow 
                && dataGridView.Columns.Count>=curColum)
            {
                dataGridView.CurrentCell = dataGridView.Rows[curRow].Cells[curColum]; ;
            }
            dataGridView.Refresh();
        }
        public static Node GetNodeById(int id)
        {
            return nodeInfo.Find(s => s.Id.Equals(id));
        }
        public static Node GetNodeByName(string name)
        {
            return nodeInfo.Find(s => s.Name.Equals(name));
        }
        public static Node GetNodeByType(NodeType nodeType = NodeType.AutoCharge)
        {
            return nodeInfo.Find(s => s.Type.Equals(nodeType));
        }
        public static Node GetNode(string name, NodeType nodeType = NodeType.Node)
        {
            return nodeInfo.Find(s => s.Name.Equals(name) && s.Type.Equals(nodeType));
        }
        public static bool CheckNode(string nodename)
        {
            Node node = nodeInfo.Find(s => s.Name.Equals(nodename));
            if (node == null)
                return false;
            else
                return true;
        }
        public static bool CheckNodes(string nodenames,out string names)
        {
            string[] nodes = nodenames.Split(',');
            names = ""; 
            foreach (string item in nodes)
            {
                Node node = nodeInfo.Find(s => s.Name.Equals(item));
                if (node == null)
                {
                    names += item + ",";
                }
            }
            if (names.Equals(""))
                return true;
            return false;
        }
        /// <summary>
        /// 是否存在节点
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public static bool HasNode(string name)
        {
            int idx = nodeInfo.FindIndex(s => s.Name.Equals(name));
            return idx == -1 ? false : true;
        }
        public static void UpdateNode(Node node)
        {
            Node node1 = NodeManager.nodeInfo.Find(s => s.Name == node.Name);
            if (node1 == null)
            {
                Common.LogUtil.error("UpdateNode: " + node.Name + " 不存在");
                return;
            }

            if (!node1.Equals(node))
            {
                node1.UpdateNode(node);
            }
        }

        public static void RegisEvent(NodeChangedEvent changedEvent, NodeChangedEvent onlineEvent)
        {
            foreach (Node node in nodeInfo)
            {
                node.NodeChanged += changedEvent;
                node.NodeOnline += onlineEvent;
            }
        }
        public static void UnRegisEvent(NodeChangedEvent changedEvent, NodeChangedEvent onlineEvent)
        {
            foreach (Node node in nodeInfo)
            {
                node.NodeChanged -= changedEvent;
                node.NodeOnline -= onlineEvent;
            }
        }
        public static void WriteAllNodeInfos()
        {
            //foreach (Node item in nodeInfo)
            //{
            //    LogUtil.info(item.ToString());
            //}
        }
    }
}