FrmLineIO.cs 15.0 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;
using System.Security.Cryptography;

namespace OnlineStore.AssemblyLine
{
    internal partial class FrmLineIO : FrmBase
    {
        internal FrmLineIO()
        {
            Control.CheckForIllegalCrossThreadCalls = false;
            InitializeComponent();
            LoadIOList();
        }
        Dictionary<string, IOTextControl> DIControlList = new Dictionary<string, IOTextControl>();
        Dictionary<string, IOTextControl> DOControlList = new Dictionary<string, IOTextControl>();
        public string getKey(int subType,string name,string electricalDefinition)
        {
            return $"{subType}#{name}#{electricalDefinition}";
        }
        private void LoadIOList()
        {
            int count = 18;
            int roleindex = 0;
            this.tableLayoutPanel1.RowStyles.Clear();
            this.tableLayoutPanel1.RowCount = count;
            //this.tableLayoutPanel3.RowStyles.Clear();
            //if (LineManager.Config.DIList.Count > count)
            //{
            //    this.tableLayoutPanel3.RowCount = LineManager.Config.DIList.Count - count;
            //}
            //else
            //{
            //    this.tableLayoutPanel3.RowCount = 0;
            //}
            foreach (DeviceConfig deviceconfig in LineManager.allConfigMap.Values)
            {
                foreach (ConfigIO ioValue in deviceconfig.DIList.Values)
                {
                    IOTextControl control = new IOTextControl(ioValue.ElectricalDefinition + "_" + ioValue.Explain, ioValue.ProName);                 
                    {
                        this.tableLayoutPanel1.RowStyles.Add(new RowStyle(SizeType.Absolute, 28));
                        this.tableLayoutPanel1.Controls.Add(control, 0, roleindex);
                    }
                    roleindex++;
                    DIControlList.Add(getKey(ioValue.SubType,ioValue.ProName,ioValue.ElectricalDefinition), control);
                }
            }
            tableLayoutPanel2.RowStyles.Clear();
            this.tableLayoutPanel2.RowCount = count;
            //this.tableLayoutPanel4.RowStyles.Clear();
            //this.tableLayoutPanel4.RowCount = count;
            roleindex = 0;
            foreach (DeviceConfig deviceconfig in LineManager.allConfigMap.Values)
            {
                foreach (ConfigIO ioValue in deviceconfig.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++;
                    DOControlList.Add(getKey(ioValue.SubType, ioValue.ProName, ioValue.ElectricalDefinition), control);
                    cmbWriteIO.Items.Add(ioValue);
                }
            }
            this.SuspendLayout();    //此处为不闪屏,一定要有的! 

            //cmbWriteIO.DataSource = new List<ConfigIO>(LineManager.Config.DOList.Values);
            //cmbWriteIO.ValueMember = "ProName";
            //cmbWriteIO.DisplayMember = "DisplayStr";
            if(cmbWriteIO.Items.Count > 0)
                cmbWriteIO.SelectedIndex = 0;
        }
        private void Control_Click(object sender, EventArgs e)
        {
            IOTextControl control = (IOTextControl)sender;
            string ElectricalDefinition = control.IOName.Split('_')[0];
            List<string> keyList = new List<string>(DOControlList.Keys);

            int index = keyList.FindIndex(s=>s.Split('#').Contains(ElectricalDefinition));
            if (index >= 0)
            {
                cmbWriteIO.SelectedIndex = index;
            }
        }
        private void timer1_Tick(object sender, EventArgs e)
        {

            if (this.Visible)
            {
                lblMsg.Text = LineManager.Line.WarnMsg;
                ReadIOList();
                lblMoveInfo.Text = LineManager.Line.GetMoveStr();
            }
        }

        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 ConfigIO GetSelectDO()
        {
            ConfigIO io = cmbWriteIO.SelectedItem as ConfigIO;
            if (io != null)
            {
                if (LineManager.allConfigMap[io.SubType].DOList.ContainsKey(io.ProName))
                {
                    return io;
                }
            }
            return null;
        }
        IOTextControl selectControl = null;
        private void WriteDO(IO_VALUE value)
        {
            string deviceName = txtDoName.Text;
            int index = FormUtil.GetIntValue(txtDOIndex);
            int time = FormUtil.GetIntValue(txtWriteTime);

            if (time > 0)
            {
                IOManager.instance.WriteSingleDO(deviceName, (byte)0, (ushort)index, value, time);
            }
            else
            {
                IOManager.instance.WriteSingleDO(deviceName, (byte)0, (ushort)index, value);
            }
        }

        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;
                    txtsubType.Text = io.SubType.ToString();
                    IOTextControl newControl = DOControlList[getKey(io.SubType, io.ProName, io.ElectricalDefinition)];
                    if (selectControl != null) { selectControl.BackColor = Color.White; }
                    newControl.BackColor = Color.SkyBlue;
                    selectControl = newControl;

                }
            }
        }
        private void btnWriteSingleDO_Click(object sender, EventArgs e)
        {
            WriteDO(IO_VALUE.HIGH);
        }
        private void button1_Click(object sender, EventArgs e)
        {
            WriteDO(IO_VALUE.LOW);
        }
        private void FrmStoreIOStatus_Load(object sender, EventArgs e)
        {
        }

        //private void btnUpdateTray_Click(object sender, EventArgs e)
        //{
        //    //int num = cmbTray.SelectedIndex + 101;
        //    int num = (int)numTrayNum.Value;
        //    DialogResult resut = MessageBox.Show("确定手动更新托盘【" + num + "】的信息?", "提示", MessageBoxButtons.YesNo);
        //    if (resut.Equals(DialogResult.Yes))
        //    {
        //        int trayType = cmbTrayType.SelectedIndex;
        //        string posId = txtTrayPosid.Text;
        //        int ph = Convert.ToInt32(cmbHeight.Text);
        //        int pw = Convert.ToInt32(cmbWidth.Text);
        //        bool hasTray = trayType > 0;
        //        bool inNg = chbInStoNG.Checked;
        //        bool isSmall = chbSmall.Checked;
        //        bool urg = rdbUrg.Checked;
        //        bool cutReel = rdbCutreel.Checked;
        //        int loc = FormUtil.GetIntValue(txtTrayLoc);
        //        string rfid = txtRfid.Text.Trim();
        //        string code = txtTrayCode.Text.Trim();
        //        if (hasTray)
        //        {
        //            if (String.IsNullOrEmpty(code))
        //            {
        //                code = "CodeTest";
        //            }
        //           // InOutParam inoutP = new InOutParam(num, code, posId, ph, pw, inNg, urg, cutReel, isSmall, rfid, loc);
        //            //TrayManager.UpdateTrayInfo(num, hasTray, trayType, inoutP, "InStoreNG ");
        //        }
        //        else
        //        {
        //            //TrayManager.UpdateTrayInfo(num, hasTray);
        //        }
        //        if (trayType.Equals(1) && inNg)
        //        {
        //           // TrayManager.UpdateInStoreNG(num, inNg, "手动更新");
        //        }

        //        //TrayInfo info = TrayManager.GetTrayInfo(num);
        //        //MessageBox.Show("更新成功:\r" + info.ToStr());
        //        //LogUtil.info("界面点击手动更新托盘信息:" + info.ToStr());
        //    }
        //}

        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];
                string[] keys = key.Split('#');
                if(keys!=null && keys.Length>1)
                {
                    int subtype = int.Parse(keys[0]);
                    int iov = (int)IOManager.IOValue(keys[1],subtype);
                    if (iov != control.IOValue)
                    {
                        control.IOValue = iov;
                        control.ShowData();
                    }
                }
            }
            foreach (string key in this.DOControlList.Keys)
            {
                IOTextControl control = DOControlList[key];
                string[] keys = key.Split('#');
                if (keys != null && keys.Length >1)
                {
                    int subtype = int.Parse(keys[0]);
                    int iov = (int)IOManager.DOValue(keys[1], subtype);
                    if (iov != control.IOValue)
                    {
                        control.IOValue = iov;
                        control.ShowData();
                    }
                }
            }
        }

        private void FrmIOStatus_Shown(object sender, EventArgs e)
        {
            timer1.Start();
        }
        protected void BtnMove(Button btn, string defaultText, string targetText, string ioHighType)
        {
            LogUtil.info(LineManager.Line.Name + "点击【" + btn.Text + "】 ");
            if (btn.Text.Equals(defaultText))
            {
                LineManager.Line.IOMove(ioHighType, IO_VALUE.HIGH);
                btn.Text = targetText;
                btn.BackColor = Color.Aqua;
            }
            else
            {
                LineManager.Line.IOMove(ioHighType, IO_VALUE.LOW);
                btn.Text = defaultText;
                btn.BackColor = Color.White;
            }
        }
        private void btnDriveMotor_Run_Click(object sender, EventArgs e)
        {
            LogUtil.info("界面点击:" + btnDriveMotor_Run.Text);
            LineManager.Line.WriteDrivetMotorRun(IO_VALUE.HIGH);
        }


        private void btnCloseAll_Click(object sender, EventArgs e)
        {
            IOManager.instance.CloseAllDO();
        }

        private void btnLineStop_Click(object sender, EventArgs e)
        {
            LogUtil.info("界面点击:" + btnLineStop.Text);
            LineManager.Line.WriteDrivetMotorRun(IO_VALUE.LOW);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            ContainerManager.ClearTrayInfo();
        }

        //private void btnClear_Click(object sender, EventArgs e)
        //{
        //    //int num = cmbTray.SelectedIndex + 101;
        //    int num = (int)numTrayNum.Value;
        //    if (num < 100)
        //    {
        //        MessageBox.Show("请输入大于100的托盘号", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
        //        return;
        //    }
        //    TrayInfo tray = TrayManager.GetTrayInfo(num);

        //    DialogResult resut = MessageBox.Show("确定手动清空托盘【" + num + "】的信息?\r\n " + tray.ToStr() + "", "提示", MessageBoxButtons.YesNo);
        //    if (resut.Equals(DialogResult.Yes))
        //    {
        //        TrayManager.ClearInstore(tray);
        //        TrayManager.UpdateTrayInfo(num);
        //        TrayInfo info = TrayManager.GetTrayInfo(num);
        //        MessageBox.Show("更新成功:\r" + info.ToStr());
        //        LogUtil.info("界面点击手动清空托盘,清空前【" + tray.ToStr() + "】,清空后:" + info.ToStr());
        //    }

        //}

        //private void numTrayNum_ValueChanged(object sender, EventArgs e)
        //{
        //    int trayNum = (int)numTrayNum.Value;
        //    if (trayNum >= 0)
        //    {
        //        TrayInfo tray = TrayManager.GetTrayInfo(trayNum);
        //        cmbTrayType.SelectedIndex = tray.InOrOutStore;


        //        if (tray.IsFull)
        //        {
        //            txtTrayCode.Text = tray.InoutPar.WareCode;
        //            txtTrayPosid.Text = tray.InoutPar.PosId;
        //            cmbWidth.Text = tray.InoutPar.PlateW.ToString();
        //            cmbHeight.Text = tray.InoutPar.PlateH.ToString();
        //            rdbCutreel.Checked = tray.InoutPar.cutReel;
        //            rdbUrg.Checked = tray.InoutPar.urgentReel;
        //            chbSmall.Checked = tray.InoutPar.smallReel;
        //            txtTrayLoc.Text = tray.InoutPar.rfidLoc.ToString();
        //            chbInStoNG.Checked = tray.InoutPar.IsNG;
        //            txtRfid.Text = tray.InoutPar.rfid;
        //        }
        //        else
        //        {
        //            txtTrayCode.Text = "";
        //            txtTrayPosid.Text = "";
        //            cmbWidth.Text = "";
        //            cmbHeight.Text = "";
        //            rdbCutreel.Checked = false;
        //            rdbUrg.Checked = false;
        //            chbSmall.Checked = false;
        //            txtTrayLoc.Text = "";
        //            chbInStoNG.Checked = false;
        //            txtRfid.Text = "";

        //        }
        //    }
        //}
    }
}