UC_LedConfig.cs 6.1 KB
using 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;

namespace TheMachine.UC
{
    using crc = OnlineStore.CodeResourceControl;
    public partial class UC_LedConfig : UserControl
    {
        public UC_LedConfig()
        {
            InitializeComponent();
            this.Tag = "not";
            crc.LanguageChangeEvent += Crc_LanguageChangeEvent;
        }
        private void Crc_LanguageChangeEvent(object sender, EventArgs e)
        {
            if (Config == null)
                return;
            LoadLedList();
        }
        private DeviceConfig _Config;
        public DeviceConfig Config
        {
            get { return _Config; }
            set
            {
                if (value == null)
                    return;
                _Config = value;
                LoadLedList();
            }
        }

        void LoadLedList()
        {

            //if (!this.Created)
            //    return;

            if (this.InvokeRequired)
            {
                this.Invoke((EventHandler)delegate
                {
                    try
                    {
                        LoadLedList();
                    }
                    catch (Exception ee)
                    {
                        MessageBox.Show("LoadLedList:" + ee.ToString());
                    }
                });
                return;
            }



            tableLayoutPanel1.SuspendLayout();
            tableLayoutPanel1.Controls.Clear();
            this.tableLayoutPanel1.RowStyles.Clear();
            var statenames = Enum.GetNames(typeof(MachineLedStateE));
            Label label_s = new Label()
            {
                Size = new Size(200, 34),
                Text = "状态,优先级从大到小",
                TextAlign = ContentAlignment.MiddleLeft
            };
            tableLayoutPanel1.Controls.Add(label_s, 0, 0);
            Label label_g = new Label()
            {
                Size = new Size(120, 34),
                Text = "绿灯",
                TextAlign = ContentAlignment.MiddleCenter
            };
            tableLayoutPanel1.Controls.Add(label_g, 1, 0);
            Label label_y = new Label()
            {
                Size = new Size(120, 34),
                Text = "黄灯",
                TextAlign = ContentAlignment.MiddleCenter
            };
            tableLayoutPanel1.Controls.Add(label_y, 2, 0);
            Label label_r = new Label()
            {
                Size = new Size(120, 34),
                Text = "红灯",
                TextAlign = ContentAlignment.MiddleCenter
            };
            tableLayoutPanel1.Controls.Add(label_r, 3, 0);
            int r = 1;
            var ledkeys = RobotManage.mainMachine.MachineLedStateName.Keys;
            foreach (var key in ledkeys)
            {
                //if (r == 3)
                //    return;
                Label button = new Label();
                button.Anchor = (AnchorStyles)(AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top | AnchorStyles.Bottom);
                button.Name = key.ToString();
                button.Text = crc.GetString("ledstate_"+ key, RobotManage.mainMachine.MachineLedStateName[key]);
                button.AutoSize = false;
                button.Size = new System.Drawing.Size(200, 34);
                button.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
                tableLayoutPanel1.Controls.Add(button, 0, r);
                int c = 1;
                foreach (var led in Led.LedColors.Keys)
                {
                    List<lightitem> lightitems = new List<lightitem>();
                    lightitems.Add(new lightitem("无动作", LedState.none));
                    lightitems.Add(new lightitem("关", LedState.off));
                    lightitems.Add(new lightitem("开", LedState.on));
                    lightitems.Add(new lightitem("闪烁", LedState.blink));

                    var selit = lightitems.FindIndex(x => x.value == RobotManage.mainMachine.MachineLedState[key][led]);
                    ComboBox comboBox = new ComboBox();
                    comboBox.DropDownStyle = ComboBoxStyle.DropDownList;
                    comboBox.FormattingEnabled = true;
                    //comboBox.Items.AddRange(new object[] { "none", "on", "off", "blink" });
                    comboBox.Items.AddRange(lightitems.ToArray());
                    comboBox.Name = key.ToString();
                    comboBox.Tag = led;
                    //comboBox.SelectedItem = selit;
                    comboBox.SelectedIndex= selit;
                    tableLayoutPanel1.Controls.Add(comboBox, c, r);
                    c++;
                }
                r++;
            }
            
            tableLayoutPanel1.ResumeLayout(true);
        }

        class lightitem {
            public string Text;
            public LedState value;
            public lightitem(string t, LedState v) {
                Text = t;
                value = v;
            }
            public override string ToString()
            {
                return Text;
            }
        }

        private void button_reset_Click(object sender, EventArgs e)
        {
            RobotManage.mainMachine.DefaultLedCfg();
            LoadLedList();
        }

        private void button_save_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < tableLayoutPanel1.Controls.Count; i++) {
                var cc = tableLayoutPanel1.Controls[i];
                if (!(cc is ComboBox))
                    continue;
                
                Enum.TryParse<MachineLedStateE>(cc.Name, out MachineLedStateE key);
                LedColor led = (LedColor)cc.Tag;

                RobotManage.mainMachine.MachineLedState[key][led] = ((lightitem)(cc as ComboBox).SelectedItem).value;

            }
            if (RobotManage.mainMachine.SaveLedCfg())
                MessageBox.Show("保存成功!");
            else
                MessageBox.Show("保存失败!");
        }
    }
}