终端一体化运控平台
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 

327 行
13 KiB

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