WindturbineParams.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. using IntelligentControlForsx.CodeGenerator;
  2. using log4net;
  3. using System;
  4. using System.Collections;
  5. using System.ComponentModel;
  6. using System.Threading;
  7. using System.Windows.Forms;
  8. using WisdomClient.data;
  9. using WisdomClient;
  10. using System.Collections.Generic;
  11. namespace IntelligentControlForsx.MyControls
  12. {
  13. public partial class WindturbineParams : UserControl
  14. {
  15. private ILog logger = LogManager.GetLogger("AppInfoLog");
  16. #region 数据绑定
  17. // This BindingSource binds the list to the DataGridView control.
  18. private BindingSource bindingSource = new BindingSource();
  19. private IDictionary<string, BindingList<dynamic>> dictBindingList;
  20. private BindingList<dynamic> getBindingList(string clsName)
  21. {
  22. if (dictBindingList == null)
  23. dictBindingList = new Dictionary<string, BindingList<dynamic>>();
  24. if (dictBindingList.ContainsKey(clsName) == false)
  25. {
  26. BindingList<dynamic> bl = createParamList(clsName);
  27. dictBindingList.Add(clsName, bl);
  28. }
  29. return dictBindingList[clsName];
  30. }
  31. private string stationId;
  32. public string StationId
  33. {
  34. get
  35. {
  36. return stationId;
  37. }
  38. set
  39. {
  40. if (value != this.stationId)
  41. {
  42. this.stationId = value;
  43. // Bind the list to the BindingSource.
  44. this.bindingSource.DataSource = getBindingList(stationId);
  45. // Attach the BindingSource to the DataGridView.
  46. this.gvWindturbine.DataSource =
  47. this.bindingSource;
  48. }
  49. }
  50. }
  51. public WindturbineParams()
  52. {
  53. InitializeComponent();
  54. }
  55. private BindingList<dynamic> createParamList(string stationId)
  56. {
  57. BindingList<dynamic> paramList = new BindingList<dynamic>();
  58. ArrayList tmArray = TableModelFactory.Instance.getTableModel(stationId);
  59. for (int i=0;i<tmArray.Count;i++)
  60. {
  61. paramList.Add(tmArray[i]);
  62. }
  63. return paramList;
  64. }
  65. private void gvWindturbine_DataError(object sender, DataGridViewDataErrorEventArgs e)
  66. {
  67. logger.Info(e.RowIndex);
  68. logger.Info(e.ColumnIndex);
  69. }
  70. #endregion
  71. #region 加载数据
  72. private void timer1_Tick(object sender, EventArgs e)
  73. {
  74. if (isLoadding == true)
  75. return;
  76. Thread ayscThread = new Thread(LoadData);
  77. ayscThread.IsBackground = true;
  78. ayscThread.Start();
  79. }
  80. public void DeActive()
  81. {
  82. timer1.Stop();
  83. }
  84. public void Active()
  85. {
  86. Thread ayscThread = new Thread(LoadData);
  87. ayscThread.IsBackground = true;
  88. ayscThread.Start();
  89. timer1.Start();
  90. }
  91. private bool isLoadding = false;
  92. private void LoadData()
  93. {
  94. if (isLoadding == true)
  95. return;
  96. isLoadding = true;
  97. try
  98. {
  99. string[] tagNames = TableModelFactory.Instance.getTablePointArray(stationId);
  100. TsData[] tagValues = RestfulClient2.findLatestByTagNames(tagNames);
  101. double[] tagDoubleValues = new double[tagNames.Length];
  102. for(int i=0;i<tagNames.Length;i++)
  103. {
  104. tagDoubleValues[i] = tagValues[i].doubleValue.HasValue ? tagValues[i].doubleValue.Value : 0;
  105. }
  106. lock (this)
  107. {
  108. this.BeginInvoke(
  109. (Action)delegate
  110. {
  111. var bl = getBindingList(stationId);
  112. int columns = TableModelFactory.Instance.getPointsColumns(stationId);
  113. int offset = 0;
  114. foreach (var em in bl)
  115. {
  116. double[] tmVals = new double[columns];
  117. Array.Copy(tagDoubleValues, offset, tmVals, 0, columns);
  118. TableModel tm = em as TableModel;
  119. tm.bindingData(tmVals);
  120. offset += columns;
  121. }
  122. this.gvWindturbine.Refresh();
  123. });
  124. }
  125. }
  126. catch (Exception ex)
  127. {
  128. logger.Info("绑定数据失败!ex=" + ex.Message);
  129. }
  130. finally
  131. {
  132. isLoadding = false;
  133. }
  134. }
  135. #endregion
  136. }
  137. }