GasValve.xaml.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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.Util;
  15. using Aitex.Core.RT.Log;
  16. using Aitex.Core.UI.ControlDataContext;
  17. namespace Aitex.Core.UI.Control
  18. {
  19. public partial class GasValve : UserControl
  20. {
  21. public GasValve()
  22. {
  23. InitializeComponent();
  24. }
  25. public event Action<string, string> clickAct;
  26. public static readonly DependencyProperty GasValveDataProperty = DependencyProperty.Register(
  27. "GasValveData", typeof(GasValveDataItem), typeof(GasValve),
  28. new FrameworkPropertyMetadata(default(GasValveDataItem), FrameworkPropertyMetadataOptions.AffectsRender));
  29. public static readonly DependencyProperty ValveDirectionProperty = DependencyProperty.Register(
  30. "ValveDirection", typeof(ValveDirection), typeof(GasValve),
  31. new FrameworkPropertyMetadata(ValveDirection.ToBottom, FrameworkPropertyMetadataOptions.AffectsRender));
  32. public static readonly DependencyProperty CommandProperty = DependencyProperty.Register(
  33. "Command", typeof(ICommand), typeof(GasValve),
  34. new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.AffectsRender));
  35. public ValveDirection ValveDirection
  36. {
  37. get
  38. {
  39. return (ValveDirection)GetValue(ValveDirectionProperty);
  40. }
  41. set
  42. {
  43. SetValue(ValveDirectionProperty, value);
  44. }
  45. }
  46. public GasValveDataItem GasValveData
  47. {
  48. get
  49. {
  50. return (GasValveDataItem)GetValue(GasValveDataProperty);
  51. }
  52. set
  53. {
  54. SetValue(GasValveDataProperty, value);
  55. }
  56. }
  57. public ICommand Command
  58. {
  59. get
  60. {
  61. return (ICommand)GetValue(CommandProperty);
  62. }
  63. set
  64. {
  65. SetValue(CommandProperty, value);
  66. }
  67. }
  68. bool _needBlinding = false;
  69. bool _blindingTag = false;
  70. TimeSpan tsBlindTime = new TimeSpan(0, 0, 3);
  71. DateTime _startBindingTime = new DateTime();
  72. bool _vCurrentOnOff = false;
  73. bool _vPreviousOnOff = false;
  74. DeviceTimer _timer = new DeviceTimer();
  75. bool _isWarning = false;
  76. void SetWarning(string tooltipmessage)
  77. {
  78. nopass.Fill = Brushes.Red;
  79. passtrigle.Fill = Brushes.Red;
  80. this.ToolTip = tooltipmessage;
  81. }
  82. protected override void OnRender(DrawingContext drawingContext)
  83. {
  84. try
  85. {
  86. base.OnRender(drawingContext);
  87. if (GasValveData != null)
  88. {
  89. _vCurrentOnOff = GasValveData.Feedback;
  90. this.ToolTip = string.Format("阀门名称:{0} \r\n设备编号:{1}\r\n默认设定:{2}\r\n当前设定:{3} \r\n实际状态:{4}",
  91. GasValveData.DisplayName,
  92. GasValveData.DeviceId,
  93. GasValveData.DefaultValue,
  94. GasValveData.SetValue,
  95. GasValveData.Feedback);
  96. nopass.Fill = Brushes.Black;
  97. passtrigle.Fill = Brushes.Green;
  98. //判断阀的设定值和实际值是否在容许时间内一致
  99. if (GasValveData.Feedback != GasValveData.SetValue)
  100. {
  101. if (_timer.IsIdle())
  102. {
  103. _timer.Start(500);
  104. }
  105. else
  106. {
  107. if (_timer.IsTimeout())
  108. {
  109. _isWarning = true;
  110. SetWarning(string.Format("设定值和实际值不一致!设定:{0},实际:{1}\r\n", GasValveData.SetValue ? "打开" : "关闭", GasValveData.Feedback ? "打开" : "关闭") + this.ToolTip);
  111. }
  112. }
  113. }
  114. else
  115. {
  116. if (!_timer.IsIdle())
  117. {
  118. _timer.Stop();
  119. }
  120. _isWarning = false;
  121. }
  122. }
  123. if (_needBlinding)
  124. {
  125. if (_blindingTag)
  126. {
  127. if (nopass.Visibility == System.Windows.Visibility.Visible) nopass.Fill = Brushes.LightCoral;
  128. if (passtrigle.Visibility == System.Windows.Visibility.Visible) passtrigle.Fill = Brushes.Gold;
  129. }
  130. else
  131. {
  132. if (nopass.Visibility == System.Windows.Visibility.Visible) nopass.Fill = Brushes.Black;
  133. if (passtrigle.Visibility == System.Windows.Visibility.Visible) passtrigle.Fill = Brushes.Green;
  134. }
  135. _blindingTag = !_blindingTag;
  136. }
  137. if (_vPreviousOnOff != _vCurrentOnOff)
  138. {
  139. _needBlinding = true;
  140. _startBindingTime = DateTime.Now;
  141. _vPreviousOnOff = _vCurrentOnOff;
  142. }
  143. else
  144. {
  145. if (_startBindingTime < DateTime.Now.Subtract(tsBlindTime) && _needBlinding)
  146. {
  147. //停止闪烁
  148. _needBlinding = false;
  149. nopass.Fill = Brushes.Black;
  150. passtrigle.Fill = Brushes.Green;
  151. }
  152. }
  153. switch (ValveDirection)
  154. {
  155. case ValveDirection.ToBottom:
  156. rotateTransform.Angle = 90;
  157. break;
  158. case ValveDirection.ToTop:
  159. rotateTransform.Angle = -90;
  160. break;
  161. case ValveDirection.ToLeft:
  162. rotateTransform.Angle = 180;
  163. break;
  164. case ValveDirection.ToRight:
  165. // No rotation is needed.
  166. break;
  167. default:
  168. break;
  169. }
  170. if (_vCurrentOnOff)
  171. {
  172. passtrigle.Visibility = Visibility.Visible;
  173. nopass.Visibility = Visibility.Hidden;
  174. }
  175. else
  176. {
  177. passtrigle.Visibility = Visibility.Hidden;
  178. nopass.Visibility = Visibility.Visible;
  179. }
  180. }
  181. catch (Exception ex)
  182. {
  183. LOG.Error(ex.Message);
  184. }
  185. }
  186. /// <summary>
  187. /// open
  188. /// </summary>
  189. /// <param name="sender"></param>
  190. /// <param name="e"></param>
  191. private void TurnOnValve(object sender, RoutedEventArgs e)
  192. {
  193. if (Command != null)
  194. {
  195. Command.Execute(new object[] { GasValveData.DeviceName, GasValveOperation.GVTurnValve, true});
  196. }
  197. else if (clickAct != null)
  198. {
  199. clickAct(((MenuItem)e.Source).Tag + "", "true");
  200. }
  201. }
  202. /// <summary>
  203. /// close
  204. /// </summary>
  205. /// <param name="sender"></param>
  206. /// <param name="e"></param>
  207. private void TurnOffValve(object sender, RoutedEventArgs e)
  208. {
  209. if (Command != null)
  210. {
  211. Command.Execute(new object[] { GasValveData.DeviceName, GasValveOperation.GVTurnValve, false });
  212. }
  213. else if (clickAct != null)
  214. {
  215. clickAct(((MenuItem)e.Source).Tag + "", "false");
  216. }
  217. }
  218. private void nopass_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  219. {
  220. if (GasValveData == null || string.IsNullOrEmpty(GasValveData.DeviceName))
  221. return;
  222. ContextMenu mouseClickMenu = new ContextMenu();
  223. MenuItem item = new MenuItem();
  224. item.Header = "_" + GasValveData.DisplayName;
  225. item.Background = Brushes.Gray;
  226. item.Foreground = Brushes.White;
  227. item.IsEnabled = false;
  228. mouseClickMenu.Items.Add(item);
  229. addOpenMenu(mouseClickMenu, item);
  230. if (_isWarning)
  231. {
  232. addCloseMenu(mouseClickMenu, item);
  233. }
  234. mouseClickMenu.IsOpen = true;
  235. }
  236. private void passtrigle_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  237. {
  238. if (GasValveData == null || string.IsNullOrEmpty(GasValveData.DeviceName))
  239. return;
  240. ContextMenu mouseClickMenu = new ContextMenu();
  241. MenuItem item = new MenuItem();
  242. item.Header = "_" + GasValveData.DisplayName;
  243. item.Background = Brushes.Gray;
  244. item.Foreground = Brushes.White;
  245. item.IsEnabled = false;
  246. mouseClickMenu.Items.Add(item);
  247. addCloseMenu(mouseClickMenu, item);
  248. if (_isWarning)
  249. {
  250. addOpenMenu(mouseClickMenu, item);
  251. }
  252. mouseClickMenu.IsOpen = true;
  253. }
  254. void addOpenMenu(ContextMenu mouseClickMenu, MenuItem item)
  255. {
  256. item = new MenuItem();
  257. item.Header = "打开 (_O)";
  258. item.Click += TurnOnValve;
  259. item.Tag = this.Tag;
  260. mouseClickMenu.Items.Add(item);
  261. }
  262. void addCloseMenu(ContextMenu mouseClickMenu, MenuItem item)
  263. {
  264. item = new MenuItem();
  265. item.Header = "关闭 (_C)";
  266. item.Tag = this.Tag;
  267. item.Click += TurnOffValve;
  268. mouseClickMenu.Items.Add(item);
  269. }
  270. private void Canvas_MouseRightButtonUp(object sender, MouseButtonEventArgs e)
  271. {
  272. if (passtrigle.Visibility == Visibility.Visible)
  273. {
  274. passtrigle_MouseLeftButtonDown(null, null);
  275. }
  276. else
  277. {
  278. nopass_MouseLeftButtonDown(null, null);
  279. }
  280. }
  281. private void Canvas_MouseEnter(object sender, MouseEventArgs e)
  282. {
  283. nopass.Fill = Brushes.Goldenrod;
  284. passtrigle.Fill = Brushes.Goldenrod;
  285. }
  286. private void Canvas_MouseLeave(object sender, MouseEventArgs e)
  287. {
  288. nopass.Fill = Brushes.Black;
  289. passtrigle.Fill = Brushes.Green;
  290. }
  291. }
  292. }