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

349 lines
14 KiB

  1. using BPASmartClient.Compiler;
  2. using BPASmartClient.DATABUS;
  3. using BPASmartClient.SCADAControl.Converters;
  4. using BPASmartClient.SCADAControl.EnumClass;
  5. using Newtonsoft.Json;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Collections.ObjectModel;
  9. using System.ComponentModel;
  10. using System.Data;
  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. /// TheDataGrid.xaml 的交互逻辑
  28. /// </summary>
  29. public partial class TheDataGrid :DataGrid, IExecutable, IDisposable
  30. {
  31. public TheDataGrid()
  32. {
  33. InitializeComponent();
  34. Width = 100;
  35. Height = 100;
  36. this.Loaded += TheDataGrid_Loaded; ;
  37. }
  38. private void TheDataGrid_Loaded(object sender, RoutedEventArgs e)
  39. {
  40. TabItems.CollectionChanged += TabItems_CollectionChanged;
  41. }
  42. private void TabItems_CollectionChanged(object? sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
  43. {
  44. if (TabItems.Count > 0)
  45. {
  46. try
  47. {
  48. TabItemsStr = JsonConvert.SerializeObject(TabItems);
  49. GenerateDataRefresh();
  50. }
  51. catch (Exception ex)
  52. {
  53. }
  54. }
  55. }
  56. public string ControlType => "控件";
  57. private bool isExecuteState;
  58. public bool IsExecuteState
  59. {
  60. get { return isExecuteState; }
  61. set
  62. {
  63. isExecuteState = value;
  64. if (IsExecuteState)
  65. {
  66. //Style = null;
  67. Register();
  68. }
  69. }
  70. }
  71. DispatcherTimer timer = new DispatcherTimer();
  72. /// <summary>
  73. /// 注册需要处理的事件
  74. /// </summary>
  75. public void Register()
  76. {
  77. if (!string.IsNullOrEmpty(TabItemsStr))
  78. {
  79. try
  80. {
  81. TabItems = JsonConvert.DeserializeObject<ObservableCollection<列显示设置>>(TabItemsStr);
  82. }
  83. catch (Exception ex)
  84. {
  85. }
  86. }
  87. Style = null;
  88. if (DataSouceType == DataTypeEnum.API接口)
  89. {
  90. timer.Interval = TimeSpan.FromMilliseconds(TimeCount);
  91. timer.Tick += Timer_Tick; ;
  92. timer.Start();
  93. }
  94. }
  95. public void Dispose()
  96. {
  97. timer.Stop();
  98. FDataSouce = "";
  99. if (IsRun) IsRun = false;
  100. GenerateDataRefresh();
  101. }
  102. private void Timer_Tick(object? sender,EventArgs e)
  103. {
  104. if (!string.IsNullOrEmpty(DataSouceInformation))
  105. {
  106. if (DataSouceType == DataTypeEnum.API接口)
  107. {
  108. if (Class_DataBus.GetInstance().Dic_APIData.ContainsKey(DataSouceInformation))
  109. {
  110. FDataSouce = Class_DataBus.GetInstance().Dic_APIData[DataSouceInformation];// = GenerateData;
  111. }
  112. }
  113. else if (DataSouceType == DataTypeEnum.Redis)
  114. {
  115. if (DataSouceInformation.Contains(".") && DataSouceInformation.Contains("{Binding "))
  116. {
  117. string[] str = DataSouceInformation.Replace("{Binding ", "").Replace("}", "").Split(".");
  118. if (str.Length > 1)
  119. {
  120. if (Class_DataBus.GetInstance().Dic_RedisData.ContainsKey(str[0]))
  121. {
  122. Dictionary<string, object> b = Class_DataBus.GetInstance().Dic_RedisData[str[0]];
  123. if (b != null && b.ContainsKey(str[1]))
  124. FDataSouce = b[str[1]].ToString();
  125. }
  126. }
  127. }
  128. }
  129. else if (DataSouceType == DataTypeEnum.静态数据)
  130. {
  131. FDataSouce = "";
  132. }
  133. }
  134. }
  135. #region 数据绑定模块
  136. [Category("名称[自动生成]")]
  137. private string TabItemsStr
  138. {
  139. get { return (string)GetValue(TabItemsStrProperty); }
  140. set { SetValue(TabItemsStrProperty, value); }
  141. }
  142. private static readonly DependencyProperty TabItemsStrProperty =
  143. DependencyProperty.Register("TabItemsStr", typeof(string), typeof(TheDataGrid), new PropertyMetadata(string.Empty));
  144. [Category("集合")]
  145. public ObservableCollection<列显示设置> TabItems
  146. {
  147. get { return (ObservableCollection<列显示设置>)GetValue(TabItemsProperty); }
  148. set { SetValue(TabItemsProperty, value); }
  149. }
  150. private static readonly DependencyProperty TabItemsProperty =
  151. DependencyProperty.Register("TabItems", typeof(ObservableCollection<列显示设置>), typeof(TheDataGrid), new PropertyMetadata(new ObservableCollection<列显示设置>()));
  152. public event EventHandler PropertyChange; //声明一个事件
  153. [Category("数据绑定-数据来源")]
  154. public DataTypeEnum DataSouceType
  155. {
  156. get { return (DataTypeEnum)GetValue(DataSouceTypeProperty); }
  157. set { SetValue(DataSouceTypeProperty,value); }
  158. }
  159. public static readonly DependencyProperty DataSouceTypeProperty =
  160. DependencyProperty.Register("DataSouceType",typeof(DataTypeEnum),typeof(TheDataGrid),new PropertyMetadata(DataTypeEnum.API接口));
  161. [Category("数据绑定-数据来源")]
  162. public int TimeCount
  163. {
  164. get { return (int)GetValue(TimeCountProperty); }
  165. set { SetValue(TimeCountProperty,value); }
  166. }
  167. public static readonly DependencyProperty TimeCountProperty =
  168. DependencyProperty.Register("TimeCount",typeof(int),typeof(TheDataGrid),new PropertyMetadata(500));
  169. [Category("数据绑定-数据来源")]
  170. public string DataSouceInformation
  171. {
  172. get { return (string)GetValue(DataSouceInformationProperty); }
  173. set { SetValue(DataSouceInformationProperty,value); }
  174. }
  175. public static readonly DependencyProperty DataSouceInformationProperty =
  176. DependencyProperty.Register("DataSouceInformation",typeof(string),typeof(TheDataGrid),new PropertyMetadata("api0"));
  177. [Category("数据绑定")]
  178. public string FDataSouce
  179. {
  180. get { return (string)GetValue(FDataSouceProperty); }
  181. set { SetValue(FDataSouceProperty,value); }
  182. }
  183. public static readonly DependencyProperty FDataSouceProperty =
  184. DependencyProperty.Register("FDataSouce",typeof(string),typeof(TheDataGrid),new PropertyMetadata(string.Empty,new PropertyChangedCallback(onFDataSouceChanged)));
  185. private static void onFDataSouceChanged(DependencyObject d,DependencyPropertyChangedEventArgs e) => (d as TheDataGrid)?.DataSouceRefresh();
  186. public void DataSouceRefresh()
  187. {
  188. try
  189. {
  190. //if (!string.IsNullOrEmpty(FDataSouce))
  191. {
  192. GenerateData = (string)CSharpConfig.GetInstance().RunCSharp(Code,new object[] { FDataSouce });
  193. if (PropertyChange != null)
  194. {
  195. PropertyChange(this,null);
  196. }
  197. }
  198. }
  199. catch (Exception ex)
  200. {
  201. }
  202. }
  203. public static string _code = "public string main(string message) \n{ \n //请在此填写你的代码\n\n return message; \n}\n";
  204. [Category("数据绑定")]
  205. public string Code
  206. {
  207. get { return (string)GetValue(CodeProperty); }
  208. set { SetValue(CodeProperty,value); }
  209. }
  210. public static readonly DependencyProperty CodeProperty =
  211. DependencyProperty.Register("Code",typeof(string),typeof(TheDataGrid),new PropertyMetadata(_code));
  212. [Category("数据绑定")]
  213. public bool IsRun
  214. {
  215. get { return (bool)GetValue(RunProperty); }
  216. set { SetValue(RunProperty, value); }
  217. }
  218. public static readonly DependencyProperty RunProperty =
  219. DependencyProperty.Register("IsRun", typeof(bool), typeof(TheDataGrid), new PropertyMetadata(false, new PropertyChangedCallback(onIsRunChanged)));
  220. private static void onIsRunChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) => (d as TheDataGrid)?.IsRunRefresh();
  221. public void IsRunRefresh()
  222. {
  223. //测试运行
  224. if (IsRun)
  225. IsExecuteState = true;
  226. else
  227. {
  228. IsExecuteState = false;
  229. Dispose();
  230. }
  231. }
  232. [Category("数据绑定")]
  233. public string GenerateData
  234. {
  235. get { return (string)GetValue(GenerateDataProperty); }
  236. set { SetValue(GenerateDataProperty,value); }
  237. }
  238. public static readonly DependencyProperty GenerateDataProperty =
  239. DependencyProperty.Register("GenerateData",typeof(string),typeof(TheDataGrid),new PropertyMetadata(string.Empty,new PropertyChangedCallback(onGenerateDataChanged)));
  240. private static void onGenerateDataChanged(DependencyObject d,DependencyPropertyChangedEventArgs e) => (d as TheDataGrid)?.GenerateDataRefresh();
  241. public void GenerateDataRefresh()
  242. {
  243. if (!string.IsNullOrEmpty(GenerateData) && GenerateData.Contains("data"))
  244. {
  245. try
  246. {
  247. Dictionary<string, object> keys = JsonConvert.DeserializeObject<Dictionary<string, object>>(GenerateData);
  248. List<Dictionary<string, object>> obj2 = JsonConvert.DeserializeObject<List<Dictionary<string, object>>>(keys["data"].ToString());
  249. //ItemsString = JsonConvert.DeserializeObject<DataSouceModel>(GenerateData);
  250. // 运行时进行项目绑定
  251. //this.AutoGenerateColumns = false;
  252. this.CanUserAddRows = false;
  253. this.CanUserDeleteRows = false;
  254. this.RowHeaderWidth = 0;
  255. this.GridLinesVisibility = DataGridGridLinesVisibility.None;
  256. this.BorderThickness = new Thickness(0);
  257. this.ItemsSource = ListToDataTable(obj2).DefaultView;
  258. }
  259. catch (Exception ex)
  260. {
  261. }
  262. }
  263. else
  264. {
  265. // this.ItemsSource = new DataTable().DefaultView;
  266. }
  267. }
  268. public DataTable ListToDataTable(List<Dictionary<string, object>> arrayList)
  269. {
  270. DataTable dataTable = new DataTable(); //实例化
  271. DataTable result;
  272. try
  273. {
  274. if (arrayList.Count > 0)
  275. {
  276. foreach (Dictionary<string, object> dictionary in arrayList)
  277. {
  278. if (dictionary.Keys.Count<string>() == 0)
  279. {
  280. result = dataTable;
  281. return result;
  282. }
  283. if (TabItems == null || TabItems.Count == 0)
  284. {
  285. foreach (string current in dictionary.Keys)
  286. {
  287. TabItems.Add(new 列显示设置 { 列标题 = current, 是否显示 = true, 显示名称 = current });
  288. }
  289. }
  290. //在第一次循环时确定datatable的列名,后续循环无需再更改
  291. if (dataTable.Columns.Count == 0)
  292. {
  293. //此处仅考虑一层json字符串的形式,多层结构需要深入得到叶子节点的key
  294. foreach (var current in TabItems)
  295. {
  296. if(current.是否显示)
  297. dataTable.Columns.Add(current.显示名称);//, dictionary[current].GetType());
  298. }
  299. }
  300. //每次循环增加datatable的Rows
  301. DataRow dataRow = dataTable.NewRow();
  302. foreach (var current in TabItems)
  303. {
  304. if (current.是否显示)
  305. dataRow[current.显示名称] = dictionary[current.列标题];
  306. }
  307. dataTable.Rows.Add(dataRow); //循环添加行到DataTable中
  308. }
  309. }
  310. }
  311. catch
  312. {
  313. }
  314. result = dataTable;
  315. return result;
  316. }
  317. #endregion
  318. }
  319. public class 列显示设置
  320. {
  321. public string 列标题 { get; set; }
  322. public string 显示名称 { get; set; }
  323. public bool 是否显示 { get; set; }
  324. }
  325. }