WindBlock.xaml.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. using NEIntelligentControl2.Models;
  2. using NEIntelligentControl2.Models.Matrix;
  3. using NEIntelligentControl2.Models.Messages;
  4. using NEIntelligentControl2.Models.Windturbine;
  5. using NEIntelligentControl2.Service.User;
  6. using NEIntelligentControl2.Service.WebSocket;
  7. using NEIntelligentControl2.Service.Windturbine;
  8. using NEIntelligentControl2.Views.Basic;
  9. using NEIntelligentControl2.Views.Infos;
  10. using NEIntelligentControl2.Windows;
  11. using System;
  12. using System.Collections.Generic;
  13. using System.Configuration;
  14. using System.Linq;
  15. using System.Text;
  16. using System.Threading.Tasks;
  17. using System.Windows;
  18. using System.Windows.Controls;
  19. using System.Windows.Data;
  20. using System.Windows.Documents;
  21. using System.Windows.Input;
  22. using System.Windows.Media;
  23. using System.Windows.Media.Imaging;
  24. using System.Windows.Navigation;
  25. using System.Windows.Shapes;
  26. namespace NEIntelligentControl2.Views.Matrix
  27. {
  28. /// <summary>
  29. /// 风机矩阵块
  30. /// </summary>
  31. public partial class WindBlock : UserControl, IOptional
  32. {
  33. // ----------依赖属性------------
  34. public static readonly DependencyProperty CardColorProperty = DependencyProperty.Register("CardState", typeof(WindturbineState), typeof(WindBlock));
  35. public static readonly DependencyProperty StationNameValueProperty = DependencyProperty.Register("StationName", typeof(string), typeof(WindBlock));
  36. public static readonly DependencyProperty WindturbineNameValueProperty = DependencyProperty.Register("WindturbineName", typeof(string), typeof(WindBlock));
  37. public static readonly DependencyProperty WindSpeedValueProperty = DependencyProperty.Register("WindSpeed", typeof(string), typeof(WindBlock));
  38. public static readonly DependencyProperty PowerValueProperty = DependencyProperty.Register("Power", typeof(string), typeof(WindBlock));
  39. public static readonly DependencyProperty RotateSpeedValueProperty = DependencyProperty.Register("RotateSpeed", typeof(string), typeof(WindBlock));
  40. public static readonly DependencyProperty ToolTipValueProperty = DependencyProperty.Register("ToolTipValue", typeof(string), typeof(WindBlock));
  41. public static readonly DependencyProperty IsSelectedProperty = DependencyProperty.Register("IsSelected", typeof(bool), typeof(WindBlock));
  42. public static readonly DependencyProperty IsLockedProperty = DependencyProperty.Register("IsLocked", typeof(bool), typeof(WindBlock));
  43. public static readonly DependencyProperty LockTypeProperty = DependencyProperty.Register("LockType", typeof(LockType), typeof(WindBlock));
  44. public event SelectedEventHandler SelectedChanged;
  45. /// <summary>
  46. /// 风机状态
  47. /// </summary>
  48. public WindturbineState CardState
  49. {
  50. get { return (WindturbineState)GetValue(CardColorProperty); }
  51. set { SetValue(CardColorProperty, value); }
  52. }
  53. /// <summary>
  54. /// 站点名称
  55. /// </summary>
  56. public string StationName
  57. {
  58. get { return (string)GetValue(StationNameValueProperty); }
  59. set { SetValue(StationNameValueProperty, value); }
  60. }
  61. /// <summary>
  62. /// 风机名称
  63. /// </summary>
  64. public string WindturbineName
  65. {
  66. get { return (string)GetValue(WindturbineNameValueProperty); }
  67. set { SetValue(WindturbineNameValueProperty, value); }
  68. }
  69. /// <summary>
  70. /// 风机风速
  71. /// </summary>
  72. public string WindSpeed
  73. {
  74. get { return (string)GetValue(WindSpeedValueProperty); }
  75. set { SetValue(WindSpeedValueProperty, value); }
  76. }
  77. /// <summary>
  78. /// 风机功率
  79. /// </summary>
  80. public string Power
  81. {
  82. get { return (string)GetValue(PowerValueProperty); }
  83. set { SetValue(PowerValueProperty, value); }
  84. }
  85. /// <summary>
  86. /// 风机转速
  87. /// </summary>
  88. public string RotateSpeed
  89. {
  90. get { return (string)GetValue(RotateSpeedValueProperty); }
  91. set { SetValue(RotateSpeedValueProperty, value); }
  92. }
  93. /// <summary>
  94. /// 提示消息
  95. /// </summary>
  96. public string ToolTipValue { get => GetValue(ToolTipValueProperty) as string; set => SetValue(ToolTipValueProperty, value); }
  97. /// <summary>
  98. /// 风机ID
  99. /// </summary>
  100. public string WindturbineId { get; set; }
  101. /// <summary>
  102. /// 是否挂牌
  103. /// </summary>
  104. public bool IsLocked { get => (bool)GetValue(IsLockedProperty); set => SetValue(IsLockedProperty, value); }
  105. /// <summary>
  106. /// 挂牌类型
  107. /// </summary>
  108. public LockType LockType { get => (LockType)GetValue(LockTypeProperty); set => SetValue(LockTypeProperty, value); }
  109. /// <summary>
  110. /// 型号
  111. /// </summary>
  112. public string ModelId { get; set; }
  113. /// <summary>
  114. /// 是否被选中
  115. /// </summary>
  116. public bool IsSelected { get => (bool)GetValue(IsSelectedProperty); set => SetValue(IsSelectedProperty, value); }
  117. /// <summary>
  118. /// 风机信息
  119. /// </summary>
  120. public WindturbineInfo WindturbineInfo { get; set; }
  121. /// <summary>
  122. /// 操作时间
  123. /// </summary>
  124. public DateTime OperateTime { get; set; }
  125. /// <summary>
  126. /// 推荐操作
  127. /// </summary>
  128. public WindturbineSuggestion Suggestion { get; set; }
  129. private long _Timestamp { get; set; }// 状态持续时长
  130. /// <summary>
  131. /// 自定义挂牌信息
  132. /// </summary>
  133. public CustomLockInfo CustomLockInfo { get; internal set; }
  134. private int _PopupInterval = 0;
  135. private Grid g;
  136. public WindBlock(WindturbineInfo v = null)
  137. {
  138. InitializeComponent();
  139. WindturbineInfo = v;
  140. _PopupInterval = App.GetService<UrlManager>().WindturbineDetailsInterval;
  141. }
  142. private void InitToolTip()
  143. {
  144. if (_PopupInterval <= 0) return;
  145. ToolTip tp = new ToolTip();
  146. tp.Opened += ToolTipOpened;
  147. if (g == null)
  148. {
  149. ContentPresenter myContentPresenter = FindVisualChild<ContentPresenter>(this);
  150. if (myContentPresenter != null)
  151. {
  152. DataTemplate myDataTemplate = myContentPresenter.ContentTemplate;
  153. g = myDataTemplate.FindName("_GDMain", myContentPresenter) as Grid;
  154. }
  155. else
  156. {
  157. g = new Grid();
  158. }
  159. }
  160. g.ToolTip = tp;
  161. ToolTipService.SetInitialShowDelay(g, _PopupInterval);
  162. }
  163. private void ToolTipOpened(object sender, RoutedEventArgs e)
  164. {
  165. if (this.WindturbineInfo == null) return;
  166. ToolTip tp = sender as ToolTip;
  167. if (tp == null) return;
  168. if (tp.Content == null)
  169. {
  170. var wdv = new WindturbineDetailsView(WindturbineInfo) { ToolTipValue = this.ToolTipValue };
  171. tp.Content = wdv;
  172. }
  173. (tp.Content as WindturbineDetailsView)?.RefreshData();
  174. }
  175. /// <summary>
  176. /// 更新数据
  177. /// </summary>
  178. public void Update(object obj)
  179. {
  180. WindturbineInfo windturbineInfo = obj as WindturbineInfo;
  181. if (windturbineInfo == null) return;
  182. WindturbineInfo = windturbineInfo;
  183. /*初始化 1
  184. 停机 2
  185. 待机 4
  186. 启动 8
  187. 加速 16
  188. 发电 32
  189. 维护 64
  190. //停机 0
  191. Stop,
  192. //上电 1
  193. OnPower,
  194. //待机 2
  195. Standby,
  196. //启动 3
  197. Start,
  198. //并网 4
  199. Online,
  200. //故障 5
  201. Fault,
  202. //维护 6
  203. Maintain,
  204. //离线 7
  205. Offline
  206. */
  207. switch (windturbineInfo.Status)
  208. {
  209. case 1:
  210. CardState = WindturbineState.Standby;
  211. break;
  212. case 2:
  213. CardState = WindturbineState.Stoped;
  214. break;
  215. case 4:
  216. CardState = WindturbineState.Standby;
  217. break;
  218. case 8:
  219. CardState = WindturbineState.StartUp;
  220. break;
  221. case 16:
  222. CardState = WindturbineState.PowerOn;
  223. break;
  224. case 32:
  225. case 33:
  226. case 34:
  227. case 35:
  228. case 36:
  229. CardState = WindturbineState.GridConnected;
  230. break;
  231. case 64:
  232. CardState = WindturbineState.Maintain;
  233. break;
  234. case 96:
  235. case 97:
  236. CardState = WindturbineState.Malfunction;
  237. break;
  238. case 100:
  239. CardState = WindturbineState.Offline;
  240. break;
  241. }
  242. WindturbineName = windturbineInfo.WindturbineId;
  243. WindSpeed = $"{windturbineInfo.WindSpeed.ToString("f2")} m/s";
  244. Power = $"{windturbineInfo.Power.ToString("f2")} kW";
  245. var rs = windturbineInfo.ModelId.ToLower().Contains("up105") ? windturbineInfo.RollSpeed * 9.55 : windturbineInfo.RollSpeed;
  246. RotateSpeed = $"{rs.ToString("f2")} rpm";
  247. IsLocked = windturbineInfo.IsLocked;
  248. this.ModelId = windturbineInfo.ModelId;
  249. if (windturbineInfo.LockValue == 8 || windturbineInfo.LockValue == 2 || (this.CustomLockInfo?.Value?.Contains("[检修]")) == true)
  250. {
  251. this.LockType = LockType.Normal;
  252. }
  253. else if (windturbineInfo.LockValue == 7 || windturbineInfo.LockValue == 3 || (this.CustomLockInfo?.Value?.StartsWith("[故障]")) == true)
  254. {
  255. this.LockType = LockType.Fault;
  256. }
  257. else if (this.IsLocked)
  258. {
  259. this.LockType = LockType.Normal;
  260. }
  261. _Timestamp = windturbineInfo.Ts;
  262. string time = _Timestamp.GetTimeString();
  263. string timespan = _Timestamp.GetTimeSpanString();
  264. ToolTipValue = $"开始时间:{time} 时长:{timespan}分钟";
  265. if (this.CustomLockInfo != null)
  266. {
  267. ToolTipValue += $"\n挂牌:{CustomLockInfo.Value} 挂牌时间:{CustomLockInfo.FollowTime.ToString("yyyy-MM-dd HH:mm:ss")}";
  268. }
  269. else if (windturbineInfo.IsLocked)
  270. {
  271. ToolTipValue += $"\n挂牌:{((HungType)windturbineInfo.LockValue).GetStringValue()}";
  272. }
  273. if (g == null) return;
  274. var v = (g.ToolTip as ToolTip)?.Content as WindturbineDetailsView;
  275. if (v == null) return;
  276. v.ToolTipValue = ToolTipValue;
  277. }
  278. private void UserControl_SizeChanged(object sender, SizeChangedEventArgs e)
  279. {
  280. //this.Height = this.ActualWidth * 0.47;
  281. }
  282. private void UserControl_MouseRightButtonUp(object sender, MouseButtonEventArgs e)
  283. {
  284. IsSelected = true;
  285. }
  286. private void UserControl_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  287. {
  288. var b = Keyboard.Modifiers == ModifierKeys.Shift;
  289. if (SelectedChanged == null)
  290. {
  291. IsSelected = !IsSelected;
  292. }
  293. else
  294. {
  295. SelectedChanged(this, b);
  296. }
  297. if (e.ClickCount > 1)
  298. {
  299. //WinForms.WindtrubineInfoWindow.ShowWindow(WindturbineInfo);
  300. //Windows.Windturbine.WindturbineInfoWindow.ShowInfo();
  301. WindturbineInfoWindow.ShowWindow(this.WindturbineInfo);
  302. }
  303. }
  304. private void ContentControl_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
  305. {
  306. e.Handled = true;
  307. string tag = ((Control)sender).Tag as string;
  308. if (tag == null) return;
  309. if (tag == "time")
  310. {
  311. //WinForms.WindtrubineInfoWindow.ShowWindow(WindturbineInfo);
  312. WindturbineInfoWindow.ShowWindow(this.WindturbineInfo);
  313. return;
  314. }
  315. var b = MessageWindow.ShowMessage("是否发送控制指令");
  316. if (!b) return;
  317. var um = App.ServiceProvider.GetService(typeof(UserManager)) as UserManager;
  318. if (!um.IsLogined)
  319. {
  320. bool b1 = um.TempLogin();
  321. if (!b1) return;
  322. }
  323. if (tag == "Lock" || tag == "UnLock")
  324. {
  325. Task.Run(sendLock);
  326. }
  327. else
  328. {
  329. Task.Run(send);
  330. }
  331. void sendLock()
  332. {
  333. try
  334. {
  335. ControlManager cm = App.ServiceProvider.GetService(typeof(ControlManager)) as ControlManager;
  336. HungType ht = HungType.UnLock;
  337. if (tag == "Lock")
  338. {
  339. ht = HungType.CheckLock;
  340. }
  341. Dictionary<string, ControlInstruction> cis = GetControlInstructions(ht);
  342. var v = cm.SendLock(cis);
  343. if (v == null) return;
  344. Dispatcher.Invoke(() =>
  345. {
  346. if (v.Length > 0)
  347. {
  348. MessageWindowBig.ShowMessage(v);
  349. }
  350. });
  351. }
  352. catch { }
  353. }
  354. void send()
  355. {
  356. try
  357. {
  358. ControlManager cm = App.ServiceProvider.GetService(typeof(ControlManager)) as ControlManager;
  359. OperateStyle os = (OperateStyle)Enum.Parse(typeof(OperateStyle), tag);
  360. Dictionary<string, ControlInstruction> cis = GetControlInstructions(os);
  361. var v = cm.SendCmd(cis);
  362. if (v == null) return;
  363. Dispatcher.Invoke(() =>
  364. {
  365. if (v.Length > 0)
  366. {
  367. MessageWindowBig.ShowMessage(v);
  368. }
  369. });
  370. }
  371. catch { }
  372. }
  373. }
  374. private Dictionary<string, ControlInstruction> GetControlInstructions(HungType ht)
  375. {
  376. Dictionary<string, ControlInstruction> ll = new Dictionary<string, ControlInstruction>();
  377. ControlInstruction ci = new ControlInstruction();
  378. ci.WindturbineId = WindturbineInfo.WindturbineId;
  379. ci.StationId = WindturbineInfo.StationId;
  380. ci.ProjectId = WindturbineInfo.ProjectId;
  381. ci.ModelId = WindturbineInfo.ModelId;
  382. ci.LockType = ht;
  383. ll.Add(ci.WindturbineId, ci);
  384. return ll;
  385. }
  386. private Dictionary<string, ControlInstruction> GetControlInstructions(OperateStyle os)
  387. {
  388. Dictionary<string, ControlInstruction> ll = new Dictionary<string, ControlInstruction>();
  389. ControlInstruction ci = new ControlInstruction();
  390. ci.WindturbineId = WindturbineInfo.WindturbineId;
  391. ci.StationId = WindturbineInfo.StationId;
  392. ci.ProjectId = WindturbineInfo.ProjectId;
  393. ci.ModelId = WindturbineInfo.ModelId;
  394. ci.ControlType = os;
  395. ci.LockType = WindturbineInfo.LockType;
  396. ll.Add(ci.WindturbineId, ci);
  397. return ll;
  398. }
  399. private void ContentControl_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  400. {
  401. e.Handled = true;
  402. }
  403. private void UserControl_Loaded(object sender, RoutedEventArgs e)
  404. {
  405. InitToolTip();
  406. }
  407. private childItem FindVisualChild<childItem>(DependencyObject obj) where childItem : DependencyObject
  408. {
  409. for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
  410. {
  411. DependencyObject child = VisualTreeHelper.GetChild(obj, i);
  412. if (child != null && child is childItem)
  413. {
  414. return (childItem)child;
  415. }
  416. else
  417. {
  418. childItem childOfChild = FindVisualChild<childItem>(child);
  419. if (childOfChild != null)
  420. return childOfChild;
  421. }
  422. }
  423. return null;
  424. }
  425. }
  426. /// <summary>
  427. /// 挂牌类型
  428. /// </summary>
  429. public enum LockType
  430. {
  431. /// <summary>
  432. /// 常规
  433. /// </summary>
  434. Normal,
  435. /// <summary>
  436. /// 检修
  437. /// </summary>
  438. Overhaul,
  439. /// <summary>
  440. /// 故障
  441. /// </summary>
  442. Fault,
  443. }
  444. }