FrmIOStatus.cs 12.7 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.ACPackingStore
{
    public partial class FrmIOStatus : FrmBase
    {
        private int StoreId = 1;
        private AC_BOX_Bean boxBean;
    //     public static readonly ILog LOGGER = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
        public FrmIOStatus()
        {
            Control.CheckForIllegalCrossThreadCalls = false;
            InitializeComponent();
        }
        public FrmIOStatus(AC_BOX_Bean store)
        {
            Control.CheckForIllegalCrossThreadCalls = false;
            InitializeComponent();
      
            this.boxBean = store;
            this.StoreId = store.DeviceID;
            LoadIOList();
        }

        private void FrmStoreIOStatus_Load(object sender, EventArgs e)
        {
            //KNDAIManager.NeedShow = true;
            this.Text = boxBean.Name + "_IO查看";
        }
        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 = boxBean.Config.DIList.Count;
            foreach (ConfigIO ioValue in boxBean.Config.DIList.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 = boxBean.Config.DOList.Count;
            roleindex = 0;
            foreach (ConfigIO ioValue in boxBean.Config.DOList.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();    //此处为不闪屏,一定要有的! 

            cmbWriteIO.DataSource = new List<ConfigIO>(boxBean.Config.DOList.Values);
            cmbWriteIO.ValueMember = "ProName";
            cmbWriteIO.DisplayMember = "DisplayStr";
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (this.Visible)
            {
                ReadIOList();
            }
        }

        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.StackTrace);
            }
        }

        private ConfigIO GetSelectDO()
        {
            string text = cmbWriteIO.SelectedValue.ToString();
            if (boxBean.Config.DOList.ContainsKey(text))
            {
                ConfigIO io = boxBean.Config.DOList[text];
                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];
                    selectControl = newControl;
                }
            }
        }


        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 btnReadIO_Click(object sender, EventArgs e)
        {
            ReadIOList();
        }

        private void ReadIOList()
        {
            foreach (string key in DIControlList.Keys)
            {
                IOTextControl control = DIControlList[key];
                int iov = (int)boxBean.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)boxBean.IOValue(key );
                if (iov != control.IOValue)
                {
                    control.IOValue = iov;
                    control.ShowData();
                }
            }

        }
        protected void BtnMove(Button btn, string defaultText, string targetText, string ioHighType)
        {
            LogUtil.info("点击【" + btn.Text + "】 ");
            if (btn.Text.Equals(defaultText))
            {
                boxBean.IOMove(ioHighType, IO_VALUE.HIGH);
                btn.Text = targetText;
                btn.BackColor = Color.Aqua;
            }
            else
            {
                boxBean.IOMove(ioHighType, IO_VALUE.LOW);
                btn.Text = defaultText;
                btn.BackColor = Color.White;
            }
        }

        protected void BtnMove(Button btn, string defaultText, string targetText, string ioLowType, string ioHighType)
        {
            LogUtil.info("点击【" + btn.Text + "】 ");
            if (btn.Text.Equals(defaultText))
            {
                boxBean.CylinderMove(null, ioLowType, ioHighType);
                btn.Text = targetText;
                btn.BackColor = Color.Aqua;
            }
            else
            {
                boxBean.CylinderMove(null, ioHighType, ioLowType);
                btn.Text = defaultText;
                btn.BackColor = Color.SkyBlue;
            }
        }


        private void btnLocationUp_Click(object sender, EventArgs e)
        {

        //    boxBean.CylinderMove(null, IO_Type.LocationCylinder_Down, IO_Type.LocationCylinder_Up);
        }

        private void btnLocationDown_Click(object sender, EventArgs e)
        {
         //   boxBean.CylinderMove(null, IO_Type.LocationCylinder_Up, IO_Type.LocationCylinder_Down);
        }

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

        private void btnOpenAxisBreak_Click(object sender, EventArgs e)
        {
            BtnMove(btnOpenAxisBreak, "打开刹车", "关闭刹车", IO_Type.Axis_Brake);
        }

        private void btnCloseAxisBreak_Click(object sender, EventArgs e)
        {
            boxBean.IOMove(IO_Type.Axis_Brake, IO_VALUE.LOW);
        }

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

        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 void btnTopUp_Click(object sender, EventArgs e)
        {
            boxBean.CylinderMove(null, IO_Type.TopCylinder_Down, IO_Type.TopCylinder_Up);
        }

        private void btnTopDown_Click(object sender, EventArgs e)
        {
            boxBean.CylinderMove(null, IO_Type.TopCylinder_Up, IO_Type.TopCylinder_Down);
        }

        private void btnNGDoorUp_Click(object sender, EventArgs e)
        {
            boxBean.CylinderMove(null, IO_Type.NGDoorCylinder_Down, IO_Type.NGDoowCylinder_Up);
        }

        private void btnNGDoorDown_Click(object sender, EventArgs e)
        {
            boxBean.CylinderMove(null, IO_Type.NGDoowCylinder_Up, IO_Type.NGDoorCylinder_Down);
        }

        private void btnCamerLed_Click(object sender, EventArgs e)
        {
            BtnMove(btnCamerLed, "打开光源", "关闭光源", IO_Type.Camera_Led);
        }

        private void button4_Click(object sender, EventArgs e)
        {
            //BtnMove(button4, "线体正转", "线体反转", IO_Type.Line_Run, IO_Type.Line_BackRun);
            boxBean.LineRun();
        }

        private void btnStopMove_Click(object sender, EventArgs e)
        {
            //boxBean.IOMove(IO_Type.Line_Run, IO_VALUE.LOW);
            //boxBean.IOMove(IO_Type.Line_BackRun, IO_VALUE.LOW);
            boxBean.LineStop();
        }

        private void btnStoreOn_Click(object sender, EventArgs e)
        {
            BtnMove(btnStoreOn, "料仓运转ON", "料仓运转OFF", IO_Type.Run_Signal);
        }

        private void btnBlow_Click(object sender, EventArgs e)
        {
            BtnMove(btnBlow, "开始吹气", "停止吹气", IO_Type.StartOrStopBlow);
        }

        private void btnCloseCyDo_Click(object sender, EventArgs e)
        {
            boxBean.DoorBean.Stop();
            boxBean.IOMove(IO_Type.EntranceDoor_Open, IO_VALUE.LOW);
            boxBean.IOMove(IO_Type.EntranceDoor_Close, IO_VALUE.LOW);
            boxBean.IOMove(IO_Type.TopCylinder_Down, IO_VALUE.LOW);
            boxBean.IOMove(IO_Type.TopCylinder_Up, IO_VALUE.LOW);
            boxBean.IOMove(IO_Type.NGDoorCylinder_Down, IO_VALUE.LOW);
            boxBean.IOMove(IO_Type.NGDoowCylinder_Up, IO_VALUE.LOW);
           // boxBean.IOMove(IO_Type.LocationCylinder_Down, IO_VALUE.LOW);
        //  boxBean.IOMove(IO_Type.LocationCylinder_Up, IO_VALUE.LOW);
        }

        private void btnOpenDoor_Click(object sender, EventArgs e)
        {
            //    boxBean.CylinderMove(null, IO_Type.EntranceDoor_Close, IO_Type.EntranceDoor_Open);
            if (!boxBean.DoorBean.StartOpen(null))
            {
                MessageBox.Show("打开移门失败");
            }
        }
        private void btnCloseDoor_Click(object sender, EventArgs e)
        {
            if (!boxBean.DoorBean.StartClose(null))
            {
                MessageBox.Show("关闭移门失败");
            }
            //   boxBean.CylinderMove(null, IO_Type.EntranceDoor_Open, IO_Type.EntranceDoor_Close);
        }
        private void btnDoorStop_Click(object sender, EventArgs e)
        {
            boxBean.DoorBean.Stop();
        }

        private void btnNgLine_Click(object sender, EventArgs e)
        {
            boxBean.IOMove(IO_Type.NGLine_Run, IO_VALUE.HIGH);
        }

        private void btnNGlineStop_Click(object sender, EventArgs e)
        {
            boxBean.IOMove(IO_Type.NGLine_Run, IO_VALUE.LOW);
        }

        private void btnLineBack_Click(object sender, EventArgs e)
        {
            boxBean.LineBackRun();
        }
    }
}