FrmEquip.cs 9.9 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 OnlineStore.DeviceLibrary;
using log4net;
using System.Reflection;
using UserFromControl;
using OnlineStore.LoadCSVLibrary;
using OnlineStore.Common; 

namespace OnlineStore.AssemblyLine
{
    internal partial class FrmEquip : FrmEquipBase
    {
        private bool IsLoad = false;

        private EquipBase equipBean;
        internal FrmEquip(EquipBase equip)
        {
            equipBase = equip;
            this.equipBean = equip;
            Control.CheckForIllegalCrossThreadCalls = false;
            InitializeComponent();
            chkDebug.Checked = equip.IsDebug;
            LoadIOList();
        }

        private void LoadIOList()
        {
            int maxCount = 20;
            int roleindex = 0;
            this.tableLayoutPanel1.RowStyles.Clear();
            this.tableLayoutPanel1.RowCount = maxCount;
            int i = 0;
            foreach (ConfigIO ioValue in equipBean.baseConfig.DIList.Values)
            {
                IOTextControl control = new IOTextControl(ioValue.ElectricalDefinition + "_" + ioValue.Explain, ioValue.ProName);
                if (i <= maxCount)
                {
                    this.tableLayoutPanel1.RowStyles.Add(new RowStyle(SizeType.Absolute, 28));
                    this.tableLayoutPanel1.Controls.Add(control, 0, roleindex);
                }
                roleindex++;
                i++;
                DIControlList.Add(ioValue.ProName, control);
            }

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

            cmbWriteIO.DataSource = new List<ConfigIO>(equipBean.baseConfig.DOList.Values);
            cmbWriteIO.ValueMember = "ProName";
            cmbWriteIO.DisplayMember = "DisplayStr";

        }
        private void Control_Click(object sender, EventArgs e)
        {
            IOTextControl control = (IOTextControl)sender;
            string name = control.Name.Substring(3, control.Name.Length - 3);
            List<string> keyList = new List<string>(DOControlList.Keys);
            int index = keyList.IndexOf(name);
            if (index >= 0)
            {
                cmbWriteIO.SelectedIndex = index;
            }
        }
        private bool IsInCheck = false;
        private void timer1_Tick(object sender, EventArgs e)
        {
            try
            {
                if (!this.Visible)
                {
                    return;
                }
                ReadIOList();
                ReadBtnDO();
                lblName.BackColor = equipBean.GetShowColor();
                if (equipBean.runStatus > LineRunStatus.Wait)
                {
                    if (btnStart.Enabled)
                    {
                        FormStatus(true);
                    }
                }
                else
                {
                    if (!btnStart.Enabled)
                    {
                        FormStatus(false);
                    }
                }
                lblStoreStatus.Text = KTK_Store.GetRunStr(equipBean.runStatus);
                lblThisSta.Text = equipBean.WarnMsg;
                string text = "";
                List<InOutParam> ps = new List<InOutParam>(equipBean.waitInStoreList);
                if (ps.Count > 0)
                {
                    text = "等待入库列表:\r";
                    foreach (InOutParam param in ps)
                    {
                        text += param.ToStr() + "\r";
                    }
                }
                if (!lblInstoreList.Text.Equals(text))
                {
                    lblInstoreList.Text = text;
                }
                lblMoveInfo.Text = equipBean.GetMoveStr();
                //lblwidth.Text = equipBean.GetLastTrayInfo();
            }
            catch (Exception ex)
            {
                LogUtil.error(equipBean.Name + "界面定时器出错:", ex);
            }

        }

        private void FrmTest_FormClosing(object sender, FormClosingEventArgs e)
        {
            // KNDAIManager.NeedShow = false;
            try
            {
                if (this.timer1.Enabled)
                {
                    this.timer1.Enabled = false;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.StackTrace, "Exception(异常)", MessageBoxButtons.OK, MessageBoxIcon.Error);
                LogUtil.error("", ex);
            }
        }
        private void btnOpenDo_Click(object sender, EventArgs e)
        {
            WriteDO(IO_VALUE.HIGH);
        }
        private void btnWriteSingleDO_Click(object sender, EventArgs e)
        {
            WriteDO(IO_VALUE.LOW);
        }
        private void WriteDO(IO_VALUE value)
        {
            string deviceName = txtDoName.Text;
            int index = FormUtil.GetIntValue(txtDOIndex);
            //   IO_VALUE value = checkBox1.Checked ? IO_VALUE.HIGH : IO_VALUE.LOW;
            int time = FormUtil.GetIntValue(txtWriteTime);
            int slaveId = FormUtil.GetIntValue(txtSlaveId);
            if (time > 0)
            {
                IOManager.instance.WriteSingleDO(deviceName, (byte)slaveId, (ushort)index, (IO_VALUE)value, time);
            }
            else
            {
                IOManager.instance.WriteSingleDO(deviceName, (byte)slaveId, (ushort)index, (IO_VALUE)value);
            }
        }

        private ConfigIO GetSelectDO()
        {
            string text = cmbWriteIO.SelectedValue.ToString();
            string newType = text.Replace("SW1_", "SW_").Replace("SW2_", "SW_").Replace("SW3_", "SW_").Replace("SW4_", "SW_");
            if (equipBean.baseConfig.DOList.ContainsKey(newType))
            {
                ConfigIO io = equipBean.baseConfig.DOList[newType];
                return io;
            }
            return null;
        }
        IOTextControl selectControl = null;
        private void cmbWriteIO_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbWriteIO.SelectedIndex >= 0)
            {
                ConfigIO io = GetSelectDO();
                if (io != null)
                {
                    // txtIp.Text = io.DeviceName;
                    txtDOIndex.Text = io.GetIOAddr().ToString();
                    txtDoName.Text = io.IO_IP;
                    txtSlaveId.Text = io.SlaveID.ToString();
                    IOTextControl newControl = DOControlList[io.ProName];
                    if (selectControl != null) { selectControl.BackColor = Color.White; }
                    newControl.BackColor = Color.SkyBlue;
                    selectControl = newControl;
                    //txtIOInfo.Text = "IP:" + io.DeviceName + "  /" + io.GetIOAddr().ToString();
                }
            }
        }


        private void FrmStoreIOStatus_Load(object sender, EventArgs e)
        {
            lblStoreStatus.Text = KTK_Store.GetRunStr(equipBean.runStatus);
            lblThisSta.Text = equipBean.WarnMsg;
            lblName.Text = equipBean.Name;
            this.Text = equipBean.Name;
            IsLoad = true;
        }

        private void cmbWriteIO_DrawItem(object sender, DrawItemEventArgs e)
        {
            if (e.Index < 0)
            {
                return;
            }
            e.DrawBackground();
            e.DrawFocusRectangle();
            if (cmbWriteIO.Items.Count > e.Index)
            {
                ConfigIO io = (ConfigIO)cmbWriteIO.Items[e.Index];
                e.Graphics.DrawString(io.DisplayStr, e.Font, new SolidBrush(e.ForeColor), e.Bounds.X, e.Bounds.Y + 3);
            }
        }

        private void FrmIOStatus_Shown(object sender, EventArgs e)
        {
            timer1.Start();
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            if (equipBase.runStatus.Equals(LineRunStatus.Wait))
            {
                LogUtil.info(equipBean.Name + "点击:启动");
                StartDebug();
            }
            else
            {
                LogUtil.info(equipBean.Name + "点击:复位");
                Reset();
            }
        }
        internal override void FormStatus(bool isStart)
        {
            //btnStart.Enabled = !isStart;
            btnStop.Enabled = true;
            if (isStart)
            {
                btnStart.Text = "复位";
            }
            else
            {
                btnStart.Text = "启动";
            }
            //btnReset.Enabled = isStart; 
        }

        private void btnStop_Click(object sender, EventArgs e)
        {
            LogUtil.info(equipBean.Name + "点击:停止");
            StopRun();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void chkDebug_CheckedChanged(object sender, EventArgs e)
        {
            StationEquip sta= equipBase as StationEquip;
            if(sta!=null)
            {
                sta.IsDebug = chkDebug.Checked;
            }
        }
    }

}