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

294 lines
11 KiB

  1. using BPASmart.Model;
  2. using BPASmartClient.Compiler;
  3. using BPASmartClient.DATABUS;
  4. using BPASmartClient.MessageName.EnumHelp;
  5. using BPASmartClient.SCADAControl;
  6. using Newtonsoft.Json;
  7. using StackExchange.Redis;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.ComponentModel;
  11. using System.Linq;
  12. using System.Text;
  13. using System.Threading.Tasks;
  14. using System.Windows;
  15. using System.Windows.Controls;
  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. /// TheAPI.xaml 的交互逻辑
  28. /// </summary>
  29. public partial class TheAPI :UserControl, IExecutable, IDisposable
  30. {
  31. TextBlock textBlock = null;
  32. public TheAPI()
  33. {
  34. InitializeComponent();
  35. Width = 40;
  36. Height = 40;
  37. this.SizeChanged += TheAPI_SizeChanged; ;
  38. }
  39. private void TheAPI_SizeChanged(object sender,SizeChangedEventArgs e)
  40. {
  41. if (textBlock == null)
  42. {
  43. foreach (TextBlock tb in FindVisualChildren<TextBlock>(this))
  44. {
  45. if (tb.Tag != null)
  46. {
  47. if (tb.Tag.ToString() == "显示文字")
  48. {
  49. textBlock = tb;
  50. }
  51. }
  52. }
  53. }
  54. }
  55. public static IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj) where T : DependencyObject
  56. {
  57. if (depObj != null)
  58. {
  59. for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
  60. {
  61. DependencyObject child = VisualTreeHelper.GetChild(depObj,i);
  62. if (child != null && child is T)
  63. {
  64. yield return (T)child;
  65. }
  66. foreach (T childOfChild in FindVisualChildren<T>(child))
  67. {
  68. yield return childOfChild;
  69. }
  70. }
  71. }
  72. }
  73. public string ControlType => "控件";
  74. private bool isExecuteState;
  75. public bool IsExecuteState
  76. {
  77. get { return isExecuteState; }
  78. set
  79. {
  80. isExecuteState = value;
  81. if (IsExecuteState)
  82. {
  83. Register();
  84. //Style = null;
  85. }
  86. }
  87. }
  88. DispatcherTimer timer = new DispatcherTimer();
  89. public void Register()
  90. {
  91. timer.Interval = TimeSpan.FromSeconds(TimeCount);
  92. timer.Tick += Timer_Tick;
  93. timer.Start();
  94. }
  95. private async void Timer_Tick(object sender,EventArgs e)
  96. {
  97. Config.GetInstance().RunJsScipt(TikcExecute);
  98. try
  99. {
  100. switch (InterfaceMode)
  101. {
  102. case InterfaceModeEnum.POST:
  103. if (!string.IsNullOrEmpty(DataSouceInformation))
  104. {
  105. Direction = 1;
  106. FDataSouce = HttpRequestHelper.HttpPostRequest(DataSouceInformation,InterfaceParameters);
  107. }
  108. break;
  109. case InterfaceModeEnum.GET:
  110. if (!string.IsNullOrEmpty(DataSouceInformation))
  111. {
  112. Direction = 1;
  113. FDataSouce = HttpRequestHelper.HttpGetRequest(DataSouceInformation);
  114. }
  115. break;
  116. case InterfaceModeEnum.PUT:
  117. if (!string.IsNullOrEmpty(DataSouceInformation))
  118. {
  119. Direction = 1;
  120. //string data = HttpRequestHelper.HttpPostRequest(DataSouceInformation,InterfaceParameters);
  121. }
  122. break;
  123. default:
  124. break;
  125. }
  126. }
  127. catch (Exception ex)
  128. {
  129. Direction = 2;
  130. }
  131. }
  132. public void Start() => timer.Start();
  133. public void Stop() => timer.Stop();
  134. public void Dispose()
  135. {
  136. timer.Stop();
  137. }
  138. #region 属性
  139. [Category("值设定")]
  140. public int Direction
  141. {
  142. get { return (int)GetValue(DirectionProperty); }
  143. set { SetValue(DirectionProperty,value); }
  144. }
  145. public static readonly DependencyProperty DirectionProperty =
  146. DependencyProperty.Register("Direction",typeof(int),typeof(TheAPI),
  147. new PropertyMetadata(0,new PropertyChangedCallback(OnPropertyChanged)));
  148. private static void OnPropertyChanged(DependencyObject d,DependencyPropertyChangedEventArgs e)
  149. {
  150. (d as TheAPI)?.Refresh();
  151. }
  152. public void Refresh()
  153. {
  154. if (textBlock != null)
  155. {
  156. if (Direction == 1)
  157. {
  158. textBlock.Visibility = Visibility.Visible;
  159. textBlock.Text = "运行中";
  160. textBlock.Foreground = new SolidColorBrush((System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString("#FF2077EC"));
  161. }
  162. else if (Direction == 2)
  163. {
  164. textBlock.Visibility = Visibility.Visible;
  165. textBlock.Text = "故障";
  166. textBlock.Foreground = new SolidColorBrush((System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString("#FFFF0202"));
  167. }
  168. else
  169. {
  170. textBlock.Visibility = Visibility.Visible;
  171. textBlock.Text = "未运行";
  172. textBlock.Foreground = new SolidColorBrush((System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString("#FF02F9FF"));
  173. }
  174. }
  175. }
  176. /// <summary>
  177. /// 执行内容
  178. /// </summary>
  179. [Category("事件")]
  180. public string TikcExecute
  181. {
  182. get { return (string)GetValue(TikcExecuteProperty); }
  183. set { SetValue(TikcExecuteProperty,value); }
  184. }
  185. public static readonly DependencyProperty TikcExecuteProperty =
  186. DependencyProperty.Register("TikcExecute",typeof(string),typeof(TheAPI),new PropertyMetadata(string.Empty));
  187. #endregion
  188. #region 数据绑定模块
  189. public event EventHandler PropertyChange; //声明一个事件
  190. [Category("数据绑定-数据来源")]
  191. public int TimeCount
  192. {
  193. get { return (int)GetValue(TimeCountProperty); }
  194. set { SetValue(TimeCountProperty,value); }
  195. }
  196. public static readonly DependencyProperty TimeCountProperty =
  197. DependencyProperty.Register("TimeCount",typeof(int),typeof(TheAPI),new PropertyMetadata(5));
  198. [Category("数据绑定-数据来源")]
  199. public InterfaceModeEnum InterfaceMode
  200. {
  201. get { return (InterfaceModeEnum)GetValue(InterfaceModeProperty); }
  202. set { SetValue(InterfaceModeProperty,value); }
  203. }
  204. public static readonly DependencyProperty InterfaceModeProperty =
  205. DependencyProperty.Register("InterfaceMode",typeof(InterfaceModeEnum),typeof(TheAPI),new PropertyMetadata(InterfaceModeEnum.GET));
  206. [Category("数据绑定-数据来源")]
  207. public string InterfaceParameters
  208. {
  209. get { return (string)GetValue(InterfaceParametersProperty); }
  210. set { SetValue(InterfaceParametersProperty,value); }
  211. }
  212. public static readonly DependencyProperty InterfaceParametersProperty =
  213. DependencyProperty.Register("InterfaceParameters",typeof(string),typeof(TheAPI),new PropertyMetadata(string.Empty));
  214. [Category("数据绑定-数据来源")]
  215. public string DataSouceInformation
  216. {
  217. get { return (string)GetValue(DataSouceInformationProperty); }
  218. set { SetValue(DataSouceInformationProperty,value); }
  219. }
  220. public static readonly DependencyProperty DataSouceInformationProperty =
  221. DependencyProperty.Register("DataSouceInformation",typeof(string),typeof(TheAPI),new PropertyMetadata("http://localhost:9092/api/User/UsersTestSwagger"));
  222. [Category("数据绑定-数据来源")]
  223. public string DeviceName
  224. {
  225. get { return (string)GetValue(DeviceNameProperty); }
  226. set { SetValue(DeviceNameProperty,value); }
  227. }
  228. public static readonly DependencyProperty DeviceNameProperty =
  229. DependencyProperty.Register("DeviceName",typeof(string),typeof(TheAPI),new PropertyMetadata(string.Empty));
  230. [Category("数据绑定")]
  231. public string FDataSouce
  232. {
  233. get { return (string)GetValue(FDataSouceProperty); }
  234. set { SetValue(FDataSouceProperty,value); }
  235. }
  236. public static readonly DependencyProperty FDataSouceProperty =
  237. DependencyProperty.Register("FDataSouce",typeof(string),typeof(TheAPI),new PropertyMetadata(string.Empty,new PropertyChangedCallback(onFDataSouceChanged)));
  238. private static void onFDataSouceChanged(DependencyObject d,DependencyPropertyChangedEventArgs e) => (d as TheAPI)?.DataSouceRefresh();
  239. public void DataSouceRefresh()
  240. {
  241. try
  242. {
  243. if (!string.IsNullOrEmpty(FDataSouce))
  244. {
  245. GenerateData = (string)CSharpConfig.GetInstance().RunCSharp(Code,new object[] { FDataSouce });
  246. Class_DataBus.GetInstance().Dic_APIData[this.Name] = GenerateData;
  247. if (PropertyChange != null)
  248. {
  249. PropertyChange(this,null);
  250. }
  251. }
  252. }
  253. catch (Exception ex)
  254. {
  255. }
  256. }
  257. public static string _code = "public string main(string message) \n{ \n //请在此填写你的代码\n\n return message; \n}\n";
  258. [Category("数据绑定")]
  259. public string Code
  260. {
  261. get { return (string)GetValue(CodeProperty); }
  262. set { SetValue(CodeProperty,value); }
  263. }
  264. public static readonly DependencyProperty CodeProperty =
  265. DependencyProperty.Register("Code",typeof(string),typeof(TheAPI),new PropertyMetadata(_code));
  266. [Category("数据绑定")]
  267. public string GenerateData
  268. {
  269. get { return (string)GetValue(GenerateDataProperty); }
  270. set { SetValue(GenerateDataProperty,value); }
  271. }
  272. public static readonly DependencyProperty GenerateDataProperty =
  273. DependencyProperty.Register("GenerateData",typeof(string),typeof(TheAPI),new PropertyMetadata(string.Empty));
  274. #endregion
  275. }
  276. }