PartialPressureViewModel.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. private bool m_MFC7IsEnable;
  38. private bool m_MFC8IsEnable;
  39. #endregion
  40. #region 属性
  41. public ObservableCollection<string> ReferenceFlow
  42. {
  43. get { return m_ReferenceFlow; }
  44. set { SetProperty(ref m_ReferenceFlow, value); }
  45. }
  46. public Dictionary<int, object> GasFlows
  47. {
  48. get { return m_GasFlows; }
  49. set { SetProperty(ref m_GasFlows, value); }
  50. }
  51. public ChartValues<double> CurrentLineSeries
  52. {
  53. get { return m_CurrentLineSeries; }
  54. set { SetProperty(ref m_CurrentLineSeries, value); }
  55. }
  56. public ChartValues<double> ReferenceLineSeries
  57. {
  58. get { return m_ReferenceLineSeries; }
  59. set { SetProperty(ref m_ReferenceLineSeries, value); }
  60. }
  61. public int GasTime
  62. {
  63. get { return m_GasTime; }
  64. set { SetProperty(ref m_GasTime, value); }
  65. }
  66. public string GasName
  67. {
  68. get { return m_GasName; }
  69. set { SetProperty(ref m_GasName, value); }
  70. }
  71. public bool MFC7IsEnable
  72. {
  73. get { return m_MFC7IsEnable; }
  74. set { SetProperty(ref m_MFC7IsEnable, value); }
  75. }
  76. public bool MFC8IsEnable
  77. {
  78. get { return m_MFC8IsEnable; }
  79. set { SetProperty(ref m_MFC8IsEnable, value); }
  80. }
  81. #endregion
  82. #region 命令
  83. private DelegateCommand<object> _SelectGasCommand;
  84. public DelegateCommand<object> SelectGasCommand =>
  85. _SelectGasCommand ?? (_SelectGasCommand = new DelegateCommand<object>(OnSelectGas));
  86. private DelegateCommand _StartCommand;
  87. public DelegateCommand StartCommand =>
  88. _StartCommand ?? (_StartCommand = new DelegateCommand(OnStart));
  89. private DelegateCommand _SaveCommand;
  90. public DelegateCommand SaveCommand =>
  91. _SaveCommand ?? (_SaveCommand = new DelegateCommand(OnSave));
  92. private DelegateCommand _LoadReferenceCommand;
  93. public DelegateCommand LoadReferenceCommand =>
  94. _LoadReferenceCommand ?? (_LoadReferenceCommand = new DelegateCommand(OnLoadReference));
  95. private DelegateCommand _AbortCommand;
  96. public DelegateCommand AbortCommand =>
  97. _AbortCommand ?? (_AbortCommand = new DelegateCommand(OnAbort));
  98. #endregion
  99. #region 构造函数
  100. public PartialPressureViewModel()
  101. {
  102. GasTime = 60;
  103. timer.Interval = TimeSpan.FromSeconds(1);
  104. timer.Tick += Timer_Tick;
  105. }
  106. #endregion
  107. #region 命令方法
  108. private void OnSelectGas(object obj)
  109. {
  110. Dictionary<int, object> CurrentGasFlows = new Dictionary<int, object>();
  111. value = $"MfcGas{obj.ToString()}";
  112. maxScale= Convert.ToInt32(QueryDataClient.Instance.Service.GetConfig($"{ModuleName}.{value}.MfcN2Scale"));
  113. var xishu = Convert.ToDouble(QueryDataClient.Instance.Service.GetConfig($"{ModuleName}.{value}.MfcScaleFactor"));
  114. GasName = QueryDataClient.Instance.Service.GetConfig($"{ModuleName}.{value}.GasName").ToString();
  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. timer.Start();
  125. InvokeClient.Instance.Service.DoOperation($"{ModuleName}.PartialPressureTest", m_GasIndex,1000* GasTime);
  126. }
  127. private void OnSave()
  128. {
  129. SerializeHelper.Instance.WriteToJsonFile<PartialPressureResult>(m_partialPressureResult, $"PartialPressureResult/{m_partialPressureResult.GasName}/{DateTime.Now.ToString("yyyyMMddHHmm")}.json");
  130. }
  131. private void OnLoadReference()
  132. {
  133. OpenFileDialog dialog = new OpenFileDialog();
  134. dialog.Filter = ".json|*.json";
  135. dialog.InitialDirectory = Path.Combine(QueryDataClient.Instance.Service.GetData("GetRTPath").ToString(), "PartialPressureResult");
  136. if (dialog.ShowDialog() == true)
  137. {
  138. string SelectedPath = dialog.FileName;
  139. var value = SerializeHelper.Instance.ReadFromJsonFile<PartialPressureResult>(SelectedPath);
  140. ReferenceLineSeries.Clear();
  141. ReferenceFlow.Clear();
  142. value.ValuePairs.ForEach(x =>
  143. {
  144. ReferenceFlow.Add(x.Flow);
  145. ReferenceLineSeries.Add(x.Pressure);
  146. });
  147. }
  148. else
  149. {
  150. ReferenceLineSeries.Clear();
  151. //ReferenceLineSeries.Insert(20, 0);
  152. ReferenceLineSeries.Add(0);
  153. ReferenceFlow.Clear();
  154. }
  155. }
  156. private void OnAbort()
  157. {
  158. timer.Stop();
  159. InvokeClient.Instance.Service.DoOperation($"{ModuleName}.Abort");
  160. CurrentLineSeries = new ChartValues<double>();
  161. }
  162. #endregion
  163. #region 私有方法
  164. private void Timer_Tick(object sender, EventArgs e)
  165. {
  166. CurrentLineSeries.Clear();
  167. var values = QueryDataClient.Instance.Service.GetData($"{ModuleName}.PartialPressureResult").ToString();
  168. values.Split(',').ToList().ForEach(x =>
  169. {
  170. if (x != "")
  171. {
  172. CurrentLineSeries.Add(Math.Round(Convert.ToDouble(x), 3));
  173. }
  174. });
  175. if (CurrentLineSeries.Count == 10)
  176. {
  177. timer.Stop();
  178. }
  179. }
  180. public void Init()
  181. {
  182. OnSelectGas(1);
  183. MFC7IsEnable = (bool)QueryDataClient.Instance.Service.GetConfig($"{ModuleName}.MfcGas7.Enable");
  184. MFC8IsEnable = (bool)QueryDataClient.Instance.Service.GetConfig($"{ModuleName}.MfcGas8.Enable");
  185. }
  186. #endregion
  187. }
  188. }