PartialPressureViewModel.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. private string m_ModuleName = "PMA";
  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 string ModuleName
  45. {
  46. get { return m_ModuleName; }
  47. set { SetProperty(ref m_ModuleName, value); }
  48. }
  49. public Dictionary<int, object> GasFlows
  50. {
  51. get { return m_GasFlows; }
  52. set { SetProperty(ref m_GasFlows, value); }
  53. }
  54. public ChartValues<double> CurrentLineSeries
  55. {
  56. get { return m_CurrentLineSeries; }
  57. set { SetProperty(ref m_CurrentLineSeries, value); }
  58. }
  59. public ChartValues<double> ReferenceLineSeries
  60. {
  61. get { return m_ReferenceLineSeries; }
  62. set { SetProperty(ref m_ReferenceLineSeries, value); }
  63. }
  64. public int GasTime
  65. {
  66. get { return m_GasTime; }
  67. set { SetProperty(ref m_GasTime, value); }
  68. }
  69. public string GasName
  70. {
  71. get { return m_GasName; }
  72. set { SetProperty(ref m_GasName, 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. GasTime = 60;
  96. timer.Interval = TimeSpan.FromSeconds(1);
  97. timer.Tick += Timer_Tick;
  98. }
  99. #endregion
  100. #region 命令方法
  101. private void OnSelectGas(object obj)
  102. {
  103. Dictionary<int, object> CurrentGasFlows = new Dictionary<int, object>();
  104. value = $"MfcGas{obj.ToString()}";
  105. maxScale= Convert.ToInt32(QueryDataClient.Instance.Service.GetConfig($"{ModuleName}.{value}.MfcN2Scale"));
  106. var xishu = Convert.ToDouble(QueryDataClient.Instance.Service.GetConfig($"{ModuleName}.{value}.MfcScaleFactor"));
  107. GasName = QueryDataClient.Instance.Service.GetConfig($"{ModuleName}.{value}.GasName").ToString();
  108. for (int i = 1; i < 11; i++)
  109. {
  110. CurrentGasFlows.Add(i, (int)(maxScale *xishu / 10 * i));
  111. }
  112. GasFlows = CurrentGasFlows;
  113. m_GasIndex = Convert.ToInt32(obj);
  114. }
  115. private void OnStart()
  116. {
  117. timer.Start();
  118. InvokeClient.Instance.Service.DoOperation($"{ModuleName}.PartialPressureTest", m_GasIndex,1000* GasTime);
  119. }
  120. private void OnSave()
  121. {
  122. SerializeHelper.Instance.WriteToJsonFile<PartialPressureResult>(m_partialPressureResult, $"PartialPressureResult/{m_partialPressureResult.GasName}/{DateTime.Now.ToString("yyyyMMddHHmm")}.json");
  123. }
  124. private void OnLoadReference()
  125. {
  126. OpenFileDialog dialog = new OpenFileDialog();
  127. dialog.Filter = ".json|*.json";
  128. dialog.InitialDirectory = Path.Combine(QueryDataClient.Instance.Service.GetData("GetRTPath").ToString(), "PartialPressureResult");
  129. if (dialog.ShowDialog() == true)
  130. {
  131. string SelectedPath = dialog.FileName;
  132. var value = SerializeHelper.Instance.ReadFromJsonFile<PartialPressureResult>(SelectedPath);
  133. ReferenceLineSeries.Clear();
  134. ReferenceFlow.Clear();
  135. value.ValuePairs.ForEach(x =>
  136. {
  137. ReferenceFlow.Add(x.Flow);
  138. ReferenceLineSeries.Add(x.Pressure);
  139. });
  140. }
  141. else
  142. {
  143. ReferenceLineSeries.Clear();
  144. //ReferenceLineSeries.Insert(20, 0);
  145. ReferenceLineSeries.Add(0);
  146. ReferenceFlow.Clear();
  147. }
  148. }
  149. private void OnAbort()
  150. {
  151. timer.Stop();
  152. InvokeClient.Instance.Service.DoOperation($"{ModuleName}.Abort");
  153. CurrentLineSeries = new ChartValues<double>();
  154. }
  155. #endregion
  156. #region 私有方法
  157. private void Timer_Tick(object sender, EventArgs e)
  158. {
  159. CurrentLineSeries.Clear();
  160. var values = QueryDataClient.Instance.Service.GetData($"{ModuleName}.PartialPressureResult").ToString();
  161. values.Split(',').ToList().ForEach(x =>
  162. {
  163. if (x != "")
  164. {
  165. CurrentLineSeries.Add(Math.Round(Convert.ToDouble(x), 3));
  166. }
  167. });
  168. if (CurrentLineSeries.Count == 10)
  169. {
  170. timer.Stop();
  171. }
  172. }
  173. public void Init()
  174. {
  175. OnSelectGas(1);
  176. }
  177. #endregion
  178. }
  179. }