FrmMain.cs 11.2 KB
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DoubleLine
{
    public partial class FrmMain : Form
    {
        private bool exit = false;
        private NotifyIcon notify;
        private ContextMenuStrip notifyMenu;
        private Asa.File.Log log = Common.log;

        public FrmMain()
        {
            InitializeComponent();
        }

        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;

            DgvEmpty.Rows.Add("小料", "");
            DgvEmpty.Rows.Add("大料", "");
            DgvEmpty.Rows.Add("包装料", "");
            DgvEmpty.Rows.Add("", "");
            log.ShowText = TxtLog;
            ChkUseAgv.Checked = true;

            for (int i = 0; i < Common.ABB_Info.Length; i++)
                DgvABB.Rows.Add(Common.ABB_Info[i].Name, Common.ABB_Info[i].IP, Common.ABB_Info[i].State, "", "");
            for (int i = 0; i < Common.RFID_Info.Length; i++)
                DgvRFID.Rows.Add(Common.RFID_Info[i].Name, Common.RFID_Info[i].IP, "", "", "Read", "");
            for (int i = 0; i < Common.IO_Info.Length; i++)
                DgvIO.Rows.Add(Common.IO_Info[i].Name, Common.IO_Info[i].IP, "");

            //用于显示完界面后在打开
            System.Threading.Thread tTemp = new System.Threading.Thread(new System.Threading.ThreadStart(Dev_Open));
            tTemp.Name = "Dev_Open";
            tTemp.Start();

        }

        private void FrmMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (!exit)
            {
                e.Cancel = true;
                Hide();
            }
        }

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

        private void ItemExit_Click(object sender, EventArgs e)
        {
            Common.log.OutString("=====准备结束程序=====");
            exit = true;
            notify.Dispose();
            Close();
        }

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

        private void BenqAGV_Status(bool status)
        {
            if (status)
                LblAgvState.BackColor = Color.Lime;
            else
                LblAgvState.BackColor = Color.Red;
        }

        private void Dev_Open()
        {
            System.Threading.Thread.Sleep(50);
            Application.DoEvents();

            Common.BenqAGV.Status += BenqAGV_Status;
            Common.Web.StatusEmpty += Web_StatusEmpty;
            Common.ControlCenter.ABB_ActionChanged += ControlCenter_ABB_ActionChanged;

            //ABB机器人
            Common.ABB.StatusChanged += Robot_StatusChanged;
            Common.ABB.ServerChanged += Robot_ServerChanged;
            Common.ABB.Start();

            //RFID
            string[] rfid_ip = new string[Common.RFID_Info.Length];
            for (int i = 0; i < Common.RFID_Info.Length; i++)
                rfid_ip[i] = Common.RFID_Info[i].IP;
            Common.RFID.ConnectStatus += ReaderAll_ConnectStatus;
            Common.RFID.ScanStatus += ReaderAll_ScanStatus;
            log.OutInfo("开启RFID");
            Common.RFID.ScanMode = true;
            Common.RFID.Open(rfid_ip);

            //双层线
            Common.LineDouble.Status += LineDouble_Status;
            Common.LineDouble.Start();

            //包装线
            Common.LinePack.Status += LinePack_Status;
            Common.LinePack.Start();

            //出料皮带线
            Common.LineDischarge.Status += DischargeLine_Status;
            Common.LineDischarge.Start();

            //控制中心
            Common.ControlCenter.Start();

        }

        private void ControlCenter_ABB_ActionChanged(int idx, string s)
        {
            Invoke(new Action(() =>
            {
                DgvABB.Rows[idx].Cells[4].Value = s;
            }));
        }

        private void Web_StatusEmpty(string[] s)
        {
            Invoke(new Action(() =>
            {
                //DgvABB.Rows[idx].Cells[2].Value = Common.ABB_Info[idx].State;
                DgvEmpty.Rows[0].Cells[1].Value = s[0];
                DgvEmpty.Rows[1].Cells[1].Value = s[1];
                DgvEmpty.Rows[2].Cells[1].Value = s[2];
            }));
        }

        private void ChkUseAgv_CheckedChanged(object sender, EventArgs e)
        {
            Common.LinePack.UseAgv(ChkUseAgv.Checked);
            if (ChkUseAgv.Checked)
                ChkUseAgv.BackColor = Color.Lime;
            else
                ChkUseAgv.BackColor = Color.Red;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            textBox1.Text = Common.ControlCenter.TestBind();
        }



        #region ABB机器人
        private void MnuABB_Click(object sender, EventArgs e)
        {
            FrmABB frm = new FrmABB { Text = MnuABB.Text };
            frm.ShowDialog();
        }

        private void Robot_StatusChanged(int idx)
        {
            Invoke(new Action(() =>
            {
                DgvABB.Rows[idx].Cells[2].Value = Common.ABB_Info[idx].State;
            }));
        }

        private void Robot_ServerChanged(int idx)
        {
            Invoke(new Action(() =>
            {
                bool rtn = Common.ABB_Info[idx].IsOpen;
                DgvABB.Rows[idx].Cells[3].Value = rtn.ToString();
                DgvABB.Rows[idx].Cells[3].Style.BackColor = rtn ? Color.Lime : Color.Red;

            }));
        }

        private void BtnABBReload_Click(object sender, EventArgs e)
        {
            Common.ABB.LoadController();
        }
        #endregion

        #region RFID
        private void ReaderAll_ConnectStatus(int idx, string ip, bool status)
        {
            Invoke(new Action(() => {
                Common.RFID_Info[idx].IsOpen = status;
                DgvRFID.Rows[idx].Cells[2].Value = status.ToString();
                DgvRFID.Rows[idx].Cells[2].Style.BackColor = status ? Color.Lime : Color.Red;
                DgvRFID.Refresh();
            }));
        }

        private void ReaderAll_ScanStatus(int idx, string ip, bool status)
        {
            Invoke(new Action(() => {
                Common.RFID_Info[idx].IsScan = status;
                DgvRFID.Rows[idx].Cells[3].Value = status.ToString();
                DgvRFID.Rows[idx].Cells[3].Style.BackColor = status ? Color.Lime : Color.Red;
                DgvRFID.Refresh();
            }));
        }

        private void DgvRFID_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex < 0) return;
            if (e.ColumnIndex != 4) return;
            string val = Common.RFID.ReadStr(Common.RFID_Info[e.RowIndex].IP);
            DgvRFID.Rows[e.RowIndex].Cells[5].Value = val;
        }

        #endregion

        #region 双层线
        private void MnuDouble_Click(object sender, EventArgs e)
        {
            FrmDoubleLine frm = new FrmDoubleLine { Text = MnuDouble.Text };
            frm.ShowDialog();
        }

        private void LineDouble_Status(int idx)
        {
            Invoke(new Action(() =>
            {
                DgvIO.Rows[idx].Cells[2].Value = Common.IO_Info[idx].Box.IsConn.ToString();
                DgvIO.Rows[idx].Cells[2].Style.BackColor = Common.IO_Info[idx].Box.IsConn ? Color.Lime : Color.Red;
            }));
        }
        #endregion

        #region 包装线
        private void MnuPackage_Click(object sender, EventArgs e)
        {
            FrmPackLine frm = new FrmPackLine { Text = MnuPackage.Text };
            frm.ShowDialog();
        }

        private void LinePack_Status(int idx)
        {
            Invoke(new Action(() =>
            {
                DgvIO.Rows[idx].Cells[2].Value = Common.IO_Info[idx].Box.IsConn.ToString();
                DgvIO.Rows[idx].Cells[2].Style.BackColor = Common.IO_Info[idx].Box.IsConn ? Color.Lime : Color.Red;
            }));
        }
        #endregion

        #region 出料线
        private void DischargeLine_Status(int idx)
        {
            Invoke(new Action(() =>
            {
                DgvIO.Rows[idx].Cells[2].Value = Common.IO_Info[idx].Box.IsConn.ToString();
                DgvIO.Rows[idx].Cells[2].Style.BackColor = Common.IO_Info[idx].Box.IsConn ? Color.Lime : Color.Red;
            }));
        }



        #endregion

        #region 功能
        private void ChkServerPause_CheckedChanged(object sender, EventArgs e)
        {
            Common.ControlCenter.ServerPause = ChkServerPause.Checked;
        }

        private void BtnNeedD_Click(object sender, EventArgs e)
        {
            Common.LineDouble.NeedTypeD = DeviceAction.Start;
        }

        private void BtnNeedC_Click(object sender, EventArgs e)
        {
            Common.LineDouble.NeedTypeC = DeviceAction.Start;
        }

        private void BtnPlaceLeft_Click(object sender, EventArgs e)
        {
            Common.LineDouble.PlaceLeft = DeviceAction.Start;
        }

        private void BtnPlaceRight_Click(object sender, EventArgs e)
        {
            Common.LineDouble.PlaceRight = DeviceAction.Start;
        }

        private void BtnGoPlace1_Click(object sender, EventArgs e)
        {
            Common.LineDouble.GoPlace1 = DeviceAction.Start;
        }

        private void BtnGoPlace2_Click(object sender, EventArgs e)
        {
            Common.LineDouble.GoPlace2 = DeviceAction.Start;
        }

        private void BtnPlace1Place2_Click(object sender, EventArgs e)
        {
            Common.LineDouble.Place1ToPlace2 = DeviceAction.Start;
        }

        private void BtnPlace1Leave_Click(object sender, EventArgs e)
        {
            Common.LineDouble.Place1Leave = DeviceAction.Start;
        }

        private void BtnPlace2Leave_Click(object sender, EventArgs e)
        {
            Common.LineDouble.Place2Leave = DeviceAction.Start;
        }

        private void BtnPackingGoPlace_Click(object sender, EventArgs e)
        {
            Common.LinePack.GoPlace = DeviceAction.Start;
        }

        private void BtnPackingPlaceLeave_Click(object sender, EventArgs e)
        {
            Common.LinePack.PlaceLeave = DeviceAction.Start;
        }

        private void BtnPackingLeave_Click(object sender, EventArgs e)
        {
            Common.LinePack.Leave = true;
        }





        #endregion
    }
}