DO.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. using System.Data;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using Aitex.Core.RT.IOCore;
  10. using Aitex.Core.Util;
  11. namespace Aitex.Core.Backend
  12. {
  13. public partial class DO : UserControl, IIOView
  14. {
  15. PeriodicJob _thread;
  16. public DO()
  17. {
  18. InitializeComponent();
  19. this.Load += new EventHandler(DO_Load);
  20. }
  21. void DO_Load(object sender, EventArgs e)
  22. {
  23. if (this.panel1.Controls.Count > 0)
  24. return;
  25. List<Tuple<int, int, string>> ioList = IO.GetIONameList("", IOType.DO);
  26. //List<Tuple<int, int, string>> ioListRemote = IO.GetIONameList("remote", IOType.DO);
  27. //foreach (var tuple in ioListRemote)
  28. //{
  29. // ioList.Add(tuple);
  30. //}
  31. int index = 0;
  32. foreach (var item in ioList)
  33. {
  34. Button button1 = new Button();
  35. //button1.Margin = new Padding(0,0,5,5);
  36. button1.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
  37. button1.Location = new System.Drawing.Point(index % 3 * 305, index / 3 * 30);
  38. //button1.Name = string.Format("DO_{0}", i);
  39. button1.Size = new System.Drawing.Size(300, 25);
  40. button1.Text = string.Format("DO-{0}. {1}", item.Item1, item.Item3);
  41. button1.Tag = item.Item3;
  42. button1.BackColor = Color.LightGray;
  43. button1.TextAlign = ContentAlignment.MiddleCenter;
  44. button1.Font = new System.Drawing.Font("Arial,SimSun", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
  45. button1.UseVisualStyleBackColor = false;
  46. button1.Click += (sender2, e2) =>
  47. {
  48. var btn = (Button)sender2;
  49. string name = (string)btn.Tag;
  50. string reason;
  51. if (!IO.DO[name].SetValue(!IO.DO[name].Value, out reason))
  52. {
  53. MessageBox.Show(reason);
  54. }
  55. };
  56. this.panel1.Controls.Add(button1);
  57. index++;
  58. }
  59. _thread = new PeriodicJob(500, OnTimer, "DOTimer", false, true);
  60. VisibleChanged += (sender1, e1) =>
  61. {
  62. if (Visible) _thread.Start(); else _thread.Pause();
  63. };
  64. }
  65. bool OnTimer()
  66. {
  67. Invoke(new Action(() =>
  68. {
  69. foreach (Button ctrl in this.panel1.Controls)
  70. {
  71. if (ctrl != null)
  72. {
  73. string name = (string)ctrl.Tag;
  74. ctrl.BackColor = IO.DO[(string)ctrl.Tag].Value ? Color.Lime : Color.LightGray;
  75. }
  76. }
  77. }));
  78. return true;
  79. }
  80. public void EnableTimer(bool enable)
  81. {
  82. if (enable)
  83. _thread.Start();
  84. else
  85. _thread.Pause();
  86. }
  87. public void Close()
  88. {
  89. if (_thread != null)
  90. _thread.Stop();
  91. }
  92. }
  93. }