IOControl.cs 8.5 KB
using DeviceLibrary;
using OnlineStore.Common;
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;

namespace TheMachine
{
    using crc = OnlineStore.CodeResourceControl;
    public partial class IOControl : UserControl
    {
        private Robot_Config Config;
        string DeviceKey = "root";


        public void Init(Robot_Config c, string devicekey) {
            Config = c;
            DeviceKey = devicekey;
            try
            {
                LoadIOList();
            }
            catch (Exception ee)
            {
                MessageBox.Show("Config LoadIOList:" + ee.ToString());
            }
        }
        readonly Timer t1 = new Timer();
        Dictionary<string, IOTextControl> DIControlList;
        Dictionary<string, IOTextControl> DOControlList;

        public IOControl()
        {
            InitializeComponent();
            this.Tag = "not";
            if (DesignMode)
                return;

            DIControlList = new Dictionary<string, IOTextControl>();
            DOControlList = new Dictionary<string, IOTextControl>();
            t1.Stop();
            t1.Interval = 500;
            t1.Tick += T1_Tick;
            GC.KeepAlive(t1);
            //RobotManage.LoadFinishEvent += RobotManage_LoadFinishEvent;
        }
        private void IOControl_Load(object sender, EventArgs e)
        {
            OnlineStore.CodeResourceControl.LanguageChangeEvent += CodeResourceControl_LanguageChange;
            CodeResourceControl_LanguageChange(null, null);
        }

        private void CodeResourceControl_LanguageChange(object sender, EventArgs e)
        {
            if (DesignMode)
                return;
            OnlineStore.CodeResourceControl.LanguageProcess(this);

            this.Invoke((EventHandler)delegate
            {
                try
                {
                    LoadIOList();
                }
                catch (Exception ee)
                {
                    MessageBox.Show("CodeResourceControl_LanguageChange LoadIOList:" + ee.ToString());
                }
            });
            
        }

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

        private void LoadIOList()
        {
            if (Config == null)
                return;

            t1.Stop();
            this.SuspendLayout();
            DIControlList.Clear();
            DOControlList.Clear();
            int roleindex = 0;
            this.tableLayoutPanel1.Controls.Clear();
            this.tableLayoutPanel1.RowStyles.Clear();
            this.tableLayoutPanel1.RowCount = Config.DIList[DeviceKey].Count;
            foreach (ConfigIO ioValue in Config.DIList[DeviceKey].Values)
            {
                //if (ioValue.SubType.Equals(0))
                {
                    this.tableLayoutPanel1.RowStyles.Add(new RowStyle(SizeType.Absolute, 26));
                    IOTextControl control = new IOTextControl(ioValue.ElectricalDefinition + "_" + crc.GetString(ioValue.ProType + "_" + ioValue.ProName, ioValue.Explain), ioValue.ProName);

                    this.tableLayoutPanel1.Controls.Add(control, 0, roleindex);
                    roleindex++;
                    DIControlList.Add(ioValue.ProName, control);
                }
            }
            this.tableLayoutPanel2.Controls.Clear();
            this.tableLayoutPanel2.RowStyles.Clear();
            this.tableLayoutPanel2.RowCount = Config.DOList.Count;
            roleindex = 0;
            foreach (ConfigIO ioValue in Config.DOList[DeviceKey].Values)
            {
                //if (ioValue.SubType.Equals(0))
                {
                    this.tableLayoutPanel2.RowStyles.Add(new RowStyle(SizeType.Absolute, 28));
                    IOTextControl control = new IOTextControl(ioValue.ElectricalDefinition + "_" + crc.GetString(ioValue.ProType + "_" + ioValue.ProName, ioValue.Explain), ioValue.ProName);
                    control.Click += Control_Click;
                    this.tableLayoutPanel2.Controls.Add(control, 0, roleindex);
                    roleindex++;
                    DOControlList.Add(ioValue.ProName, control);
                }
            }


            cmbWriteIO.DataSource = new List<ConfigIO>(Config.DOList[DeviceKey].Values);
            cmbWriteIO.ValueMember = "ProName";
            cmbWriteIO.DisplayMember = "DisplayStr";
            this.ResumeLayout(false);
            t1.Start();
        }
        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 ReadIOList()
        {
            foreach (string key in DIControlList.Keys)
            {
                IOTextControl control = DIControlList[key];
                ConfigIO io = Config.DIList[DeviceKey][key];
                int iov = (int)IOManager.GetDIValue("", 0, io.GetIOAddr());
                if (iov != control.IOValue)
                {
                    control.IOValue = iov;
                    control.ShowData();
                }
            }
            foreach (string key in this.DOControlList.Keys)
            {
                IOTextControl control = DOControlList[key];
                ConfigIO io = Config.DOList[DeviceKey][key];
                int iov = (int)IOManager.GetDOValue("", 0, io.GetIOAddr());
                if (iov != control.IOValue)
                {
                    control.IOValue = iov;
                    control.ShowData();
                }
            }

        }

        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();
                    IOTextControl newControl = DOControlList[io.ProName];
                    if (selectControl != null) { selectControl.BackColor = Color.White; }
                    newControl.BackColor = Color.SkyBlue;
                    selectControl = newControl;
                }
            }
        }
        private ConfigIO GetSelectDO()
        {
            string text = cmbWriteIO.SelectedValue.ToString();
            if (Config.DOList[DeviceKey].ContainsKey(text))
            {
                ConfigIO io = Config.DOList[DeviceKey][text];
                return io;
            }
            return null;
        }
        private void btnOpenDo_Click(object sender, EventArgs e)
        {
            WriteDO(IO_VALUE.HIGH);
        }
        private void btnCloseDO_Click(object sender, EventArgs e)
        {
            WriteDO(IO_VALUE.LOW);
        }
        private void WriteDO(IO_VALUE value)
        {
            int index = FormUtil.GetIntValue(txtDOIndex);
            //   IO_VALUE value = checkBox1.Checked ? IO_VALUE.HIGH : IO_VALUE.LOW;
            int time = FormUtil.GetIntValue(txtWriteTime);
            int slaveId = 0;
            if (time > 0)
            {
                IOManager.WriteSingleDO("HC", (byte)slaveId, (ushort)index, (IO_VALUE)value, time);
            }
            else
            {
                IOManager.WriteSingleDO("HC", (byte)slaveId, (ushort)index, (IO_VALUE)value);
            }
        }

        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);
                e.Graphics.DrawString(io.ElectricalDefinition + "_" + crc.GetString(io.ProName, io.Explain), e.Font, new SolidBrush(e.ForeColor), e.Bounds.X, e.Bounds.Y + 3);
            }
        }
    }
}