RealTimeViewModel.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.Linq;
  5. using System.Windows;
  6. using Aitex.Core.RT.Log;
  7. using Aitex.Core.Util;
  8. using MECF.Framework.Common.ControlDataContext;
  9. using MECF.Framework.Common.DataCenter;
  10. using MECF.Framework.Common.Utilities;
  11. using OpenSEMI.ClientBase;
  12. using SciChart.Charting.Visuals.Axes;
  13. using SciChart.Charting.Visuals.RenderableSeries;
  14. using Virgo_DUI.Client.Models.History.ProcessHistory;
  15. using Virgo_DUI.Client.Models.Sys;
  16. namespace Virgo_DUI.Client.Models.Operate.RealTime
  17. {
  18. public class RealTimeViewModel : UiViewModelBase
  19. {
  20. #region Property
  21. private const int MAX_PARAMETERS = 20;
  22. private ObservableCollection<ParameterNode> _ParameterNodes;
  23. public ObservableCollection<ParameterNode> ParameterNodes
  24. {
  25. get { return _ParameterNodes; }
  26. set { _ParameterNodes = value; NotifyOfPropertyChange("ParameterNodes"); }
  27. }
  28. public ObservableCollection<IRenderableSeries> SelectedData { get; set; }
  29. object _lockSelection = new object();
  30. public AutoRange ChartAutoRange
  31. {
  32. get { return EnableAutoZoom ? AutoRange.Always : AutoRange.Never; }
  33. }
  34. private bool _enableAutoZoom = true;
  35. public bool EnableAutoZoom
  36. {
  37. get { return _enableAutoZoom; }
  38. set
  39. {
  40. _enableAutoZoom = value;
  41. NotifyOfPropertyChange(nameof(EnableAutoZoom));
  42. NotifyOfPropertyChange(nameof(ChartAutoRange));
  43. }
  44. }
  45. RealtimeProvider _provider = new RealtimeProvider();
  46. private PeriodicJob _thread;
  47. #endregion
  48. #region Function
  49. public RealTimeViewModel()
  50. {
  51. DisplayName = "RealTime";
  52. SelectedData = new ObservableCollection<IRenderableSeries>();
  53. ParameterNodes = _provider.GetParameters();
  54. _thread = new PeriodicJob(100, MonitorData, "RealTime", true);
  55. }
  56. protected override void OnActivate()
  57. {
  58. base.OnActivate();
  59. }
  60. protected bool MonitorData()
  61. {
  62. try
  63. {
  64. Dictionary<string, object> data = null;
  65. if (SelectedData.Count > 0)
  66. {
  67. data = QueryDataClient.Instance.Service.PollData(Array.ConvertAll(SelectedData.ToArray(), x => (x as ChartDataLine).DataName));
  68. }
  69. Application.Current.Dispatcher.Invoke(new Action(() =>
  70. {
  71. ParameterNodes = _provider.GetParameters();
  72. AppendData(data);
  73. }));
  74. }
  75. catch (Exception ex)
  76. {
  77. LOG.Error(ex.Message);
  78. }
  79. return true;
  80. }
  81. public void AppendData(Dictionary<string, object> data)
  82. {
  83. if (data == null)
  84. return;
  85. DateTime dt = DateTime.Now;
  86. foreach (var item in SelectedData)
  87. {
  88. var seriesItem = item as ChartDataLine;
  89. if (!data.ContainsKey(seriesItem.DataName))
  90. continue;
  91. seriesItem.Append(dt, Convert.ToDouble(data[seriesItem.DataName]));
  92. }
  93. }
  94. public void Preset()
  95. {
  96. }
  97. public void Clear()
  98. {
  99. }
  100. public void Apply()
  101. {
  102. }
  103. public void ParameterCheck(ParameterNode node)
  104. {
  105. bool result = RefreshTreeStatusToChild(node);
  106. if (!result)
  107. node.Selected = !node.Selected;
  108. else
  109. RefreshTreeStatusToParent(node);
  110. }
  111. /// <summary>
  112. /// Refresh tree node status from current to children, and add data to SelectedData
  113. /// </summary>
  114. private bool RefreshTreeStatusToChild(ParameterNode node)
  115. {
  116. if (node.ChildNodes.Count > 0)
  117. {
  118. for (int i = 0; i < node.ChildNodes.Count; i++)
  119. {
  120. ParameterNode n = node.ChildNodes[i];
  121. n.Selected = node.Selected;
  122. if (!RefreshTreeStatusToChild(n))
  123. {
  124. //uncheck left node
  125. for (int j = i; j < node.ChildNodes.Count; j++)
  126. {
  127. node.ChildNodes[j].Selected = !node.Selected;
  128. }
  129. node.Selected = !node.Selected;
  130. return false;
  131. }
  132. }
  133. }
  134. else //leaf node
  135. {
  136. lock (_lockSelection)
  137. {
  138. bool isExist = SelectedData.FirstOrDefault(x => (x as ChartDataLine).DataName == node.Name) != null;
  139. if (node.Selected && !isExist)
  140. {
  141. if (SelectedData.Count < MAX_PARAMETERS)
  142. {
  143. var line = new ChartDataLine(node.Name);
  144. line.Tag = node;
  145. SelectedData.Add(line);
  146. }
  147. else
  148. {
  149. DialogBox.ShowWarning($"The max number of parameters is {MAX_PARAMETERS}.");
  150. return false;
  151. }
  152. }
  153. else if (!node.Selected && isExist)
  154. {
  155. //SelectedParameters.Remove(node.Name);
  156. var data = SelectedData.FirstOrDefault(d => (d as ChartDataLine).DataName == node.Name);
  157. SelectedData.Remove(data);
  158. }
  159. }
  160. }
  161. return true;
  162. }
  163. /// <summary>
  164. /// Refresh tree node status from current to parent
  165. /// </summary>
  166. /// <param name="node"></param>
  167. /// <returns></returns>
  168. private void RefreshTreeStatusToParent(ParameterNode node)
  169. {
  170. if (node.ParentNode != null)
  171. {
  172. if (node.Selected)
  173. {
  174. bool flag = true;
  175. for (int i = 0; i < node.ParentNode.ChildNodes.Count; i++)
  176. {
  177. if (!node.ParentNode.ChildNodes[i].Selected)
  178. {
  179. flag = false; //as least one child is unselected
  180. break;
  181. }
  182. }
  183. if (flag)
  184. node.ParentNode.Selected = true;
  185. }
  186. else
  187. {
  188. node.ParentNode.Selected = false;
  189. }
  190. RefreshTreeStatusToParent(node.ParentNode);
  191. }
  192. }
  193. #region Parameter Grid Control
  194. public void DeleteAll()
  195. {
  196. //uncheck all tree nodes
  197. foreach (ChartDataLine cp in SelectedData)
  198. {
  199. (cp.Tag as ParameterNode).Selected = false;
  200. RefreshTreeStatusToParent(cp.Tag as ParameterNode);
  201. }
  202. SelectedData.Clear();
  203. }
  204. private void SetParameterNode(ObservableCollection<ParameterNode> nodes, bool isChecked)
  205. {
  206. foreach (ParameterNode n in nodes)
  207. {
  208. n.Selected = isChecked;
  209. SetParameterNode(n.ChildNodes, isChecked);
  210. }
  211. }
  212. public void Delete(ChartDataLine cp)
  213. {
  214. if (cp != null && SelectedData.Contains(cp))
  215. {
  216. //uncheck tree node
  217. (cp.Tag as ParameterNode).Selected = false;
  218. RefreshTreeStatusToParent(cp.Tag as ParameterNode);
  219. SelectedData.Remove(cp);
  220. }
  221. }
  222. public void ExportAll()
  223. {
  224. try
  225. {
  226. Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
  227. dlg.DefaultExt = ".xlsx"; // Default file extension
  228. dlg.Filter = "Excel数据表格文件(*.xlsx)|*.xlsx"; // Filter files by extension
  229. dlg.FileName = $"Export_{DateTime.Now:yyyyMMdd_HHmmss}";
  230. Nullable<bool> result = dlg.ShowDialog();// Show open file dialog box
  231. if (result == true) // Process open file dialog box results
  232. {
  233. System.Data.DataSet ds = new System.Data.DataSet();
  234. ds.Tables.Add(new System.Data.DataTable($"Export_{DateTime.Now:yyyyMMdd_HHmmss}"));
  235. ds.Tables[0].Columns.Add("Time");
  236. ds.Tables[0].Columns[0].DataType = typeof(DateTime);
  237. lock (_lockSelection)
  238. {
  239. Dictionary<DateTime, double[]> timeValue = new Dictionary<DateTime, double[]>();
  240. for (int i = 0; i < SelectedData.Count; i++)
  241. {
  242. List<Tuple<DateTime, double >> data = (SelectedData[i] as ChartDataLine).Points;
  243. foreach (var tuple in data)
  244. {
  245. if (!timeValue.ContainsKey(tuple.Item1))
  246. timeValue[tuple.Item1] = new double[SelectedData.Count];
  247. timeValue[tuple.Item1][i] = tuple.Item2;
  248. }
  249. ds.Tables[0].Columns.Add((SelectedData[i] as ChartDataLine).DataName);
  250. ds.Tables[0].Columns[i+1].DataType = typeof(double);
  251. }
  252. foreach (var item in timeValue)
  253. {
  254. var row = ds.Tables[0].NewRow();
  255. row[0] = item.Key;
  256. for (int j = 0; j < item.Value.Length; j++)
  257. {
  258. row[j+1] = item.Value[j];
  259. }
  260. ds.Tables[0].Rows.Add(row);
  261. }
  262. }
  263. if (!ExcelHelper.ExportToExcel(dlg.FileName, ds, out string reason))
  264. {
  265. MessageBox.Show($"Export failed, {reason}", "Export", MessageBoxButton.OK, MessageBoxImage.Warning);
  266. return;
  267. }
  268. MessageBox.Show($"Export succeed, file save as {dlg.FileName}", "Export", MessageBoxButton.OK, MessageBoxImage.Information);
  269. }
  270. }
  271. catch (Exception ex)
  272. {
  273. LOG.Write(ex);
  274. MessageBox.Show("Write failed," + ex.Message, "export failed", MessageBoxButton.OK, MessageBoxImage.Warning);
  275. }
  276. }
  277. public void Export(ChartDataLine cp)
  278. {
  279. try
  280. {
  281. Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
  282. dlg.DefaultExt = ".xlsx"; // Default file extension
  283. dlg.Filter = "Excel数据表格文件(*.xlsx)|*.xlsx"; // Filter files by extension
  284. dlg.FileName = $"{cp.DataName}_{DateTime.Now:yyyyMMdd_HHmmss}";
  285. Nullable<bool> result = dlg.ShowDialog();// Show open file dialog box
  286. if (result == true) // Process open file dialog box results
  287. {
  288. System.Data.DataSet ds = new System.Data.DataSet();
  289. ds.Tables.Add(new System.Data.DataTable(cp.DataName));
  290. ds.Tables[0].Columns.Add("Time");
  291. ds.Tables[0].Columns[0].DataType = typeof(DateTime);
  292. ds.Tables[0].Columns.Add(cp.DataName);
  293. ds.Tables[0].Columns[1].DataType = typeof(double);
  294. foreach (var item in cp.Points)
  295. {
  296. var row = ds.Tables[0].NewRow();
  297. row[0] = item.Item1;
  298. row[1] = item.Item2;
  299. ds.Tables[0].Rows.Add(row);
  300. }
  301. if (!ExcelHelper.ExportToExcel(dlg.FileName, ds, out string reason))
  302. {
  303. MessageBox.Show($"Export failed, {reason}" , "Export", MessageBoxButton.OK, MessageBoxImage.Warning);
  304. return;
  305. }
  306. MessageBox.Show($"Export succeed, file save as {dlg.FileName}", "Export", MessageBoxButton.OK, MessageBoxImage.Information);
  307. }
  308. }
  309. catch (Exception ex)
  310. {
  311. LOG.Write(ex);
  312. MessageBox.Show("Write failed,"+ex.Message, "export failed", MessageBoxButton.OK, MessageBoxImage.Warning);
  313. }
  314. }
  315. public void SelectColor(ChartDataLine cp)
  316. {
  317. if (cp == null)
  318. return;
  319. var dlg = new System.Windows.Forms.ColorDialog();
  320. if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  321. {
  322. cp.Stroke = new System.Windows.Media.Color() { A = dlg.Color.A, B = dlg.Color.B, G = dlg.Color.G, R = dlg.Color.R };
  323. }
  324. }
  325. #endregion
  326. #endregion
  327. }
  328. }