DataHistoryViewModel.cs 17 KB

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