DataHistoryViewModel.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. using Aitex.Core.UI.ControlDataContext;
  2. using MECF.Framework.Common.DataCenter;
  3. using Newtonsoft.Json;
  4. using Prism.Commands;
  5. using Prism.Mvvm;
  6. using System;
  7. using System.Collections.Concurrent;
  8. using System.Collections.Generic;
  9. using System.Collections.ObjectModel;
  10. using System.Data;
  11. using System.Linq;
  12. using System.Windows;
  13. using System.Windows.Media;
  14. using System.Windows.Threading;
  15. using Venus_MainPages.Unity;
  16. using Venus_MainPages.Views;
  17. using WPF.Themes.UserControls;
  18. namespace Venus_MainPages.ViewModels
  19. {
  20. public class DataHistoryViewModel : BindableBase
  21. {
  22. #region 私有字段
  23. private DataHistoryView DataHistoryView;
  24. private ObservableCollection<ParameterNode> _ParameterNodes;
  25. RealtimeProvider _provider = new RealtimeProvider();
  26. //private object _lockSelection = new object();
  27. //ConcurrentBag<QueryIndexer> _lstTokenTimeData = new ConcurrentBag<QueryIndexer>();
  28. ObservableCollection<string> m_Keys=new ObservableCollection<string> ();
  29. ObservableCollection<KeyData> m_KeyDataObservableCollection = new ObservableCollection<KeyData>();
  30. DispatcherTimer timer = new DispatcherTimer();
  31. public List<SolidColorBrush> solidColorBrushes = new List<SolidColorBrush> ();
  32. DateTime currentTime;
  33. private bool firstFlag = true;
  34. private DateTime startTime=DateTime.Now;
  35. private bool m_IsShowStep;
  36. #endregion
  37. #region 属性
  38. public ObservableCollection<KeyData> OldKeyDataCollection { get; set; }
  39. public ObservableCollection<ParameterNode> ParameterNodes
  40. {
  41. get { return _ParameterNodes; }
  42. set { SetProperty(ref _ParameterNodes, value); }
  43. }
  44. public ObservableCollection<string> Keys
  45. {
  46. get { return m_Keys; }
  47. set { SetProperty(ref m_Keys, value); }
  48. }
  49. public ObservableCollection<KeyData> KeyDataObservableCollection
  50. {
  51. get { return m_KeyDataObservableCollection; }
  52. set { SetProperty(ref m_KeyDataObservableCollection, value); }
  53. }
  54. public bool IsShowStep
  55. {
  56. get { return m_IsShowStep; }
  57. set { SetProperty(ref m_IsShowStep, value); }
  58. }
  59. #endregion
  60. #region 命令
  61. private DelegateCommand<object> _LoadCommand;
  62. public DelegateCommand<object> LoadCommand =>
  63. _LoadCommand ?? (_LoadCommand = new DelegateCommand<object>(OnLoad));
  64. private DelegateCommand _UnLoadCommand;
  65. public DelegateCommand UnLoadCommand =>
  66. _UnLoadCommand ?? (_UnLoadCommand = new DelegateCommand(OnUnLoad));
  67. private DelegateCommand<object> _ParameterCheckCommand;
  68. public DelegateCommand<object> ParameterCheckCommand =>
  69. _ParameterCheckCommand ?? (_ParameterCheckCommand = new DelegateCommand<object>(OnParameterCheck));
  70. private DelegateCommand _StartCommand;
  71. public DelegateCommand StartCommand =>
  72. _StartCommand ?? (_StartCommand = new DelegateCommand(OnStart));
  73. private DelegateCommand _ClearCommand;
  74. public DelegateCommand ClearCommand =>
  75. _ClearCommand ?? (_ClearCommand = new DelegateCommand(OnClear));
  76. private DelegateCommand<object> _StartRealTimeCommand;
  77. public DelegateCommand<object> StartRealTimeCommand =>
  78. _StartRealTimeCommand ?? (_StartRealTimeCommand = new DelegateCommand<object>(OnStartRealTime));
  79. private DelegateCommand _StopRealTimeCommand;
  80. public DelegateCommand StopRealTimeCommand =>
  81. _StopRealTimeCommand ?? (_StopRealTimeCommand = new DelegateCommand(OnStopRealTime));
  82. #endregion
  83. #region 构造函数
  84. public DataHistoryViewModel()
  85. {
  86. OldKeyDataCollection = new ObservableCollection<KeyData>();
  87. ParameterNodes = _provider.GetParameters();
  88. timer.Interval = TimeSpan.FromSeconds(1);
  89. timer.Tick += Timer_Tick;
  90. solidColorBrushes.Add(new SolidColorBrush (Colors.Green));
  91. solidColorBrushes.Add(new SolidColorBrush(Colors.Red));
  92. solidColorBrushes.Add(new SolidColorBrush(Colors.Blue));
  93. solidColorBrushes.Add(new SolidColorBrush(Colors.Orange));
  94. solidColorBrushes.Add(new SolidColorBrush(Colors.Yellow));
  95. solidColorBrushes.Add(new SolidColorBrush(Colors.YellowGreen));
  96. solidColorBrushes.Add(new SolidColorBrush(Colors.AliceBlue));
  97. solidColorBrushes.Add(new SolidColorBrush(Colors.Chocolate));
  98. solidColorBrushes.Add(new SolidColorBrush(Colors.Cyan));
  99. solidColorBrushes.Add(new SolidColorBrush(Colors.DarkGreen));
  100. }
  101. #endregion
  102. #region 命令方法
  103. private void OnLoad(Object eventView)
  104. {
  105. if (firstFlag)
  106. {
  107. this.DataHistoryView = (DataHistoryView)eventView;
  108. this.DataHistoryView.wfTimeFrom.Value = DateTime.Today;
  109. this.DataHistoryView.wfTimeTo.Value = new DateTime(DateTime.Today.Year, DateTime.Today.Month, DateTime.Today.Day, 23, 59, 59, 999);
  110. //this.DataHistoryView.MyDrawGraphicsControl.PointCollections=new PointCollection(new Point[] {})
  111. firstFlag = false;
  112. }
  113. }
  114. private void OnUnLoad()
  115. {
  116. GC.Collect(); // This should pick up the control removed at a previous MouseDown
  117. GC.WaitForPendingFinalizers(); // Doesn't help either
  118. GC.Collect();
  119. GC.WaitForPendingFinalizers(); // Doesn't help either
  120. }
  121. private void OnParameterCheck(object obj)
  122. {
  123. startTime= DateTime.Now;
  124. ParameterNode node = obj as ParameterNode;
  125. if (!RefreshTreeStatusToChild(node))
  126. {
  127. node.Selected = !node.Selected;
  128. }
  129. else
  130. {
  131. RefreshTreeStatusToParent(node);
  132. }
  133. Keys.Clear();
  134. for (int i = 0; i < ParameterNodes.Count; i++)
  135. {
  136. CalKeys(ParameterNodes[i]);
  137. }
  138. OldKeyDataCollection.Clear();
  139. OldKeyDataCollection = DeepCopyJson(KeyDataObservableCollection);
  140. KeyDataObservableCollection.Clear();
  141. for (int i = 0; i < Keys.Count; i++)
  142. {
  143. if (i == 10)
  144. {
  145. break;
  146. }
  147. KeyDataObservableCollection.Add(new KeyData() { Key = Keys[i], Color = solidColorBrushes[i],UniqueId=i });
  148. }
  149. Compare(OldKeyDataCollection, KeyDataObservableCollection);
  150. KeyDataObservableCollection = DeepCopyJson(OldKeyDataCollection);
  151. }
  152. public void Compare(ObservableCollection<KeyData> olddata, ObservableCollection<KeyData> newdata)
  153. {
  154. if (newdata != null)
  155. {
  156. for (int i = 0; i < newdata.Count; i++)
  157. {
  158. var lists = olddata.ToList().Find(t => t.Key == newdata[i].Key);
  159. if (lists == null)
  160. {
  161. olddata.Add(new KeyData() { Key = newdata[i].Key, Color = solidColorBrushes[i], UniqueId = olddata.Count + 1 });
  162. }
  163. }
  164. for (int i = 0; i < olddata.Count; i++)
  165. {
  166. var lists = newdata.ToList().Find(t => t.Key == olddata[i].Key);
  167. if (lists == null)
  168. {
  169. olddata.Remove(olddata[i]);
  170. }
  171. }
  172. }
  173. }
  174. public static T DeepCopyJson<T>(T obj)
  175. {
  176. // 序列化
  177. string json = JsonConvert.SerializeObject(obj);
  178. // 反序列化
  179. return JsonConvert.DeserializeObject<T>(json);
  180. }
  181. private void OnStartRealTime(object obj)
  182. {
  183. if (Keys.Count > 10)
  184. {
  185. ((System.Windows.Controls.CheckBox)obj).IsChecked= false;
  186. timer.Stop();
  187. WPFMessageBox.ShowWarning("最多显示10个数据");
  188. return;
  189. }
  190. currentTime = DateTime.Now;
  191. timer.Start();
  192. }
  193. private void OnStopRealTime()
  194. {
  195. timer.Stop();
  196. }
  197. public void OnStart()
  198. {
  199. if (Keys.Count > 10)
  200. {
  201. WPFMessageBox.ShowWarning("最多显示10个数据");
  202. return;
  203. }
  204. this.DataHistoryView.MyDrawGraphicsControl.ClearPlotPoints();
  205. var result = GetData(Keys.Distinct().ToList(), this.DataHistoryView.wfTimeFrom.Value, this.DataHistoryView.wfTimeTo.Value);
  206. if (result == null)
  207. {
  208. return;
  209. }
  210. List<PointCollection> cls = new List<PointCollection>();
  211. for (int i = 0; i < Keys.Count; i++)
  212. {
  213. PointCollection points = new PointCollection();
  214. int k = 1;
  215. result[Keys[i]].ForEach(point =>
  216. {
  217. points.Add(new Point() { X = point.dateTime.ToOADate(), Y = point.value });
  218. k += 1;
  219. });
  220. cls.Add(points);
  221. }
  222. //for (int i = 0; i < KeyDataObservableCollection.Count(); i++)
  223. //{
  224. // var _color = KeyDataObservableCollection[i].Color.Color;
  225. // this.DataHistoryView.MyDrawGraphicsControl.m_PenCollencteions[i] = new System.Drawing.Pen(System.Drawing.Color.FromArgb(_color.A, _color.R, _color.G, _color.B), 2);
  226. //}
  227. //ColorChanged();
  228. this.DataHistoryView.MyDrawGraphicsControl.PointCollections = cls;
  229. this.DataHistoryView.MyDrawGraphicsControl.FitControl();
  230. this.DataHistoryView.MyDrawGraphicsControl.YPoints.Clear();
  231. if (IsShowStep)
  232. {
  233. var item = QueryDataClient.Instance.Service.GetHistorySteps(this.DataHistoryView.wfTimeFrom.Value, this.DataHistoryView.wfTimeTo.Value);
  234. if (item != null)
  235. {
  236. item.ForEach(x =>
  237. {
  238. this.DataHistoryView.MyDrawGraphicsControl.YPoints.Add(new Venus_Core.StepItem() { StartValue = x.StartTime.ToOADate(), Information = $"{x.RecipeId}\n{x.StepNo}\n{x.StartTime.ToString("HH:mm:ss:fff")}" });
  239. });
  240. }
  241. }
  242. }
  243. private void CloseAll(ObservableCollection<ParameterNode> parameterNodes)
  244. {
  245. foreach (var item in parameterNodes)
  246. {
  247. item.Selected = false;
  248. if (item.ChildNodes.Count > 0)
  249. {
  250. CloseAll(item.ChildNodes);
  251. }
  252. }
  253. }
  254. private void OnClear()
  255. {
  256. this.DataHistoryView.MyDrawGraphicsControl.ClearPlotPoints();
  257. this.DataHistoryView.MyDrawGraphicsControl.YPoints = new List<Venus_Core.StepItem>();
  258. KeyDataObservableCollection.Clear();
  259. CloseAll(ParameterNodes);
  260. Keys.Clear();
  261. this.DataHistoryView.MyDrawGraphicsControl.InitPens();
  262. }
  263. #endregion
  264. #region 私有方法
  265. private bool RefreshTreeStatusToChild(ParameterNode node)
  266. {
  267. if (node.ChildNodes.Count > 0)
  268. {
  269. for (int i = 0; i < node.ChildNodes.Count; i++)
  270. {
  271. ParameterNode n = node.ChildNodes[i];
  272. n.Selected = node.Selected;
  273. if (!RefreshTreeStatusToChild(n))
  274. {
  275. //uncheck left node
  276. for (int j = i; j < node.ChildNodes.Count; j++)
  277. {
  278. node.ChildNodes[j].Selected = !node.Selected;
  279. }
  280. //node.Selected = !node.Selected;
  281. return false;
  282. }
  283. }
  284. }
  285. //else
  286. //{
  287. // if (node.Selected == true)
  288. // {
  289. // keys.Add(node.Name);
  290. // }
  291. // else
  292. // {
  293. // keys.Remove(node.Name);
  294. // }
  295. //}
  296. return true;
  297. }
  298. private void RefreshTreeStatusToParent(ParameterNode node)
  299. {
  300. if (node.ParentNode != null)
  301. {
  302. if (node.Selected)
  303. {
  304. bool flag = true;
  305. for (int i = 0; i < node.ParentNode.ChildNodes.Count; i++)
  306. {
  307. if (!node.ParentNode.ChildNodes[i].Selected)
  308. {
  309. flag = false; //as least one child is unselected
  310. break;
  311. }
  312. }
  313. if (flag)
  314. node.ParentNode.Selected = true;
  315. }
  316. else
  317. {
  318. node.ParentNode.Selected = false;
  319. }
  320. RefreshTreeStatusToParent(node.ParentNode);
  321. }
  322. }
  323. private Dictionary<string, List<HistoryDataItem>> GetData(List<string> keys, DateTime from, DateTime to)
  324. {
  325. string sql = "select time AS InternalTimeStamp";
  326. foreach (var dataId in keys)
  327. {
  328. sql += "," + string.Format("\"{0}\"", dataId);
  329. }
  330. sql += string.Format(" from \"{0}\" where time > {1} and time <= {2} order by time asc",
  331. from.ToString("yyyyMMdd") + "." + "Data", from.Ticks, to.Ticks);
  332. DataTable dataTable = QueryDataClient.Instance.Service.QueryData(sql);
  333. Dictionary<string, List<HistoryDataItem>> historyData = new Dictionary<string, List<HistoryDataItem>>();
  334. if (dataTable == null || dataTable.Rows.Count == 0)
  335. return null;
  336. DateTime dt = new DateTime();
  337. Dictionary<int, string> colName = new Dictionary<int, string>();
  338. for (int colNo = 0; colNo < dataTable.Columns.Count; colNo++)
  339. {
  340. colName.Add(colNo, dataTable.Columns[colNo].ColumnName);
  341. historyData[dataTable.Columns[colNo].ColumnName] = new List<HistoryDataItem>();
  342. }
  343. for (int rowNo = 0; rowNo < dataTable.Rows.Count; rowNo++)
  344. {
  345. PointCollection points = new PointCollection();
  346. var row = dataTable.Rows[rowNo];
  347. for (int i = 0; i < dataTable.Columns.Count; i++)
  348. {
  349. HistoryDataItem data = new HistoryDataItem();
  350. if (i == 0)
  351. {
  352. long ticks = (long)row[i];
  353. dt = new DateTime(ticks);
  354. continue;
  355. }
  356. else
  357. {
  358. string dataId = colName[i];
  359. if (row[i] is DBNull || row[i] == null)
  360. {
  361. data.dateTime = dt;
  362. data.dbName = colName[i];
  363. data.value = 0;
  364. }
  365. else if (row[i] is bool)
  366. {
  367. data.dateTime = dt;
  368. data.dbName = colName[i];
  369. data.value = (bool)row[i] ? 1 : 0;
  370. }
  371. else
  372. {
  373. data.dateTime = dt;
  374. data.dbName = colName[i];
  375. data.value = float.Parse(row[i].ToString());
  376. }
  377. }
  378. historyData[data.dbName].Add(data);
  379. }
  380. }
  381. foreach (var item in historyData)
  382. {
  383. item.Value.Sort((x, y) => DateTime.Compare(x.dateTime, y.dateTime));
  384. }
  385. return historyData;
  386. }
  387. private void CalKeys(ParameterNode parameterNode)
  388. {
  389. if (parameterNode.ChildNodes.Count > 0)
  390. {
  391. foreach (var item in parameterNode.ChildNodes)
  392. {
  393. CalKeys(item);
  394. }
  395. }
  396. else
  397. {
  398. if (parameterNode.Selected == true)
  399. {
  400. Keys.Add(parameterNode.Name);
  401. }
  402. }
  403. }
  404. private void Timer_Tick(object sender, EventArgs e)
  405. {
  406. if (Keys.Count > 10)
  407. {
  408. WPFMessageBox.ShowWarning("最多显示10个数据");
  409. timer.Stop();
  410. return;
  411. }
  412. this.DataHistoryView.MyDrawGraphicsControl.ClearPlotPoints();
  413. var result = GetData(Keys.Distinct().ToList(), currentTime.AddMinutes(-1), DateTime.Now);
  414. if (result == null)
  415. {
  416. return;
  417. }
  418. List<PointCollection> cls = new List<PointCollection>();
  419. for (int i = 0; i < Keys.Count; i++)
  420. {
  421. PointCollection points = new PointCollection();
  422. int k = 1;
  423. result[Keys[i]].ForEach(point =>
  424. {
  425. points.Add(new Point() { X = point.dateTime.ToOADate(), Y = point.value });
  426. k += 1;
  427. });
  428. cls.Add(points);
  429. }
  430. this.DataHistoryView.MyDrawGraphicsControl.PointCollections = cls;
  431. //if (this.DataHistoryView.MyDrawGraphicsControl.IsHorizontalNavigationEnabled == false && this.DataHistoryView.MyDrawGraphicsControl.IsVerticalNavigationEnabled == false)
  432. //{
  433. // this.DataHistoryView.MyDrawGraphicsControl.FitControlWithoutDrawing();
  434. //}
  435. //DateTime dateTimeNow = DateTime.Now;
  436. this.DataHistoryView.MyDrawGraphicsControl.YPoints.Clear();
  437. if (IsShowStep)
  438. {
  439. var item = QueryDataClient.Instance.Service.GetHistorySteps(startTime, DateTime.Now);
  440. if (item.Count > 0)
  441. {
  442. item.ForEach(x =>
  443. {
  444. this.DataHistoryView.MyDrawGraphicsControl.YPoints.Add(new Venus_Core.StepItem() { StartValue = x.StartTime.ToOADate(), Information =$"{x.RecipeId}\n{x.StepNo}\n{x.StartTime.ToString("HH:mm:ss:fff")}" });
  445. });
  446. }
  447. }
  448. else
  449. {
  450. this.DataHistoryView.MyDrawGraphicsControl.YPoints.Clear();
  451. }
  452. }
  453. public void ColorChanged()
  454. {
  455. for (int i = 0; i < KeyDataObservableCollection.Count(); i++)
  456. {
  457. var _color = KeyDataObservableCollection[i].Color.Color;
  458. this.DataHistoryView.MyDrawGraphicsControl.m_PenCollencteions[i] = new System.Drawing.Pen(System.Drawing.Color.FromArgb(_color.A, _color.R, _color.G, _color.B), 2);
  459. }
  460. }
  461. #endregion
  462. }
  463. public class QueryIndexer
  464. {
  465. public DateTime TimeToken { get; set; }
  466. public List<string> DataList { get; set; }
  467. public string Module { get; set; }
  468. }
  469. public class KeyData:BindableBase
  470. {
  471. public string Key { get; set; }
  472. public SolidColorBrush _Color;
  473. public SolidColorBrush Color
  474. {
  475. get { return _Color; }
  476. set { SetProperty(ref _Color, value); }
  477. }
  478. public int UniqueId { get; set; }
  479. }
  480. }