DataHistoryViewModel.cs 14 KB

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