DataHistoryViewModel.cs 18 KB

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