ConfirmWindow.xaml.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. using NEIntelligentControl2.Models;
  2. using NEIntelligentControl2.Models.Messages;
  3. using NEIntelligentControl2.Models.Windturbine;
  4. using NEIntelligentControl2.Service.User;
  5. using NEIntelligentControl2.Service.WebSocket;
  6. using NEIntelligentControl2.Service.Windturbine;
  7. using NEIntelligentControl2.Views.Matrix;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Configuration;
  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.Controls.Primitives;
  17. using System.Windows.Data;
  18. using System.Windows.Documents;
  19. using System.Windows.Input;
  20. using System.Windows.Media;
  21. using System.Windows.Media.Imaging;
  22. using System.Windows.Shapes;
  23. namespace NEIntelligentControl2.Windows
  24. {
  25. /// <summary>
  26. /// 确认窗口
  27. /// </summary>
  28. public partial class ConfirmWindow : Window
  29. {
  30. private OperateStyle _OperateStyle;
  31. private List<WindturbineInfo> _WindturbineInfos;
  32. private WindturbineInfoBridge _WindturbineInfoBridge;// 风机信息数据桥
  33. private MessageBridge _MessageBridge;
  34. private List<WindturbineInfo> _AllowSendWindturbines;
  35. private ControlManager _ControlManager;
  36. private HungType _HungType;
  37. private bool _IsLockOrUnLock;
  38. private string _CustomLock;// 自定义挂牌信息
  39. public ConfirmWindow(MessageBridge bridge, ControlManager cm)
  40. {
  41. InitializeComponent();
  42. _MessageBridge = bridge;
  43. _ControlManager = cm;
  44. _WindturbineInfoBridge = new WindturbineInfoBridge() { Messaged = OnMessage };
  45. _MessageBridge.Register(_WindturbineInfoBridge);
  46. }
  47. private void OnMessage(Dictionary<string, WindturbineInfo> obj)
  48. {
  49. if (obj == null) return;
  50. Dispatcher.Invoke(() =>
  51. {
  52. Update(_UGAllow, obj);
  53. Update(_UGNotAllow, obj);
  54. });
  55. }
  56. private void Update(UniformGrid ug, Dictionary<string, WindturbineInfo> obj)
  57. {
  58. foreach (var v in ug.Children)
  59. {
  60. var w = v as WindBlock;
  61. if (w == null || !obj.ContainsKey(w.WindturbineId)) continue;
  62. w.Update(obj[w.WindturbineId]);
  63. }
  64. }
  65. internal bool Show(OperateStyle os, List<WindturbineInfo> bks)
  66. {
  67. _TBTitle.Text = $"发送{os.GetStringValue()}指令";
  68. this.Owner = Application.Current.MainWindow;
  69. _OperateStyle = os;
  70. _WindturbineInfos = bks;
  71. InitView();
  72. var b = ShowDialog();
  73. return b != null && b == true;
  74. }
  75. internal bool Show(HungType hungType, List<WindturbineInfo> ls, string customLock = null)
  76. {
  77. _TBTitle.Text = $"发送挂牌指令";
  78. this.Owner = Application.Current.MainWindow;
  79. _HungType = hungType;
  80. _IsLockOrUnLock = true;
  81. _WindturbineInfos = ls;
  82. _CustomLock = customLock;
  83. InitView();
  84. var b = ShowDialog();
  85. return b != null && b == true;
  86. }
  87. private void InitView()
  88. {
  89. _AllowSendWindturbines = new List<WindturbineInfo>();
  90. foreach (var v in _WindturbineInfos)
  91. {
  92. WindBlock wb = new WindBlock(v) { WindturbineId = v.WindturbineId };
  93. wb.Update(v);
  94. if (GetIsAllowSend(v))
  95. {
  96. _UGAllow.Children.Add(wb);
  97. _AllowSendWindturbines.Add(v);
  98. }
  99. else
  100. {
  101. _UGNotAllow.Children.Add(wb);
  102. }
  103. }
  104. }
  105. private bool GetIsAllowSend(WindturbineInfo v)
  106. {
  107. if (v.IsLocked)
  108. {
  109. return _IsLockOrUnLock && _HungType == HungType.UnLock;
  110. }
  111. else if(_IsLockOrUnLock)
  112. {
  113. return v.LockType == HungType.UnLock;
  114. }
  115. switch (_OperateStyle)
  116. {
  117. case OperateStyle.Start:
  118. return v.State == WindturbineState.Standby;
  119. case OperateStyle.Stop:
  120. return v.State == WindturbineState.GridConnected;
  121. case OperateStyle.Reset:
  122. return v.State == WindturbineState.Malfunction;
  123. case OperateStyle.Maintain:
  124. return v.State == WindturbineState.Standby && v.ModelId != "UP105-2000-S";
  125. case OperateStyle.UnMaintain:
  126. return v.State == WindturbineState.Maintain && v.ModelId != "UP105-2000-S";
  127. default: return false;
  128. }
  129. }
  130. private void Button_Click(object sender, RoutedEventArgs e)
  131. {
  132. switch (((Control)sender).Tag)
  133. {
  134. case "send":
  135. Send();
  136. break;
  137. case "cancel":
  138. this.DialogResult = false;
  139. break;
  140. default: return;
  141. }
  142. }
  143. /// <summary>
  144. /// 发送控制指令
  145. /// </summary>
  146. private void Send()
  147. {
  148. var um = App.ServiceProvider.GetService(typeof(UserManager)) as UserManager;
  149. if (!um.IsLogined)
  150. {
  151. bool b1 = um.TempLogin();
  152. if (!b1) return;
  153. }
  154. if (_IsLockOrUnLock)
  155. {
  156. Task.Run(SendLock);
  157. }
  158. else
  159. {
  160. Task.Run(SendCmd);
  161. }
  162. try
  163. {
  164. this.DialogResult = true;
  165. }
  166. catch { }
  167. }
  168. private void SendLock()
  169. {
  170. var cis = GetControlInstructions();
  171. var v = _ControlManager.SendLock(cis);
  172. if (v == null)
  173. {
  174. return;
  175. }
  176. Dispatcher.Invoke(() =>
  177. {
  178. if (v.Length > 0)
  179. {
  180. MessageWindowBig.ShowMessage(v);
  181. }
  182. });
  183. }
  184. void SendCmd()
  185. {
  186. var cis = GetControlInstructions();
  187. var v = _ControlManager.SendCmd(cis);
  188. if (v == null)
  189. {
  190. return;
  191. }
  192. Dispatcher.Invoke(() =>
  193. {
  194. if (v.Length > 0)
  195. {
  196. MessageWindowBig.ShowMessage(v);
  197. }
  198. });
  199. }
  200. private Dictionary<string, ControlInstruction> GetControlInstructions()
  201. {
  202. Dictionary<string, ControlInstruction> ll = new Dictionary<string, ControlInstruction>();
  203. foreach (var v in _AllowSendWindturbines)
  204. {
  205. ControlInstruction ci = new ControlInstruction();
  206. ci.WindturbineId = v.WindturbineId;
  207. ci.StationId = v.StationId;
  208. ci.ProjectId = v.ProjectId;
  209. ci.ModelId = v.ModelId;
  210. ci.LockType = _HungType;
  211. ci.LockValue = _CustomLock;
  212. ci.ControlType = _OperateStyle;
  213. ll.Add(ci.WindturbineId, ci);
  214. }
  215. return ll;
  216. }
  217. private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
  218. {
  219. _MessageBridge.Unregister(_WindturbineInfoBridge);
  220. }
  221. }
  222. }