PartialPressureViewModel.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. foreach (var item in QueryDataClient.Instance.Service.PollConfig(m_RtConfigKeys))
  100. {
  101. var newkey = item.Key.Substring(4, item.Key.Length-4);
  102. RtConfigValues.Add(newkey, item.Value);
  103. }
  104. timer.Interval = TimeSpan.FromSeconds(1);
  105. timer.Tick += Timer_Tick;
  106. OnSelectGas(1);
  107. }
  108. #endregion
  109. #region 命令方法
  110. private void OnSelectGas(object obj)
  111. {
  112. Dictionary<int, object> CurrentGasFlows = new Dictionary<int, object>();
  113. value = $"MfcGas{obj.ToString()}";
  114. maxScale = Convert.ToInt32(RtConfigValues[$"{value}.MfcN2Scale"]);
  115. var xishu = Convert.ToDouble(QueryDataClient.Instance.Service.GetConfig($"{ModuleName}.{value}.MfcScaleFactor"));
  116. for (int i = 1; i < 11; i++)
  117. {
  118. CurrentGasFlows.Add(i, (int)(maxScale *xishu / 10 * i));
  119. }
  120. GasFlows = CurrentGasFlows;
  121. m_GasIndex = Convert.ToInt32(obj);
  122. }
  123. private void OnStart()
  124. {
  125. m_partialPressureResult = new PartialPressureResult();
  126. m_partialPressureResult.StartTime = DateTime.Now.ToString();
  127. m_partialPressureResult.FlowTime = GasTime;
  128. m_partialPressureResult.GasName = value;
  129. CurrentLineSeries = new ChartValues<double>();
  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(Directory.GetCurrentDirectory(), "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. }
  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. }