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

332 lines
13 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 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. }
  160. #region 属性
  161. [Category("值设定")]
  162. public int Direction
  163. {
  164. get { return (int)GetValue(DirectionProperty); }
  165. set { SetValue(DirectionProperty,value); }
  166. }
  167. public static readonly DependencyProperty DirectionProperty =
  168. DependencyProperty.Register("Direction",typeof(int),typeof(TheAPI),
  169. new PropertyMetadata(0,new PropertyChangedCallback(OnPropertyChanged)));
  170. private static void OnPropertyChanged(DependencyObject d,DependencyPropertyChangedEventArgs e)
  171. {
  172. (d as TheAPI)?.Refresh();
  173. }
  174. public void Refresh()
  175. {
  176. if (textBlock != null)
  177. {
  178. if (Direction == 1)
  179. {
  180. textBlock.Visibility = Visibility.Visible;
  181. textBlock.Text = "运行中";
  182. textBlock.Foreground = new SolidColorBrush((System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString("#FF2077EC"));
  183. }
  184. else if (Direction == 2)
  185. {
  186. textBlock.Visibility = Visibility.Visible;
  187. textBlock.Text = "故障";
  188. textBlock.Foreground = new SolidColorBrush((System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString("#FFFF0202"));
  189. }
  190. else
  191. {
  192. textBlock.Visibility = Visibility.Visible;
  193. textBlock.Text = "未运行";
  194. textBlock.Foreground = new SolidColorBrush((System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString("#FF02F9FF"));
  195. }
  196. }
  197. }
  198. /// <summary>
  199. /// 执行内容
  200. /// </summary>
  201. [Category("事件")]
  202. public string TikcExecute
  203. {
  204. get { return (string)GetValue(TikcExecuteProperty); }
  205. set { SetValue(TikcExecuteProperty,value); }
  206. }
  207. public static readonly DependencyProperty TikcExecuteProperty =
  208. DependencyProperty.Register("TikcExecute",typeof(string),typeof(TheAPI),new PropertyMetadata(string.Empty));
  209. #endregion
  210. #region 数据绑定模块
  211. public event EventHandler PropertyChange; //声明一个事件
  212. [Category("数据绑定-数据来源")]
  213. public int TimeCount
  214. {
  215. get { return (int)GetValue(TimeCountProperty); }
  216. set { SetValue(TimeCountProperty,value); }
  217. }
  218. public static readonly DependencyProperty TimeCountProperty =
  219. DependencyProperty.Register("TimeCount",typeof(int),typeof(TheAPI),new PropertyMetadata(2000));
  220. [Category("数据绑定-数据来源")]
  221. public InterfaceModeEnum InterfaceMode
  222. {
  223. get { return (InterfaceModeEnum)GetValue(InterfaceModeProperty); }
  224. set { SetValue(InterfaceModeProperty,value); }
  225. }
  226. public static readonly DependencyProperty InterfaceModeProperty =
  227. DependencyProperty.Register("InterfaceMode",typeof(InterfaceModeEnum),typeof(TheAPI),new PropertyMetadata(InterfaceModeEnum.GET));
  228. [Category("数据绑定-数据来源")]
  229. public string InterfaceParameters
  230. {
  231. get { return (string)GetValue(InterfaceParametersProperty); }
  232. set { SetValue(InterfaceParametersProperty,value); }
  233. }
  234. public static readonly DependencyProperty InterfaceParametersProperty =
  235. DependencyProperty.Register("InterfaceParameters",typeof(string),typeof(TheAPI),new PropertyMetadata(string.Empty));
  236. [Category("数据绑定-数据来源")]
  237. public string DataSouceInformation
  238. {
  239. get { return (string)GetValue(DataSouceInformationProperty); }
  240. set { SetValue(DataSouceInformationProperty,value); }
  241. }
  242. public static readonly DependencyProperty DataSouceInformationProperty =
  243. DependencyProperty.Register("DataSouceInformation",typeof(string),typeof(TheAPI),new PropertyMetadata("http://datav.dev1.com/api/Alarm/Query"));
  244. //[Category("数据绑定-数据来源")]
  245. //public string DeviceName
  246. //{
  247. // get { return (string)GetValue(DeviceNameProperty); }
  248. // set { SetValue(DeviceNameProperty,value); }
  249. //}
  250. //public static readonly DependencyProperty DeviceNameProperty =
  251. // DependencyProperty.Register("DeviceName",typeof(string),typeof(TheAPI),new PropertyMetadata(string.Empty));
  252. [Category("数据绑定")]
  253. public string FDataSouce
  254. {
  255. get { return (string)GetValue(FDataSouceProperty); }
  256. set { SetValue(FDataSouceProperty,value); }
  257. }
  258. public static readonly DependencyProperty FDataSouceProperty =
  259. DependencyProperty.Register("FDataSouce",typeof(string),typeof(TheAPI),new PropertyMetadata(string.Empty,new PropertyChangedCallback(onFDataSouceChanged)));
  260. private static void onFDataSouceChanged(DependencyObject d,DependencyPropertyChangedEventArgs e) => (d as TheAPI)?.DataSouceRefresh();
  261. public void DataSouceRefresh()
  262. {
  263. try
  264. {
  265. if (!string.IsNullOrEmpty(FDataSouce))
  266. {
  267. GenerateData = (string)CSharpConfig.GetInstance().RunCSharp(Code,new object[] { FDataSouce });
  268. Class_DataBus.GetInstance().Dic_APIData[this.Name] = GenerateData;
  269. if (PropertyChange != null)
  270. {
  271. PropertyChange(this,null);
  272. }
  273. }
  274. }
  275. catch (Exception ex)
  276. {
  277. }
  278. }
  279. public static string _code = "public string main(string message) \n{ \n //请在此填写你的代码\n\n return message; \n}\n";
  280. [Category("数据绑定")]
  281. public string Code
  282. {
  283. get { return (string)GetValue(CodeProperty); }
  284. set { SetValue(CodeProperty,value); }
  285. }
  286. public static readonly DependencyProperty CodeProperty =
  287. DependencyProperty.Register("Code",typeof(string),typeof(TheAPI),new PropertyMetadata(_code));
  288. [Category("数据绑定")]
  289. public bool IsRun
  290. {
  291. get { return (bool)GetValue(RunProperty); }
  292. set { SetValue(RunProperty, value); }
  293. }
  294. public static readonly DependencyProperty RunProperty =
  295. DependencyProperty.Register("IsRun", typeof(bool), typeof(TheAPI), new PropertyMetadata(false, new PropertyChangedCallback(onIsRunChanged)));
  296. private static void onIsRunChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) => (d as TheAPI)?.IsRunRefresh();
  297. public void IsRunRefresh()
  298. {
  299. //测试运行
  300. }
  301. [Category("数据绑定")]
  302. public string GenerateData
  303. {
  304. get { return (string)GetValue(GenerateDataProperty); }
  305. set { SetValue(GenerateDataProperty,value); }
  306. }
  307. public static readonly DependencyProperty GenerateDataProperty =
  308. DependencyProperty.Register("GenerateData",typeof(string),typeof(TheAPI),new PropertyMetadata(string.Empty));
  309. #endregion
  310. }
  311. }