AITGasValve.xaml.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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.Animation;
  12. using System.Windows.Media.Imaging;
  13. using System.Windows.Navigation;
  14. using System.Windows.Shapes;
  15. using System.Windows.Threading;
  16. using Aitex.Core.Common.DeviceData;
  17. using Aitex.Core.UI.Control;
  18. using Aitex.Core.Util;
  19. using MECF.Framework.Common.OperationCenter;
  20. namespace Aitex.Core.UI.DeviceControl
  21. {
  22. /// <summary>
  23. /// TexGasValve.xaml 的交互逻辑
  24. /// </summary>
  25. public partial class AITGasValve : UserControl
  26. {
  27. // define dependency properties
  28. public static readonly DependencyProperty CommandProperty = DependencyProperty.Register(
  29. "Command", typeof(ICommand), typeof(AITGasValve),
  30. new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.AffectsRender));
  31. public ICommand Command
  32. {
  33. get
  34. {
  35. return (ICommand)this.GetValue(CommandProperty);
  36. }
  37. set
  38. {
  39. this.SetValue(CommandProperty, value);
  40. }
  41. }
  42. public Visibility MenuVisibility
  43. {
  44. get { return (Visibility)this.GetValue(MenuVisibilityProperty); }
  45. set { this.SetValue(MenuVisibilityProperty, value); }
  46. }
  47. // Using a DependencyProperty as the backing store for MenuVisibility. This enables animation, styling, binding, etc...
  48. public static readonly DependencyProperty MenuVisibilityProperty =
  49. DependencyProperty.Register("MenuVisibility", typeof(Visibility), typeof(AITGasValve),
  50. new FrameworkPropertyMetadata(Visibility.Visible));
  51. public static readonly DependencyProperty DeviceDataProperty = DependencyProperty.Register(
  52. "DeviceData", typeof(AITValveData), typeof(AITGasValve),
  53. new FrameworkPropertyMetadata(new AITValveData(), FrameworkPropertyMetadataOptions.AffectsRender));
  54. public AITValveData DeviceData
  55. {
  56. get
  57. {
  58. var data = (AITValveData)this.GetValue(DeviceDataProperty);
  59. rdTrig.CLK = (data == null || data.IsOpen);
  60. if (rdTrig.R)
  61. {
  62. Application.Current.Dispatcher.BeginInvoke(new Action(() =>
  63. {
  64. Storyboard sb = this.FindResource("OpenValveStoryBoard") as Storyboard;
  65. sb.Begin();
  66. if (imgValveClose.ContextMenu.Items != null && imgValveClose.ContextMenu.Items.Count == 2)
  67. {
  68. ((MenuItem)imgValveClose.ContextMenu.Items[0]).IsEnabled = !IsOpen;
  69. ((MenuItem)imgValveClose.ContextMenu.Items[1]).IsEnabled = IsOpen;
  70. }
  71. }));
  72. }
  73. if (rdTrig.T)
  74. {
  75. Application.Current.Dispatcher.BeginInvoke(new Action(() =>
  76. {
  77. Storyboard sb = this.FindResource("CloseValveStoryBoard") as Storyboard;
  78. sb.Begin();
  79. if (imgValveClose.ContextMenu.Items != null && imgValveClose.ContextMenu.Items.Count == 2)
  80. {
  81. ((MenuItem)imgValveClose.ContextMenu.Items[0]).IsEnabled = !IsOpen;
  82. ((MenuItem)imgValveClose.ContextMenu.Items[1]).IsEnabled = IsOpen;
  83. }
  84. }));
  85. }
  86. return data;
  87. }
  88. set
  89. {
  90. this.SetValue(DeviceDataProperty, value);
  91. }
  92. }
  93. public static readonly DependencyProperty EnableServiceControlProperty = DependencyProperty.Register(
  94. "EnableServiceControl", typeof(bool), typeof(AITGasValve),
  95. new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.AffectsRender));
  96. public bool EnableServiceControl
  97. {
  98. get
  99. {
  100. return (bool)this.GetValue(EnableServiceControlProperty);
  101. }
  102. set
  103. {
  104. this.SetValue(EnableServiceControlProperty, value);
  105. }
  106. }
  107. public static readonly DependencyProperty DeviceData2Property = DependencyProperty.Register(
  108. "DeviceData2", typeof(AITValveData), typeof(AITGasValve),
  109. new FrameworkPropertyMetadata(new AITValveData(), FrameworkPropertyMetadataOptions.AffectsRender));
  110. public AITValveData DeviceData2
  111. {
  112. get
  113. {
  114. return (AITValveData)this.GetValue(DeviceData2Property);
  115. }
  116. set
  117. {
  118. this.SetValue(DeviceData2Property, value);
  119. }
  120. }
  121. public static readonly DependencyProperty ValveOpenOrientationProperty = DependencyProperty.Register(
  122. "ValveOpenOrientation", typeof(LineOrientation), typeof(AITGasValve),
  123. new FrameworkPropertyMetadata(LineOrientation.Horizontal, FrameworkPropertyMetadataOptions.AffectsRender));
  124. public LineOrientation ValveOpenOrientation
  125. {
  126. get { return (LineOrientation)GetValue(ValveOpenOrientationProperty); }
  127. set { SetValue(ValveOpenOrientationProperty, value); }
  128. }
  129. public bool IsOpen => DeviceData == null || DeviceData.IsOpen;
  130. private DispatcherTimer valveTimer = new DispatcherTimer();
  131. RD_TRIG rdTrig = new RD_TRIG();
  132. public AITGasValve()
  133. {
  134. InitializeComponent();
  135. }
  136. private BitmapSource GetImage(string name)
  137. {
  138. return new BitmapImage(new Uri(string.Format("pack://application:,,,/MECF.Framework.Common;component/Resources/Valve/{0}", name)));
  139. }
  140. protected override void OnRender(DrawingContext drawingContext)
  141. {
  142. if (DeviceData != null)
  143. {
  144. this.ToolTip =
  145. $"Valve Name:{DeviceData.DisplayName} \r\n Device ID:{DeviceData.DeviceSchematicId} \r\n SetPoint:{DeviceData.SetPoint} \r\n Status:{DeviceData.Feedback}";
  146. }
  147. base.OnRender(drawingContext);
  148. }
  149. private void OpenValve(object sender, RoutedEventArgs e)
  150. {
  151. if (EnableServiceControl)
  152. {
  153. InvokeClient.Instance.Service.DoOperation($"{DeviceData.UniqueName}.{AITValveOperation.GVTurnValve}", true);
  154. return;
  155. }
  156. if (Command == null)
  157. return;
  158. Command.Execute(new object[] { DeviceData.DeviceName, AITValveOperation.GVTurnValve, true });
  159. if (DeviceData2 != null && !string.IsNullOrEmpty(DeviceData2.DeviceName))
  160. {
  161. Command.Execute(new object[] { DeviceData2.DeviceName, AITValveOperation.GVTurnValve, false });
  162. }
  163. }
  164. private void CloseValve(object sender, RoutedEventArgs e)
  165. {
  166. if (EnableServiceControl)
  167. {
  168. InvokeClient.Instance.Service.DoOperation($"{DeviceData.UniqueName}.{AITValveOperation.GVTurnValve}", false);
  169. return;
  170. }
  171. if (Command == null)
  172. return;
  173. Command.Execute(new object[] { DeviceData.DeviceName, AITValveOperation.GVTurnValve, false });
  174. if (DeviceData2 != null && !string.IsNullOrEmpty(DeviceData2.DeviceName))
  175. {
  176. Command.Execute(new object[] { DeviceData2.DeviceName, AITValveOperation.GVTurnValve, true });
  177. }
  178. }
  179. private void AITGasValve_OnLoaded(object sender, RoutedEventArgs e)
  180. {
  181. imgValveOpen.Source = GetImage(ValveOpenOrientation == LineOrientation.Horizontal ? "ValveOpenVertical.png" : "ValveOpenHorizontal.png");
  182. imgValveClose.Source = GetImage(ValveOpenOrientation == LineOrientation.Vertical ? "ValveCloseVertical.png" : "ValveCloseHorizontal.png");
  183. if (DeviceData == null || DeviceData.IsOpen)
  184. {
  185. Application.Current.Dispatcher.BeginInvoke(new Action(() =>
  186. {
  187. Storyboard sb = this.FindResource("OpenValveStoryBoard") as Storyboard;
  188. sb.Begin();
  189. if (imgValveClose.ContextMenu.Items != null && imgValveClose.ContextMenu.Items.Count == 2)
  190. {
  191. ((MenuItem)imgValveClose.ContextMenu.Items[0]).IsEnabled = !IsOpen;
  192. ((MenuItem)imgValveClose.ContextMenu.Items[1]).IsEnabled = IsOpen;
  193. }
  194. }));
  195. }
  196. else
  197. {
  198. Application.Current.Dispatcher.BeginInvoke(new Action(() =>
  199. {
  200. Storyboard sb = this.FindResource("CloseValveStoryBoard") as Storyboard;
  201. sb.Begin();
  202. if (imgValveClose.ContextMenu.Items != null && imgValveClose.ContextMenu.Items.Count == 2)
  203. {
  204. ((MenuItem)imgValveClose.ContextMenu.Items[0]).IsEnabled = !IsOpen;
  205. ((MenuItem)imgValveClose.ContextMenu.Items[1]).IsEnabled = IsOpen;
  206. }
  207. }));
  208. }
  209. //valveTimer.Start();
  210. }
  211. }
  212. }