FrmMain.cs 13.4 KB
using System;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using Common;
using DeviceLibrary;
using System.Collections.Generic;
namespace AGVControl
{
    public partial class FrmMain : Form
    {
        private bool exit = false;
        private bool change;
        private NotifyIcon notify;
        private ContextMenuStrip notifyMenu;
        private System.Timers.Timer showTimer;
        public FrmMain()
        {
            InitializeComponent();
            showTimer = new System.Timers.Timer();
            showTimer.Interval = 2000;
            showTimer.Enabled = true;
            showTimer.AutoReset = true;
            showTimer.Elapsed += ShowTimer_Elapsed;
            showTimer.Start();

            DeviceLibrary.Context.control.limitArea.ScopeLimitStateChangedEvent += LimitArea_ScopeLimitStateChangedEvent;
        }

        /// <summary>
        /// 限制区域有小车
        /// </summary>
        /// <param name="ip"></param>
        private void LimitArea_ScopeLimitStateChangedEvent(string ip, bool state)
        {
            Invoke(new Action(() =>
            {
                if (DeviceLibrary.Context.control.limitArea.Available)
                    lblLimit.Text = "限制区域车辆:";
                else
                {
                    lblLimit.Text = "限制区域车辆:" + ip;
                }
            }
            ));
        }
        static Dictionary<string, Form> agvManual = new Dictionary<string, Form>();
        private void ShowConfig()
        {
            int n;
            for (int i = 0; i < DeviceLibrary.Context.agvInfo.Count; i++)
            {
                MiR_API.Get_IO_Status(DeviceLibrary.Context.agvInfo[i], out bool[] input, out bool[] output);
                if (input != null && input.Length == 4)
                    DeviceLibrary.Context.agvInfo[i].IsExistShelf = input[3];
                flowPanelKanBan.Controls.Add(DeviceLibrary.Context.agvInfo[i].StateKanban);
                //AddForm(DeviceLibrary.Context.agvInfo[i].Name, new Manual(DeviceLibrary.Context.agvInfo[i]));
                DeviceLibrary.Context.agvInfo[i].StateKanban.Init();
                Context.agvInfo[i].StateKanban.Click += StateKanban_Click;
                agvManual.Add(DeviceLibrary.Context.agvInfo[i].Name, new Manual(DeviceLibrary.Context.agvInfo[i]));
            }

            for (int i = 0; i < DeviceLibrary.Context.nodeInfo.Count; i++)
            {
                Agv.ClientNode node = DeviceLibrary.Context.nodeInfo[i];
                if (node.Name.StartsWith("MP"))
                    continue;
                n = DgvNode.Rows.Add(node.LineName, node.Action, node.Shelf, node.RFID, node.Online, node.IsUse);
                DgvNode.Rows[n].HeaderCell.Value = (n + 1).ToString();
                if (i % 2 == 0)
                    DgvNode.Rows[n].DefaultCellStyle.BackColor = Color.LightBlue;
                if (!DeviceLibrary.Context.nodeInfo[i].Online)
                    DgvNode.Rows[n].DefaultCellStyle.ForeColor = Color.Red;
            }
        }

        private void StateKanban_Click(object sender, EventArgs e)
        {
            if (sender is AGV_UI.Status)
            {
                AGV_UI.Status status = (AGV_UI.Status)sender;
                if (agvManual[status.AGVName].IsDisposed)
                {
                    Agv_Info agv = Context.agvInfo.Find(s => s.Name.Equals(status.AGVName));
                    if(agv !=null)
                    {
                        agvManual[status.AGVName] = new Manual(agv);
                        agvManual[status.AGVName].Show();
                    }
                }
                else
                    agvManual[status.AGVName].Show();
            }
        }

        private void ItemShow_Click(object sender, EventArgs e)
        {
            Show();
            if (WindowState == FormWindowState.Minimized)
                WindowState = FormWindowState.Normal;
        }

        private void ItemExit_Click(object sender, EventArgs e)
        {
            foreach (var item in DeviceLibrary.Context.agvInfo)
            {
                DeviceLibrary.Context.WriteIni(item.Name, SettingString.RFID, item.RFID);
            }
            notify.Dispose();
            exit = true;
            Close();
        }

        private void Notify_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            Show();
            if (WindowState == FormWindowState.Minimized)
                WindowState = FormWindowState.Normal;
        }
        private void FrmMain_Load(object sender, EventArgs e)
        {
            //托盘菜单
            notifyMenu = new ContextMenuStrip();
            ToolStripMenuItem itemShow = new ToolStripMenuItem("显示(&S)");
            itemShow.Click += ItemShow_Click;
            ToolStripMenuItem itemExit = new ToolStripMenuItem("退出(&X)");
            itemExit.Click += ItemExit_Click;
            notifyMenu.Items.Add(itemShow);
            notifyMenu.Items.Add(itemExit);

            //托盘控件
            notify = new NotifyIcon { Icon = Icon, Visible = true, ContextMenuStrip = notifyMenu, Text = Text };
            notify.MouseDoubleClick += Notify_MouseDoubleClick;

            ShowConfig();
            change = true;
            change = false;
            DeviceLibrary.Context.server.NodeChanged += Server_NodeChanged; ;
            DeviceLibrary.Context.server.NodeOnline += Server_NodeOnline; ;
            string ip = AppConfigHelper.GetValue(SettingString.AGVServerIp);
            int port = AppConfigHelper.GetIntValue(SettingString.AGVServerPort);
            DeviceLibrary.Context.server.Start(ip, port);
            LogUtil.logBox = richTextBox1;
        }

        private void Server_NodeOnline(string nodeName, bool online)
        {
            this.Invoke(new Action(() =>
            {
                int idx = DeviceLibrary.Context.nodeInfo.FindIndex(s => s.Name.Equals(nodeName));
                if (idx > -1)
                {
                    DeviceLibrary.Context.nodeInfo[idx].Online = online;
                    bool isuse = DeviceLibrary.Context.nodeInfo[idx].IsUse;
                    DgvNode.Rows[idx].DefaultCellStyle.ForeColor = online && isuse ? Color.Black : Color.Red;
                    DgvNode.Rows[idx].SetValues(DeviceLibrary.Context.nodeInfo[idx].LineName, DeviceLibrary.Context.nodeInfo[idx].Action, DeviceLibrary.Context.nodeInfo[idx].Shelf, DeviceLibrary.Context.nodeInfo[idx].RFID, DeviceLibrary.Context.nodeInfo[idx].Online, DeviceLibrary.Context.nodeInfo[idx].IsUse);
                }
            }
            ));
        }

        private void Server_NodeChanged(Agv.Node clientNode)
        {
            int idx = DeviceLibrary.Context.nodeInfo.FindIndex(s => s.Name.Equals(clientNode.Name));
            if (idx > -1)
            {
                DeviceLibrary.Context.nodeInfo[idx].Action = clientNode.Action;
                DeviceLibrary.Context.nodeInfo[idx].Shelf = clientNode.Shelf;
                DeviceLibrary.Context.nodeInfo[idx].RFID = clientNode.RFID;
                DgvNode.Rows[idx].SetValues(DeviceLibrary.Context.nodeInfo[idx].LineName, DeviceLibrary.Context.nodeInfo[idx].Action, DeviceLibrary.Context.nodeInfo[idx].Shelf, DeviceLibrary.Context.nodeInfo[idx].RFID, DeviceLibrary.Context.nodeInfo[idx].Online, DeviceLibrary.Context.nodeInfo[idx].IsUse);
            }
        }

        private void FrmMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (!exit)
            {
                e.Cancel = true;
                Hide();
            }
            foreach (var item in DeviceLibrary.Context.agvInfo)
            {
                DeviceLibrary.Context.WriteIni(item.Name, SettingString.RFID, item.RFID);
            }

            //showTimer.Enabled = false;
            showTimer.Stop();
            // CommonVar.control.AgvChanged -= Control_AgvChanged;
            DeviceLibrary.Context.server.NodeChanged -= Server_NodeChanged;
            DeviceLibrary.Context.server.NodeOnline -= Server_NodeOnline;
        }

        private void DgvNode_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex == -1) return;

            if (e.ColumnIndex == DgvNode.Columns.Count - 1)  //调用
            {
                DeviceLibrary.Context.nodeInfo[e.RowIndex].IsUse = !DeviceLibrary.Context.nodeInfo[e.RowIndex].IsUse;
                DgvNode.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = DeviceLibrary.Context.nodeInfo[e.RowIndex].IsUse.ToString();
                DeviceLibrary.Context.WriteIni(DeviceLibrary.Context.nodeInfo[e.RowIndex].Name, SettingString.IsUse, DeviceLibrary.Context.nodeInfo[e.RowIndex].IsUse.ToString());
                Agv.ClientNode node = DeviceLibrary.Context.nodeInfo[e.RowIndex];
                DgvNode.Rows[e.RowIndex].SetValues(node.LineName, node.Action, node.Shelf, node.RFID, node.Online, node.IsUse);
            }
        }

        bool InShhow = false;
        Dictionary<string, string> destInfoMap = new Dictionary<string, string>();
        private void ShowTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            //ShowEmptyTask();
            if (InShhow)
                return;
            InShhow = true;
            BoxDestInfo destInfo;
            Agv.ClientNode node;
            Invoke(new Action(() =>
            {
                node = DeviceLibrary.Context.nodeInfo.Find(s => s.Name.Equals(SettingString.D2));
                if (!destInfoMap.ContainsKey(SettingString.D2))
                {
                    if (RequestManager.FindFullShelfTarget(node.RFID, out destInfo))
                    {
                        label1.Text = destInfo.ToString();
                        destInfoMap.Add(SettingString.D2, node.RFID);
                    }
                    else
                    {
                        label1.Text = "";
                    }

                }
                else if (!destInfoMap[SettingString.D2].Equals(node.RFID))
                {
                    if (RequestManager.FindFullShelfTarget(node.RFID, out destInfo))
                    {
                        label1.Text = destInfo.ToString();
                        destInfoMap[SettingString.D2] = node.RFID;
                    }
                    else
                    {
                        label1.Text = "";
                    }

                }

                node = DeviceLibrary.Context.nodeInfo.Find(s => s.Name.Equals(SettingString.D4));
                if (!destInfoMap.ContainsKey(SettingString.D4))
                {
                    if (RequestManager.FindFullShelfTarget(node.RFID, out destInfo))
                    {
                        label2.Text = destInfo.ToString();
                        destInfoMap.Add(SettingString.D4, node.RFID);
                    }
                    else
                    {
                        label2.Text = "";
                    }

                }
                else if (!destInfoMap[SettingString.D4].Equals(node.RFID))
                {
                    if (RequestManager.FindFullShelfTarget(node.RFID, out destInfo))
                    {
                        label2.Text = destInfo.ToString();
                        destInfoMap[SettingString.D4] = node.RFID;
                    }
                    else
                    {
                        label2.Text = "";
                    }

                }

            }));
            Application.DoEvents();
            InShhow = false;
        }
        /// <summary>
        /// 添加窗体
        /// </summary>
        /// <param name="text"></param>
        /// <param name="form"></param>
        private void AddForm(string text, Form form)
        {
            text = text.PadLeft(10, ' ');
            TabPage lineTabPage = new TabPage(text);
            // lineTabPage.AutoScroll = true;
            // lineTabPage.Tag = lineBean;
            Panel linePan = new Panel();
            linePan.Dock = DockStyle.Fill;
            linePan.AutoScroll = true;
            lineTabPage.Controls.Add(linePan);
            form.FormBorderStyle = FormBorderStyle.None;
            form.TopLevel = false;
            linePan.Controls.Add(form);
            //form.Dock = DockStyle.Fill;
            //linePan.Anchor = ((AnchorStyles)((AnchorStyles.Top | AnchorStyles.Right | AnchorStyles.Bottom | AnchorStyles.Left)));
            //form.Anchor = ((AnchorStyles)((AnchorStyles.Top | AnchorStyles.Right | AnchorStyles.Bottom | AnchorStyles.Left)));
            form.Show();
            // tabPageList.Add(lineTabPage);
            //tabControlAGV.Controls.Add(lineTabPage);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            label3.Text = "";
            if (textBox1.Text.Equals(""))
                return;
            if (RequestManager.AgvRemoveRfid(textBox1.Text.ToUpper()))
            {
                LogUtil.info("手动清理料架缓存:" + textBox1.Text.ToUpper());
                label3.Text = "清理料架缓存成功:" + textBox1.Text.ToUpper();
                label3.BackColor = Color.Green;
            }
            else
            {
                label3.Text = "清理料架缓存失败:" + textBox1.Text.ToUpper();
                label3.BackColor = Color.Red;
            }
            textBox1.Text = "";
        }

        private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (tabControl1.SelectedIndex == 1)
                LogUtil.logBox = richTextBox1;
        }
    }
}