GasValveV2.xaml.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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 CoupleValve_V2.xaml
  21. /// 双向阀,一开一关,默认开者状态为主,如果说阀关闭则指的是主阀关闭,阀开指的是主阀打开
  22. ///
  23. /// </summary>
  24. public partial class GasValveV2: UserControl
  25. {
  26. public GasValveV2()
  27. {
  28. InitializeComponent();
  29. Loaded+=new RoutedEventHandler(GasValveV2_Loaded);
  30. }
  31. void GasValveV2_Loaded(object sender, RoutedEventArgs e)
  32. {
  33. switch (GasValveV2Direction)
  34. {
  35. case ValveDirection.ToLeft:
  36. rotateTransform.Angle = -270;
  37. break;
  38. case ValveDirection.ToRight:
  39. rotateTransform.Angle = -90;
  40. break;
  41. case ValveDirection.ToBottom:
  42. break;
  43. case ValveDirection.ToTop:
  44. rotateTransform.Angle = 180;
  45. break;
  46. default:
  47. break;
  48. }
  49. }
  50. //public event Action<string, string> ClickCoupleAct;
  51. public static readonly DependencyProperty GasValveV2DataProperty = DependencyProperty.Register(
  52. "GasValveV2Data", typeof(GasValveDataItem), typeof(GasValveV2),
  53. new FrameworkPropertyMetadata(default(GasValveDataItem), FrameworkPropertyMetadataOptions.AffectsRender));
  54. public static readonly DependencyProperty GasValveV2DirectionProperty = DependencyProperty.Register(
  55. "GasValveV2Direction", typeof(ValveDirection), typeof(GasValveV2),
  56. new FrameworkPropertyMetadata(ValveDirection.ToBottom, FrameworkPropertyMetadataOptions.AffectsRender));
  57. public static readonly DependencyProperty CommandProperty = DependencyProperty.Register(
  58. "Command", typeof(ICommand), typeof(GasValveV2),
  59. new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.AffectsRender));
  60. public ValveDirection GasValveV2Direction
  61. {
  62. get
  63. {
  64. return (ValveDirection)GetValue(GasValveV2DirectionProperty);
  65. }
  66. set
  67. {
  68. SetValue(GasValveV2DirectionProperty, value);
  69. }
  70. }
  71. public GasValveDataItem GasValveV2Data
  72. {
  73. get
  74. {
  75. return (GasValveDataItem)GetValue(GasValveV2DataProperty);
  76. }
  77. set
  78. {
  79. SetValue(GasValveV2DataProperty, value);
  80. }
  81. }
  82. public ICommand Command
  83. {
  84. get
  85. {
  86. return (ICommand)GetValue(CommandProperty);
  87. }
  88. set
  89. {
  90. SetValue(CommandProperty, value);
  91. }
  92. }
  93. public string LeftValveTag { get; set; }
  94. public string RightValveTag { get; set; }
  95. public Thickness nothick = new Thickness(0);
  96. bool _needBlinding = false;
  97. bool _blindingTag = false;
  98. TimeSpan tsBlindTime = new TimeSpan(0, 0, 3);
  99. /// <summary>
  100. /// 容许设定值和真实值不同的时间段 ,在这段时间内不一致可以接受,超出时间就显示警告色
  101. /// </summary>
  102. TimeSpan tsTolerateTime = new TimeSpan(0, 0, 0, 500);
  103. DateTime _startBindingTime = new DateTime();
  104. bool _vCurrentOnOff = false;
  105. bool _vPreviousOnOff = false;
  106. DeviceTimer timer = new DeviceTimer();
  107. /// <summary>
  108. /// 设置警告时显示
  109. /// </summary>
  110. /// <param name="tooltipmessage"></param>
  111. void SetWarning(string tooltipmessage)
  112. {
  113. nopass.Fill = Brushes.Red;
  114. passtrigle.Fill = Brushes.Red;
  115. nopass2.Fill = Brushes.Red;
  116. passtrigle2.Fill = Brushes.Red;
  117. this.ToolTip = tooltipmessage;
  118. }
  119. bool IsWarningSet()
  120. {
  121. return nopass.Fill == Brushes.Red;
  122. }
  123. /// <summary>
  124. /// over rendering behavior
  125. /// </summary>
  126. /// <param name="drawingContext"></param>
  127. protected override void OnRender(DrawingContext drawingContext)
  128. {
  129. try
  130. {
  131. base.OnRender(drawingContext);
  132. //两阀有关联
  133. if (GasValveV2Data != null)
  134. {
  135. this.ToolTip = string.Format("阀门名称:{0} \r\n设备编号:{1}\r\n默认设定:{2}\r\n当前设定:{3} \r\n实际状态:{4}",
  136. GasValveV2Data.DisplayName,
  137. GasValveV2Data.DeviceId,
  138. GasValveV2Data.DefaultValue,
  139. GasValveV2Data.SetValue,
  140. GasValveV2Data.Feedback);
  141. if (GasValveV2Data.Feedback != GasValveV2Data.SetValue)
  142. {
  143. if (timer.IsIdle())
  144. timer.Start(500);
  145. else
  146. {
  147. if (timer.IsTimeout())
  148. {
  149. //if (!IsWarningSet())
  150. SetWarning(string.Format("设定值和实际值不一致!设定:{0},实际:{1}\r\n", GasValveV2Data.SetValue ? "打开" : "关闭", GasValveV2Data.Feedback ? "打开" : "关闭") + this.ToolTip);
  151. }
  152. }
  153. }
  154. else
  155. {
  156. if (!timer.IsIdle())
  157. timer.Stop();
  158. if (nopass.Fill == Brushes.Red)
  159. {
  160. nopass.Fill = Brushes.Black;
  161. passtrigle.Fill = Brushes.Green;
  162. nopass2.Fill = Brushes.Black;
  163. passtrigle2.Fill = Brushes.Green;
  164. }
  165. }
  166. }
  167. if (GasValveV2Data == null)
  168. {
  169. _vCurrentOnOff = false;
  170. }
  171. else
  172. {
  173. _vCurrentOnOff = GasValveV2Data.Feedback;
  174. }
  175. if (_vCurrentOnOff)
  176. {
  177. passtrigle.Visibility = Visibility.Visible;
  178. nopass.Visibility = Visibility.Hidden;
  179. passtrigle2.Visibility = Visibility.Hidden;
  180. nopass2.Visibility = Visibility.Visible;
  181. }
  182. else
  183. {
  184. passtrigle.Visibility = Visibility.Hidden;
  185. nopass.Visibility = Visibility.Visible;
  186. passtrigle2.Visibility = Visibility.Visible;
  187. nopass2.Visibility = Visibility.Hidden;
  188. }
  189. if (_needBlinding)
  190. {
  191. if (_blindingTag)
  192. {
  193. nopass.Fill = Brushes.LightCoral;
  194. nopass2.Fill = Brushes.LightCoral;
  195. passtrigle.Fill = Brushes.Gold;
  196. passtrigle2.Fill = Brushes.Gold;
  197. }
  198. else
  199. {
  200. nopass.Fill = Brushes.Black;
  201. passtrigle.Fill = Brushes.Green;
  202. nopass2.Fill = Brushes.Black;
  203. passtrigle2.Fill = Brushes.Green;
  204. }
  205. _blindingTag = !_blindingTag;
  206. }
  207. if (_vPreviousOnOff != _vCurrentOnOff)
  208. {
  209. _needBlinding = true;
  210. _startBindingTime = DateTime.Now;
  211. _vPreviousOnOff = _vCurrentOnOff;
  212. }
  213. else
  214. {
  215. if (_startBindingTime < DateTime.Now.Subtract(tsBlindTime))
  216. {
  217. //停止闪烁
  218. if (_needBlinding)
  219. {
  220. _needBlinding = false;
  221. nopass.Fill = Brushes.Black;
  222. passtrigle.Fill = Brushes.Green;
  223. nopass2.Fill = Brushes.Black;
  224. passtrigle2.Fill = Brushes.Green;
  225. }
  226. }
  227. }
  228. }
  229. catch (Exception ex)
  230. {
  231. throw ex;
  232. }
  233. }
  234. void SetRectVisible()
  235. {
  236. }
  237. void SetValveDirection(ValveDirection Valve_Direction, RotateTransform rotateTransform)
  238. {
  239. switch (Valve_Direction)
  240. {
  241. case ValveDirection.ToBottom:
  242. rotateTransform.Angle = 90;
  243. break;
  244. case ValveDirection.ToTop:
  245. rotateTransform.Angle = -90;
  246. break;
  247. case ValveDirection.ToLeft:
  248. rotateTransform.Angle = 180;
  249. break;
  250. case ValveDirection.ToRight:
  251. break;
  252. default:
  253. break;
  254. }
  255. }
  256. private void SwitchValve(object sender, RoutedEventArgs e)
  257. {
  258. if ((Command != null) && (GasValveV2Data != null))
  259. {
  260. Command.Execute(new object[] { GasValveV2Data.DeviceName, GasValveOperation.GVTurnValve, !GasValveV2Data.Feedback });
  261. }
  262. }
  263. private void canvasmain_MouseRightButtonUp(object sender, MouseButtonEventArgs e)
  264. {
  265. if (GasValveV2Data == null || string.IsNullOrEmpty(GasValveV2Data.DeviceName))
  266. return;
  267. ContextMenu mouseClickMenu = new ContextMenu();
  268. MenuItem item = new MenuItem();
  269. item.Header = "_" + GasValveV2Data.DisplayName;
  270. item.Background = Brushes.Gray;
  271. item.Foreground = Brushes.White;
  272. item.IsEnabled = false;
  273. mouseClickMenu.Items.Add(item);
  274. item = new MenuItem();
  275. item.Header = "切换 (_C)";
  276. item.Tag = this.Tag;
  277. item.Click += SwitchValve;
  278. mouseClickMenu.Items.Add(item);
  279. mouseClickMenu.IsOpen = true;
  280. }
  281. }
  282. }