Box_IO.cs 8.9 KB
using OnlineStore.Common;
using OnlineStore.DeviceLibrary;
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;
using UserFromControl;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.Button;

namespace OnlineStore.XLRStore.inputForm.uc
{
    public partial class Box_IO : UserControl
    {
        public Box_IO(BoxEquip equip)
        {
            Control.CheckForIllegalCrossThreadCalls = false;
            InitializeComponent();
            this.equip = equip;
            LoadIOList();
            humitempures.Add(ucHumitempure1);
            humitempures.Add(ucHumitempure2);
            humitempures.Add(ucHumitempure3);
            humitempures.Add(ucHumitempure4);
        }
        List<UCHumitempure> humitempures = new List<UCHumitempure>();
        private BoxEquip equip;
        IOTextControl selectControl = null;
        protected Dictionary<string, IOTextControl> DIControlList = new Dictionary<string, IOTextControl>();
        protected Dictionary<string, IOTextControl> DOControlList = new Dictionary<string, IOTextControl>();
        protected void ReadIOList()
        {
            foreach (string key in DIControlList.Keys)
            {
                IOTextControl control = DIControlList[key];
                int iov = (int)IOManager.IOValue(key, equip.DeviceID);
                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, equip.DeviceID);
                if (iov != control.IOValue)
                {
                    control.IOValue = iov;
                    control.ShowData();
                }
            }

        }
        private void LoadIOList()
        {
            int maxCount = 20;
            int roleindex = 0;
            this.tableLayoutPanel1.RowStyles.Clear();
            this.tableLayoutPanel1.RowCount = maxCount;
            //this.tableLayoutPanel3.RowStyles.Clear();
            //this.tableLayoutPanel3.RowCount = maxCount;
            int i = 0;
            foreach (ConfigIO ioValue in equip.Config.DIList.Values)
            {
                IOTextControl control = new IOTextControl(ioValue.ElectricalDefinition + "_" + ioValue.Explain, ioValue.ProName);
                //if (i >= maxCount)
                //{
                //    this.tableLayoutPanel3.RowStyles.Add(new RowStyle(SizeType.Absolute, 28));
                //    this.tableLayoutPanel3.Controls.Add(control, 0, roleindex - maxCount);
                //}
                //else
                {
                    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 equip.Config.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>(equip.Config.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 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();
                    lblAddr.Text = txtDoName.Text + "_" + txtDOIndex.Text;
                    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 ConfigIO GetSelectDO()
        {
            string text = cmbWriteIO.SelectedValue.ToString();
            string newType = text.Replace("SW1_", "SW_").Replace("SW2_", "SW_").Replace("SW3_", "SW_").Replace("SW4_", "SW_");
            if (equip.Config.DOList.ContainsKey(newType))
            {
                ConfigIO io = equip.Config.DOList[newType];
                return io;
            }
            return null;
        }
        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 btnCloseAll_Click(object sender, EventArgs e)
        {
            //foreach (Control con in groupDO.Controls)
            //{
            //    if (con is Button)
            //    {
            //        con.BackColor = BackColor = Color.White;
            //    }
            //}
            //foreach (ConfigIO io in inputEquip.baseConfig.DOList.Values)
            //{
            //    IOManager.instance.WriteSingleDO(io.IO_IP, io.SlaveID, io.GetIOAddr(), IO_VALUE.LOW);
            //}
        }


        private void timer1_Tick(object sender, EventArgs e)
        {
            try
            {
                if (!this.Visible)
                {
                    return;
                }
                ReadIOList();
                List<HumitureParam> huparam = HumitureBean.GetHumitureParams();
                for (int i = 0; i < humitempures.Count; i++)
                {
                    humitempures[i].Humiti = huparam[i].Humidity;
                    humitempures[i].Tempure = huparam[i].Temperate;
                    humitempures[i].Nitrogen = 100 - huparam[i].OxygenV;
                    humitempures[i].HumiName = huparam[i].DeviceAddress;
                }
            }
            catch (Exception ex)
            {
                LogUtil.error(equip.Name + "读取IO定时器出错:", 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 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 Input_IO_Load(object sender, EventArgs e)
        {
            timer1.Start();
        }
    }
}