PartialPressureViewModel.cs 7.6 KB

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