FrmMain.cs 8.2 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.lift;
using DeviceLibrary.manager;
using DeviceLibrary.service.model;

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;
            CheckForIllegalCrossThreadCalls = false;
            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.MissionManager.InitView(dgvInfos);
            DeviceLibrary.manager.MissionManager.MissionChangedEvent += FixMissionManager_MissionChangedEvent;
        }

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

        private void Server_NodeChanged(Node node)
        {
            Invoke(new Action(() =>
            {
                NodeManager.UpdateDataSource(DgvNode, curRow, curColum);
            }));
        }

        private void Server_NodeOnline(Node node)
        {
            Invoke(new Action(() =>
            {
                NodeManager.UpdateDataSource(DgvNode, curRow, curColum);
            }));
        }

        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();
            NodeManager.RegisEvent(Server_NodeChanged, Server_NodeOnline);
        }

        private void FrmMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (!exit)
            {
                e.Cancel = true;
                Hide();
            }
            LogUtil.logBox = null;
            //showTimer.Enabled = false;
            showTimer.Stop();
            NodeManager.UnRegisEvent(Server_NodeChanged, Server_NodeOnline);
        }
        private int curRow = 0, curColum = 0;
        private void DgvNode_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex == -1) return;
            curRow = e.RowIndex;
            curColum = e.ColumnIndex;
            if (e.ColumnIndex == 0)  //调用
            {

                NodeManager.nodeInfo[curRow].IsUse = !NodeManager.nodeInfo[curRow].IsUse;
                NodeManager.UpdateDataSource(DgvNode, curRow, curColum);
            }
        }





        bool InShhow = false;

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

            Invoke(new Action(() =>
            {
                try
                {
                    txtShow.Text = "AUO占用:"+JsonHelper.SerializeObject(MissionManager.AUOResult) + "\r\n" 
                    + "电梯信息:"+LiftContext.GetLiftInfos() + "\r\n"
                    +LiftContext.GetLiftNodeOccupiedInfo();
                }
                catch { }

            }));

            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;
            }
            this.Invoke(new Action(() =>
            {
                DeviceLibrary.manager.MissionManager.UpdateDataSource(dgvInfos);
            }));
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            DialogResult dialogResult = MessageBox.Show("确定AGV收线?", "提示", MessageBoxButtons.YesNo);
            if (dialogResult.Equals(DialogResult.Yes))
            {
                MissionManager.InUpdating = true;
                checkBox1.Checked = true;
                LogUtil.info("切换为AGV收线状态");
            }
            else
            {
                MissionManager.InUpdating = false;
                checkBox1.Checked = false;
                LogUtil.info("从AGV收线状态恢复");
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            NodeManager.ReLoadNodes();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            AgvTaskManager.ReloadAgvTask();
        }

        private void dgvInfos_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {
            LogUtil.error($"表格式异常:{e.RowIndex},{e.ColumnIndex},{e.Exception.Message}");
        }

        private void tabPage3_Click(object sender, EventArgs e)
        {
            ConfigHelper.AdvanceConfigForm.ShowEditDialog(this);
        }

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

    }
}