DataHistoryViewModel.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. using Aitex.Core.RT.DataCenter;
  2. using Aitex.Core.RT.Log;
  3. using Aitex.Core.UI.ControlDataContext;
  4. using ControlzEx.Standard;
  5. using LiveCharts.Wpf;
  6. using MECF.Framework.Common.CommonData;
  7. using MECF.Framework.Common.ControlDataContext;
  8. using MECF.Framework.Common.DataCenter;
  9. using OpenSEMI.ClientBase;
  10. using Prism.Commands;
  11. using Prism.Mvvm;
  12. using System;
  13. using System.Collections.Concurrent;
  14. using System.Collections.Generic;
  15. using System.Collections.ObjectModel;
  16. using System.Data;
  17. using System.Linq;
  18. using System.Text;
  19. using System.Threading.Tasks;
  20. using System.Windows;
  21. using System.Windows.Forms;
  22. using System.Windows.Input;
  23. using System.Windows.Media;
  24. using System.Windows.Threading;
  25. using Venus_MainPages.Unity;
  26. using Venus_MainPages.Views;
  27. using WPF.Themes.UserControls;
  28. using Xceed.Wpf.DataGrid;
  29. namespace Venus_MainPages.ViewModels
  30. {
  31. public class DataHistoryViewModel : BindableBase
  32. {
  33. #region 私有字段
  34. private DataHistoryView DataHistoryView;
  35. private ObservableCollection<ParameterNode> _ParameterNodes;
  36. RealtimeProvider _provider = new RealtimeProvider();
  37. private object _lockSelection = new object();
  38. ConcurrentBag<QueryIndexer> _lstTokenTimeData = new ConcurrentBag<QueryIndexer>();
  39. List<string> keys=new List<string> ();
  40. DispatcherTimer timer = new DispatcherTimer();
  41. DateTime currentTime;
  42. #endregion
  43. #region 属性
  44. public ObservableCollection<ParameterNode> ParameterNodes
  45. {
  46. get { return _ParameterNodes; }
  47. set { SetProperty(ref _ParameterNodes, value); }
  48. }
  49. #endregion
  50. #region 命令
  51. private DelegateCommand<object> _LoadCommand;
  52. public DelegateCommand<object> LoadCommand =>
  53. _LoadCommand ?? (_LoadCommand = new DelegateCommand<object>(OnLoad));
  54. private DelegateCommand<object> _ParameterCheckCommand;
  55. public DelegateCommand<object> ParameterCheckCommand =>
  56. _ParameterCheckCommand ?? (_ParameterCheckCommand = new DelegateCommand<object>(OnParameterCheck));
  57. private DelegateCommand _StartCommand;
  58. public DelegateCommand StartCommand =>
  59. _StartCommand ?? (_StartCommand = new DelegateCommand(OnStart));
  60. private DelegateCommand _ClearCommand;
  61. public DelegateCommand ClearCommand =>
  62. _ClearCommand ?? (_ClearCommand = new DelegateCommand(OnClear));
  63. private DelegateCommand _StartRealTimeCommand;
  64. public DelegateCommand StartRealTimeCommand =>
  65. _StartRealTimeCommand ?? (_StartRealTimeCommand = new DelegateCommand(OnStartRealTime));
  66. private DelegateCommand _StopRealTimeCommand;
  67. public DelegateCommand StopRealTimeCommand =>
  68. _StopRealTimeCommand ?? (_StopRealTimeCommand = new DelegateCommand(OnStopRealTime));
  69. #endregion
  70. #region 构造函数
  71. public DataHistoryViewModel()
  72. {
  73. ParameterNodes = _provider.GetParameters();
  74. timer.Interval = TimeSpan.FromSeconds(0.5);
  75. timer.Tick += Timer_Tick; ;
  76. }
  77. #endregion
  78. #region 命令方法
  79. private void OnLoad(Object eventView)
  80. {
  81. this.DataHistoryView = (DataHistoryView)eventView;
  82. this.DataHistoryView.wfTimeFrom.Value = DateTime.Today;
  83. this.DataHistoryView.wfTimeTo.Value = new DateTime(DateTime.Today.Year, DateTime.Today.Month, DateTime.Today.Day, 23, 59, 59, 999);
  84. //this.DataHistoryView.MyDrawGraphicsControl.PointCollections=new PointCollection(new Point[] {})
  85. }
  86. private void OnParameterCheck(object obj)
  87. {
  88. ParameterNode node = obj as ParameterNode;
  89. if (!RefreshTreeStatusToChild(node))
  90. {
  91. node.Selected = !node.Selected;
  92. }
  93. else
  94. {
  95. RefreshTreeStatusToParent(node);
  96. }
  97. }
  98. private void OnStartRealTime()
  99. {
  100. currentTime = DateTime.Now;
  101. timer.Start();
  102. }
  103. private void OnStopRealTime()
  104. {
  105. timer.Stop();
  106. }
  107. private void OnStart()
  108. {
  109. keys.Clear();
  110. for (int i = 0; i < ParameterNodes.Count; i++)
  111. {
  112. CalKeys(ParameterNodes[i]);
  113. }
  114. if (keys.Count > 10)
  115. {
  116. WPFMessageBox.ShowWarning("最多显示10个数据");
  117. return;
  118. }
  119. this.DataHistoryView.MyDrawGraphicsControl.ClearPlotPoints();
  120. var result = GetData(keys.Distinct().ToList(), this.DataHistoryView.wfTimeFrom.Value, this.DataHistoryView.wfTimeTo.Value);
  121. if (result == null)
  122. {
  123. return;
  124. }
  125. List<PointCollection> cls = new List<PointCollection>();
  126. for (int i = 0; i < keys.Count; i++)
  127. {
  128. PointCollection points = new PointCollection();
  129. int k = 1;
  130. result[keys[i]].ForEach(point =>
  131. {
  132. points.Add(new Point() { X = point.dateTime.ToOADate(), Y = point.value });
  133. k += 1;
  134. });
  135. cls.Add(points);
  136. }
  137. this.DataHistoryView.MyDrawGraphicsControl.PointCollections = cls;
  138. this.DataHistoryView.MyDrawGraphicsControl.FitControl();
  139. }
  140. private void OnClear()
  141. {
  142. this.DataHistoryView.MyDrawGraphicsControl.ClearPlotPoints();
  143. }
  144. #endregion
  145. #region 私有方法
  146. private bool RefreshTreeStatusToChild(ParameterNode node)
  147. {
  148. if (node.ChildNodes.Count > 0)
  149. {
  150. for (int i = 0; i < node.ChildNodes.Count; i++)
  151. {
  152. ParameterNode n = node.ChildNodes[i];
  153. n.Selected = node.Selected;
  154. if (!RefreshTreeStatusToChild(n))
  155. {
  156. //uncheck left node
  157. for (int j = i; j < node.ChildNodes.Count; j++)
  158. {
  159. node.ChildNodes[j].Selected = !node.Selected;
  160. }
  161. //node.Selected = !node.Selected;
  162. return false;
  163. }
  164. }
  165. }
  166. //else
  167. //{
  168. // if (node.Selected == true)
  169. // {
  170. // keys.Add(node.Name);
  171. // }
  172. // else
  173. // {
  174. // keys.Remove(node.Name);
  175. // }
  176. //}
  177. return true;
  178. }
  179. private void RefreshTreeStatusToParent(ParameterNode node)
  180. {
  181. if (node.ParentNode != null)
  182. {
  183. if (node.Selected)
  184. {
  185. bool flag = true;
  186. for (int i = 0; i < node.ParentNode.ChildNodes.Count; i++)
  187. {
  188. if (!node.ParentNode.ChildNodes[i].Selected)
  189. {
  190. flag = false; //as least one child is unselected
  191. break;
  192. }
  193. }
  194. if (flag)
  195. node.ParentNode.Selected = true;
  196. }
  197. else
  198. {
  199. node.ParentNode.Selected = false;
  200. }
  201. RefreshTreeStatusToParent(node.ParentNode);
  202. }
  203. }
  204. private Dictionary<string, List<HistoryDataItem>> GetData(List<string> keys, DateTime from, DateTime to)
  205. {
  206. string sql = "select time AS InternalTimeStamp";
  207. foreach (var dataId in keys)
  208. {
  209. sql += "," + string.Format("\"{0}\"", dataId);
  210. }
  211. sql += string.Format(" from \"{0}\" where time > {1} and time <= {2} order by time asc",
  212. from.ToString("yyyyMMdd") + "." + "Data", from.Ticks, to.Ticks);
  213. DataTable dataTable = QueryDataClient.Instance.Service.QueryData(sql);
  214. Dictionary<string, List<HistoryDataItem>> historyData = new Dictionary<string, List<HistoryDataItem>>();
  215. if (dataTable == null || dataTable.Rows.Count == 0)
  216. return null;
  217. DateTime dt = new DateTime();
  218. Dictionary<int, string> colName = new Dictionary<int, string>();
  219. for (int colNo = 0; colNo < dataTable.Columns.Count; colNo++)
  220. {
  221. colName.Add(colNo, dataTable.Columns[colNo].ColumnName);
  222. historyData[dataTable.Columns[colNo].ColumnName] = new List<HistoryDataItem>();
  223. }
  224. for (int rowNo = 0; rowNo < dataTable.Rows.Count; rowNo++)
  225. {
  226. PointCollection points = new PointCollection();
  227. var row = dataTable.Rows[rowNo];
  228. for (int i = 0; i < dataTable.Columns.Count; i++)
  229. {
  230. HistoryDataItem data = new HistoryDataItem();
  231. if (i == 0)
  232. {
  233. long ticks = (long)row[i];
  234. dt = new DateTime(ticks);
  235. continue;
  236. }
  237. else
  238. {
  239. string dataId = colName[i];
  240. if (row[i] is DBNull || row[i] == null)
  241. {
  242. data.dateTime = dt;
  243. data.dbName = colName[i];
  244. data.value = 0;
  245. }
  246. else if (row[i] is bool)
  247. {
  248. data.dateTime = dt;
  249. data.dbName = colName[i];
  250. data.value = (bool)row[i] ? 1 : 0;
  251. }
  252. else
  253. {
  254. data.dateTime = dt;
  255. data.dbName = colName[i];
  256. data.value = float.Parse(row[i].ToString());
  257. }
  258. }
  259. historyData[data.dbName].Add(data);
  260. }
  261. }
  262. foreach (var item in historyData)
  263. {
  264. item.Value.Sort((x, y) => DateTime.Compare(x.dateTime, y.dateTime));
  265. }
  266. return historyData;
  267. }
  268. private void CalKeys(ParameterNode parameterNode)
  269. {
  270. if (parameterNode.ChildNodes.Count > 0)
  271. {
  272. foreach (var item in parameterNode.ChildNodes)
  273. {
  274. CalKeys(item);
  275. }
  276. }
  277. else
  278. {
  279. if (parameterNode.Selected == true)
  280. {
  281. keys.Add(parameterNode.Name);
  282. }
  283. }
  284. }
  285. private void Timer_Tick(object sender, EventArgs e)
  286. {
  287. keys.Clear();
  288. for (int i = 0; i < ParameterNodes.Count; i++)
  289. {
  290. CalKeys(ParameterNodes[i]);
  291. }
  292. if (keys.Count > 10)
  293. {
  294. WPFMessageBox.ShowWarning("最多显示10个数据");
  295. return;
  296. }
  297. this.DataHistoryView.MyDrawGraphicsControl.ClearPlotPoints();
  298. var result = GetData(keys.Distinct().ToList(), currentTime, DateTime.Now);
  299. if (result == null)
  300. {
  301. return;
  302. }
  303. List<PointCollection> cls = new List<PointCollection>();
  304. for (int i = 0; i < keys.Count; i++)
  305. {
  306. PointCollection points = new PointCollection();
  307. int k = 1;
  308. result[keys[i]].ForEach(point =>
  309. {
  310. points.Add(new Point() { X = point.dateTime.ToOADate(), Y = point.value });
  311. k += 1;
  312. });
  313. cls.Add(points);
  314. }
  315. this.DataHistoryView.MyDrawGraphicsControl.PointCollections = cls;
  316. //this.DataHistoryView.MyDrawGraphicsControl.FitControl();
  317. }
  318. #endregion
  319. }
  320. public class QueryIndexer
  321. {
  322. public DateTime TimeToken { get; set; }
  323. public List<string> DataList { get; set; }
  324. public string Module { get; set; }
  325. }
  326. }