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

TheAPI.xaml.cs 13 KiB

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