GateValve.xaml.cs 9.9 KB

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