PartialPressureViewModel.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. using LiveCharts;
  2. using MECF.Framework.Common.DataCenter;
  3. using MECF.Framework.Common.OperationCenter;
  4. using Microsoft.Win32;
  5. using Prism.Commands;
  6. using Prism.Mvvm;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Collections.ObjectModel;
  10. using System.IO;
  11. using System.Linq;
  12. using System.Text;
  13. using System.Threading.Tasks;
  14. using System.Windows.Threading;
  15. using Venus_Core;
  16. //using Venus_Core;
  17. using Venus_MainPages.Unity;
  18. using Venus_Unity;
  19. namespace Venus_MainPages.ViewModels
  20. {
  21. internal class PartialPressureViewModel : BindableBase
  22. {
  23. #region 私有字段
  24. private Dictionary<int, object> m_GasFlows = new Dictionary<int, object>();
  25. private object[] m_GasPressures = new object[10];
  26. public string ModuleName;
  27. private int m_GasIndex;
  28. ChartValues<double> m_CurrentLineSeries= new ChartValues<double>();
  29. ChartValues<double> m_ReferenceLineSeries = new ChartValues<double>();
  30. DispatcherTimer timer = new DispatcherTimer();
  31. private int m_GasTime;
  32. private string m_GasName;
  33. private PartialPressureResult m_partialPressureResult;
  34. string value;
  35. int maxScale;
  36. ObservableCollection<string> m_ReferenceFlow = new ObservableCollection<string>();
  37. #endregion
  38. #region 属性
  39. public ObservableCollection<string> ReferenceFlow
  40. {
  41. get { return m_ReferenceFlow; }
  42. set { SetProperty(ref m_ReferenceFlow, value); }
  43. }
  44. public Dictionary<int, object> GasFlows
  45. {
  46. get { return m_GasFlows; }
  47. set { SetProperty(ref m_GasFlows, value); }
  48. }
  49. public ChartValues<double> CurrentLineSeries
  50. {
  51. get { return m_CurrentLineSeries; }
  52. set { SetProperty(ref m_CurrentLineSeries, value); }
  53. }
  54. public ChartValues<double> ReferenceLineSeries
  55. {
  56. get { return m_ReferenceLineSeries; }
  57. set { SetProperty(ref m_ReferenceLineSeries, value); }
  58. }
  59. public int GasTime
  60. {
  61. get { return m_GasTime; }
  62. set { SetProperty(ref m_GasTime, value); }
  63. }
  64. public string GasName
  65. {
  66. get { return m_GasName; }
  67. set { SetProperty(ref m_GasName, value); }
  68. }
  69. #endregion
  70. #region 命令
  71. private DelegateCommand<object> _SelectGasCommand;
  72. public DelegateCommand<object> SelectGasCommand =>
  73. _SelectGasCommand ?? (_SelectGasCommand = new DelegateCommand<object>(OnSelectGas));
  74. private DelegateCommand _StartCommand;
  75. public DelegateCommand StartCommand =>
  76. _StartCommand ?? (_StartCommand = new DelegateCommand(OnStart));
  77. private DelegateCommand _SaveCommand;
  78. public DelegateCommand SaveCommand =>
  79. _SaveCommand ?? (_SaveCommand = new DelegateCommand(OnSave));
  80. private DelegateCommand _LoadReferenceCommand;
  81. public DelegateCommand LoadReferenceCommand =>
  82. _LoadReferenceCommand ?? (_LoadReferenceCommand = new DelegateCommand(OnLoadReference));
  83. private DelegateCommand _AbortCommand;
  84. public DelegateCommand AbortCommand =>
  85. _AbortCommand ?? (_AbortCommand = new DelegateCommand(OnAbort));
  86. #endregion
  87. #region 构造函数
  88. public PartialPressureViewModel()
  89. {
  90. GasTime = 60;
  91. timer.Interval = TimeSpan.FromSeconds(1);
  92. timer.Tick += Timer_Tick;
  93. }
  94. #endregion
  95. #region 命令方法
  96. private void OnSelectGas(object obj)
  97. {
  98. Dictionary<int, object> CurrentGasFlows = new Dictionary<int, object>();
  99. value = $"MfcGas{obj.ToString()}";
  100. maxScale= Convert.ToInt32(QueryDataClient.Instance.Service.GetConfig($"{ModuleName}.{value}.MfcN2Scale"));
  101. var xishu = Convert.ToDouble(QueryDataClient.Instance.Service.GetConfig($"{ModuleName}.{value}.MfcScaleFactor"));
  102. GasName = QueryDataClient.Instance.Service.GetConfig($"{ModuleName}.{value}.GasName").ToString();
  103. for (int i = 1; i < 11; i++)
  104. {
  105. CurrentGasFlows.Add(i, (int)(maxScale *xishu / 10 * i));
  106. }
  107. GasFlows = CurrentGasFlows;
  108. m_GasIndex = Convert.ToInt32(obj);
  109. }
  110. private void OnStart()
  111. {
  112. timer.Start();
  113. InvokeClient.Instance.Service.DoOperation($"{ModuleName}.PartialPressureTest", m_GasIndex,1000* GasTime);
  114. }
  115. private void OnSave()
  116. {
  117. SerializeHelper.Instance.WriteToJsonFile<PartialPressureResult>(m_partialPressureResult, $"PartialPressureResult/{m_partialPressureResult.GasName}/{DateTime.Now.ToString("yyyyMMddHHmm")}.json");
  118. }
  119. private void OnLoadReference()
  120. {
  121. OpenFileDialog dialog = new OpenFileDialog();
  122. dialog.Filter = ".json|*.json";
  123. dialog.InitialDirectory = Path.Combine(QueryDataClient.Instance.Service.GetData("GetRTPath").ToString(), "PartialPressureResult");
  124. if (dialog.ShowDialog() == true)
  125. {
  126. string SelectedPath = dialog.FileName;
  127. var value = SerializeHelper.Instance.ReadFromJsonFile<PartialPressureResult>(SelectedPath);
  128. ReferenceLineSeries.Clear();
  129. ReferenceFlow.Clear();
  130. value.ValuePairs.ForEach(x =>
  131. {
  132. ReferenceFlow.Add(x.Flow);
  133. ReferenceLineSeries.Add(x.Pressure);
  134. });
  135. }
  136. else
  137. {
  138. ReferenceLineSeries.Clear();
  139. //ReferenceLineSeries.Insert(20, 0);
  140. ReferenceLineSeries.Add(0);
  141. ReferenceFlow.Clear();
  142. }
  143. }
  144. private void OnAbort()
  145. {
  146. timer.Stop();
  147. InvokeClient.Instance.Service.DoOperation($"{ModuleName}.Abort");
  148. CurrentLineSeries = new ChartValues<double>();
  149. }
  150. #endregion
  151. #region 私有方法
  152. private void Timer_Tick(object sender, EventArgs e)
  153. {
  154. CurrentLineSeries.Clear();
  155. var values = QueryDataClient.Instance.Service.GetData($"{ModuleName}.PartialPressureResult").ToString();
  156. values.Split(',').ToList().ForEach(x =>
  157. {
  158. if (x != "")
  159. {
  160. CurrentLineSeries.Add(Math.Round(Convert.ToDouble(x), 3));
  161. }
  162. });
  163. if (CurrentLineSeries.Count == 10)
  164. {
  165. timer.Stop();
  166. }
  167. }
  168. public void Init()
  169. {
  170. OnSelectGas(1);
  171. }
  172. #endregion
  173. }
  174. }