PartialPressureViewModel.cs 8.8 KB

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