GasVerificationViewModel.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. using Aitex.Core.Common.DeviceData;
  2. using ExcelLibrary.SpreadSheet;
  3. using MECF.Framework.Common.DataCenter;
  4. using MECF.Framework.Common.DBCore;
  5. using MECF.Framework.Common.Device.Bases;
  6. using MECF.Framework.Common.Equipment;
  7. using MECF.Framework.Common.OperationCenter;
  8. using Prism.Commands;
  9. using Prism.Mvvm;
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Collections.ObjectModel;
  13. using System.IO;
  14. using System.Linq;
  15. using System.Text;
  16. using System.Threading.Tasks;
  17. using System.Windows.Threading;
  18. using Venus_Core;
  19. using Venus_MainPages.Unity;
  20. namespace Venus_MainPages.ViewModels
  21. {
  22. public class GasVerificationViewModel : BindableBase
  23. {
  24. #region 私有变量
  25. private ObservableCollection<VerificationOneData> m_VerificationOnePointDataRecords;
  26. private ObservableCollection<VerificationOneData> m_VerificationDataTenPointRecords;
  27. private DispatcherTimer timer = new DispatcherTimer();
  28. private PMState m_PMCurrentState;
  29. private ObservableCollection<MFCCalibrationTenPointsData> m_MFCCalibrationOnePointData;
  30. private ObservableCollection<MFCCalibrationTenPointsData> m_MFCCalibrationTenPointsData;
  31. private ObservableCollection<int> m_MFCVerificationPoints;
  32. private string OnePointIndex = "MfcGas1";
  33. private int m_OnePointSetPoint;
  34. private bool m_firsflag = true;
  35. #endregion
  36. #region 公开变量
  37. //private Dictionary<string, object> m_RtDataValues;
  38. public string ModuleName;
  39. public ObservableCollection<VerificationOneData> VerificationDataOnePointRecords
  40. {
  41. get { return m_VerificationOnePointDataRecords; }
  42. set { SetProperty(ref m_VerificationOnePointDataRecords, value); }
  43. }
  44. public ObservableCollection<VerificationOneData> VerificationDataTenPointRecords
  45. {
  46. get { return m_VerificationDataTenPointRecords; }
  47. set { SetProperty(ref m_VerificationDataTenPointRecords, value); }
  48. }
  49. public int OnePointSetPoint
  50. {
  51. get { return m_OnePointSetPoint; }
  52. set { SetProperty(ref m_OnePointSetPoint, value); }
  53. }
  54. public PMState PMCurrentState
  55. {
  56. get { return m_PMCurrentState; }
  57. set
  58. {
  59. if ((m_PMCurrentState == PMState.MFCVerification || m_PMCurrentState == PMState.AllMFCVerification) && value == PMState.Idle)
  60. {
  61. InitTable();
  62. }
  63. SetProperty(ref m_PMCurrentState, value);
  64. }
  65. }
  66. public ObservableCollection<MFCCalibrationTenPointsData> MFCCalibrationOnePointDataRecords
  67. {
  68. get { return m_MFCCalibrationOnePointData; }
  69. set { SetProperty(ref m_MFCCalibrationOnePointData, value); }
  70. }
  71. public ObservableCollection<MFCCalibrationTenPointsData> MFCCalibrationTenPointsDataRecords
  72. {
  73. get { return m_MFCCalibrationTenPointsData; }
  74. set { SetProperty(ref m_MFCCalibrationTenPointsData, value); }
  75. }
  76. public ObservableCollection<int> MFCVerificationPoints
  77. {
  78. get { return m_MFCVerificationPoints; }
  79. set
  80. {
  81. SetProperty(ref m_MFCVerificationPoints, value);
  82. }
  83. }
  84. private DelegateCommand<object> _SelectOneVerificaton;
  85. private DelegateCommand<object> _LoadCommand;
  86. public DelegateCommand<object> LoadCommand =>
  87. _LoadCommand ?? (_LoadCommand = new DelegateCommand<object>(OnLoad));
  88. public DelegateCommand<object> SelectOneVerificaton =>
  89. _SelectOneVerificaton ?? (_SelectOneVerificaton = new DelegateCommand<object>(OnSelectGas));
  90. private DelegateCommand _StartOnePointVerificationCommand;
  91. public DelegateCommand StartOnePointVerificationCommand =>
  92. _StartOnePointVerificationCommand ?? (_StartOnePointVerificationCommand = new DelegateCommand(OnStartOnePointVerification));
  93. private DelegateCommand _StartTenPointVerificationCommand;
  94. public DelegateCommand StartTenPointVerificationCommand =>
  95. _StartTenPointVerificationCommand ?? (_StartTenPointVerificationCommand = new DelegateCommand(OnStartTenPointVerification));
  96. private DelegateCommand _AbortCommand;
  97. public DelegateCommand AbortCommand =>
  98. _AbortCommand ?? (_AbortCommand = new DelegateCommand(OnAbort));
  99. private DelegateCommand _FlashCommand;
  100. public DelegateCommand FlashCommand =>
  101. _FlashCommand ?? (_FlashCommand = new DelegateCommand(OnFlash));
  102. private DelegateCommand<object> _ExportCommand;
  103. public DelegateCommand<object> ExportCommand =>
  104. _ExportCommand ?? (_ExportCommand = new DelegateCommand<object>(OnExport));
  105. #endregion
  106. #region 初始函数
  107. public GasVerificationViewModel()
  108. {
  109. timer.Interval = TimeSpan.FromSeconds(1);
  110. timer.Tick += Timer_Tick;
  111. timer.Start();
  112. MFCCalibrationOnePointDataRecords = new ObservableCollection<MFCCalibrationTenPointsData>();
  113. MFCCalibrationTenPointsDataRecords = new ObservableCollection<MFCCalibrationTenPointsData>();
  114. MFCVerificationPoints = new ObservableCollection<int>();
  115. for (int i = 0; i < 10; i++)
  116. {
  117. MFCCalibrationTenPointsDataRecords.Add(new MFCCalibrationTenPointsData());
  118. }
  119. for (int i = 0; i < 1; i++)
  120. {
  121. MFCCalibrationOnePointDataRecords.Add(new MFCCalibrationTenPointsData());
  122. }
  123. }
  124. public void Init()
  125. {
  126. //OnSelectGas();
  127. }
  128. #endregion
  129. #region 私有方法
  130. private void Timer_Tick(object sender, EventArgs e)
  131. {
  132. //InitTable();
  133. PMCurrentState = (PMState)QueryDataClient.Instance.Service.GetData($"{ModuleName}.FsmState");
  134. if (VerificationDataOnePointRecords!=null && OnePointIndex != VerificationDataOnePointRecords.First(x => x.IsCheckGas == true).GasIndex)
  135. {
  136. MFCVerificationPoints.Clear();
  137. var MFCData = (AITMfcData)QueryDataClient.Instance.Service.GetData($"{ModuleName}.{VerificationDataOnePointRecords.First(x => x.IsCheckGas == true).GasIndex}");
  138. if (MFCData != null)
  139. {
  140. int delta1 = (int)MFCData.Scale / 10;
  141. for (int i = 0; i < 10; i++)
  142. {
  143. MFCVerificationPoints.Add(delta1 + delta1 * i);
  144. }
  145. OnePointIndex = VerificationDataOnePointRecords.First(x => x.IsCheckGas == true).GasIndex;
  146. OnePointSetPoint = MFCVerificationPoints[0];
  147. }
  148. }
  149. }
  150. private void OnLoad(object mFCVerificationView)
  151. {
  152. if (m_firsflag)
  153. {
  154. m_firsflag = false;
  155. InitTable();
  156. if (VerificationDataOnePointRecords != null)
  157. {
  158. MFCVerificationPoints.Clear();
  159. var MFCData = (AITMfcData)QueryDataClient.Instance.Service.GetData($"{ModuleName}.{VerificationDataOnePointRecords.First(x => x.IsCheckGas == true).GasIndex}");
  160. if (MFCData != null)
  161. {
  162. int delta1 = (int)MFCData.Scale / 10;
  163. for (int i = 0; i < 10; i++)
  164. {
  165. MFCVerificationPoints.Add(delta1 + delta1 * i);
  166. }
  167. OnePointIndex = VerificationDataOnePointRecords.First(x => x.IsCheckGas == true).GasIndex;
  168. OnePointSetPoint = MFCVerificationPoints[0];
  169. }
  170. }
  171. }
  172. }
  173. private void OnStartOnePointVerification()
  174. {
  175. InvokeClient.Instance.Service.DoOperation($"{ModuleName}.{RtOperation.MFCVerification}", VerificationDataOnePointRecords.First(x => x.IsCheckGas == true).GasIndex.Replace("MfcGas",""), OnePointSetPoint, 1);
  176. }
  177. private void OnStartTenPointVerification()
  178. {
  179. InvokeClient.Instance.Service.DoOperation($"{ModuleName}.{RtOperation.MFCVerification}", VerificationDataTenPointRecords.First(x => x.IsCheckGas == true).GasIndex.Replace("MfcGas", ""), 2, 10);
  180. }
  181. public void OnAbort()
  182. {
  183. InvokeClient.Instance.Service.DoOperation($"{ModuleName}.Abort");
  184. }
  185. public void OnFlash()
  186. {
  187. InitTable();
  188. }
  189. public void OnExport(object data)
  190. {
  191. int datatype = Convert.ToInt32(data);
  192. switch (datatype)
  193. {
  194. case 1:
  195. List2Excel(VerificationDataOnePointRecords);
  196. break;
  197. case 10:
  198. List2Excel(VerificationDataTenPointRecords);
  199. break;
  200. }
  201. }
  202. private void List2Excel(ObservableCollection<VerificationOneData> datas)
  203. {
  204. if (datas == null || datas.Count == 0) return;
  205. Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
  206. dlg.DefaultExt = ".xls"; // Default file extension
  207. dlg.Filter = "数据表格文件|*.xls"; // Filter files by extension
  208. dlg.FileName = $"{DateTime.Now.ToString("yyyyMMddHHmmss")}";
  209. Nullable<bool> result = dlg.ShowDialog();// Show open file dialog box
  210. if (result != true) // open file dialog box results
  211. return;
  212. if (File.Exists(dlg.FileName))
  213. {
  214. File.Delete(dlg.FileName);
  215. }
  216. Workbook workbook = new Workbook();
  217. Worksheet worksheet = new Worksheet("Sheet1");
  218. //增加行名
  219. int col = 0;
  220. worksheet.Cells[0, col++] = new Cell("GasIndex");
  221. worksheet.Cells[0, col++] = new Cell("GasName");
  222. worksheet.Cells[0, col++] = new Cell("FullRange");
  223. worksheet.Cells[0, col++] = new Cell("Setpoint");
  224. worksheet.Cells[0, col++] = new Cell("Calculate");
  225. worksheet.Cells[0, col++] = new Cell("Error(%)");
  226. worksheet.Cells[0, col++] = new Cell("OperateTime");
  227. // 添加数据行
  228. for (int i = 0; i < datas.Count * 2; i++)
  229. {
  230. if (i < datas.Count)
  231. {
  232. int colidx = 0;
  233. worksheet.Cells[i + 1, colidx++] = new Cell(string.IsNullOrEmpty(datas[i].GasIndex) ? "" : datas[i].GasIndex);
  234. worksheet.Cells[i + 1, colidx++] = new Cell(string.IsNullOrEmpty(datas[i].GasName) ? "" : datas[i].GasName);
  235. worksheet.Cells[i + 1, colidx++] = new Cell(string.IsNullOrEmpty(datas[i].FullRange) ? "" : datas[i].FullRange);
  236. worksheet.Cells[i + 1, colidx++] = new Cell(datas[i].Setpoint.ToString());
  237. worksheet.Cells[i + 1, colidx++] = new Cell(datas[i].Calculate.ToString());
  238. worksheet.Cells[i + 1, colidx++] = new Cell(MFCError(datas[i].Setpoint, datas[i].Calculate).ToString());
  239. worksheet.Cells[i + 1, colidx++] = new Cell(string.IsNullOrEmpty(datas[i].OperateTime) ? "" : datas[i].OperateTime);
  240. }
  241. else
  242. {
  243. //增加empty cell 否则无法正常打开
  244. int colidx = 0;
  245. worksheet.Cells[i + 1, colidx++] = new Cell($"");
  246. worksheet.Cells[i + 2, colidx++] = new Cell($"");
  247. worksheet.Cells[i + 3, colidx++] = new Cell($"");
  248. worksheet.Cells[i + 4, colidx++] = new Cell($"");
  249. worksheet.Cells[i + 5, colidx++] = new Cell($"");
  250. worksheet.Cells[i + 6, colidx++] = new Cell($"");
  251. worksheet.Cells[i + 7, colidx++] = new Cell($"");
  252. }
  253. }
  254. // 保存文件
  255. workbook.Worksheets.Add(worksheet);
  256. workbook.Save(dlg.FileName);
  257. }
  258. private float MFCError(float v1, float v2)
  259. {
  260. float setPoint = v1;
  261. float calculate = v2;
  262. if (calculate == 0)
  263. {
  264. return 0;
  265. }
  266. return (Math.Abs(calculate) - Math.Abs(setPoint)) / Math.Abs(setPoint) * 100;
  267. }
  268. private void OnSelectGas(object gasindex)
  269. {
  270. if (gasindex != null)
  271. {
  272. }
  273. }
  274. private void InitTable()
  275. {
  276. var onePointData = QueryDataClient.Instance.Service.GetMFCVerificationOnePointData();
  277. if (onePointData != null)
  278. {
  279. var _onePointData = onePointData.Where(x => x.Module == ModuleName);
  280. VerificationDataOnePointRecords = new ObservableCollection<VerificationOneData>();
  281. for (int i = 1; i <= 12; i++)
  282. {
  283. if ((bool)QueryDataClient.Instance.Service.GetConfig($"{ModuleName}.MfcGas{i}.Enable"))
  284. {
  285. VerificationDataOnePointRecords.Add(new VerificationOneData()
  286. {
  287. GasIndex = $"MfcGas{i}",
  288. GasName = QueryDataClient.Instance.Service.GetConfig($"{ModuleName}.MfcGas{i}.GasName").ToString(),
  289. Setpoint = _onePointData.Count(x => x.Name == $"MfcGas{i}") > 0 ? _onePointData.FirstOrDefault(x=>x.Name== $"MfcGas{i}").Setpoint : 0f,
  290. Calculate = _onePointData.Count(x => x.Name == $"MfcGas{i}") > 0 ? _onePointData.FirstOrDefault(x => x.Name == $"MfcGas{i}").Calculate : 0f,
  291. OperateTime = _onePointData.Count(x => x.Name == $"MfcGas{i}") > 0 ? DateTime.Parse(_onePointData.FirstOrDefault(x => x.Name == $"MfcGas{i}").OperateTime.ToString()).ToString("yyyy/MM/dd HH:mm:ss") : "",
  292. FullRange = QueryDataClient.Instance.Service.GetConfig($"{ModuleName}.MfcGas{i}.MfcN2Scale").ToString(),
  293. IsCheckGas = false
  294. });
  295. }
  296. }
  297. if(VerificationDataOnePointRecords.Count > 0)
  298. VerificationDataOnePointRecords[0].IsCheckGas = true;
  299. }
  300. var tenPointsData = QueryDataClient.Instance.Service.GetMFCVerificationTenPointsData();
  301. if (tenPointsData != null)
  302. {
  303. var _tenPointData = tenPointsData.Where(x => x.Module == ModuleName);
  304. VerificationDataTenPointRecords = new ObservableCollection<VerificationOneData>();
  305. for (int i = 1; i <= 12; i++)
  306. {
  307. if ((bool)QueryDataClient.Instance.Service.GetConfig($"{ModuleName}.MfcGas{i}.Enable"))
  308. {
  309. VerificationDataTenPointRecords.Add(new VerificationOneData()
  310. {
  311. GasIndex = $"MfcGas{i}",
  312. GasName = QueryDataClient.Instance.Service.GetConfig($"{ModuleName}.MfcGas{i}.GasName").ToString(),
  313. Setpoint = _tenPointData.Count(x => x.Name == $"MfcGas{i}") > 0 ? _tenPointData.FirstOrDefault(x => x.Name == $"MfcGas{i}").Percent10Setpoint : 0f,
  314. Calculate = _tenPointData.Count(x => x.Name == $"MfcGas{i}") > 0 ? _tenPointData.FirstOrDefault(x => x.Name == $"MfcGas{i}").Percent10Calculate : 0f,
  315. OperateTime = _tenPointData.Count(x => x.Name == $"MfcGas{i}") > 0 ? DateTime.Parse(_tenPointData.FirstOrDefault(x => x.Name == $"MfcGas{i}").OperateTime.ToString()).ToString("yyyy/MM/dd HH:mm:ss") : "",
  316. FullRange = QueryDataClient.Instance.Service.GetConfig($"{ModuleName}.MfcGas{i}.MfcN2Scale").ToString(),
  317. IsCheckGas = false,
  318. ShowIndex = true
  319. }); ;
  320. VerificationDataTenPointRecords.Add(new VerificationOneData()
  321. {
  322. Setpoint = _tenPointData.Count(x => x.Name == $"MfcGas{i}") > 0 ? _tenPointData.FirstOrDefault(x => x.Name == $"MfcGas{i}").Percent20Setpoint : 0f,
  323. Calculate = _tenPointData.Count(x => x.Name == $"MfcGas{i}") > 0 ? _tenPointData.FirstOrDefault(x => x.Name == $"MfcGas{i}").Percent20Calculate : 0f,
  324. ShowIndex = false
  325. });
  326. VerificationDataTenPointRecords.Add(new VerificationOneData()
  327. {
  328. Setpoint = _tenPointData.Count(x => x.Name == $"MfcGas{i}") > 0 ? _tenPointData.FirstOrDefault(x => x.Name == $"MfcGas{i}").Percent30Setpoint : 0f,
  329. Calculate = _tenPointData.Count(x => x.Name == $"MfcGas{i}") > 0 ? _tenPointData.FirstOrDefault(x => x.Name == $"MfcGas{i}").Percent30Calculate : 0f,
  330. ShowIndex = false
  331. });
  332. VerificationDataTenPointRecords.Add(new VerificationOneData()
  333. {
  334. Setpoint = _tenPointData.Count(x => x.Name == $"MfcGas{i}") > 0 ? _tenPointData.FirstOrDefault(x => x.Name == $"MfcGas{i}").Percent40Setpoint : 0f,
  335. Calculate = _tenPointData.Count(x => x.Name == $"MfcGas{i}") > 0 ? _tenPointData.FirstOrDefault(x => x.Name == $"MfcGas{i}").Percent40Calculate : 0f,
  336. ShowIndex = false
  337. });
  338. VerificationDataTenPointRecords.Add(new VerificationOneData()
  339. {
  340. Setpoint = _tenPointData.Count(x => x.Name == $"MfcGas{i}") > 0 ? _tenPointData.FirstOrDefault(x => x.Name == $"MfcGas{i}").Percent50Setpoint : 0f,
  341. Calculate = _tenPointData.Count(x => x.Name == $"MfcGas{i}") > 0 ? _tenPointData.FirstOrDefault(x => x.Name == $"MfcGas{i}").Percent50Calculate : 0f,
  342. ShowIndex = false
  343. });
  344. VerificationDataTenPointRecords.Add(new VerificationOneData()
  345. {
  346. Setpoint = _tenPointData.Count(x => x.Name == $"MfcGas{i}") > 0 ? _tenPointData.FirstOrDefault(x => x.Name == $"MfcGas{i}").Percent60Setpoint : 0f,
  347. Calculate = _tenPointData.Count(x => x.Name == $"MfcGas{i}") > 0 ? _tenPointData.FirstOrDefault(x => x.Name == $"MfcGas{i}").Percent60Calculate : 0f,
  348. ShowIndex = false
  349. });
  350. VerificationDataTenPointRecords.Add(new VerificationOneData()
  351. {
  352. Setpoint = _tenPointData.Count(x => x.Name == $"MfcGas{i}") > 0 ? _tenPointData.FirstOrDefault(x => x.Name == $"MfcGas{i}").Percent70Setpoint : 0f,
  353. Calculate = _tenPointData.Count(x => x.Name == $"MfcGas{i}") > 0 ? _tenPointData.FirstOrDefault(x => x.Name == $"MfcGas{i}").Percent70Calculate : 0f,
  354. ShowIndex = false
  355. });
  356. VerificationDataTenPointRecords.Add(new VerificationOneData()
  357. {
  358. Setpoint = _tenPointData.Count(x => x.Name == $"MfcGas{i}") > 0 ? _tenPointData.FirstOrDefault(x => x.Name == $"MfcGas{i}").Percent80Setpoint : 0f,
  359. Calculate = _tenPointData.Count(x => x.Name == $"MfcGas{i}") > 0 ? _tenPointData.FirstOrDefault(x => x.Name == $"MfcGas{i}").Percent80Calculate : 0f,
  360. ShowIndex = false
  361. });
  362. VerificationDataTenPointRecords.Add(new VerificationOneData()
  363. {
  364. Setpoint = _tenPointData.Count(x => x.Name == $"MfcGas{i}") > 0 ? _tenPointData.FirstOrDefault(x => x.Name == $"MfcGas{i}").Percent90Setpoint : 0f,
  365. Calculate = _tenPointData.Count(x => x.Name == $"MfcGas{i}") > 0 ? _tenPointData.FirstOrDefault(x => x.Name == $"MfcGas{i}").Percent90Calculate : 0f,
  366. ShowIndex = false
  367. });
  368. VerificationDataTenPointRecords.Add(new VerificationOneData()
  369. {
  370. Setpoint = _tenPointData.Count(x => x.Name == $"MfcGas{i}") > 0 ? _tenPointData.FirstOrDefault(x => x.Name == $"MfcGas{i}").Percent100Setpoint : 0f,
  371. Calculate = _tenPointData.Count(x => x.Name == $"MfcGas{i}") > 0 ? _tenPointData.FirstOrDefault(x => x.Name == $"MfcGas{i}").Percent100Calculate : 0f,
  372. ShowIndex = false
  373. });
  374. }
  375. }
  376. }
  377. }
  378. #endregion
  379. }
  380. public class VerificationOneData
  381. {
  382. public string GasIndex { get; set; }
  383. public string GasName { get; set; }
  384. public string FullRange { get; set; }
  385. public float Setpoint { get; set; }
  386. public float Calculate { get; set; }
  387. public string OperateTime { get; set; }
  388. public bool IsCheckGas { get; set; }
  389. public bool ShowIndex { get;set; }
  390. }
  391. }