ValveRound.xaml.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. namespace Aitex.Core.UI.Control
  16. {
  17. /// <summary>
  18. /// Interaction logic for ValveRound.xaml
  19. /// </summary>
  20. public partial class ValveRound : UserControl
  21. {
  22. public ValveRound()
  23. {
  24. InitializeComponent();
  25. }
  26. public static readonly DependencyProperty CommandProperty = DependencyProperty.Register(
  27. "Command", typeof(ICommand), typeof(ValveRound),
  28. new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.AffectsRender));
  29. public static readonly DependencyProperty CommandParameterProperty = DependencyProperty.Register(
  30. "CommandParameter", typeof(string), typeof(ValveRound),
  31. new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.AffectsRender));
  32. public static readonly DependencyProperty ValveNameProperty = DependencyProperty.Register(
  33. "ValveName", typeof(string), typeof(ValveRound),
  34. new FrameworkPropertyMetadata("未知阀门", FrameworkPropertyMetadataOptions.AffectsRender));
  35. public static readonly DependencyProperty OrientationProperty = DependencyProperty.Register(
  36. "Orientation", typeof(ValveDirection), typeof(ValveRound),
  37. new FrameworkPropertyMetadata(ValveDirection.ToBottom, FrameworkPropertyMetadataOptions.AffectsRender));
  38. public static readonly DependencyProperty FastValveStateProperty = DependencyProperty.Register(
  39. "FastValveState", typeof(GasValveDataItem), typeof(ValveRound),
  40. new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.AffectsRender));
  41. public static readonly DependencyProperty SlowValveStateProperty = DependencyProperty.Register(
  42. "SlowValveState", typeof(GasValveDataItem), typeof(ValveRound),
  43. new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.AffectsRender));
  44. /// <summary>
  45. /// Command to execute when opening/closing valve.
  46. /// </summary>
  47. public ICommand Command
  48. {
  49. get
  50. {
  51. return (ICommand)this.GetValue(CommandProperty);
  52. }
  53. set
  54. {
  55. this.SetValue(CommandProperty, value);
  56. }
  57. }
  58. public string CommandParameter
  59. {
  60. get
  61. {
  62. return (string)this.GetValue(CommandParameterProperty);
  63. }
  64. set
  65. {
  66. this.SetValue(CommandParameterProperty, value);
  67. }
  68. }
  69. /// <summary>
  70. /// Fast valve state
  71. /// </summary>
  72. public GasValveDataItem FastValveState
  73. {
  74. get
  75. {
  76. return (GasValveDataItem)GetValue(FastValveStateProperty);
  77. }
  78. set
  79. {
  80. SetValue(FastValveStateProperty, value);
  81. }
  82. }
  83. /// <summary>
  84. /// Slow valve state
  85. /// </summary>
  86. public GasValveDataItem SlowValveState
  87. {
  88. get
  89. {
  90. return (GasValveDataItem)GetValue(SlowValveStateProperty);
  91. }
  92. set
  93. {
  94. SetValue(SlowValveStateProperty, value);
  95. }
  96. }
  97. /// <summary>
  98. /// 0关闭,1快充,2慢充
  99. /// </summary>
  100. private int ValveState;
  101. /// <summary>
  102. /// direction of the valve
  103. /// </summary>
  104. public ValveDirection Orientation
  105. {
  106. get
  107. {
  108. return (ValveDirection)GetValue(OrientationProperty);
  109. }
  110. set
  111. {
  112. SetValue(OrientationProperty, value);
  113. }
  114. }
  115. /// <summary>
  116. /// valve name
  117. /// </summary>
  118. public string ValveName
  119. {
  120. get
  121. {
  122. return (string)GetValue(ValveNameProperty);
  123. }
  124. set
  125. {
  126. SetValue(ValveNameProperty, value);
  127. }
  128. }
  129. private BitmapSource GetImage(string uri)
  130. {
  131. BitmapImage image = new BitmapImage(new Uri(string.Format("pack://application:,,,/Core;component/Resources/Valve/{0}", uri)));
  132. return image;
  133. }
  134. protected override void OnRender(DrawingContext drawingContext)
  135. {
  136. switch (Orientation)
  137. {
  138. case ValveDirection.ToBottom:
  139. case ValveDirection.ToTop:
  140. rotateTransform.Angle = 0;
  141. break;
  142. case ValveDirection.ToLeft:
  143. case ValveDirection.ToRight:
  144. rotateTransform.Angle = 90;
  145. break;
  146. }
  147. if (FastValveState!=null && FastValveState.Feedback)
  148. {
  149. ValveState = 1;
  150. }
  151. else if (SlowValveState!=null && SlowValveState.Feedback)
  152. {
  153. ValveState = 2;
  154. }
  155. else
  156. {
  157. ValveState = 0;
  158. }
  159. switch (ValveState)
  160. {
  161. case 1:
  162. imagePicture.Source = GetImage("FastPumpValve.png");
  163. break;
  164. case 2:
  165. imagePicture.Source = GetImage("SlowPumpValve.png");
  166. break;
  167. default:
  168. imagePicture.Source = GetImage("ValveClosed.png");
  169. break;
  170. }
  171. base.OnRender(drawingContext);
  172. this.ToolTip = this.ValveName;
  173. }
  174. /// <summary>
  175. /// Turns on/off fast/slow valves.
  176. /// </summary>
  177. /// <param name="sender"></param>
  178. /// <param name="e"></param>
  179. private void SetValve(object sender, RoutedEventArgs e)
  180. {
  181. MenuItem mi = sender as MenuItem;
  182. if (FastValveState == null || SlowValveState == null)
  183. return;
  184. if (Command != null)
  185. {
  186. switch (int.Parse(mi.Tag.ToString()))
  187. {
  188. case 0:
  189. Command.Execute(new object[] { FastValveState.DeviceName, GasValveOperation.GVTurnValve, true });
  190. break;
  191. case 1:
  192. Command.Execute(new object[] { SlowValveState.DeviceName, GasValveOperation.GVTurnValve, true });
  193. break;
  194. case 2:
  195. Command.Execute(new object[] { FastValveState.DeviceName, GasValveOperation.GVTurnValve, false });
  196. break;
  197. case 3:
  198. Command.Execute(new object[] { SlowValveState.DeviceName, GasValveOperation.GVTurnValve, false });
  199. break;
  200. }
  201. }
  202. }
  203. }
  204. }