FrmVMIClient.cs 13.1 KB
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.CompilerServices;
using System.Threading;
using System.IO;
using System.Runtime.InteropServices;
using System.Reflection;
using log4net; 
using UserFromControl;
using OnlineStore.DeviceLibrary;
using OnlineStore.Common;
using OnlineStore.ACSingleStore;
using OnlineStore.LoadCSVLibrary;

namespace OnlineStore.ACSingleStore
{
    public partial class FrmVMIClient : FrmBase
    {

        private VMILineBean vmiLine;
        public static readonly ILog LOGGER = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
        public FrmVMIClient()
        {
            Control.CheckForIllegalCrossThreadCalls = false;
            InitializeComponent();
            chbAuto.Checked = ConfigAppSettings.GetIntValue(Setting_Init.App_AutoRun).Equals(1);
        }

        #region "初始化界面数据"
        private bool LoadOk = false;

        private void FrmTest_Load(object sender, EventArgs e)
        {
            LogUtil.logBox = this.richTextBox1;
            cmbStopList.SelectedIndex = 0;
            this.vmiLine = LineManager.InitStore();
            if (vmiLine == null)
            {
                LogUtil.error(LOGGER, "加载设备失败");
                this.Close();
                return;
            }

            timer1.Enabled = true;
            groupBox5.Text = "AGV调度通信,入料线体 [" + vmiLine.Config.InL_AgvName + "] ,出料线体 [" + vmiLine.Config.OutL_AgvName + "] ";
            chbDebug.Checked = vmiLine.IsDebug;
            this.ShowInTaskbar = true;
            LoadIOList();
            notifyIcon1.Text = ConfigAppSettings.GetValue(Setting_Init.App_Title);
            HideForm();
            this.Opacity = 1;

            LoadOk = true;
        }
        #endregion

        Dictionary<string, IOTextControl> DIControlList = new Dictionary<string, IOTextControl>();
        Dictionary<string, IOTextControl> DOControlList = new Dictionary<string, IOTextControl>();
        private void LoadIOList()
        {
            int roleindex = 0;
            this.tableLayoutPanel1.RowStyles.Clear();
            this.tableLayoutPanel1.RowCount = vmiLine.Config.StoreDIList.Count;
            foreach (ConfigIO ioValue in vmiLine.Config.StoreDIList.Values)
            {
                this.tableLayoutPanel1.RowStyles.Add(new RowStyle(SizeType.Absolute, 26));
                IOTextControl control = new IOTextControl(ioValue.ElectricalDefinition + "_" + ioValue.Explain, ioValue.ProName);
                this.tableLayoutPanel1.Controls.Add(control, 0, roleindex);
                roleindex++;
                DIControlList.Add(ioValue.ProName, control);
            }

            tableLayoutPanel2.RowStyles.Clear();
            this.tableLayoutPanel2.RowCount = vmiLine.Config.StoreDOList.Count;
            roleindex = 0;
            foreach (ConfigIO ioValue in vmiLine.Config.StoreDOList.Values)
            {
                this.tableLayoutPanel2.RowStyles.Add(new RowStyle(SizeType.Absolute, 28));
                IOTextControl control = new IOTextControl(ioValue.ElectricalDefinition + "_" + ioValue.Explain, ioValue.ProName);
                this.tableLayoutPanel2.Controls.Add(control, 0, roleindex);
                roleindex++;
                DOControlList.Add(ioValue.ProName, control);
            }
            this.SuspendLayout();    //此处为不闪屏,一定要有的! 

        }
        private void ReadIOList()
        {
            foreach (string key in DIControlList.Keys)
            {
                IOTextControl control = DIControlList[key];
                int iov = (int)IOManager.IOValue(key);
                if (iov != control.IOValue)
                {
                    control.IOValue = iov;
                    control.ShowData();
                }
            }
            foreach (string key in this.DOControlList.Keys)
            {
                IOTextControl control = DOControlList[key];
                int iov = (int)IOManager.DOValue(key);
                if (iov != control.IOValue)
                {
                    control.IOValue = iov;
                    control.ShowData();
                }
            }
        }


        private void timer1_Tick(object sender, EventArgs e)
        {
            if (!this.Visible)
            {
                return;
            }
            ReadIOList();
            lblWarnMsg.Text = vmiLine.WarnMsg;
            lblThisSta.Text = LineManager.GetRunStr(vmiLine.runStatus);
        }

        private void FrmTest_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (e.CloseReason == CloseReason.UserClosing)//当用户点击窗体右上角X按钮或(Alt + F4)时 发生           
            {
                e.Cancel = true;
                HideForm();
            }
        }

        private void btnVmiStart_Click(object sender, EventArgs e)
        {
            if (vmiLine.runStatus >= RunStatus.HomeMoving)
            {
                btnVmiStart.Text = "复位";
                vmiLine.Reset();
            }
            else if (vmiLine.StartRun())
            {
                btnVmiStart.Text = "启动";
            }
        }

        private void btnVmiStop_Click(object sender, EventArgs e)
        {
            try
            {
                if (vmiLine.runStatus >= RunStatus.HomeMoving)
                {
                    vmiLine.StopRun();
                }
                else
                {
                    MessageBox.Show("清先启动VMI流水线");
                }
            }
            catch (Exception ex)
            {
                LogUtil.error(LOGGER, "出错:" + ex.StackTrace);
            }
        }

        private void btnLogDebug_Click(object sender, EventArgs e)
        {
            if (LogUtil.debug_opened == false)
            {
                btnLogDebug.Text = "关闭DEBUG";
                LogUtil.debug_opened = true;
            }
            else
            {
                btnLogDebug.Text = "开启DEBUG";
                LogUtil.debug_opened = false;
            }
        }

        private void FrmStoreBox_Shown(object sender, EventArgs e)
        {

        }
        private void chbAuto_CheckedChanged(object sender, EventArgs e)
        {
            if (!LoadOk)
            {
                return;
            }
            if (chbAuto.Checked)
            {
                ConfigAppSettings.SaveValue(Setting_Init.App_AutoRun, 1);
                ManagerUtil.AutoRun(Application.ExecutablePath, true);
            }
            else
            {
                ConfigAppSettings.SaveValue(Setting_Init.App_AutoRun, 0);
                ManagerUtil.AutoRun(Application.ExecutablePath, false);
            }
        }


        private void ExitApp()
        {
            try
            {
                if (this.timer1.Enabled)
                {
                    this.timer1.Enabled = false;
                }
                if (vmiLine.runStatus >= RunStatus.HomeMoving)
                {
                    vmiLine.StopRun();
                }

                IOManager.instance.CloseAllDO();
                IOManager.instance.CloseAllConnection();

                System.Environment.Exit(System.Environment.ExitCode);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.StackTrace, "Exception(异常)", MessageBoxButtons.OK, MessageBoxIcon.Error);
                LogUtil.error(LOGGER, ex.StackTrace);
            }
        }

        /// <summary>
        /// 隐藏窗口
        /// </summary>
        private void HideForm()
        {
            this.Opacity = 0;
            this.ShowInTaskbar = false;
            this.notifyIcon1.Visible = true;
            this.Hide();
        }
        private void FrmStoreBox_FormClosed(object sender, FormClosedEventArgs e)
        {
            ExitApp();
        }

        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DialogResult result = MessageBox.Show("是否确定退出?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (result.Equals(DialogResult.Yes))
            {
                ExitApp();
            }
        }

        private void btnClearL_Click(object sender, EventArgs e)
        {
            LogUtil.ClearLog();
        }


        private void chbDebug_CheckedChanged(object sender, EventArgs e)
        {
            if (!LoadOk)
            {
                return;
            }

            if (chbDebug.Checked)
            {
                if (!vmiLine.IsDebug)
                {
                    DialogResult result = MessageBox.Show("是否切换到调试状态?", "是否确认切换", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);
                    if (result.Equals(DialogResult.Yes))
                    {
                        vmiLine.IsDebug = true;
                        ConfigAppSettings.SaveValue(Setting_Init.IsInDebug, 1);
                        LogUtil.info("用户切换到调试状态 ");
                    }
                }
            }
            else
            {
                if (vmiLine.IsDebug)
                {
                    DialogResult result = MessageBox.Show("是否切换到正常工作状态?", "是否确认切换", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);
                    if (result.Equals(DialogResult.Yes))
                    {
                        vmiLine.IsDebug = false;
                        ConfigAppSettings.SaveValue(Setting_Init.IsInDebug, 0);
                        LogUtil.info("用户切换到正常工作状态 ");
                    }
                }
            }
        }


        private void richTextBox1_VisibleChanged(object sender, EventArgs e)
        {
            LogUtil.UpdateLogbox();
        }

        private void 显示ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Opacity = 100;
            this.Visible = true;
            this.WindowState = FormWindowState.Maximized;
            this.notifyIcon1.Visible = false;
            this.ShowInTaskbar = true;

        }
        protected void BtnMove(Button btn, string defaultText, string targetText, string ioHighType)
        {
            LogUtil.info(vmiLine.Name + "点击【" + btn.Text + "】 ");
            if (btn.Text.Equals(defaultText))
            {
                IOManager.IOMove(ioHighType, IO_VALUE.HIGH);
                btn.Text = targetText;
                btn.BackColor = Color.Aqua;
            }
            else
            {
                IOManager.IOMove(ioHighType, IO_VALUE.LOW);
                btn.Text = defaultText;
                btn.BackColor = Color.White;
            }
        }
        private void btnInLineRun_Click(object sender, EventArgs e)
        {
            BtnMove(btnInLineRun, "进料线体运转", "进料线体停止", IO_Type.InL_Run);
        }

        private void btnInLInStopDown_Click(object sender, EventArgs e)
        {
            BtnMove(btnInLInStopDown, "进料线入口阻挡下降", "进料线入口阻挡上升", IO_Type.InL_Run);
        }

        private void btnInLOutStopDown_Click(object sender, EventArgs e)
        {
            BtnMove(btnInLOutStopDown, "进料线出口阻挡下降", "进料线出口阻挡上升", IO_Type.InL_Run);
        }

        private void btnOLInStopDown_Click(object sender, EventArgs e)
        {
            BtnMove(btnOLInStopDown, "出料线入口阻挡下降", "出料线入口阻挡上升", IO_Type.InL_Run);
        }

        private void btnOutL_Click(object sender, EventArgs e)
        {
            BtnMove(btnOutL, "出料线体运转", "出料线体运转", IO_Type.InL_Run);
        }

        private void btnOLOutStopDown_Click(object sender, EventArgs e)
        {
            BtnMove(btnOLOutStopDown, "出料线出口阻挡下降", "出料线出口阻挡上升", IO_Type.InL_Run);
        }

        private void btnCloseA_Click(object sender, EventArgs e)
        {
            vmiLine.StopMove();
        }

        private void btnNeedLeave_Click(object sender, EventArgs e)
        {
            AgvClient.NeedLeave(vmiLine.Config.InL_AgvName);
        }

        private void btnNeedEntry_Click(object sender, EventArgs e)
        {

            AgvClient.NeedEnter(vmiLine.Config.OutL_AgvName);
        }

        private void btnInit_Click(object sender, EventArgs e)
        {
            AgvClient.Init();
        }

        private void btnDis_Click(object sender, EventArgs e)
        {
            AgvClient.Dispose();
        }

        private void btnStopTDown_Click(object sender, EventArgs e)
        {
            int value = (int)numericUpDown1.Value;
            string iotype = IO_Type.OutL_InStopDown;
            if (cmbStopList.SelectedIndex.Equals(1))
            {
                iotype = IO_Type.OutL_OutStopDown;
            }
            else if (cmbStopList.SelectedIndex.Equals(2))
            {
                iotype = IO_Type.InL_InStopDown;
            }
            vmiLine.StopIOMove(iotype, value);
        }
    }
}