FrmMain.cs 8.2 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();
           
            CommonVar.control.limitArea.ScopeLimitStateChangedEvent += LimitArea_ScopeLimitStateChangedEvent;
        }

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

            for (int i = 0; i < CommonVar.nodeInfo.Count; i++)
            {
                n = DgvNode.Rows.Add(CommonVar.nodeInfo[i].ToRow());
                DgvNode.Rows[n].HeaderCell.Value = (n + 1).ToString();
                if (i % 2 == 0)
                    DgvNode.Rows[n].DefaultCellStyle.BackColor = Color.LightBlue;
                if (!CommonVar.nodeInfo[i].Online)
                    DgvNode.Rows[n].DefaultCellStyle.ForeColor = Color.Red;
            }
        }

        private void Server_NodeChanged(int nodeIndex)
        {
            Invoke(new Action(() =>
            {
                for (int i=0;i<CommonVar.nodeInfo.Count;i++)
                {
                    DgvNode.Rows[i].SetValues(CommonVar.nodeInfo[i].ToRow());
                }
                DgvNode.Refresh();
            }));
            System.GC.Collect();
        }

        private void Server_NodeOnline(int nodeIndex)
        {
            Invoke(new Action(() =>
            {
                for (int i = 0; i < CommonVar.nodeInfo.Count; i++)
                    DgvNode.Rows[nodeIndex].DefaultCellStyle.ForeColor = CommonVar.nodeInfo[nodeIndex].Online ? Color.Black : Color.Red;
                DgvNode.Refresh();
            }));
            System.GC.Collect();
        }

        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 CommonVar.agvInfo)
            {
                CommonVar.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;
            CommonVar.server.NodeChanged += Server_NodeChanged;
            CommonVar.server.NodeOnline += Server_NodeOnline;
            LogUtil.logBox= richTextBox1;
        }

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

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

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

            if (e.ColumnIndex == DgvNode.Columns.Count - 1)  //调用
            {
                CommonVar.nodeInfo[e.RowIndex].IsUse = !CommonVar.nodeInfo[e.RowIndex].IsUse;
                DgvNode.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = CommonVar.nodeInfo[e.RowIndex].IsUse.ToString();
                CommonVar.WriteIni(CommonVar.nodeInfo[e.RowIndex].Name, SettingString.IsUse, CommonVar.nodeInfo[e.RowIndex].IsUse.ToString());
                DgvNode.Rows[e.RowIndex].SetValues(CommonVar.nodeInfo[e.RowIndex].ToRow());
            }
        }

        bool InShhow = false;

        private void ShowTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            //ShowEmptyTask();
            if (InShhow)
                return;
            InShhow = true;

            Invoke(new Action(() =>
            {
                lblCharge1.Text = "充电桩1:" + CommonVar.Charge.Station[Charge.AutoCharge1];
                //lblCharge2.Text = "充电桩2:" + CommonVar.Charge.Station[Charge.AutoCharge2];
                lblStandy1.Text = "待机位1:" + CommonVar.Standby.Station[0];
                lblStandy2.Text = "待机位2:" + CommonVar.Standby.Station[1];
                lblStandy3.Text = "待机位3:" + CommonVar.Standby.Station[2];
                lblStandy4.Text = "待机位4:" + CommonVar.Standby.Station[3];
            }));
            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);
        }
    }
}