PartialPressureViewModel.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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_MainPages.Unity;
  17. using Venus_Unity;
  18. namespace Venus_MainPages.ViewModels
  19. {
  20. internal class PartialPressureViewModel : BindableBase
  21. {
  22. #region 私有字段
  23. private List<string> m_RtConfigKeys=new List<string>();
  24. private Dictionary<string, object> m_RtConfigValues=new Dictionary<string, object> ();
  25. private Dictionary<int, object> m_GasFlows = new Dictionary<int, object>();
  26. private object[] m_GasPressures = new object[10];
  27. private string m_ModuleName = "PMA";
  28. private int m_GasIndex;
  29. ChartValues<double> m_CurrentLineSeries= new ChartValues<double>();
  30. ChartValues<double> m_ReferenceLineSeries = new ChartValues<double>();
  31. DispatcherTimer timer = new DispatcherTimer();
  32. private int m_GasTime;
  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<string, object> RtConfigValues
  45. {
  46. get { return m_RtConfigValues; }
  47. set { SetProperty(ref m_RtConfigValues, value); }
  48. }
  49. public string ModuleName
  50. {
  51. get { return m_ModuleName; }
  52. set { SetProperty(ref m_ModuleName, value); }
  53. }
  54. public Dictionary<int, object> GasFlows
  55. {
  56. get { return m_GasFlows; }
  57. set { SetProperty(ref m_GasFlows, value); }
  58. }
  59. public ChartValues<double> CurrentLineSeries
  60. {
  61. get { return m_CurrentLineSeries; }
  62. set { SetProperty(ref m_CurrentLineSeries, value); }
  63. }
  64. public ChartValues<double> ReferenceLineSeries
  65. {
  66. get { return m_ReferenceLineSeries; }
  67. set { SetProperty(ref m_ReferenceLineSeries, value); }
  68. }
  69. public int GasTime
  70. {
  71. get { return m_GasTime; }
  72. set { SetProperty(ref m_GasTime, value); }
  73. }
  74. #endregion
  75. #region 命令
  76. private DelegateCommand<object> _SelectGasCommand;
  77. public DelegateCommand<object> SelectGasCommand =>
  78. _SelectGasCommand ?? (_SelectGasCommand = new DelegateCommand<object>(OnSelectGas));
  79. private DelegateCommand _StartCommand;
  80. public DelegateCommand StartCommand =>
  81. _StartCommand ?? (_StartCommand = new DelegateCommand(OnStart));
  82. private DelegateCommand _SaveCommand;
  83. public DelegateCommand SaveCommand =>
  84. _SaveCommand ?? (_SaveCommand = new DelegateCommand(OnSave));
  85. private DelegateCommand _LoadReferenceCommand;
  86. public DelegateCommand LoadReferenceCommand =>
  87. _LoadReferenceCommand ?? (_LoadReferenceCommand = new DelegateCommand(OnLoadReference));
  88. private DelegateCommand _AbortCommand;
  89. public DelegateCommand AbortCommand =>
  90. _AbortCommand ?? (_AbortCommand = new DelegateCommand(OnAbort));
  91. #endregion
  92. #region 构造函数
  93. public PartialPressureViewModel()
  94. {
  95. ModuleName = "PMA";
  96. addConfigKeys();
  97. GasTime = 60;
  98. foreach (var item in QueryDataClient.Instance.Service.PollConfig(m_RtConfigKeys))
  99. {
  100. var newkey = item.Key.Substring(4, item.Key.Length-4);
  101. RtConfigValues.Add(newkey, item.Value);
  102. }
  103. timer.Interval = TimeSpan.FromSeconds(1);
  104. timer.Tick += Timer_Tick;
  105. OnSelectGas(1);
  106. }
  107. #endregion
  108. #region 命令方法
  109. private void OnSelectGas(object obj)
  110. {
  111. Dictionary<int, object> CurrentGasFlows = new Dictionary<int, object>();
  112. value = $"MfcGas{obj.ToString()}";
  113. maxScale = Convert.ToInt32(RtConfigValues[$"{value}.MfcN2Scale"]);
  114. var xishu = Convert.ToDouble(QueryDataClient.Instance.Service.GetConfig($"{ModuleName}.{value}.MfcScaleFactor"));
  115. for (int i = 1; i < 11; i++)
  116. {
  117. CurrentGasFlows.Add(i, (int)(maxScale *xishu / 10 * i));
  118. }
  119. GasFlows = CurrentGasFlows;
  120. m_GasIndex = Convert.ToInt32(obj);
  121. }
  122. private void OnStart()
  123. {
  124. m_partialPressureResult = new PartialPressureResult();
  125. m_partialPressureResult.StartTime = DateTime.Now.ToString();
  126. m_partialPressureResult.FlowTime = GasTime;
  127. m_partialPressureResult.GasName = value;
  128. CurrentLineSeries = new ChartValues<double>();
  129. timer.Start();
  130. InvokeClient.Instance.Service.DoOperation($"{ModuleName}.PartialPressureTest", m_GasIndex,1000* GasTime);
  131. }
  132. private void OnSave()
  133. {
  134. SerializeHelper.Instance.WriteToJsonFile<PartialPressureResult>(m_partialPressureResult, $"PartialPressureResult/{m_partialPressureResult.GasName}/{DateTime.Now.ToString("yyyyMMddHHmm")}.json");
  135. }
  136. private void OnLoadReference()
  137. {
  138. OpenFileDialog dialog = new OpenFileDialog();
  139. dialog.Filter = ".json|*.json";
  140. dialog.InitialDirectory = Path.Combine(Directory.GetCurrentDirectory(), "PartialPressureResult") ;
  141. if (dialog.ShowDialog() == true)
  142. {
  143. string SelectedPath = dialog.FileName;
  144. var value = SerializeHelper.Instance.ReadFromJsonFile<PartialPressureResult>(SelectedPath);
  145. ReferenceLineSeries.Clear();
  146. ReferenceFlow.Clear();
  147. value.ValuePairs.ForEach(x =>
  148. {
  149. ReferenceFlow.Add(x.Flow);
  150. ReferenceLineSeries.Add(x.Pressure);
  151. });
  152. }
  153. else
  154. {
  155. ReferenceLineSeries.Clear();
  156. //ReferenceLineSeries.Insert(20, 0);
  157. ReferenceLineSeries.Add(0);
  158. ReferenceFlow.Clear();
  159. }
  160. }
  161. private void OnAbort()
  162. {
  163. timer.Stop();
  164. InvokeClient.Instance.Service.DoOperation($"{ModuleName}.Abort");
  165. CurrentLineSeries = new ChartValues<double>();
  166. }
  167. #endregion
  168. #region 私有方法
  169. private void Timer_Tick(object sender, EventArgs e)
  170. {
  171. var counter= (int)QueryDataClient.Instance.Service.GetData($"{ModuleName}.PartialPressureCounter");
  172. if (counter >=2 && counter<=11 && counter>CurrentLineSeries.Count+1)
  173. {
  174. var pressureValue = Convert.ToDouble(QueryDataClient.Instance.Service.GetData($"{ModuleName}.ProcessPressureLow.Value"));
  175. CurrentLineSeries.Add(pressureValue);
  176. //m_partialPressureResult.ValuePairs.Add($"{(counter-1) * 10}%", pressureValue);
  177. m_partialPressureResult.ValuePairs.Add(new Value() { Flow = GasFlows[counter - 1].ToString(), Pressure = Math.Round(pressureValue,3) });
  178. }
  179. if (counter == 11)
  180. {
  181. m_partialPressureResult.EndTime = DateTime.Now.ToString();
  182. //m_partialPressureResult.Result = "Success";
  183. timer.Stop();
  184. }
  185. }
  186. private void addConfigKeys()
  187. {
  188. m_RtConfigKeys.Add($"{ModuleName}.MfcGas1.GasName");
  189. m_RtConfigKeys.Add($"{ModuleName}.MfcGas1.MfcN2Scale");
  190. m_RtConfigKeys.Add($"{ModuleName}.MfcGas2.GasName");
  191. m_RtConfigKeys.Add($"{ModuleName}.MfcGas2.MfcN2Scale");
  192. m_RtConfigKeys.Add($"{ModuleName}.MfcGas3.GasName");
  193. m_RtConfigKeys.Add($"{ModuleName}.MfcGas3.MfcN2Scale");
  194. m_RtConfigKeys.Add($"{ModuleName}.MfcGas4.GasName");
  195. m_RtConfigKeys.Add($"{ModuleName}.MfcGas4.MfcN2Scale");
  196. m_RtConfigKeys.Add($"{ModuleName}.MfcGas5.GasName");
  197. m_RtConfigKeys.Add($"{ModuleName}.MfcGas5.MfcN2Scale");
  198. m_RtConfigKeys.Add($"{ModuleName}.MfcGas6.GasName");
  199. m_RtConfigKeys.Add($"{ModuleName}.MfcGas6.MfcN2Scale");
  200. m_RtConfigKeys.Add($"{ModuleName}.MfcGas7.GasName");
  201. m_RtConfigKeys.Add($"{ModuleName}.MfcGas7.MfcN2Scale");
  202. m_RtConfigKeys.Add($"{ModuleName}.MfcGas8.GasName");
  203. m_RtConfigKeys.Add($"{ModuleName}.MfcGas8.MfcN2Scale");
  204. }
  205. #endregion
  206. }
  207. }