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

            this.boxBean = store;
            this.StoreId = store.StoreID;
            LoadIOList();
        }

        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.StoreDIList.Count;
            foreach (ConfigIO ioValue in boxBean.Config.StoreDIList.Values)
            {
                this.tableLayoutPanel1.RowStyles.Add(new RowStyle(SizeType.Absolute, 28));
                IOTextControl control = new IOTextControl(ioValue.DisplayName, ioValue.ProName);
                control.Click += Control_Click; 
                this.tableLayoutPanel1.Controls.Add(control, 0, roleindex);
                roleindex++;
                DIControlList.Add(ioValue.ProName, control);
            }

            tableLayoutPanel2.RowStyles.Clear();
            this.tableLayoutPanel2.RowCount = boxBean.Config.StoreDOList.Count;
            roleindex = 0;
            foreach (ConfigIO ioValue in boxBean.Config.StoreDOList.Values)
            {
                this.tableLayoutPanel2.RowStyles.Add(new RowStyle(SizeType.Absolute, 28));
                IOTextControl control = new IOTextControl(ioValue.DisplayName, ioValue.ProName);
                control.Click += Control_Click; 
                this.tableLayoutPanel2.Controls.Add(control, 0, roleindex);
                roleindex++;
                DOControlList.Add(ioValue.ProName, control);
            }
            this.SuspendLayout();    //此处为不闪屏,一定要有的! 

            cmbWriteIO.DataSource = new List<ConfigIO>(boxBean.Config.StoreDOList.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 timer1_Tick(object sender, EventArgs e)
        {
            if (chbAutoRead.Checked && this.Visible)
            {
                ReadIOList();
            }
        }

        private void FrmTest_FormClosing(object sender, FormClosingEventArgs e)
        {
            //AIManager.Instance.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(LOGGER, ex.StackTrace);
            }
        }
        

        private ConfigIO GetSelectDO()
        {
            string text = cmbWriteIO.SelectedValue.ToString();
            if (boxBean.Config.StoreDOList.ContainsKey(text))
            {
                ConfigIO io = boxBean.Config.StoreDOList[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;
                    IOTextControl newControl = DOControlList[io.ProName];
                    if (selectControl != null) { selectControl.BackColor = Color.White; }
                    newControl.BackColor = Color.SkyBlue;
                    selectControl = newControl;

                }
            }
        }

        private void FrmStoreIOStatus_Load(object sender, EventArgs e)
        {
            //AIManager.Instance.NeedShow = true;
            if (boxBean.Config.StoreDOList.ContainsKey(IO_Type.Device_Led))
            {
                btnDevideLed.Visible = true;
                btnCDLed.Visible = true;
            }
            else
            {
                btnDevideLed.Visible = false;
                btnCDLed.Visible = false;
            }
        }

        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)IOManager.instance.GetIOValue(boxBean.Config.StoreDIList[key]);
                if (iov != control.IOValue)
                {
                    control.IOValue = iov;
                    control.ShowData();
                }
            } foreach (string key in this.DOControlList.Keys)
            {
                IOTextControl control = DOControlList[key];
                int iov = (int)IOManager.instance.GetIOValue(boxBean.Config.StoreDOList[key]);
                if (iov != control.IOValue)
                {
                    control.IOValue = iov;
                    control.ShowData();
                }
            }

            double ai1Value = IOManager.GetADIOValue(StoreManager.Config.AIDI1_Addr);
            double ai2Value = IOManager.GetADIOValue(StoreManager.Config.AIDI2_Addr);
            double ai3Value = IOManager.GetADIOValue(StoreManager.Config.AIDI3_Addr);
            double ai4Value = IOManager.GetADIOValue(StoreManager.Config.AIDI4_Addr);
            //double ai3Value = AIManager.Instance.GetAIValue(boxBean.Config.AIDevice_IP, 3);

            txtAI1.Text = ai1Value.ToString();
            txtAI2.Text = ai2Value.ToString();
            txtAI3.Text = ai3Value.ToString();
            txtAI4.Text = ai4Value.ToString();
            //txtAI3.Text = ai3Value.ToString();

            txtAIResult1.Text = ConvertAI(ai1Value,boxBean.Config.AIDI1_DefaultPosition).ToString();
            txtAIResult2.Text = ConvertAI(ai2Value, boxBean.Config.AIDI2_DefaultPosition).ToString();
            txtAIResult3.Text = ConvertAI(ai3Value, boxBean.Config.AIDI3_DefaultPosition).ToString();
            txtAIResult4.Text = ConvertAI(ai4Value, boxBean.Config.AIDI4_DefaultPosition).ToString();
            //txtAIResult3.Text = AIManager.ConvertAI(ai3Value, boxBean.Config.AIDI3_DefaultPosition).ToString();

            txtHeight.Text = boxBean.GetHeight().ToString();
            txtSize.Text = boxBean.GetSize().ToString();
        }
        public static double ConvertAI(double aiValue, double defaultValue)
        {
            double xishu = (double)StoreManager.Config.AI_ConvertPosition;
            double result = Math.Round((aiValue - defaultValue) / xishu, 2);
            return result;
        }
        private void btnOpenDoor_Click(object sender, EventArgs e)
        {
            LogUtil.info("点击按钮:" + btnOpenDoor.Text);
            boxBean.OpenDoor(false);
        } 
        private void btnCloseDoor_Click(object sender, EventArgs e)
        {
            LogUtil.info("点击按钮:" + btnCloseDoor.Text);
            boxBean.CloseDoor(false);
        }
         
        private void FrmIOStatus_Shown(object sender, EventArgs e)
        {  
            timer1.Start();
        }

        private void btnOpenAxisBreak_Click(object sender, EventArgs e)
        {
            //IOManager.IOMove(IO_Type.Axis_Brake, IO_VALUE.HIGH);
        }

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

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

        private void btnOpenLed_Click(object sender, EventArgs e)
        { 
            IOManager.IOMove(IO_Type.Camera_Led, IO_VALUE.HIGH);
        }

        private void btnCloseLed_Click(object sender, EventArgs e)
        { 
            IOManager.IOMove(IO_Type.Camera_Led, IO_VALUE.LOW);
        }

        private void btnDevideLed_Click(object sender, EventArgs e)
        {

            IOManager.IOMove(IO_Type.Device_Led, IO_VALUE.HIGH);
        }

        private void btnCDLed_Click(object sender, EventArgs e)
        {

            IOManager.IOMove(IO_Type.Device_Led, IO_VALUE.LOW);
        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }

        private void btn_SetHigh_Click(object sender, EventArgs e)
        {
            string deviceName = txtDoName.Text;
            int index = FormUtil.GetIntValue(txtDOIndex);
            IO_VALUE value = IO_VALUE.HIGH;
            int time = FormUtil.GetIntValue(txtWriteTime);
            int slaveId = 0;// 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 btn_setlow_Click(object sender, EventArgs e)
        {
            string deviceName = txtDoName.Text;
            int index = FormUtil.GetIntValue(txtDOIndex);
            IO_VALUE value = IO_VALUE.LOW;
            int time = FormUtil.GetIntValue(txtWriteTime);
            int slaveId = 0;// 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);
            }
        }
    }
}