FollowedMatrix.xaml.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. using NEIntelligentControl2.Models.Windturbine;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Windows;
  8. using System.Windows.Controls;
  9. using System.Windows.Data;
  10. using System.Windows.Documents;
  11. using System.Windows.Input;
  12. using System.Windows.Media;
  13. using System.Windows.Media.Imaging;
  14. using System.Windows.Navigation;
  15. using System.Windows.Shapes;
  16. namespace NEIntelligentControl2.Views.Matrix
  17. {
  18. /// <summary>
  19. /// 关注风机
  20. /// </summary>
  21. public partial class FollowedMatrix : UserControl
  22. {
  23. private bool _IsActive; // 此页面是否活跃
  24. private Dictionary<string, WindBlockSmall> _BlockPairs; // 数据集合
  25. public FollowedMatrix()
  26. {
  27. InitializeComponent();
  28. _BlockPairs = new Dictionary<string, WindBlockSmall>();
  29. }
  30. /// <summary>
  31. /// 更新数据
  32. /// </summary>
  33. internal void UpdateData(Dictionary<string, WindturbineInfo> vs)
  34. {
  35. try
  36. {
  37. Dispatcher.Invoke(() =>
  38. {
  39. foreach (var v in _BlockPairs)
  40. {
  41. if (!vs.ContainsKey(v.Key)) continue;
  42. v.Value.Update(vs[v.Key]);
  43. }
  44. });
  45. }
  46. catch { }
  47. }
  48. private void UserControl_Loaded(object sender, RoutedEventArgs e)
  49. {
  50. _IsActive = true;
  51. Task.Factory.StartNew(RefreshData, TaskCreationOptions.LongRunning);
  52. }
  53. /// <summary>
  54. /// 刷新数据
  55. /// </summary>
  56. private async void RefreshData()
  57. {
  58. while (_IsActive)
  59. {
  60. try
  61. {
  62. Refresh();
  63. await Task.Delay(3000);
  64. }
  65. catch { }
  66. }
  67. }
  68. /// <summary>
  69. /// 刷新数据
  70. /// </summary>
  71. private void Refresh()
  72. {
  73. try
  74. {
  75. /*var vs = RestfulClient.GetWindturbineFollows();
  76. if (vs == null) return;
  77. HashSet<string> set = new HashSet<string>();
  78. foreach (var v in vs)
  79. {
  80. set.Add(v.WindturbineID);
  81. if (!_BlockPairs.ContainsKey(v.WindturbineID))
  82. {
  83. this.Dispatcher.Invoke(() =>
  84. {
  85. BlockSmallWPF bs = new BlockSmallWPF();
  86. _BlockPairs.Add(v.WindturbineID, bs);
  87. _UGMain.Children.Add(bs);
  88. });
  89. }
  90. }
  91. List<BlockSmallWPF> ll = new List<BlockSmallWPF>();
  92. foreach (var v in _BlockPairs)
  93. {
  94. if (v.Value.WindturbineInfo != null && !set.Contains(v.Value.WindturbineInfo.WindturbineId))
  95. {
  96. ll.Add(v.Value);
  97. }
  98. }
  99. this.Dispatcher.Invoke(() =>
  100. {
  101. foreach (var v in ll)
  102. {
  103. _BlockPairs.Remove(v.WindturbineInfo.WindturbineId);
  104. _UGMain.Children.Remove(v);
  105. }
  106. });*/
  107. }
  108. catch { }
  109. }
  110. private void UserControl_Unloaded(object sender, RoutedEventArgs e)
  111. {
  112. _IsActive = false;
  113. }
  114. private void MenuItem_Click(object sender, RoutedEventArgs e)
  115. {
  116. switch (((Control)sender).Tag)
  117. {
  118. case "removefollow":// 取消关注
  119. RemoveFollow();
  120. break;
  121. }
  122. }
  123. /// <summary>
  124. /// 取消关注
  125. /// </summary>
  126. private void RemoveFollow()
  127. {
  128. try
  129. {
  130. var ls = GetSelectedIds();
  131. //var s = ls.GetString(v => v.WindturbineInfo.WindturbineId);
  132. //RestfulClient.RemoveWindturbineFollows(s);
  133. Refresh();
  134. }
  135. catch (Exception e)
  136. {
  137. Console.WriteLine($"取消关注出现错误:{e.Message}");
  138. }
  139. }
  140. /// <summary>
  141. /// 获取被选中的风机
  142. /// </summary>
  143. /// <returns></returns>
  144. private List<WindBlockSmall> GetSelectedIds()
  145. {
  146. List<WindBlockSmall> ls = new List<WindBlockSmall>();
  147. foreach (var v in _BlockPairs)
  148. {
  149. if (v.Value.IsSelected)
  150. {
  151. ls.Add(v.Value);
  152. }
  153. }
  154. return ls;
  155. }
  156. }
  157. }