Valve.xaml.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Data;
  8. using System.Windows.Documents;
  9. using System.Windows.Input;
  10. using System.Windows.Media;
  11. using System.Windows.Media.Imaging;
  12. using System.Windows.Navigation;
  13. using System.Windows.Shapes;
  14. using Aitex.Core.Common.DeviceData;
  15. using Aitex.Core.RT.Log;
  16. using Aitex.Core.UI.ControlDataContext;
  17. using Aitex.Core.UI.DeviceControl;
  18. using Aitex.Core.Util;
  19. namespace Aitex.Core.UI.Control
  20. {
  21. /// <summary>
  22. /// Interaction logic for ValveRound.xaml
  23. /// </summary>
  24. public partial class Valve : UserControl
  25. {
  26. public Valve()
  27. {
  28. InitializeComponent();
  29. }
  30. public event Action<string, string> clickAct;
  31. public static readonly DependencyProperty OnOffProperty = DependencyProperty.Register(
  32. "OnOff", typeof(object), typeof(Valve),
  33. new FrameworkPropertyMetadata(true, FrameworkPropertyMetadataOptions.AffectsRender));
  34. public static readonly DependencyProperty IsManualModeProperty = DependencyProperty.Register(
  35. "IsManualModeValve", typeof(bool), typeof(Valve),
  36. new FrameworkPropertyMetadata(true, FrameworkPropertyMetadataOptions.AffectsRender));
  37. public static readonly DependencyProperty ValveNameProperty = DependencyProperty.Register(
  38. "ValveName", typeof(string), typeof(Valve),
  39. new FrameworkPropertyMetadata("未知阀门", FrameworkPropertyMetadataOptions.AffectsRender));
  40. public static readonly DependencyProperty ValveDirectionProperty = DependencyProperty.Register(
  41. "ValveDirection", typeof(ValveDirection), typeof(Valve),
  42. new FrameworkPropertyMetadata(ValveDirection.ToBottom, FrameworkPropertyMetadataOptions.AffectsRender));
  43. public static readonly DependencyProperty CommandProperty = DependencyProperty.Register(
  44. "Command", typeof(ICommand), typeof(Valve),
  45. new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.AffectsRender));
  46. public static readonly DependencyProperty IsOpenedProperty = DependencyProperty.Register(
  47. "IsOpened", typeof(bool), typeof(Valve));
  48. public static readonly DependencyProperty HideBlindingProperty = DependencyProperty.Register(
  49. "HideBlinding", typeof(bool), typeof(Valve));
  50. public bool IsOpened
  51. {
  52. get
  53. {
  54. return (bool)this.GetValue(IsOpenedProperty);
  55. }
  56. set
  57. {
  58. this.SetValue(IsOpenedProperty, value);
  59. }
  60. }
  61. public bool HideBlinding
  62. {
  63. get
  64. {
  65. return (bool)this.GetValue(HideBlindingProperty);
  66. }
  67. set
  68. {
  69. this.SetValue(HideBlindingProperty, value);
  70. }
  71. }
  72. /// <summary>
  73. /// direction of the valve
  74. /// </summary>
  75. public ValveDirection ValveDirection
  76. {
  77. get
  78. {
  79. return (ValveDirection)GetValue(ValveDirectionProperty);
  80. }
  81. set
  82. {
  83. SetValue(ValveDirectionProperty, value);
  84. }
  85. }
  86. /// <summary>
  87. /// valve name
  88. /// </summary>
  89. public string ValveName
  90. {
  91. get
  92. {
  93. return (string)GetValue(ValveNameProperty);
  94. }
  95. set
  96. {
  97. SetValue(ValveNameProperty, value);
  98. }
  99. }
  100. /// <summary>
  101. /// Valve_V2 on/off status
  102. /// </summary>
  103. public object OnOff
  104. {
  105. get
  106. {
  107. return GetValue(OnOffProperty);
  108. }
  109. set
  110. {
  111. SetValue(OnOffProperty, value);
  112. }
  113. }
  114. /// <summary>
  115. /// Valve_V2 on/off status
  116. /// </summary>
  117. public bool IsManualModeValve
  118. {
  119. get
  120. {
  121. return (bool)GetValue(IsManualModeProperty);
  122. }
  123. set
  124. {
  125. SetValue(IsManualModeProperty, value);
  126. }
  127. }
  128. public ICommand Command
  129. {
  130. get
  131. {
  132. return (ICommand)GetValue(CommandProperty);
  133. }
  134. set
  135. {
  136. SetValue(CommandProperty, value);
  137. }
  138. }
  139. bool _needBlinding = false;
  140. bool _blindingTag = false;
  141. TimeSpan tsBlindTime = new TimeSpan(0, 0, 3);
  142. DateTime _startBindingTime = new DateTime();
  143. bool vOnOff = false;
  144. bool vOnOffTemp = false;
  145. DeviceTimer timer = new DeviceTimer();
  146. /// <summary>
  147. /// 设置警告时显示
  148. /// </summary>
  149. /// <param name="tooltipmessage"></param>
  150. void SetWarning(string tooltipmessage)
  151. {
  152. nopass.Fill = Brushes.Red;
  153. passtrigle.Fill = Brushes.Red;
  154. this.ToolTip = tooltipmessage;
  155. }
  156. bool isWarning = false;
  157. /// <summary>
  158. /// over rendering behavior
  159. /// </summary>
  160. /// <param name="drawingContext"></param>
  161. protected override void OnRender(DrawingContext drawingContext)
  162. {
  163. try
  164. {
  165. base.OnRender(drawingContext);
  166. AITValveData deviceData = OnOff as AITValveData;
  167. if (deviceData != null)
  168. {
  169. bool.TryParse(OnOff + "", out vOnOff);
  170. string tooltipmsg = string.Format("阀门:{0}\r\nID:{1}", ValveName, deviceData.DeviceSchematicId, deviceData.DefaultValue, deviceData.SetPoint, deviceData.Feedback);
  171. this.ToolTip = tooltipmsg;
  172. nopass.Fill = Brushes.Black;
  173. passtrigle.Fill = Brushes.Green;
  174. #region 判断阀的设定值和实际值是否在容许时间内一致
  175. if (deviceData.Feedback != deviceData.SetPoint)
  176. {
  177. if (timer.IsIdle())
  178. {
  179. timer.Start(500);
  180. }
  181. else
  182. {
  183. if (timer.IsTimeout())
  184. {
  185. isWarning = true;
  186. SetWarning(string.Format("设定值和实际值不一致!设定:{0},实际:{1}\r\n", deviceData.SetPoint ? "打开" : "关闭", deviceData.Feedback ? "打开" : "关闭") + tooltipmsg);
  187. }
  188. }
  189. }
  190. else
  191. {
  192. if (!timer.IsIdle())
  193. {
  194. timer.Stop();
  195. }
  196. isWarning = false;
  197. }
  198. #endregion
  199. }
  200. else if ((OnOff + "").ToString().ToLower() == "false")
  201. {
  202. vOnOff = false;
  203. }
  204. else if ((OnOff + "").ToString().ToLower() == "true")
  205. {
  206. vOnOff = true;
  207. }
  208. IsOpened = vOnOff;
  209. if (!HideBlinding)
  210. {
  211. #region 闪烁
  212. if (_needBlinding)
  213. {
  214. if (_blindingTag)
  215. {
  216. if (nopass.Visibility == System.Windows.Visibility.Visible) nopass.Fill = Brushes.LightCoral;
  217. if (passtrigle.Visibility == System.Windows.Visibility.Visible) passtrigle.Fill = Brushes.Gold;
  218. }
  219. else
  220. {
  221. if (nopass.Visibility == System.Windows.Visibility.Visible) nopass.Fill = Brushes.Black;
  222. if (passtrigle.Visibility == System.Windows.Visibility.Visible) passtrigle.Fill = Brushes.Green;
  223. }
  224. _blindingTag = !_blindingTag;
  225. }
  226. else
  227. {
  228. //nopass.Fill = Brushes.Black;
  229. //passtrigle.Fill = Brushes.Green;
  230. }
  231. if (vOnOffTemp != vOnOff)
  232. {
  233. _needBlinding = true;
  234. _startBindingTime = DateTime.Now;
  235. vOnOffTemp = vOnOff;
  236. }
  237. else
  238. {
  239. if (_startBindingTime < DateTime.Now.Subtract(tsBlindTime) && _needBlinding)
  240. {
  241. //停止闪烁
  242. _needBlinding = false;
  243. nopass.Fill = Brushes.Black;
  244. passtrigle.Fill = Brushes.Green;
  245. }
  246. }
  247. #endregion
  248. }
  249. switch (ValveDirection)
  250. {
  251. case ValveDirection.ToBottom:
  252. rotateTransform.Angle = 90;
  253. break;
  254. case ValveDirection.ToTop:
  255. rotateTransform.Angle = -90;
  256. break;
  257. case ValveDirection.ToLeft:
  258. rotateTransform.Angle = 180;
  259. break;
  260. case ValveDirection.ToRight:
  261. // No rotation is needed.
  262. break;
  263. default:
  264. break;
  265. }
  266. if (vOnOff)
  267. {
  268. passtrigle.Visibility = Visibility.Visible;
  269. nopass.Visibility = Visibility.Hidden;
  270. }
  271. else
  272. {
  273. passtrigle.Visibility = Visibility.Hidden;
  274. nopass.Visibility = Visibility.Visible;
  275. }
  276. }
  277. catch (Exception ex)
  278. {
  279. LOG.Write(ex);
  280. }
  281. }
  282. /// <summary>
  283. /// open
  284. /// </summary>
  285. /// <param name="sender"></param>
  286. /// <param name="e"></param>
  287. private void TurnOnValve(object sender, RoutedEventArgs e)
  288. {
  289. if (Command != null || clickAct != null)
  290. {
  291. AITValveData deviceData = OnOff as AITValveData;
  292. if (deviceData != null)
  293. {
  294. deviceData.SetPoint = true;
  295. }
  296. }
  297. if (Command != null)
  298. {
  299. KeyValuePair<string, string> pair = new KeyValuePair<string, string>(((MenuItem)e.Source).Tag + "", "true");
  300. Command.Execute(pair);
  301. }
  302. else if (clickAct != null)
  303. {
  304. clickAct(((MenuItem)e.Source).Tag + "", "true");
  305. }
  306. }
  307. /// <summary>
  308. /// close
  309. /// </summary>
  310. /// <param name="sender"></param>
  311. /// <param name="e"></param>
  312. private void TurnOffValve(object sender, RoutedEventArgs e)
  313. {
  314. if (Command != null || clickAct != null)
  315. {
  316. AITValveData deviceData = OnOff as AITValveData;
  317. if (deviceData != null)
  318. {
  319. deviceData.SetPoint = false;
  320. }
  321. }
  322. if (Command != null)
  323. {
  324. KeyValuePair<string, string> pair = new KeyValuePair<string, string>(((MenuItem)e.Source).Tag + "", "false");
  325. Command.Execute(pair);
  326. }
  327. else if (clickAct != null)
  328. {
  329. clickAct(((MenuItem)e.Source).Tag + "", "false");
  330. }
  331. }
  332. private void nopass_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  333. {
  334. if (!IsManualModeValve)
  335. return;
  336. ContextMenu mouseClickMenu = new ContextMenu();
  337. MenuItem item = new MenuItem();
  338. item.Header = "_" + ValveName;
  339. item.Background = Brushes.LightGray;
  340. item.Foreground = Brushes.White;
  341. item.IsEnabled = false;
  342. mouseClickMenu.Items.Add(item);
  343. addOpenMenu(mouseClickMenu, item);
  344. if (isWarning)
  345. {
  346. addCloseMenu(mouseClickMenu, item);
  347. }
  348. mouseClickMenu.IsOpen = true;
  349. }
  350. private void passtrigle_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  351. {
  352. if (!IsManualModeValve)
  353. return;
  354. ContextMenu mouseClickMenu = new ContextMenu();
  355. MenuItem item = new MenuItem();
  356. item.Header = "_" + ValveName;
  357. item.Background = Brushes.LightGray;
  358. item.Foreground = Brushes.White;
  359. item.IsEnabled = false;
  360. mouseClickMenu.Items.Add(item);
  361. addCloseMenu(mouseClickMenu, item);
  362. if (isWarning)
  363. {
  364. addOpenMenu(mouseClickMenu, item);
  365. }
  366. mouseClickMenu.IsOpen = true;
  367. }
  368. void addOpenMenu(ContextMenu mouseClickMenu, MenuItem item)
  369. {
  370. item = new MenuItem();
  371. item.Header = "打开 (_O)";
  372. item.Click += TurnOnValve;
  373. item.Tag = this.Tag;
  374. mouseClickMenu.Items.Add(item);
  375. }
  376. void addCloseMenu(ContextMenu mouseClickMenu, MenuItem item)
  377. {
  378. item = new MenuItem();
  379. item.Header = "关闭 (_C)";
  380. item.Tag = this.Tag;
  381. item.Click += TurnOffValve;
  382. mouseClickMenu.Items.Add(item);
  383. }
  384. private void Canvas_MouseRightButtonUp(object sender, MouseButtonEventArgs e)
  385. {
  386. if (passtrigle.Visibility == Visibility.Visible)
  387. {
  388. passtrigle_MouseLeftButtonDown(null, null);
  389. }
  390. else
  391. {
  392. nopass_MouseLeftButtonDown(null, null);
  393. }
  394. }
  395. private void Canvas_MouseEnter(object sender, MouseEventArgs e)
  396. {
  397. nopass.Fill = Brushes.Goldenrod;
  398. passtrigle.Fill = Brushes.Goldenrod;
  399. ToolTip = $"Name: {ValveName}\r\nStatus: {IsOpened}";
  400. }
  401. private void Canvas_MouseLeave(object sender, MouseEventArgs e)
  402. {
  403. nopass.Fill = Brushes.Black;
  404. passtrigle.Fill = Brushes.Green;
  405. }
  406. }
  407. }