终端一体化运控平台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

265 lines
9.7 KiB

  1. using BPASmart.Model;
  2. using BPASmartClient.Compiler;
  3. using BPASmartClient.DATABUS;
  4. using Newtonsoft.Json;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.ComponentModel;
  8. using System.Linq;
  9. using System.Reflection;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. using System.Windows;
  13. using System.Windows.Controls;
  14. using System.Windows.Controls.Primitives;
  15. using System.Windows.Data;
  16. using System.Windows.Documents;
  17. using System.Windows.Input;
  18. using System.Windows.Media;
  19. using System.Windows.Media.Animation;
  20. using System.Windows.Media.Imaging;
  21. using System.Windows.Navigation;
  22. using System.Windows.Shapes;
  23. using System.Windows.Threading;
  24. namespace BPASmartClient.SCADAControl.CustomerControls
  25. {
  26. /// <summary>
  27. /// 开关button
  28. /// </summary>
  29. [TemplatePart(Name = ELLIPSE, Type = typeof(FrameworkElement))]
  30. [TemplatePart(Name = TranslateX, Type = typeof(TranslateTransform))]
  31. public class SwitchButton : ToggleButton, IExecutable
  32. {
  33. public event EventHandler PropertyChange; //声明一个事件
  34. public SwitchButton()
  35. {
  36. SetCurrentValue(WidthProperty, 120d);
  37. SetCurrentValue(HeightProperty, 40d);
  38. }
  39. public const string ELLIPSE = "ELLIPSE";
  40. public const string TranslateX = "TranslateX";
  41. static SwitchButton()
  42. {
  43. DefaultStyleKeyProperty.OverrideMetadata(typeof(SwitchButton), new FrameworkPropertyMetadata(typeof(SwitchButton)));
  44. }
  45. [Category("事件")]
  46. public string BindingIsChecked
  47. {
  48. get { return (string)GetValue(BingIsCheckedProperty); }
  49. set { SetValue(BingIsCheckedProperty, value); }
  50. }
  51. public static readonly DependencyProperty BingIsCheckedProperty =
  52. DependencyProperty.Register("BindingIsChecked", typeof(string),typeof(SwitchButton),new PropertyMetadata(string.Empty));
  53. [Category("事件")]
  54. public string SendText
  55. {
  56. get { return (string)GetValue(SendTextProperty); }
  57. set { SetValue(SendTextProperty, value); }
  58. }
  59. public static readonly DependencyProperty SendTextProperty =
  60. DependencyProperty.Register("SendText", typeof(string),typeof(SwitchButton),new PropertyMetadata(string.Empty));
  61. /// <summary>
  62. /// 不勾选时执行代码
  63. /// </summary>
  64. [Category("事件")]
  65. public string UnCheckedExec
  66. {
  67. get { return (string)GetValue(UnCheckedExecProperty); }
  68. set { SetValue(UnCheckedExecProperty, value); }
  69. }
  70. public static readonly DependencyProperty UnCheckedExecProperty =
  71. DependencyProperty.Register("UnCheckedExec", typeof(string), typeof(SwitchButton), new PropertyMetadata(string.Empty));
  72. /// <summary>
  73. /// 勾选时执行代码
  74. /// </summary>
  75. [Category("事件")]
  76. public string CheckedExec
  77. {
  78. get { return (string)GetValue(CheckedExecProperty); }
  79. set { SetValue(CheckedExecProperty, value); }
  80. }
  81. public static readonly DependencyProperty CheckedExecProperty =
  82. DependencyProperty.Register("CheckedExec", typeof(string), typeof(SwitchButton), new PropertyMetadata(string.Empty));
  83. [Category("数据绑定-数据来源")]
  84. public int TimeCount
  85. {
  86. get { return (int)GetValue(TimeCountProperty); }
  87. set { SetValue(TimeCountProperty,value); }
  88. }
  89. public static readonly DependencyProperty TimeCountProperty =
  90. DependencyProperty.Register("TimeCount",typeof(int),typeof(SwitchButton),new PropertyMetadata(100));
  91. protected override void OnRender(DrawingContext drawingContext)
  92. {
  93. base.OnRender(drawingContext);
  94. ellipse.Width = Height - 8;
  95. ellipse.Height = Height - 8;
  96. Refresh();
  97. }
  98. TranslateTransform transX;
  99. Ellipse ellipse;
  100. public override void OnApplyTemplate()
  101. {
  102. base.OnApplyTemplate();
  103. ellipse = GetTemplateChild(ELLIPSE) as Ellipse;
  104. transX = GetTemplateChild(TranslateX) as TranslateTransform;
  105. }
  106. protected override void OnChecked(RoutedEventArgs e)
  107. {
  108. base.OnChecked(e);
  109. Refresh();
  110. }
  111. protected override void OnUnchecked(RoutedEventArgs e)
  112. {
  113. base.OnUnchecked(e);
  114. Refresh();
  115. }
  116. void Refresh()
  117. {
  118. if (ellipse == null)
  119. {
  120. return;
  121. }
  122. DoubleAnimation da = new DoubleAnimation();
  123. da.Duration = new Duration(TimeSpan.FromMilliseconds(250));
  124. da.EasingFunction = new CubicEase() { EasingMode = EasingMode.EaseOut };
  125. if (IsChecked == true)
  126. {
  127. da.To = ActualWidth - ellipse.ActualWidth - 5;
  128. ellipse.SetCurrentValue(Ellipse.FillProperty, Background);
  129. }
  130. else
  131. {
  132. da.To = 3;
  133. ellipse.SetCurrentValue(Ellipse.FillProperty, Brushes.Gray);
  134. }
  135. transX.BeginAnimation(TranslateTransform.XProperty, da);
  136. }
  137. private bool isExecuteState;
  138. public bool IsExecuteState
  139. {
  140. get { return isExecuteState; }
  141. set
  142. {
  143. isExecuteState = value;
  144. if (IsExecuteState)
  145. {
  146. Register();
  147. }
  148. }
  149. }
  150. /// <summary>
  151. /// 属性刷新器
  152. /// </summary>
  153. DispatcherTimer timer = new DispatcherTimer();
  154. /// <summary>
  155. /// 属性绑定变量集合
  156. /// </summary>
  157. Dictionary<string,string> propertyBing = new Dictionary<string,string>();
  158. public void Register()
  159. {
  160. PropertyInfo[] propertyInfos = this.GetType().GetProperties();
  161. foreach (PropertyInfo propertyInfo in propertyInfos)
  162. {
  163. var propName = propertyInfo?.GetValue(this,null);
  164. if (propName is string && propName != null && propName.ToString().Contains("Binding ") && propName.ToString().Contains("."))
  165. {
  166. propertyBing[propertyInfo.Name] = propName.ToString();
  167. }
  168. }
  169. if (propertyBing.Count > 0)
  170. {
  171. timer.Interval = TimeSpan.FromMilliseconds(TimeCount);
  172. timer.Tick += Timer_Tick; ;
  173. timer.Start();
  174. }
  175. this.Click += SwitchButton_Click;
  176. }
  177. /// <summary>
  178. /// 属性刷新事件
  179. /// </summary>
  180. /// <param name="sender"></param>
  181. /// <param name="e"></param>
  182. private void Timer_Tick(object? sender,EventArgs e)
  183. {
  184. try
  185. {
  186. foreach (var item in propertyBing)
  187. {
  188. //{Binding 测试设备.VAR_A_2}
  189. string[] str = item.Value.Replace("{Binding ","").Replace("}","").Split(".");
  190. if (str.Length > 1)
  191. {
  192. if (Class_DataBus.GetInstance().Dic_DeviceData.ContainsKey(str[0]))
  193. {
  194. Dictionary<string,DeviceDataModel> b = Class_DataBus.GetInstance().Dic_DeviceData[str[0]];
  195. if (b != null && b.ContainsKey(str[1]))
  196. {
  197. object _value = b[str[1]].VarVaule;
  198. bool _checked = false;
  199. try
  200. {
  201. _checked = bool.Parse(_value.ToString());
  202. }
  203. catch (Exception ex)
  204. {
  205. _checked = false;
  206. }
  207. EDataType eDataType = (EDataType)Enum.Parse(typeof(EDataType),b[str[1]].DataType);
  208. SendText = JsonConvert.SerializeObject(new PublishModel { DeviceName = str[0],VarName = str[1],Value = _checked.ToString(),DataType = eDataType });
  209. this.GetType().GetProperty("IsChecked").SetValue(this,_checked);
  210. }
  211. }
  212. }
  213. }
  214. }
  215. catch (Exception ex)
  216. {
  217. }
  218. }
  219. private void SwitchButton_Click(object sender,RoutedEventArgs e)
  220. {
  221. timer.Stop();
  222. foreach (var item in propertyBing)
  223. {
  224. //{Binding 测试设备.VAR_A_2}
  225. string[] str = item.Value.Replace("{Binding ","").Replace("}","").Split(".");
  226. if (str.Length > 1)
  227. {
  228. if (Class_DataBus.GetInstance().Dic_DeviceData.ContainsKey(str[0]))
  229. {
  230. Dictionary<string,DeviceDataModel> b = Class_DataBus.GetInstance().Dic_DeviceData[str[0]];
  231. if (b != null && b.ContainsKey(str[1]))
  232. {
  233. object _value = b[str[1]].VarVaule;
  234. EDataType eDataType = (EDataType)Enum.Parse(typeof(EDataType),b[str[1]].DataType);
  235. SendText = JsonConvert.SerializeObject(new PublishModel { DeviceName = str[0],VarName = str[1],Value = this.IsChecked.ToString(),DataType = eDataType });
  236. }
  237. }
  238. }
  239. }
  240. if(this.IsChecked==true) Config.GetInstance().RunJsScipt(CheckedExec);
  241. else Config.GetInstance().RunJsScipt(UnCheckedExec);
  242. timer.Start();
  243. }
  244. public string ControlType => "控件";
  245. }
  246. }