RealTimeViewModel.cs 13 KB

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