终端一体化运控平台
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.
 
 
 

226 lines
8.6 KiB

  1. using BPASmart.Model;
  2. using BPASmartClient.Compiler;
  3. using BPASmartClient.DATABUS;
  4. using BPASmartClient.SCADAControl;
  5. using Newtonsoft.Json;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.ComponentModel;
  9. using System.Linq;
  10. using System.Reflection;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13. using System.Windows;
  14. using System.Windows.Controls;
  15. using System.Windows.Controls.Primitives;
  16. using System.Windows.Data;
  17. using System.Windows.Documents;
  18. using System.Windows.Input;
  19. using System.Windows.Media;
  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. /// TheToggleButton.xaml 的交互逻辑
  28. /// </summary>
  29. public partial class TheToggleButton : ToggleButton, IExecutable
  30. {
  31. public event EventHandler PropertyChange; //声明一个事件
  32. /// <summary>
  33. /// 开关按钮
  34. /// </summary>
  35. public TheToggleButton()
  36. {
  37. InitializeComponent();
  38. Width = 80;
  39. Height = 30;
  40. ResourceDictionary languageResDic = new ResourceDictionary();
  41. languageResDic.Source = new Uri(@"/BPASmartClient.SCADAControl;component/Themes/Generic.xaml",UriKind.RelativeOrAbsolute);
  42. this.Resources.MergedDictionaries.Add(languageResDic);
  43. }
  44. public string ControlType => "控件";
  45. private bool isExecuteState;
  46. public bool IsExecuteState
  47. {
  48. get { return isExecuteState; }
  49. set
  50. {
  51. isExecuteState = value;
  52. if (IsExecuteState)
  53. {
  54. //Style = Application.Current.Resources["ExecuteToggleButton"] as Style;//FindResource("ExecuteToggleButton") as Style;
  55. Register();
  56. }
  57. }
  58. }
  59. [Category("事件")]
  60. public string BindingIsChecked
  61. {
  62. get { return (string)GetValue(BindingIsCheckedProperty); }
  63. set { SetValue(BindingIsCheckedProperty, value); }
  64. }
  65. public static readonly DependencyProperty BindingIsCheckedProperty =
  66. DependencyProperty.Register("BindingIsChecked", typeof(string),typeof(TheToggleButton),new PropertyMetadata(string.Empty));
  67. [Category("事件")]
  68. public string SendText
  69. {
  70. get { return (string)GetValue(SendTextProperty); }
  71. set { SetValue(SendTextProperty, value); }
  72. }
  73. public static readonly DependencyProperty SendTextProperty =
  74. DependencyProperty.Register("SendText", typeof(string),typeof(TheToggleButton),new PropertyMetadata(string.Empty));
  75. /// <summary>
  76. /// 不勾选时执行代码
  77. /// </summary>
  78. [Category("事件")]
  79. public string UnCheckedExec
  80. {
  81. get { return (string)GetValue(UnCheckedExecProperty); }
  82. set { SetValue(UnCheckedExecProperty, value); }
  83. }
  84. public static readonly DependencyProperty UnCheckedExecProperty =
  85. DependencyProperty.Register("UnCheckedExec", typeof(string), typeof(TheToggleButton), new PropertyMetadata(string.Empty));
  86. /// <summary>
  87. /// 勾选时执行代码
  88. /// </summary>
  89. [Category("事件")]
  90. public string CheckedExec
  91. {
  92. get { return (string)GetValue(CheckedExecProperty); }
  93. set { SetValue(CheckedExecProperty, value); }
  94. }
  95. public static readonly DependencyProperty CheckedExecProperty =
  96. DependencyProperty.Register("CheckedExec", typeof(string), typeof(TheToggleButton), new PropertyMetadata(string.Empty));
  97. [Category("数据绑定-数据来源")]
  98. public int TimeCount
  99. {
  100. get { return (int)GetValue(TimeCountProperty); }
  101. set { SetValue(TimeCountProperty,value); }
  102. }
  103. public static readonly DependencyProperty TimeCountProperty =
  104. DependencyProperty.Register("TimeCount",typeof(int),typeof(TheToggleButton),new PropertyMetadata(100));
  105. /// <summary>
  106. /// 属性刷新器
  107. /// </summary>
  108. DispatcherTimer timer = new DispatcherTimer();
  109. /// <summary>
  110. /// 属性绑定变量集合
  111. /// </summary>
  112. Dictionary<string,string> propertyBing = new Dictionary<string,string>();
  113. public void Register()
  114. {
  115. PropertyInfo[] propertyInfos = this.GetType().GetProperties();
  116. foreach (PropertyInfo propertyInfo in propertyInfos)
  117. {
  118. var propName = propertyInfo?.GetValue(this,null);
  119. if (propName is string && propName != null && propName.ToString().Contains("Binding ") && propName.ToString().Contains("."))
  120. {
  121. propertyBing[propertyInfo.Name] = propName.ToString();
  122. }
  123. }
  124. if (propertyBing.Count > 0)
  125. {
  126. timer.Interval = TimeSpan.FromMilliseconds(TimeCount);
  127. timer.Tick += Timer_Tick; ;
  128. timer.Start();
  129. }
  130. Click += TheToggleButton_Click;
  131. Checked += TheCheckBox_Checked;
  132. Unchecked += TheCheckBox_Unchecked;
  133. }
  134. /// <summary>
  135. /// 属性刷新事件
  136. /// </summary>
  137. /// <param name="sender"></param>
  138. /// <param name="e"></param>
  139. private void Timer_Tick(object? sender,EventArgs e)
  140. {
  141. try
  142. {
  143. foreach (var item in propertyBing)
  144. {
  145. //{Binding 测试设备.VAR_A_2}
  146. string[] str = item.Value.Replace("{Binding ","").Replace("}","").Split(".");
  147. if (str.Length > 1)
  148. {
  149. if (Class_DataBus.GetInstance().Dic_DeviceData.ContainsKey(str[0]))
  150. {
  151. Dictionary<string,DeviceDataModel> b = Class_DataBus.GetInstance().Dic_DeviceData[str[0]];
  152. if (b != null && b.ContainsKey(str[1]))
  153. {
  154. object _value = b[str[1]].VarVaule;
  155. bool _checked = false;
  156. try
  157. {
  158. _checked = bool.Parse(_value.ToString());
  159. }
  160. catch (Exception ex)
  161. {
  162. _checked = false;
  163. }
  164. EDataType eDataType = (EDataType)Enum.Parse(typeof(EDataType),b[str[1]].DataType);
  165. SendText = JsonConvert.SerializeObject(new PublishModel { DeviceName = str[0],VarName = str[1],Value = _checked.ToString(),DataType = eDataType });
  166. this.GetType().GetProperty("IsChecked").SetValue(this,_checked);
  167. }
  168. }
  169. }
  170. }
  171. }
  172. catch (Exception ex)
  173. {
  174. }
  175. }
  176. private void TheToggleButton_Click(object sender,RoutedEventArgs e)
  177. {
  178. timer.Stop();
  179. foreach (var item in propertyBing)
  180. {
  181. //{Binding 测试设备.VAR_A_2}
  182. string[] str = item.Value.Replace("{Binding ","").Replace("}","").Split(".");
  183. if (str.Length > 1)
  184. {
  185. if (Class_DataBus.GetInstance().Dic_DeviceData.ContainsKey(str[0]))
  186. {
  187. Dictionary<string,DeviceDataModel> b = Class_DataBus.GetInstance().Dic_DeviceData[str[0]];
  188. if (b != null && b.ContainsKey(str[1]))
  189. {
  190. object _value = b[str[1]].VarVaule;
  191. EDataType eDataType = (EDataType)Enum.Parse(typeof(EDataType),b[str[1]].DataType);
  192. SendText = JsonConvert.SerializeObject(new PublishModel { DeviceName = str[0],VarName = str[1],Value = "False",DataType = eDataType });
  193. }
  194. }
  195. }
  196. }
  197. if (this.IsChecked == true) Config.GetInstance().RunJsScipt(CheckedExec);
  198. else Config.GetInstance().RunJsScipt(UnCheckedExec);
  199. timer.Start();
  200. }
  201. private void TheCheckBox_Unchecked(object sender, RoutedEventArgs e)
  202. {
  203. }
  204. private void TheCheckBox_Checked(object sender, RoutedEventArgs e)
  205. {
  206. }
  207. }
  208. }