FrmMain.cs 7.8 KB
using System;
using System.CodeDom;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using Common;
using DeviceLibrary;
using DeviceLibrary.manager;

namespace AGVControl
{
    public partial class FrmMain : Form
    {
        private bool exit = false;
        private NotifyIcon notify;
        private ContextMenuStrip notifyMenu;
        private System.Timers.Timer showTimer;
        //private string[] agvRunStep;
        public FrmMain()
        {
            InitializeComponent();
            showTimer = new System.Timers.Timer();
            showTimer.Interval = 5000;
            //showTimer.Enabled = true;
            showTimer.AutoReset = true;
            showTimer.Elapsed += ShowTimer_Elapsed;
            showTimer.Start();
            LogUtil.logBox = richTextBox1;
        }

        private void ShowConfig()
        {
            //agvRunStep = new string[Common.agvInfo.Count];
            for (int i = 0; i < AGVManager.agvInfo.Count; i++)
            {
                MiR_API.Get_IO_Status(AGVManager.agvInfo[i]);
                AGVManager.agvInfo[i].IsExistShelf = AGVManager.agvInfo[i].GetInput(0,3);
                flowLayoutPanel1.Controls.Add(AGVManager.agvInfo[i].StateKanban);
                AddForm(AGVManager.agvInfo[i].Name, new Manual(AGVManager.agvInfo[i]));
                AGVManager.agvInfo[i].StateKanban.Init(AGVManager.agvInfo[i].Name);
            }

            NodeManager.InitView(DgvNode);
            DeviceLibrary.manager.FixMissionManager.InitView(dgvInfos);
            DeviceLibrary.manager.FixMissionManager.MissionChangedEvent += FixMissionManager_MissionChangedEvent;
        }

        private void FixMissionManager_MissionChangedEvent()
        {
            this.Invoke(new Action(()=> {
                DeviceLibrary.manager.FixMissionManager.UpdateDataSource(dgvInfos);
            }));
        }

        private void Server_NodeChanged(Node node)
        {
            Invoke(new Action(() =>
            {
                for (int i = 0; i < DgvNode.Rows.Count; i++)
                {
                    DgvNode.Rows[i].DefaultCellStyle.ForeColor = NodeManager.nodeInfo[i].Online && NodeManager.nodeInfo[i].IsUse ? Color.Black : Color.Red;
                    DgvNode.Rows[i].SetValues(NodeManager.nodeInfo[i].ToRow());
                }

            }));
        }

        private void Server_NodeOnline(Node node)
        {
            Invoke(new Action(() =>
            {
                for (int i = 0; i < DgvNode.Rows.Count; i++)
                {
                    DgvNode.Rows[i].DefaultCellStyle.ForeColor = NodeManager.nodeInfo[i].Online && NodeManager.nodeInfo[i].IsUse ? Color.Black : Color.Red;
                    DgvNode.Rows[i].SetValues(NodeManager.nodeInfo[i].ToRow());
                }
            }));
        }

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

        private void ItemExit_Click(object sender, EventArgs e)
        {
            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();
            AGVManager.server.NodeChanged += Server_NodeChanged;
            AGVManager.server.NodeOnline += Server_NodeOnline;
        }

        private void FrmMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (!exit)
            {
                e.Cancel = true;
                Hide();
            }
            LogUtil.logBox = null;
            //showTimer.Enabled = false;
            showTimer.Stop();
            AGVManager.server.NodeChanged -= Server_NodeChanged;
            AGVManager.server.NodeOnline -= Server_NodeOnline;
        }

        private void DgvNode_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex == -1) return;
            int idx = e.RowIndex;
            if (e.ColumnIndex == 6)  //调用
            {
                DataGridView dgv = sender as DataGridView;
                if (dgv != null)
                {

                }
                NodeManager.nodeInfo[idx].IsUse = !NodeManager.nodeInfo[idx].IsUse;
                DgvNode.Rows[idx].DefaultCellStyle.ForeColor = NodeManager.nodeInfo[idx].Online && NodeManager.nodeInfo[idx].IsUse ? Color.Black : Color.Red;
                DgvNode.Rows[idx].SetValues(NodeManager.nodeInfo[idx].ToRow());
            }
        }





        bool InShhow = false;

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

            Invoke(new Action(() =>
            {

                Application.DoEvents();
                if (DeviceLibrary.manager.FixMissionManager.IsOpen())
                {
                    pictureBox1.Visible = false;
                    pictureBox2.Visible = true;
                }
                else
                {
                    pictureBox1.Visible = true;
                    pictureBox2.Visible = false;
                }

            }));

            InShhow = false;
        }

        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);
            tabControlManual.Controls.Add(lineTabPage);
        }

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

        private void dgvInfos_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 4)
            {
                if (e.RowIndex == -1)
                    return;
                DeviceLibrary.manager.FixMissionManager.SetMissionState(e.RowIndex);
                DeviceLibrary.manager.FixMissionManager.UpdateDataSource(dgvInfos);
            }
        }
    }
}