Homepage.cs 12.1 KB
using DeviceLibrary;
using OnlineStore;
using OnlineStore.Common;
using OnlineStore.LoadCSVLibrary;
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;
using static AutoScanAndLabel.Form1;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.StartPanel;

namespace AutoScanAndLabel
{
    public partial class Homepage : UserControl
    {
        public Homepage()
        {
            InitializeComponent();
           
            listView1.View = View.Details;
            ColumnHeader emptycol = new ColumnHeader();
            emptycol.Text = "";
            emptycol.Width = 0;
            ColumnHeader msgcol = new ColumnHeader();
            msgcol.Text = crc.GetString("Form1_tabControl1_tabPage1_Text", "信息");
            msgcol.Width = 600;
            ColumnHeader timecol = new ColumnHeader();
            timecol.Text = crc.GetString("Res0004", "时间");
            timecol.Width = 150;
            listView1.Columns.Add(emptycol);
            listView1.Columns.Add(timecol);
            listView1.Columns.Add(msgcol);
            listView1.ColumnWidthChanging += listView_ColumnWidthChanging;
            listView1.Columns[1].Text = crc.GetString("Res0004", "时间");
            listView1.Columns[2].Text = crc.GetString("Res0003", "信息");
        }
        private void listView_ColumnWidthChanging(object sender, ColumnWidthChangingEventArgs e)
        {
            e.NewWidth = (sender as ListView).Columns[e.ColumnIndex].Width;
            e.Cancel = true;
        }
        bool userpause = false;
        private void btn_run_Click(object sender, EventArgs e)
        {
            if (!RobotManage.isRunning)
            {
                RobotManage.Start();
                userpause = false;
                if (RobotManage.isRunning)
                {
                    btn_stop.Enabled = true;
                    RobotManage_UserPauseSet(this, false);
                }
                LogUtil.info("用户按下启动");
            }
            else if (!userpause)
            {
                userpause = true;
                RobotManage.UserPause(userpause);
                LogUtil.info("用户按下暂停1");
            }
            else if (userpause)
            {
                userpause = false;
                RobotManage.UserPause(userpause);
                LogUtil.info("用户取消暂停2");
            }
        }

        private void RobotManage_UserPauseSet(object sender, bool e)
        {
            if (this.InvokeRequired)
            {
                this.Invoke((EventHandler<bool>)delegate
                {
                    RobotManage_UserPauseSet(sender, e);
                }, sender, e);
                return;
            }
            if (e)
            {
                userpause = true;
                (btn_run as Button).Text = crc.GetString("Res0014", "恢复运行");
                (btn_run as Button).BackColor = Color.LightGreen;
                cb_IgnoreSafecheck.Enabled = true;
            }
            else
            {
                userpause = false;
                (btn_run as Button).Text = crc.GetString("Res0013", "暂停运行");
                (btn_run as Button).BackColor = Color.Yellow;
                cb_IgnoreSafecheck.Enabled = false;
            }

        }

        private void btn_releaseleftshelf_Click(object sender, EventArgs e)
        {
            if (RobotManage.mainMachine.LeftMoveInfo.MoveStep == MoveStep.Wait)
            {
                MessageBox.Show(crc.GetString("Res0018", "料串已是释放状态"));
                return;
            }
            else if (RobotManage.mainMachine.SafeReleaseLeftShelf)
            {
                MessageBox.Show(crc.GetString("Res0019", "正在执行料串释放"));
                return;
            }
            else if (RobotManage.mainMachine.LeftMoveInfo.MoveStep < MoveStep.L04 || RobotManage.mainMachine.LeftMoveInfo.MoveStep > MoveStep.L30_LabelFinish)
            {
                MessageBox.Show(crc.GetString("Res0083", "左侧料串正在提升或下降, 不能释放"));
                return;
            }

            RobotManage.mainMachine.SafeReleaseLeftShelf = true;
            btn_releaseleftshelf.BackColor = Color.OrangeRed;
            Task.Run(() =>
            {
                int timeout = 2 * 60;
                while (RobotManage.mainMachine.SafeReleaseLeftShelf && timeout > 0)
                {
                    Task.Delay(500).Wait();
                    timeout--;
                    if (timeout % 2 == 0)
                    {
                        Invoke((EventHandler)delegate
                        {
                            btn_releaseleftshelf.Text = string.Format(crc.GetString("Res0084", "左侧料串等待释放中({0})"), timeout / 2);
                        });
                    }
                    if ((RobotManage.mainMachine.LeftMoveInfo.MoveStep == MoveStep.L20_WaitLabel || RobotManage.mainMachine.LeftMoveInfo.MoveStep == MoveStep.L10_WaitReelPut) && (RobotManage.mainMachine.RightMoveInfo.MoveStep == MoveStep.Wait || RobotManage.mainMachine.RightMoveInfo.MoveStep == MoveStep.REND))
                    {
                        RobotManage.mainMachine.LeftMoveInfo.NextMoveStep(MoveStep.L30_LabelFinish);
                    }
                }
                RobotManage.mainMachine.SafeReleaseLeftShelf = false;
                Invoke((EventHandler)delegate
                {
                    btn_releaseleftshelf.BackColor = Color.Transparent;
                    btn_releaseleftshelf.Text = crc.GetString("Res0085", "安全释放左侧料串");
                });
            });
        }

        private void btn_releaserightshelf_Click(object sender, EventArgs e)
        {
            if (RobotManage.mainMachine.RightMoveInfo.MoveStep == MoveStep.Wait)
            {
                MessageBox.Show(crc.GetString("Res0018", "料串已是释放状态"));
                return;
            }
            else if (RobotManage.mainMachine.SafeReleaseRightShelf)
            {
                MessageBox.Show(crc.GetString("Res0019", "正在执行料串释放"));
                return;
            }
            else if (RobotManage.mainMachine.RightMoveInfo.MoveStep < MoveStep.R04 || RobotManage.mainMachine.RightMoveInfo.MoveStep > MoveStep.R11_NextReel)
            {
                MessageBox.Show(crc.GetString("Res0086", "右侧料串当前没有就位, 不能释放"));
                return;
            }

            RobotManage.mainMachine.SafeReleaseRightShelf = true;
            btn_releaserightshelf.BackColor = Color.OrangeRed;
            Task.Run(() =>
            {
                int timeout = 2 * 60;
                while (RobotManage.mainMachine.SafeReleaseRightShelf && timeout > 0)
                {
                    Task.Delay(500).Wait();
                    timeout--;
                    if (timeout % 2 == 0)
                    {
                        Invoke((EventHandler)delegate
                        {
                            btn_releaserightshelf.Text = string.Format(crc.GetString("Res0087", "右侧料串等待释放中({0})"), timeout / 2);
                        });
                    }
                }
                RobotManage.mainMachine.SafeReleaseRightShelf = false;
                Invoke((EventHandler)delegate
                {
                    btn_releaserightshelf.BackColor = Color.Transparent;
                    btn_releaserightshelf.Text = crc.GetString("Res0088", "安全释放右侧料串");
                });
            });
        }

        private void btn_leftempty_Click(object sender, EventArgs e)
        {
            if (RobotManage.mainMachine.LeftMoveInfo.MoveStep != MoveStep.Wait
               && RobotManage.mainMachine.LeftMoveInfo.MoveStep != MoveStep.L10_WaitReelPut)
            {
                MessageBox.Show(crc.GetString("Res0090", "左侧料串正在运行,不能手动上料"));
                return;
            }
            if (RobotManage.mainMachine.IOValue(IO_Type.LeftEnd_Check).Equals(IO_VALUE.LOW))
            {
                MessageBox.Show(crc.GetString("Res0027", "没有检测到料串,不能手动上料"));
                return;
            }
            RobotManage.mainMachine.LeftMoveInfo.NewMove(MoveStep.L01);
            LogUtil.info("手动:左侧已清空,重新上升");
        }

        private void btn_maulstart_Click(object sender, EventArgs e)
        {
            if (RobotManage.mainMachine.RightMoveInfo.MoveStep != MoveStep.Wait)
            {
                MessageBox.Show(crc.GetString("Res0089", "右侧料串正在运行,不能手动上料"));
                return;
            }
            if (RobotManage.mainMachine.IOValue(IO_Type.RightEnd_Check).Equals(IO_VALUE.LOW))
            {
                MessageBox.Show(crc.GetString("Res0027", "没有检测到料串,不能手动上料"));
                return;
            }
            RobotManage.mainMachine.RightMoveInfo.NewMove(MoveStep.R01);
            LogUtil.info("手动:右侧有新料,准备上料");
        }

        private void btn_stop_Click(object sender, EventArgs e)
        {
            RobotManage.UserPause(false);
            userpause = false;
            Task.Run(() => { RobotManage.Stop(); });
            btn_run.Text = crc.GetString("Form1_btn_run_Text", "启动");
            btn_run.BackColor = Color.Transparent;
            btn_stop.Enabled = false;
        }

        //void LanguageProcess()
        //{
        //    crc.LanguageProcess(this, this.GetType().Name);
        //    crc.ProcessListItem(menuStrip1.Items, "menuStrip1");
        //    this.Text = crc.GetString("App_Title", "自动贴标机");//Config.Get(Setting_Init.App_Title);
        //    listView1.Columns[1].Text = crc.GetString("Res0004", "时间");
        //    listView1.Columns[2].Text = crc.GetString("Res0003", "信息");

        //    stateView.Columns[1].Text = crc.GetString("Res0005", "模块");
        //    stateView.Columns[2].Text = crc.GetString("Res0006", "步骤");
        //    stateView.Columns[3].Text = crc.GetString("Form1_tabControl1_tabPage1_Text", "信息");

        //    启用调试模式ToolStripMenuItem.Text = !RobotManage.IsDebug ? crc.GetString("Res0101", "启用配置模式") : crc.GetString("Res0012", "停用配置模式");

        //}

        void SetMsg(List<Msg> msgs)
        {
            this.SuspendLayout();
            listView1.Items.Clear();
            foreach (Msg msg in msgs)
            {
                ListViewItem lvi = new ListViewItem(new string[] { "", msg.datetime.ToString(), msg.msgtxt });
                if (msg.msgLevel == MsgLevel.info)
                    lvi.ForeColor = Color.DarkGreen;
                else
                    lvi.ForeColor = Color.Red;
                listView1.Items.Add(lvi);
            }
            this.ResumeLayout(true);
        }
        private void MainMachine_ProcessMsgEvent(List<Msg> msgs)
        {
            var d = new setmsgdelegate(SetMsg);
            if (this.IsHandleCreated)
                this.Invoke(d, msgs);
        }
        private void RobotManage_LoadFinishEvent(bool state, string msg)
        {
            if (state)
            {
                btn_run.Enabled = true;
                //btn_stop.Enabled = true;
                RobotManage.mainMachine.ProcessMsgEvent += MainMachine_ProcessMsgEvent;
                RobotManage.mainMachine.ProcessMoveinfoEvent += MainMachine_ProcessMoveinfoEvent;
            }
            var lm = new List<Msg>();
            foreach (string ms in msg.Split(new char[] { '\n' }, StringSplitOptions.RemoveEmptyEntries))
            {
                var m = new Msg();
                m.datetime = DateTime.Now;
                m.msgtxt = ms;
                m.msgLevel = state ? MsgLevel.info : MsgLevel.warning;
                lm.Add(m);
            }
            SetMsg(lm);
        }

        private void MainMachine_ProcessMoveinfoEvent(List<MoveInfo> moveinfoList)
        {
            //var d = new setstatedelegate(SetState);
            //this.Invoke(d, moveinfoList);
        }
    }
}