UCStoreMachine.cs 8.5 KB
using ConfigHelper;
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;

namespace TheMachine
{
    public partial class UCStoreMachine : UserControl
    {
        StoreMachine Machine;
        SIOControls sio;
        AxisControl ac;
        uc_boxdebug bd;
        StoreSettingControl sc;
        public event EventHandler<ListView.ListViewItemCollection> UpdateStatusList;
        public UCStoreMachine(StoreMachine machine)
        {
            Machine = machine;
            Machine.ProcessMsgEvent += Machine_ProcessMsgEvent;
            Machine.PauseEvent += Machine_PauseEvent;
            InitializeComponent();
            RobotManage.LoadFinishEvent += RobotManage_LoadFinishEvent;
            sio = new SIOControls(Machine);
            ac = new AxisControl(Machine);
            bd = new uc_boxdebug(Machine);
            sc = new StoreSettingControl(Machine);

            bd.Config = Machine.Config;
            sio.Config = Machine.Config;
            Common.AlertListViewSet(listView);
            this.Tag = "not";
            
            crc.LanguageChangeEvent += Crc_LanguageChangeEvent;
            Machine_PauseEvent(this, Machine.UserPause);
        }
        private void Crc_LanguageChangeEvent(object sender, EventArgs e)
        {
            
            crc.LanguageProcess(this);
            Machine_PauseEvent(this, Machine.UserPause);
        }
        private void Machine_ProcessMsgEvent(List<Msg> msg)
        {
            if (!this.Created)
                return;
            if (msg == null)
                return;

            try
            {
                this.Invoke((EventHandler<List<Msg>>)delegate(object s,List<Msg> msgs) {
                    SetMsg(msg);
                },null,msg);
            }
            catch (Exception e)
            {
                LogUtil.info("MainMachine_ProcessMsgEvent:" + e.ToString());
            }
        }

        void SetMsg(List<Msg> msgs)
        {
            //this.SuspendLayout();
            listView.Items.Clear();
            msgs.Sort((a, b) =>
            {
                if (a == null)
                    return 4;
                if (b == null)
                    return 4;
                if (a.msgLevel == MsgLevel.alarm)
                    return 1;
                if (a.msgLevel == MsgLevel.warning)
                    return 2;
                return 3;
            });
            foreach (Msg msg in msgs)
            {
                if (string.IsNullOrEmpty(msg.msgtxt) || msg.datetime == null)
                    continue;

                ListViewItem lvi = new ListViewItem(new string[] { "", msg.datetime.ToString("HH:mm:ss"), msg.msgtxt });
                if (msg.msgLevel == MsgLevel.info)
                    lvi.ForeColor = Color.DarkGreen;
                else
                    lvi.ForeColor = Color.Red;
                listView.Items.Add(lvi);
                if (msg.errInfo == ErrInfo.X09_BoxNotDetect)
                {
                    //btn_IgnoreX09.Visible = true;
                }
                else if (msg.errInfo == ErrInfo.X09_Clear)
                {
                    //btn_IgnoreX09.Visible = false;
                }
                else if (msg.errInfo == ErrInfo.RunBtn || msg.errInfo == ErrInfo.ResetBtn)
                {
                    Task.Run(() => {
                        if (!RobotManage.isRunning)
                            btn_run_Click(this, EventArgs.Empty);
                    });

                }
                else if (msg.errInfo == ErrInfo.SuddenStop)
                {
                    Task.Run(() => {
                        if (RobotManage.isRunning)
                            //btn_run_Click(this, EventArgs.Empty);
                            btn_stop_Click(this, EventArgs.Empty);
                    });

                }
            }
            //this.ResumeLayout(true);
            UpdateStatusList?.Invoke(Machine.MachineSide, listView.Items);
            Machine_PauseEvent(this, Machine.UserPause);
        }
        private void RobotManage_LoadFinishEvent(bool state, string msg)
        {
            if (!state)
                return;            
        }

        private void UCStoreMachine_Load(object sender, EventArgs e)
        {
            Config.PropertyBind($"Device_{Machine.MachineSide}_Disable", checkBox_disable, "Checked", "CheckedChanged");
            AddForm("tab_sio", crc.GetString("Form1_tabc_tab_io_Text","IO调试"), sio);
            AddForm("tab_axis",crc.GetString("Res0192","伺服调试"), ac);
            AddForm("tab_store", crc.GetString("Res0193","库位调试"), bd);
            AddForm("tab_setting", crc.GetString("Res0194","相关设置"), sc);

            crc.LanguageProcess(this);
        }

        private void AddForm(string id, string text, UserControl form)
        {
            foreach (TabPage tp in tabc.TabPages)
            {
                if (tp.Name == id)
                {
                    tp.Text = text;
                    return;
                }
            }

            TabPage lineTabPage = new TabPage(text);
            lineTabPage.Name = id;
            Panel linePan = new Panel();
            linePan.Dock = DockStyle.Fill;
            linePan.AutoScroll = true;
            lineTabPage.Controls.Add(linePan);
            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();
            tabc.TabPages.Add(lineTabPage);
        }
        private void Machine_PauseEvent(object sender, bool e)
        {
            if (this.InvokeRequired)
            {
                this.Invoke((EventHandler<bool>)delegate
                {
                    Machine_PauseEvent(sender, e);
                }, sender, e);
                return;
            }
            btn_stop.Text = crc.GetString("UCStoreMachine_btn_stop_Text", "停止");
            if (!Machine.isRunning) {

                btn_stop.Enabled = false;
                btn_run.Text = crc.GetString("Res0195","启动");
                btn_run.BackColor = Color.Transparent;
                return;
            }

            if (e)
            {
                //Machine.UserPause = true;
                (btn_run as Button).Text = crc.GetString("Res0196","恢复运行");
                (btn_run as Button).BackColor = Color.LightGreen;
            }
            else
            {
                //Machine.UserPause = false;
                (btn_run as Button).Text = crc.GetString("Res0197","暂停运行");
                (btn_run as Button).BackColor = Color.Yellow;
                btn_stop.Enabled = true;
            }
            
        }
        private void btn_run_Click(object sender, EventArgs e)
        {
            if (!Machine.isRunning)
            {
                //if (Machine.IOValue(IO_Type.SuddenStop_BTN, RobotManage.Config).Equals(IO_VALUE.LOW))
                //{
                //    Machine.Msg.add(crc.GetString("Res0182","急停中,无法启动"), MsgLevel.warning);
                //    Machine_ProcessMsgEvent(Machine.Msg.get()[Machine.MachineSide]);
                //    return;
                //}
                RobotManage.Start(Machine.MachineSide);
                if (Machine.isRunning)
                {
                    btn_stop.Enabled = true;
                    Machine_PauseEvent(this, false);
                    //(sender as Button).Text = "暂停运行";
                }
                LogUtil.info("用户按下启动");
            }
            else {
                Machine.UserPause=!Machine.UserPause;
            }
        }
        private void btn_stop_Click(object sender, EventArgs e)
        {
            if (this.InvokeRequired)
            {
                this.Invoke((EventHandler)delegate
                {
                    btn_stop_Click(sender, e);
                }, sender, e);
                return;
            }

            Task.Run(() => { Machine.Stop(); });
            Machine_PauseEvent(this, Machine.UserPause);
            //btn_stop.Enabled = false;
            //btn_run.Text = "启动";
            //btn_run.BackColor = Color.Transparent;
        }
    }
}