DI.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 DI : UserControl, IIOView
  14. {
  15. PeriodicJob _thread;
  16. public DI()
  17. {
  18. InitializeComponent();
  19. this.Load += new EventHandler(DI_Load);
  20. }
  21. void DI_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.DI);
  26. //List<Tuple<int, int, string>> ioListRemote = IO.GetIONameList("remote", IOType.DI);
  27. //foreach (var tuple in ioListRemote)
  28. //{
  29. // ioList.Add(tuple);
  30. //}
  31. int totalColumn = 3;
  32. int countPerColumn = (ioList.Count / totalColumn) + ((ioList.Count % totalColumn) > 0 ? 1 : 0);
  33. int index = 0;
  34. foreach (var item in ioList)
  35. {
  36. int row = index % countPerColumn;
  37. int col = index / countPerColumn;
  38. DICtrl diCtrl1 = new DICtrl();
  39. diCtrl1.Location = new System.Drawing.Point(col * 305, row * 30);
  40. diCtrl1.SetName(string.Format("DI-{0}. {1}", item.Item1, item.Item3));
  41. diCtrl1.SetIoName("local", item.Item3);
  42. diCtrl1.Name = string.Format("DI_{0}", item.Item1);
  43. diCtrl1.Size = new System.Drawing.Size(300, 25);
  44. diCtrl1.Tag = item.Item3;
  45. //diCtrl1.Margin = new Padding(0, 0, 5, 5);
  46. this.panel1.Controls.Add(diCtrl1);
  47. index++;
  48. }
  49. _thread = new PeriodicJob(500, OnTimer, "DITimer", false, true);
  50. VisibleChanged += (sender1, e1) =>
  51. {
  52. if (Visible) _thread.Start(); else _thread.Pause();
  53. };
  54. }
  55. bool OnTimer()
  56. {
  57. Invoke(new Action(() =>
  58. {
  59. foreach (DICtrl ctrl in panel1.Controls)
  60. {
  61. string name = (string)ctrl.Tag;
  62. if (ctrl != null)
  63. {
  64. ctrl.SetValue(IO.DI[name].Value, IO.DI[name].Value);
  65. }
  66. }
  67. }));
  68. return true;
  69. }
  70. public void EnableTimer(bool enable)
  71. {
  72. if (enable)
  73. _thread.Start();
  74. else
  75. _thread.Pause();
  76. }
  77. public void Close()
  78. {
  79. if (_thread != null)
  80. _thread.Stop();
  81. }
  82. }
  83. }