CylinderButton.cs 6.2 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;

namespace TheMachine
{
    using crc = OnlineStore.CodeResourceControl;
    public class CylinderButton : Button
    {
        Timer timer;
        public CylinderButton()
        {
            this.FlatStyle = FlatStyle.Flat;
            this.BackColor = Color.White;
            this.Tag = "not";
            //RobotManage.LoadFinishEvent += RobotManage_LoadFinishEvent;
            this.Click += CylinderButton_Click;
            timer = new Timer();
            timer.Interval = 1000;
            timer.Tick += Timer_Tick;
            GC.KeepAlive(timer);
            this.HandleCreated += CylinderButton_HandleCreated;
        }

        private void CylinderButton_HandleCreated(object sender, EventArgs e)
        {
            if (!DesignMode)
                DataUpdate();
        }

        private void Timer_Tick(object sender, EventArgs e)
        {
            if(Visible)
                StateUpdate();
        }
        private void RobotManage_LoadFinishEvent(bool state, string msg)
        {
            if (state)
            {
                DataUpdate();
                //this.Invoke((EventHandler)delegate { DataUpdate(); });            
            }
        }

        string io_high;
        string io_low;
        ConfigIO configio_high;
        ConfigIO configio_low;
        IO_VALUE io_state = IO_VALUE.LOW;
        [Category("数据"), Description("IO名称"), Browsable(true)]
        public string IO_HIGH
        {
            get
            {
                return io_high;
            }
            set
            {
                io_high = value;
                this.Text = io_high;
            }
        }
        [Category("数据"), Description("IO名称"), Browsable(true)]
        public string IO_LOW
        {
            get
            {
                return io_low;
            }
            set
            {
                io_low = value;

            }
        }
        string devicetype = "root";
        [Category("数据"), Description("设备ID"), Browsable(true)]
        public string DeviceType
        {
            get
            {
                return devicetype;
            }
            set
            {
                devicetype = value;
                DataUpdate();

            }
        }
        void DataUpdate() {
            if (string.IsNullOrEmpty(io_high) || !RobotManage.Config.DOList[devicetype].ContainsKey(io_high))
                this.Text = $"IO {io_high} Not Find";
            else
            {
                if (RobotManage.Config.DOList[devicetype].ContainsKey(io_high))
                    configio_high = RobotManage.Config.DOList[devicetype][io_high];
            }

            if (!string.IsNullOrEmpty(io_low))
            {
                if (!RobotManage.Config.DOList[devicetype].ContainsKey(io_low))
                    this.Text = $"IO {io_low} Not Find";
                else
                {
                    if (RobotManage.Config.DOList[devicetype].ContainsKey(io_low))
                        configio_low = RobotManage.Config.DOList[devicetype][io_low];
                }
            }
            //this.Text = DeviceConfig.AllDOlist[io_high].Explain;
            StateUpdate();
            timer.Start();
        }
        void StateUpdate()
        {
            if (configio_high == null)
                return;

            io_state = IOManager.GetDOValue(configio_high.DeviceName, configio_high.SlaveID, configio_high.GetIOAddr());
            if (configio_low != null)
            {
                if (io_state.Equals(IO_VALUE.LOW))
                {
                    this.Text = configio_high.ElectricalDefinition+" "+crc.GetString(configio_high.ProName,configio_high.Explain);
                    this.BackColor = Color.White;
                }
                else
                {
                    this.Text = configio_low.ElectricalDefinition + " "+ crc.GetString(configio_low.ProName,configio_low.Explain);
                    this.BackColor = Color.LightGreen;
                }
            }
            else
            {
                if (io_state.Equals(IO_VALUE.LOW))
                {
                    this.Text = "(ON) " + configio_high.ElectricalDefinition + " " + crc.GetString(configio_high.ProName, configio_high.Explain);
                    this.BackColor = Color.White;
                    
                }
                else if (configio_low == null)
                {
                    this.Text = "(OFF) " + configio_high.ElectricalDefinition + " " + crc.GetString(configio_high.ProName, configio_high.Explain);
                    this.BackColor = Color.LightGreen;
                }
            }
        }
        private void CylinderButton_Click(object sender, EventArgs e)
        {

            if (io_state.Equals(IO_VALUE.LOW))
            {
                LogUtil.info($"手动点击: Set {configio_high.ElectricalDefinition}{configio_high.Explain}=HIGH");
                IOManager.WriteSingleDO(configio_high.DeviceName, configio_high.SlaveID, configio_high.GetIOAddr(), IO_VALUE.HIGH);
                if (configio_low != null)
                    IOManager.WriteSingleDO(configio_low.DeviceName, configio_low.SlaveID, configio_low.GetIOAddr(), IO_VALUE.LOW);
            }
            else if (configio_low != null)
            {
                LogUtil.info($"手动点击: Set {configio_high.ElectricalDefinition}{configio_high.Explain}=LOW");
                IOManager.WriteSingleDO(configio_high.DeviceName, configio_high.SlaveID, configio_high.GetIOAddr(), IO_VALUE.LOW);
                IOManager.WriteSingleDO(configio_low.DeviceName, configio_low.SlaveID, configio_low.GetIOAddr(), IO_VALUE.HIGH);
            }
            else {
                LogUtil.info($"手动点击: Set {configio_high.ElectricalDefinition}{configio_high.Explain}=LOW");
                IOManager.WriteSingleDO(configio_high.DeviceName, configio_high.SlaveID, configio_high.GetIOAddr(), IO_VALUE.LOW);
            }
        }
    }
}