GasValveBig.xaml.cs 11 KB

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