DataHistoryViewModel.cs 18 KB

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