NodeManager.cs 11.8 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;
using System.Runtime.CompilerServices;
using static System.Net.Mime.MediaTypeNames;
using System.Runtime.ConstrainedExecution;

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

        public static void InitNodesInfos()
        {
            InitLinesInAirShower();
            nodeInfo = new List<Node>();
            XmlConfigOperation.LoadNodeInfos(nodeInfo);
            WriteAllNodeInfos();
        }
        public static void ReLoadNodes()
        {
            try
            {
                List<Node> nodes = new List<Node>();
                XmlConfigOperation.LoadNodeInfos(nodes);
                nodeInfo = nodes;
            }
            catch (Exception ex)
            {
                MessageBox.Show("重载节点失败:" + ex.StackTrace, "提示");
            }
        }
        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());
            //}
        }
        #region 跨门 跨风淋门
        /// <summary>
        /// 风淋门内的线体
        /// key:车间名
        /// val:线体名
        /// </summary>
        static Dictionary<string, List<string>> linesInAirShower = new Dictionary<string, List<string>>();
        static void InitLinesInAirShower()
        {
            //添加3D的
            linesInAirShower.Add("3D", new List<string>() { "S1Tail" });
            //添加4C的
            linesInAirShower.Add("4C", new List<string>() { "C14Tail", "C15Tail" });
        }
        /// <summary>
        /// 检查是否需要跨门
        /// </summary>
        /// <param name="cur"></param>
        /// <param name="next"></param>
        /// <returns></returns>
        public static bool NeedCrossDoor(AgvInfo agv, Node cur, Node next, out bool isIn, out bool isAirDoor)
        {
            isIn = true;
            isAirDoor = false;
            if (agv.Scope.Workshop.Equals("1F")) return false;
            return false;
            try
            {
                if (cur == null) cur = agv.Place;
                if (next == null) next = agv.Place;
                if (!cur.Workshop.Equals(next.Workshop))
                {
                    if (cur.Equals("4D") && next.Workshop.Equals("4C"))
                    {
                        return true;
                    }
                    else if (cur.Workshop.Equals("4C") && next.Workshop.Equals("4D"))
                    {
                        isIn = false;
                        return true;
                    }
                }
                else//在相同车间
                {
                    if (linesInAirShower.ContainsKey(cur.Workshop))//当前点在风淋门内
                    {
                        if (linesInAirShower.ContainsKey(next.Workshop))//目标点在风淋门内
                        {
                            return false;
                        }
                        else//目标点在风淋门外
                        {
                            isAirDoor = true;
                            isIn = false;
                            return true;
                        }
                    }
                    else//当前点在风淋门外
                    {
                        if (linesInAirShower.ContainsKey(next.Workshop))//目标点在风淋门内
                        {
                            isAirDoor = true;
                            return true;
                        }
                        else//目标点在风淋门外
                        {
                            return false;
                        }
                    }
                }

            }
            catch (Exception ex)
            {
                LogUtil.error("NeedCrossDoor", ex);
            }
            return false;
        }
        /// <summary>
        /// 是否需要跨两个门
        /// </summary>
        /// <param name="agv"></param>
        /// <param name="node"></param>
        /// <returns></returns>
        public static bool NeedCrossTwoDoor(AgvInfo agv, Node src, Node dst)
        {
            if (agv.Scope.Workshop.Equals("1F")) return false;
            return false;
            try
            {
                if (src == null) src = agv.Place;
                if (dst == null) dst = agv.Place;
                if (!src.Workshop.Equals(dst.Workshop) && linesInAirShower.ContainsKey(dst.Workshop))
                {
                    return true;
                }

            }
            catch (Exception ex)
            {
                LogUtil.error("NeedCrossDoor", ex);
            }
            return false;
        }
        #endregion
    }
}