RecomendWindow.xaml.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. using NEIntelligentControl2.Models.Messages;
  2. using NEIntelligentControl2.Models.Station;
  3. using NEIntelligentControl2.Models.Windturbine;
  4. using NEIntelligentControl2.Service.Station;
  5. using NEIntelligentControl2.Service.User;
  6. using NEIntelligentControl2.Service.WebSocket;
  7. using NEIntelligentControl2.Service.Windturbine;
  8. using NEIntelligentControl2.Views.Matrix;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Linq;
  12. using System.Text;
  13. using System.Threading.Tasks;
  14. using System.Windows;
  15. using System.Windows.Controls;
  16. using System.Windows.Data;
  17. using System.Windows.Documents;
  18. using System.Windows.Input;
  19. using System.Windows.Media;
  20. using System.Windows.Media.Imaging;
  21. using System.Windows.Shapes;
  22. namespace NEIntelligentControl2.Windows
  23. {
  24. /// <summary>
  25. /// 推荐窗口
  26. /// </summary>
  27. public partial class RecomendWindow : Window
  28. {
  29. private SuggestionManager _SuggestionManager;
  30. private WindturbineInfoBridge _WindturbineInfoBridge;
  31. private MessageBridge _MessageBridge;
  32. private StationManager _StationManager;
  33. private UserManager _UserManager;
  34. /// <summary>
  35. /// 矩阵块
  36. /// </summary>
  37. private List<WindBlock> _WindBlocks;
  38. private HashSet<string> _WindturbineIds;
  39. private StationInfo _CurrentStationInfo;
  40. private string _OperationType;
  41. private ControlManager _ControlManager;
  42. private CacheManager _CacheManager;
  43. public RecomendWindow(SuggestionManager sm, MessageBridge mb, StationManager smm, ControlManager cm, UserManager um, CacheManager cam)
  44. {
  45. InitializeComponent();
  46. _WindBlocks = new List<WindBlock>();
  47. _WindturbineIds = new HashSet<string>();
  48. _SuggestionManager = sm;
  49. _MessageBridge = mb;
  50. _StationManager = smm;
  51. _ControlManager = cm;
  52. _UserManager = um;
  53. _CacheManager = cam;
  54. _SuggestionManager.OnSuggestion = OnSuggestion;
  55. _WindturbineInfoBridge = new WindturbineInfoBridge() { Messaged = WindturbineMessaged };
  56. _MessageBridge.Register(_WindturbineInfoBridge);
  57. Init();
  58. }
  59. private void WindturbineMessaged(Dictionary<string, WindturbineInfo> obj)
  60. {
  61. Dispatcher.Invoke(() =>
  62. {
  63. foreach(var v in _WindBlocks)
  64. {
  65. if (!obj.ContainsKey(v.WindturbineId)) continue;
  66. v.Update(obj[v.WindturbineId]);
  67. }
  68. });
  69. }
  70. private void Init()
  71. {
  72. var vs = _StationManager.GetStationInfos().Where(s => s.Type == Models.Station.StationType.Wind).ToList();
  73. vs.Add(new Models.Station.StationInfo() { Name = "全部", Id = "all" });
  74. _LBMain.ItemsSource = vs;
  75. _LBMain.SelectedIndex = vs.Count - 1;
  76. }
  77. private void OnSuggestion(List<WindturbineSuggestion> obj)
  78. {
  79. try
  80. {
  81. if (obj == null)
  82. {
  83. obj = new List<WindturbineSuggestion>();
  84. }
  85. var ids = obj.Select(s => s.WindturbineId).ToList();
  86. var removed = _WindBlocks.Where(w => !ids.Contains(w.WindturbineId)).ToList();
  87. Dispatcher.Invoke(() =>
  88. {
  89. InitCount(obj);
  90. foreach (var v in obj)
  91. {
  92. if (_WindturbineIds.Contains(v.WindturbineId)) continue;
  93. _WindturbineIds.Add(v.WindturbineId);
  94. WindBlock wb = new WindBlock() { WindturbineId = v.WindturbineId, Suggestion = v };
  95. if(_CacheManager.Windturbineinfos!=null&& _CacheManager.Windturbineinfos.ContainsKey(v.WindturbineId))
  96. {
  97. wb.Update(_CacheManager.Windturbineinfos[v.WindturbineId]);
  98. }
  99. _WindBlocks.Add(wb);
  100. if (IsFilterOK(wb))
  101. {
  102. _OPUGMain.Add(wb);
  103. }
  104. }
  105. foreach (var v in removed)
  106. {
  107. _WindBlocks.Remove(v);
  108. _WindturbineIds.Remove(v.WindturbineId);
  109. _OPUGMain.Remove(v);
  110. }
  111. });
  112. }
  113. catch { }
  114. }
  115. /// <summary>
  116. /// 初始化数据
  117. /// </summary>
  118. private void InitCount(List<WindturbineSuggestion> obj)
  119. {
  120. _TBAll.Text = obj.Count.ToString();
  121. _TBStart.Text = obj.Where(w => w.OperateType == OperateStyle.Start).ToArray().Length.ToString();
  122. _TBStop.Text = obj.Where(w => w.OperateType == OperateStyle.Stop).ToArray().Length.ToString();
  123. _TBReset.Text = obj.Where(w => w.OperateType == OperateStyle.Reset).ToArray().Length.ToString();
  124. _TBMaintain.Text = obj.Where(w => w.OperateType == OperateStyle.Maintain).ToArray().Length.ToString();
  125. _TBUnMaintain.Text = obj.Where(w => w.OperateType == OperateStyle.UnMaintain).ToArray().Length.ToString();
  126. }
  127. private bool IsFilterOK(WindBlock wb)
  128. {
  129. if (_OperationType != null && _OperationType != "all")
  130. {
  131. Enum.TryParse(_OperationType, out OperateStyle ot);
  132. if(_CurrentStationInfo.Id == "all")
  133. {
  134. return wb.Suggestion.OperateType == ot;
  135. }
  136. else
  137. {
  138. return wb.Suggestion.OperateType == ot && wb.WindturbineInfo?.StationId == _CurrentStationInfo.Id;
  139. }
  140. }
  141. else
  142. {
  143. if (_CurrentStationInfo.Id != "all")
  144. {
  145. return wb.WindturbineInfo?.StationId == _CurrentStationInfo.Id;
  146. }
  147. }
  148. return true;
  149. }
  150. public static void ShowWindow()
  151. {
  152. RecomendWindow rw = App.ServiceProvider.GetService(typeof(RecomendWindow)) as RecomendWindow;
  153. rw.Owner = Application.Current.MainWindow;
  154. rw.ShowDialog();
  155. }
  156. private void Button_Click(object sender, RoutedEventArgs e)
  157. {
  158. Send();
  159. }
  160. /// <summary>
  161. /// 发送控制指令
  162. /// </summary>
  163. private void Send()
  164. {
  165. if (!_UserManager.IsLogined)
  166. {
  167. bool b = _UserManager.TempLogin();
  168. if (!b) return;
  169. }
  170. var cis = GetControlInstructions();
  171. Task.Run(send);
  172. MessageWindow.ShowMessage("指令发送成功!");
  173. void send()
  174. {
  175. var v = _ControlManager.SendCmdNotCheck(cis);
  176. if (v == null)
  177. {
  178. return;
  179. }
  180. Dispatcher.Invoke(() =>
  181. {
  182. if (v.Length > 0)
  183. {
  184. MessageWindowBig.ShowMessage(v);
  185. }
  186. });
  187. }
  188. }
  189. private Dictionary<string, ControlInstruction> GetControlInstructions()
  190. {
  191. Dictionary<string, ControlInstruction> ll = new Dictionary<string, ControlInstruction>();
  192. List<WindBlock> ls = GetSelectedItems();
  193. foreach (var v in ls)
  194. {
  195. ControlInstruction ci = new ControlInstruction();
  196. ci.WindturbineId = v.WindturbineId;
  197. ci.StationId = v.WindturbineInfo.StationId;
  198. ci.ProjectId = v.WindturbineInfo.ProjectId;
  199. ci.ModelId = v.WindturbineInfo.ModelId;
  200. ci.ControlType = v.Suggestion.OperateType;
  201. ci.LockType = v.WindturbineInfo.LockType;
  202. ll.Add(ci.WindturbineId, ci);
  203. }
  204. return ll;
  205. }
  206. private List<WindBlock> GetSelectedItems()
  207. {
  208. List<WindBlock> ls = new List<WindBlock>();
  209. foreach(var v in _WindBlocks)
  210. {
  211. if (v.IsSelected)
  212. {
  213. ls.Add(v);
  214. }
  215. }
  216. return ls;
  217. }
  218. private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
  219. {
  220. _MessageBridge.Unregister(_WindturbineInfoBridge);
  221. }
  222. /// <summary>
  223. /// 场站选择
  224. /// </summary>
  225. private void _LBMain_SelectionChanged(object sender, SelectionChangedEventArgs e)
  226. {
  227. if (e.AddedItems.Count <= 0) return;
  228. var v = e.AddedItems[0] as StationInfo;
  229. if (v == null) return;
  230. _CurrentStationInfo = v;
  231. ToFilter();
  232. }
  233. /// <summary>
  234. /// 操作选择
  235. /// </summary>
  236. private void _LBOperation_SelectionChanged(object sender, SelectionChangedEventArgs e)
  237. {
  238. if (e.AddedItems.Count <= 0) return;
  239. var v = e.AddedItems[0] as ListBoxItem;
  240. if (v == null) return;
  241. _OperationType = v.Tag as string;
  242. ToFilter();
  243. }
  244. private void ToFilter()
  245. {
  246. if (_WindBlocks == null) return;
  247. _OPUGMain?.Clear();
  248. foreach(var v in _WindBlocks)
  249. {
  250. if (IsFilterOK(v))
  251. {
  252. _OPUGMain.Add(v);
  253. }
  254. }
  255. }
  256. private void Button_Click_1(object sender, RoutedEventArgs e)
  257. {
  258. this.Close();
  259. }
  260. private void Window_Loaded(object sender, RoutedEventArgs e)
  261. {
  262. OnSuggestion(_SuggestionManager.WindturbineSuggestions);
  263. }
  264. }
  265. }