FrmDoubleClient.cs 13.4 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.DoubleLineClient;
using OnlineStore.LoadCSVLibrary;

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

        private DoubleLineBean DoubleLine;
     //   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);
            this.WindowState = FormWindowState.Maximized;
        }

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

        private void FrmTest_Load(object sender, EventArgs e)
        {
            LogUtil.logBox = this.richTextBox1;

            this.DoubleLine = LineManager.InitStore();
            if (DoubleLine == null)
            {
                LogUtil.error( "加载设备失败");
                this.Close();
                return;
            }
            timer1.Enabled = true; 
            chbDebug.Checked = DoubleLine.IsDebug;
            this.ShowInTaskbar = true; 
            notifyIcon1.Text = ConfigAppSettings.GetValue(Setting_Init.App_Title); 
            this.Opacity = 1;

            FrmAbbRobot frm = new FrmAbbRobot();
            FrmIOStatus frmi = new FrmIOStatus();
            AddForm("  ABB调试  ", frm);
            AddForm("  IO列表   ", frmi);
            frm.btnBack.Click += BtnBack_Click;
            frmi.btnBack.Click += BtnBack_Click;


            FrmDbLineIO frmdb = new FrmDbLineIO();
            AddForm("  线体操作调试  ", frmdb);
            frmdb.btnBack.Click += BtnBack_Click;

            PackageLineClien pakageLineClient = new PackageLineClien();
            AddForm("  包装线  ", pakageLineClient);

            LoadOk = true;
        }

        private void BtnBack_Click(object sender, EventArgs e)
        {
            this.tabPageList.SelectedIndex=0;
        }
        #endregion

        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);
            tabPageList.Controls.Add(lineTabPage);
        }

         int showAllTaskCnt = 0;
        private void timer1_Tick(object sender, EventArgs e)
        {
            if (!this.Visible)
            {
                return;
            }
            lblThisSta.Text = LineManager.GetRunStr(DoubleLine.runStatus);
            //上报异常
            bool isAlarm = false;
            List<AlarmMsg> msglist = new List<AlarmMsg>();
            showAllTaskCnt++;
            if (showAllTaskCnt >=10)//10秒刷新一次任务显示
            {
                isAlarm = true;
                showAllTaskCnt = 0;
                msglist.Add(new AlarmMsg("双层线任务", "doubleLine.AllTaskInfo", DoubleLine.AllTaskInfo.ToStr()));
            }
            if (!DoubleLine.LeftRobot.WarnMsg.Equals(""))
            {
                isAlarm = true;
                msglist.Add(new AlarmMsg("2号机器人", "doubleLine.LeftRobot.WarnMsg", DoubleLine.LeftRobot.WarnMsg));
            }
            if (!DoubleLine.RightRobot.WarnMsg.Equals(""))
            {
                isAlarm = true;
                msglist.Add(new AlarmMsg("1号机器人", "doubleLine.RightRobot.WarnMsg", DoubleLine.RightRobot.WarnMsg));
            }
            if (!LineManager.packageLine.A4_Shelf_Exist)
            {
                isAlarm = true;
                msglist.Add(new AlarmMsg("A4包装线出口状态", "doubleLine.packageLine.agvA4Step.Msg", LineManager.packageLine.agvA4Step.Msg));
            }
            if (!LineManager.packageLine.LeftAllowIn)
            {
                isAlarm = true;
                msglist.Add(new AlarmMsg("A5入口状态", "doubleLine.packageLine.benQInStepLeft.Msg", LineManager.packageLine.A5_State));
            }
            if (!LineManager.packageLine.A6_ReadRFID_OK)
            {
                isAlarm = true;
                msglist.Add(new AlarmMsg("A6出口RFID读取状态", "doubleLine.packageLine.benQOutStep.Msg", LineManager.packageLine.benQOutStep.Msg));
            }
            if (isAlarm)
                HttpServer.updateDeviceAlarmMsg(msglist);
            List<string> alarmMsg = new List<string>() { DoubleLine.WarnMsg, DoubleLine.LeftRobot.WarnMsg, DoubleLine.RightRobot.WarnMsg };
            string aMsg = "";
            foreach (string msg in alarmMsg)
            {
                if (msg.Equals(""))
                    continue;
                aMsg += msg + "\r\n";
            }
            if (!lblWarnMsg.Text.Equals(aMsg))
            {
                lblWarnMsg.Text = aMsg;
            }
            lblInfo.Text = DoubleLine.GetRunInfo();
        }

        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 (DoubleLine.runStatus >= RunStatus.HomeMoving)
            {
                btnVmiStart.Text = "复位";
                DoubleLine.Reset();
            }
            else if (DoubleLine.StartRun())
            {
                btnVmiStart.Text = "复位";
            }
            else
            {
                btnVmiStart.Text = "启动";
            }
        }

        private void btnVmiStop_Click(object sender, EventArgs e)
        {
            try
            {
                if (DoubleLine.runStatus >= RunStatus.HomeMoving)
                {
                    DoubleLine.StopRun();
                    btnVmiStart.Text = "启动";
                }
                else
                {
                    MessageBox.Show("清先启动双层流水线");
                }
            }
            catch (Exception ex)
            {
                LogUtil.error( "出错:" + 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 (DoubleLine.runStatus >= RunStatus.HomeMoving)
                {
                    DoubleLine.StopRun();
                }

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

                RFIDManager.Close();
                //    AgvClient.Dispose();
                System.Environment.Exit(System.Environment.ExitCode);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.StackTrace, "Exception(异常)", MessageBoxButtons.OK, MessageBoxIcon.Error);
                LogUtil.error( 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 (!DoubleLine.IsDebug)
                {
                    DialogResult result = MessageBox.Show("是否切换到调试状态?", "是否确认切换", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);
                    if (result.Equals(DialogResult.Yes))
                    {
                        DoubleLine.IsDebug = true;
                        ConfigAppSettings.SaveValue(Setting_Init.IsInDebug, 1);
                        LogUtil.info("用户切换到调试状态 ");
                    }
                }
            }
            else
            {
                if (DoubleLine.IsDebug)
                {
                    DialogResult result = MessageBox.Show("是否切换到正常工作状态?", "是否确认切换", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);
                    if (result.Equals(DialogResult.Yes))
                    {
                        DoubleLine.IsDebug = false;
                        ConfigAppSettings.SaveValue(Setting_Init.IsInDebug, 0);
                        LogUtil.info("用户切换到正常工作状态 ");
                    }
                }
            }
        }


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

        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(DoubleLine.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)
        {

            FrmIOStatus frm = new FrmIOStatus();
            frm.ShowDialog();
        }
         
        private void chbMoveStop_CheckedChanged(object sender, EventArgs e)
        {
            DoubleLine.MoveStop = chbMoveStop.Checked;
            LogUtil.info(DoubleLine.Name + "用户切换是否暂停: " + DoubleLine.MoveStop);
        }

        private void btnLAbbReset_Click(object sender, EventArgs e)
        {
            LogUtil.info("用户点击:" + btnLAbbReset.Text);
            DoubleLine.LeftRobot.Reset();
        }

        private void btnRAbbReset_Click(object sender, EventArgs e)
        {

            LogUtil.info("用户点击:" + btnRAbbReset.Text);
            DoubleLine.RightRobot.Reset();
        }

        private void tabPageList_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (tabPageList.SelectedIndex == 0)
            {
                LogUtil.logBox = this.richTextBox1;
            }
        }
    }
}