WindBlock.xaml.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  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. DataTemplate myDataTemplate = myContentPresenter.ContentTemplate;
  151. g = myDataTemplate.FindName("_GDMain", myContentPresenter) as Grid;
  152. }
  153. g.ToolTip = tp;
  154. ToolTipService.SetInitialShowDelay(g, _PopupInterval);
  155. }
  156. private void ToolTipOpened(object sender, RoutedEventArgs e)
  157. {
  158. if (this.WindturbineInfo == null) return;
  159. ToolTip tp = sender as ToolTip;
  160. if (tp == null) return;
  161. if (tp.Content == null)
  162. {
  163. var wdv = new WindturbineDetailsView(WindturbineInfo) { ToolTipValue = this.ToolTipValue };
  164. tp.Content = wdv;
  165. }
  166. (tp.Content as WindturbineDetailsView)?.RefreshData();
  167. }
  168. /// <summary>
  169. /// 更新数据
  170. /// </summary>
  171. public void Update(object obj)
  172. {
  173. WindturbineInfo windturbineInfo = obj as WindturbineInfo;
  174. if (windturbineInfo == null) return;
  175. WindturbineInfo = windturbineInfo;
  176. /*初始化 1
  177. 停机 2
  178. 待机 4
  179. 启动 8
  180. 加速 16
  181. 发电 32
  182. 维护 64
  183. //停机 0
  184. Stop,
  185. //上电 1
  186. OnPower,
  187. //待机 2
  188. Standby,
  189. //启动 3
  190. Start,
  191. //并网 4
  192. Online,
  193. //故障 5
  194. Fault,
  195. //维护 6
  196. Maintain,
  197. //离线 7
  198. Offline
  199. */
  200. switch (windturbineInfo.Status)
  201. {
  202. case 1:
  203. CardState = WindturbineState.Standby;
  204. break;
  205. case 2:
  206. CardState = WindturbineState.Stoped;
  207. break;
  208. case 4:
  209. CardState = WindturbineState.Standby;
  210. break;
  211. case 8:
  212. CardState = WindturbineState.StartUp;
  213. break;
  214. case 16:
  215. CardState = WindturbineState.PowerOn;
  216. break;
  217. case 32:
  218. case 33:
  219. case 34:
  220. case 35:
  221. case 36:
  222. CardState = WindturbineState.GridConnected;
  223. break;
  224. case 64:
  225. CardState = WindturbineState.Maintain;
  226. break;
  227. case 96:
  228. case 97:
  229. CardState = WindturbineState.Malfunction;
  230. break;
  231. case 100:
  232. CardState = WindturbineState.Offline;
  233. break;
  234. }
  235. WindturbineName = windturbineInfo.WindturbineId;
  236. WindSpeed = $"{windturbineInfo.WindSpeed.ToString("f2")} m/s";
  237. Power = $"{windturbineInfo.Power.ToString("f2")} kW";
  238. var rs = windturbineInfo.ModelId.ToLower().Contains("up105") ? windturbineInfo.RollSpeed * 9.55 : windturbineInfo.RollSpeed;
  239. RotateSpeed = $"{rs.ToString("f2")} rpm";
  240. IsLocked = windturbineInfo.IsLocked;
  241. this.ModelId = windturbineInfo.ModelId;
  242. if (windturbineInfo.LockValue == 8 || windturbineInfo.LockValue == 2 || (this.CustomLockInfo?.Value?.Contains("[检修]")) == true)
  243. {
  244. this.LockType = LockType.Normal;
  245. }
  246. else if (windturbineInfo.LockValue == 7 || windturbineInfo.LockValue == 3 || (this.CustomLockInfo?.Value?.StartsWith("[故障]")) == true)
  247. {
  248. this.LockType = LockType.Fault;
  249. }
  250. else if (this.IsLocked)
  251. {
  252. this.LockType = LockType.Normal;
  253. }
  254. _Timestamp = windturbineInfo.Ts;
  255. string time = _Timestamp.GetTimeString();
  256. string timespan = _Timestamp.GetTimeSpanString();
  257. ToolTipValue = $"开始时间:{time} 时长:{timespan}分钟";
  258. if (this.CustomLockInfo != null)
  259. {
  260. ToolTipValue += $"\n挂牌:{CustomLockInfo.Value} 挂牌时间:{CustomLockInfo.FollowTime.ToString("yyyy-MM-dd HH:mm:ss")}";
  261. }
  262. else if (windturbineInfo.IsLocked)
  263. {
  264. ToolTipValue += $"\n挂牌:{((HungType)windturbineInfo.LockValue).GetStringValue()}";
  265. }
  266. if (g == null) return;
  267. var v = (g.ToolTip as ToolTip)?.Content as WindturbineDetailsView;
  268. if (v == null) return;
  269. v.ToolTipValue = ToolTipValue;
  270. }
  271. private void UserControl_SizeChanged(object sender, SizeChangedEventArgs e)
  272. {
  273. //this.Height = this.ActualWidth * 0.47;
  274. }
  275. private void UserControl_MouseRightButtonUp(object sender, MouseButtonEventArgs e)
  276. {
  277. IsSelected = true;
  278. }
  279. private void UserControl_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  280. {
  281. var b = Keyboard.Modifiers == ModifierKeys.Shift;
  282. if (SelectedChanged == null)
  283. {
  284. IsSelected = !IsSelected;
  285. }
  286. else
  287. {
  288. SelectedChanged(this, b);
  289. }
  290. if (e.ClickCount > 1)
  291. {
  292. //WinForms.WindtrubineInfoWindow.ShowWindow(WindturbineInfo);
  293. //Windows.Windturbine.WindturbineInfoWindow.ShowInfo();
  294. WindturbineInfoWindow.ShowWindow(this.WindturbineInfo);
  295. }
  296. }
  297. private void ContentControl_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
  298. {
  299. e.Handled = true;
  300. string tag = ((Control)sender).Tag as string;
  301. if (tag == null) return;
  302. if (tag == "time")
  303. {
  304. //WinForms.WindtrubineInfoWindow.ShowWindow(WindturbineInfo);
  305. WindturbineInfoWindow.ShowWindow(this.WindturbineInfo);
  306. return;
  307. }
  308. var b = MessageWindow.ShowMessage("是否发送控制指令");
  309. if (!b) return;
  310. var um = App.ServiceProvider.GetService(typeof(UserManager)) as UserManager;
  311. if (!um.IsLogined)
  312. {
  313. bool b1 = um.TempLogin();
  314. if (!b1) return;
  315. }
  316. if (tag == "Lock" || tag == "UnLock")
  317. {
  318. Task.Run(sendLock);
  319. }
  320. else
  321. {
  322. Task.Run(send);
  323. }
  324. void sendLock()
  325. {
  326. try
  327. {
  328. ControlManager cm = App.ServiceProvider.GetService(typeof(ControlManager)) as ControlManager;
  329. HungType ht = HungType.UnLock;
  330. if (tag == "Lock")
  331. {
  332. ht = HungType.CheckLock;
  333. }
  334. Dictionary<string, ControlInstruction> cis = GetControlInstructions(ht);
  335. var v = cm.SendLock(cis);
  336. if (v == null) return;
  337. Dispatcher.Invoke(() =>
  338. {
  339. if (v.Length > 0)
  340. {
  341. MessageWindowBig.ShowMessage(v);
  342. }
  343. });
  344. }
  345. catch { }
  346. }
  347. void send()
  348. {
  349. try
  350. {
  351. ControlManager cm = App.ServiceProvider.GetService(typeof(ControlManager)) as ControlManager;
  352. OperateStyle os = (OperateStyle)Enum.Parse(typeof(OperateStyle), tag);
  353. Dictionary<string, ControlInstruction> cis = GetControlInstructions(os);
  354. var v = cm.SendCmd(cis);
  355. if (v == null) return;
  356. Dispatcher.Invoke(() =>
  357. {
  358. if (v.Length > 0)
  359. {
  360. MessageWindowBig.ShowMessage(v);
  361. }
  362. });
  363. }
  364. catch { }
  365. }
  366. }
  367. private Dictionary<string, ControlInstruction> GetControlInstructions(HungType ht)
  368. {
  369. Dictionary<string, ControlInstruction> ll = new Dictionary<string, ControlInstruction>();
  370. ControlInstruction ci = new ControlInstruction();
  371. ci.WindturbineId = WindturbineInfo.WindturbineId;
  372. ci.StationId = WindturbineInfo.StationId;
  373. ci.ProjectId = WindturbineInfo.ProjectId;
  374. ci.ModelId = WindturbineInfo.ModelId;
  375. ci.LockType = ht;
  376. ll.Add(ci.WindturbineId, ci);
  377. return ll;
  378. }
  379. private Dictionary<string, ControlInstruction> GetControlInstructions(OperateStyle os)
  380. {
  381. Dictionary<string, ControlInstruction> ll = new Dictionary<string, ControlInstruction>();
  382. ControlInstruction ci = new ControlInstruction();
  383. ci.WindturbineId = WindturbineInfo.WindturbineId;
  384. ci.StationId = WindturbineInfo.StationId;
  385. ci.ProjectId = WindturbineInfo.ProjectId;
  386. ci.ModelId = WindturbineInfo.ModelId;
  387. ci.ControlType = os;
  388. ci.LockType = WindturbineInfo.LockType;
  389. ll.Add(ci.WindturbineId, ci);
  390. return ll;
  391. }
  392. private void ContentControl_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  393. {
  394. e.Handled = true;
  395. }
  396. private void UserControl_Loaded(object sender, RoutedEventArgs e)
  397. {
  398. InitToolTip();
  399. }
  400. private childItem FindVisualChild<childItem>(DependencyObject obj) where childItem : DependencyObject
  401. {
  402. for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
  403. {
  404. DependencyObject child = VisualTreeHelper.GetChild(obj, i);
  405. if (child != null && child is childItem)
  406. {
  407. return (childItem)child;
  408. }
  409. else
  410. {
  411. childItem childOfChild = FindVisualChild<childItem>(child);
  412. if (childOfChild != null)
  413. return childOfChild;
  414. }
  415. }
  416. return null;
  417. }
  418. }
  419. /// <summary>
  420. /// 挂牌类型
  421. /// </summary>
  422. public enum LockType
  423. {
  424. /// <summary>
  425. /// 常规
  426. /// </summary>
  427. Normal,
  428. /// <summary>
  429. /// 检修
  430. /// </summary>
  431. Overhaul,
  432. /// <summary>
  433. /// 故障
  434. /// </summary>
  435. Fault,
  436. }
  437. }