ProcessHistoryViewModel.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  1. using MECF.Framework.Common.DataCenter;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Data;
  5. using System.Windows.Media;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using Venus_Core;
  10. using Prism.Commands;
  11. using Prism.Mvvm;
  12. using Venus_MainPages.Unity;
  13. using Venus_MainPages.Views;
  14. using System.Collections.ObjectModel;
  15. using System.Windows.Threading;
  16. using System.Windows;
  17. using Aitex.Core.RT.Log;
  18. using Aitex.Core.UI.ControlDataContext;
  19. using WPF.Themes.UserControls;
  20. using System.IO;
  21. using System.Diagnostics;
  22. using Aitex.Core.RT.Routine;
  23. using System.Xml.Linq;
  24. using System.Windows.Forms;
  25. using ExcelLibrary.BinaryFileFormat;
  26. using Aitex.Core.UI.View.Common;
  27. namespace Venus_MainPages.ViewModels
  28. {
  29. public class ProcessHistoryViewModel : BindableBase
  30. {
  31. #region 私有字段
  32. private ProcessHistoryView view;
  33. List<string> keys = new List<string>();
  34. RealtimeProvider _provider = new RealtimeProvider();
  35. private ObservableCollection<ParameterNode> _ParameterNodes;
  36. DispatcherTimer timer = new DispatcherTimer();
  37. ObservableCollection<PdKeyData>_PdKeyDataObservation= new ObservableCollection<PdKeyData>();
  38. public List<string> RecipesAdd=new List<string>();
  39. public List<SolidColorBrush> solidColorBrushes = new List<SolidColorBrush>();
  40. DateTime currentTime;
  41. private RecipeItem selectedRecipeItem;
  42. #endregion
  43. #region 属性
  44. public List<HistoryDataItem> ProcessData { get; set; }
  45. public List<Recipeslist> CheboxRecipes { get; set; }
  46. public ProcessDataChartDataItem ProcessChartData{ get; set; }
  47. public DateTime StartDateTime { get; set; }
  48. public DateTime EndDateTime { get; set; }
  49. public string SelectedValuePM { get; set; }
  50. public string RecipeName { get; set; }
  51. public ObservableCollection<RecipeItem> Recipes { get; set; }
  52. public class Recipeslist
  53. {
  54. public string BoxName { get; set; }
  55. }
  56. public class RecipeAdd
  57. {
  58. public string Recipesname { get; set; }
  59. }
  60. public ObservableCollection<ParameterNode> ParameterNodes
  61. {
  62. get { return _ParameterNodes; }
  63. set { SetProperty(ref _ParameterNodes, value); }
  64. }
  65. public ObservableCollection<PdKeyData> PdKeyDataCollection
  66. {
  67. get { return _PdKeyDataObservation; }
  68. set { SetProperty(ref _PdKeyDataObservation,value); }
  69. }
  70. #endregion
  71. #region 命令
  72. private DelegateCommand<object> _LoadCommandPD;
  73. public DelegateCommand<object> LoadCommandPD =>
  74. _LoadCommandPD ?? (_LoadCommandPD = new DelegateCommand<object>(OnLoadPd));
  75. private DelegateCommand _SearchRecipeCommand;
  76. public DelegateCommand SearchRecipeCommand =>
  77. _SearchRecipeCommand ?? (_SearchRecipeCommand = new DelegateCommand(SearchRecipes));
  78. private DelegateCommand<object> _PdParameterCheckCommand;
  79. public DelegateCommand<object> PdParameterCheckCommand=>
  80. _PdParameterCheckCommand??(_PdParameterCheckCommand=new DelegateCommand<object>(OnParameterCheck));
  81. private DelegateCommand<object> _DataGridSelectionChangedCommand;
  82. public DelegateCommand<object> DataGridSelectionChangedCommand =>
  83. _DataGridSelectionChangedCommand ?? (_DataGridSelectionChangedCommand = new DelegateCommand<object>(OnDataGridSelectionChanged));
  84. private DelegateCommand _SearchDataCommand;
  85. public DelegateCommand SearchDataCommand =>
  86. _SearchDataCommand ?? (_SearchDataCommand = new DelegateCommand(OnSearchData));
  87. private DelegateCommand _ClearDataCommand;
  88. public DelegateCommand ClearDataCommand =>
  89. _ClearDataCommand ?? (_ClearDataCommand = new DelegateCommand(OnClearData));
  90. #endregion
  91. #region 构造函数
  92. public ProcessHistoryViewModel()
  93. {
  94. ProcessChartData = new ProcessDataChartDataItem(60000);
  95. Recipes = new ObservableCollection<RecipeItem>();
  96. timer.Interval = TimeSpan.FromSeconds(0.5);
  97. CheboxRecipes = new List<Recipeslist>();
  98. solidColorBrushes.Add(new SolidColorBrush(Colors.Green));
  99. solidColorBrushes.Add(new SolidColorBrush(Colors.Red));
  100. solidColorBrushes.Add(new SolidColorBrush(Colors.Blue));
  101. solidColorBrushes.Add(new SolidColorBrush(Colors.Orange));
  102. solidColorBrushes.Add(new SolidColorBrush(Colors.Yellow));
  103. solidColorBrushes.Add(new SolidColorBrush(Colors.YellowGreen));
  104. solidColorBrushes.Add(new SolidColorBrush(Colors.AliceBlue));
  105. solidColorBrushes.Add(new SolidColorBrush(Colors.Chocolate));
  106. solidColorBrushes.Add(new SolidColorBrush(Colors.Cyan));
  107. solidColorBrushes.Add(new SolidColorBrush(Colors.DarkGreen));
  108. }
  109. #endregion
  110. #region 命令方法
  111. private void OnLoadPd(Object eventView)
  112. {
  113. this.view = (ProcessHistoryView)eventView;
  114. this.view.wfTimeFrom.Value = DateTime.Today;
  115. this.view.wfTimeTo.Value = new DateTime(DateTime.Today.Year, DateTime.Today.Month, DateTime.Today.Day, 23, 59, 59, 999);
  116. this.LoadRecipeCheckBox();
  117. }
  118. private void LoadRecipeCheckBox()
  119. {
  120. List<string> chamber = new List<string>() { "PMA","PMB","PMC","PMD"};
  121. CheboxRecipes.Clear();
  122. foreach (string item in chamber)
  123. {
  124. string path = Path.Combine(QueryDataClient.Instance.Service.GetData("GetRTPath").ToString(), "Recipes", item);
  125. if (Directory.Exists(path))
  126. {
  127. string[] dir = Directory.GetFiles(path);
  128. for (int i = 0; i < dir.Length; i++)
  129. {
  130. CheboxRecipes.Add(new Recipeslist { BoxName = Path.GetFileName(dir[i]) });
  131. }
  132. }
  133. }
  134. }
  135. public void SearchRecipes()
  136. {
  137. SearchRecipe(this.view.wfTimeFrom.Value, this.view.wfTimeTo.Value);
  138. }
  139. public void SearchRecipe(DateTime start,DateTime end )
  140. {
  141. this.StartDateTime = start;
  142. this.EndDateTime = end;
  143. Recipes.Clear();
  144. try
  145. {
  146. string sql = $"SELECT * FROM \"process_data\" where \"process_begin_time\" >='{StartDateTime.ToString("yyyy/MM/dd HH:mm:ss.fff")}' and \"process_begin_time\" <='{EndDateTime.ToString("yyyy/MM/dd HH:mm:ss.fff")}' ";
  147. if (!string.IsNullOrEmpty(SelectedValuePM))
  148. {
  149. string[] pms = SelectedValuePM.Split(',');
  150. if (pms.Length > 0)
  151. {
  152. sql += " and (FALSE ";
  153. foreach (var pm in pms)
  154. {
  155. sql += $" OR \"process_in\"='{pm}' ";
  156. }
  157. sql += " ) ";
  158. }
  159. }
  160. if (!string.IsNullOrEmpty(RecipeName))
  161. {
  162. sql += string.Format(" and lower(\"recipe_name\") like '%{0}%'", RecipeName.ToLower());
  163. }
  164. sql += " order by \"process_begin_time\" ASC;";
  165. DataTable dbData = QueryDataClient.Instance.Service.QueryData(sql);
  166. System.Windows.Application.Current.Dispatcher.BeginInvoke(new Action(() =>
  167. {
  168. if (dbData == null || dbData.Rows.Count == 0) return;
  169. for (int i = 0; i < dbData.Rows.Count; i++)
  170. {
  171. RecipeItem item = new RecipeItem();
  172. item.Selected = false;
  173. item.Recipe = dbData.Rows[i]["recipe_name"].ToString();
  174. item.Guid = dbData.Rows[i]["guid"].ToString();
  175. item.RecipeRunGuid = dbData.Rows[i]["wafer_data_guid"].ToString();
  176. item.Chamber = dbData.Rows[i]["process_in"].ToString();
  177. item.Status = dbData.Rows[i]["process_status"].ToString();
  178. item.SlotID = dbData.Rows[i]["slot_id"].ToString();
  179. item.LotID = dbData.Rows[i]["lot_id"].ToString();
  180. if (!dbData.Rows[i]["process_begin_time"].Equals(DBNull.Value))
  181. item.StartTime = ((DateTime)dbData.Rows[i]["process_begin_time"]);
  182. if (!dbData.Rows[i]["process_end_time"].Equals(DBNull.Value))
  183. item.EndTime = ((DateTime)dbData.Rows[i]["process_end_time"]);
  184. Recipes.Add(item);
  185. }
  186. }));
  187. }
  188. catch (Exception e)
  189. {
  190. LOG.WriteExeption(e);
  191. }
  192. }
  193. private void CalKeys(ParameterNode parameterNode)
  194. {
  195. if (parameterNode.ChildNodes.Count > 0)
  196. {
  197. foreach (var item in parameterNode.ChildNodes)
  198. {
  199. CalKeys(item);
  200. }
  201. }
  202. else
  203. {
  204. if (parameterNode.Selected == true)
  205. {
  206. keys.Add(parameterNode.Name);
  207. }
  208. }
  209. }
  210. private Dictionary<string, List<HistoryDataItem>> GetData(List<string> keys, DateTime from, DateTime to)
  211. {
  212. string sql = "select time AS InternalTimeStamp";
  213. foreach (var dataId in keys)
  214. {
  215. sql += "," + string.Format("\"{0}\"", dataId);
  216. }
  217. sql += string.Format(" from \"{0}\" where time > {1} and time <= {2} order by time asc",
  218. from.ToString("yyyyMMdd") + "." + "Data", from.Ticks, to.Ticks);
  219. DataTable dataTable = QueryDataClient.Instance.Service.QueryData(sql);
  220. Dictionary<string, List<HistoryDataItem>> historyData = new Dictionary<string, List<HistoryDataItem>>();
  221. if (dataTable == null || dataTable.Rows.Count == 0)
  222. return null;
  223. DateTime dt = new DateTime();
  224. Dictionary<int, string> colName = new Dictionary<int, string>();
  225. for (int colNo = 0; colNo < dataTable.Columns.Count; colNo++)
  226. {
  227. colName.Add(colNo, dataTable.Columns[colNo].ColumnName);
  228. historyData[dataTable.Columns[colNo].ColumnName] = new List<HistoryDataItem>();
  229. }
  230. for (int rowNo = 0; rowNo < dataTable.Rows.Count; rowNo++)
  231. {
  232. PointCollection points = new PointCollection();
  233. var row = dataTable.Rows[rowNo];
  234. for (int i = 0; i < dataTable.Columns.Count; i++)
  235. {
  236. HistoryDataItem data = new HistoryDataItem();
  237. if (i == 0)
  238. {
  239. long ticks = (long)row[i];
  240. dt = new DateTime(ticks);
  241. continue;
  242. }
  243. else
  244. {
  245. string dataId = colName[i];
  246. if (row[i] is DBNull || row[i] == null)
  247. {
  248. data.dateTime = dt;
  249. data.dbName = colName[i];
  250. data.value = 0;
  251. }
  252. else if (row[i] is bool)
  253. {
  254. data.dateTime = dt;
  255. data.dbName = colName[i];
  256. data.value = (bool)row[i] ? 1 : 0;
  257. }
  258. else
  259. {
  260. data.dateTime = dt;
  261. data.dbName = colName[i];
  262. data.value = float.Parse(row[i].ToString());
  263. }
  264. }
  265. historyData[data.dbName].Add(data);
  266. }
  267. }
  268. foreach (var item in historyData)
  269. {
  270. item.Value.Sort((x, y) => DateTime.Compare(x.dateTime, y.dateTime));
  271. }
  272. return historyData;
  273. }
  274. private bool RefreshTreeStatusToChild(ParameterNode node)
  275. {
  276. if (node.ChildNodes.Count > 0)
  277. {
  278. for (int i = 0; i < node.ChildNodes.Count; i++)
  279. {
  280. ParameterNode n = node.ChildNodes[i];
  281. n.Selected = node.Selected;
  282. if (!RefreshTreeStatusToChild(n))
  283. {
  284. //uncheck left node
  285. for (int j = i; j < node.ChildNodes.Count; j++)
  286. {
  287. node.ChildNodes[j].Selected = !node.Selected;
  288. }
  289. //node.Selected = !node.Selected;
  290. return false;
  291. }
  292. }
  293. }
  294. //else
  295. //{
  296. // if (node.Selected == true)
  297. // {
  298. // keys.Add(node.Name);
  299. // }
  300. // else
  301. // {
  302. // keys.Remove(node.Name);
  303. // }
  304. //}
  305. return true;
  306. }
  307. private void RefreshTreeStatusToParent(ParameterNode node)
  308. {
  309. if (node.ParentNode != null)
  310. {
  311. if (node.Selected)
  312. {
  313. bool flag = true;
  314. for (int i = 0; i < node.ParentNode.ChildNodes.Count; i++)
  315. {
  316. if (!node.ParentNode.ChildNodes[i].Selected)
  317. {
  318. flag = false; //as least one child is unselected
  319. break;
  320. }
  321. }
  322. if (flag)
  323. node.ParentNode.Selected = true;
  324. }
  325. else
  326. {
  327. node.ParentNode.Selected = false;
  328. }
  329. RefreshTreeStatusToParent(node.ParentNode);
  330. }
  331. }
  332. private void OnParameterCheck(object obj)
  333. {
  334. ParameterNode node = obj as ParameterNode;
  335. if (!RefreshTreeStatusToChild(node))
  336. {
  337. node.Selected = !node.Selected;
  338. }
  339. else
  340. {
  341. RefreshTreeStatusToParent(node);
  342. }
  343. keys.Clear();
  344. for (int i = 0; i < ParameterNodes.Count; i++)
  345. {
  346. CalKeys(ParameterNodes[i]);
  347. }
  348. if (keys.Count > 10)
  349. {
  350. WPFMessageBox.ShowWarning("最多显示10个数据");
  351. return;
  352. }
  353. PdKeyDataCollection.Clear();
  354. for (int i = 0; i < keys.Count; i++)
  355. {
  356. if (i == 10)
  357. {
  358. break;
  359. }
  360. PdKeyDataCollection.Add(new PdKeyData() { Key = keys[i], Color = solidColorBrushes[i] });
  361. }
  362. }
  363. public void UpdateData(RecipeItem dataLog)
  364. {
  365. if (dataLog == null)
  366. {
  367. this.view.MyDrawGraphicsControl.ClearPlotPoints();
  368. return;
  369. }
  370. keys.Clear();
  371. for (int i = 0; i < ParameterNodes.Count; i++)
  372. {
  373. CalKeys(ParameterNodes[i]);
  374. }
  375. if (keys.Count > 10)
  376. {
  377. WPFMessageBox.ShowWarning("最多显示10个数据");
  378. return;
  379. }
  380. //Application.Current.Dispatcher.Invoke(new Action(() =>
  381. //{
  382. // string type = "A";
  383. // string s = dataLog.Chamber;
  384. // if (dataLog.Chamber == "PMA")
  385. // {
  386. // //keys = new List<string> { "IO.PMA.AI_Chamber_Pressure", "PMA.Rf.PowerSetPoint", "PMA.Rf.ForwardPower", "PMA.Rf.ReflectPower", "PMA.IoMfc.MfcGas1.FeedBack", "PMA.IoMfc.MfcGas1.SetPoint", "PMA.IoMfc.MfcGas2.FeedBack", "PMA.IoMfc.MfcGas2.SetPoint", "PMA.IoMfc.MfcGas3.FeedBack", "PMA.IoMfc.MfcGas3.SetPoint" };
  387. // if ((bool)QueryDataClient.Instance.Service.GetConfig($"PMA.BiasRf.EnableBiasRF"))
  388. // type = "A";
  389. // }
  390. // else if (dataLog.Chamber == "PMB")
  391. // {
  392. // // keys = new List<string> { "IO.PMB.AI_Chamber_Pressure", "PMB.Rf.PowerSetPoint", "PMB.Rf.ForwardPower", "PMB.Rf.ReflectPower", "PMB.IoMfc.MfcGas1.FeedBack", "PMB.IoMfc.MfcGas1.SetPoint", "PMB.IoMfc.MfcGas2.FeedBack", "PMB.IoMfc.MfcGas2.SetPoint", "PMB.IoMfc.MfcGas3.FeedBack", "PMB.IoMfc.MfcGas3.SetPoint" };
  393. // if ((bool)QueryDataClient.Instance.Service.GetConfig($"PMB.BiasRf.EnableBiasRF"))
  394. // type = "B";
  395. // }
  396. //}));
  397. this.view.MyDrawGraphicsControl.ClearPlotPoints();
  398. DateTime dtFrom = dataLog.StartTime;
  399. //DateTime dtFrom = Convert.ToDateTime(dataLog.StartTime);
  400. DateTime dtTo = dtFrom.AddMinutes(10);
  401. dtTo = dataLog.EndTime;
  402. //if (!string.IsNullOrEmpty(dataLog.EndTime))
  403. //{
  404. // dtTo = Convert.ToDateTime(dataLog.EndTime);
  405. //}
  406. var result = GetData(keys.Distinct().ToList(), dtFrom, dtTo);
  407. List<PointCollection> cls = new List<PointCollection>();
  408. for (int i = 0; i < keys.Count; i++)
  409. {
  410. PointCollection points = new PointCollection();
  411. int k = 1;
  412. result[keys[i]].ForEach(point =>
  413. {
  414. points.Add(new Point() { X = point.dateTime.ToOADate(), Y = point.value });
  415. k += 1;
  416. });
  417. cls.Add(points);
  418. }
  419. this.view.MyDrawGraphicsControl.PointCollections = cls;
  420. this.view.MyDrawGraphicsControl.FitControl();
  421. }
  422. public void OnDataGridSelectionChanged(object obj)
  423. {
  424. selectedRecipeItem = obj as RecipeItem;
  425. ParameterNodes = new ObservableCollection<ParameterNode>(_provider.GetParameters().Where(x => x.Name == selectedRecipeItem?.Chamber));
  426. }
  427. private void OnSearchData()
  428. {
  429. this.view.MyDrawGraphicsControl.ClearPlotPoints();
  430. var Keys = new List<string>();
  431. PdKeyDataCollection.ToList().ForEach(key =>
  432. {
  433. Keys.Add(key.Key);
  434. });
  435. if (keys.Count == 0)
  436. {
  437. return;
  438. }
  439. var result = GetData(Keys.Distinct().ToList(), Convert.ToDateTime(selectedRecipeItem?.StartTime) , Convert.ToDateTime(selectedRecipeItem?.EndTime) );
  440. if (result == null)
  441. {
  442. return;
  443. }
  444. List<PointCollection> cls = new List<PointCollection>();
  445. for (int i = 0; i < Keys.Count; i++)
  446. {
  447. PointCollection points = new PointCollection();
  448. int k = 1;
  449. result[Keys[i]].ForEach(point =>
  450. {
  451. points.Add(new Point() { X = point.dateTime.ToOADate(), Y = point.value });
  452. k += 1;
  453. });
  454. cls.Add(points);
  455. }
  456. this.view.MyDrawGraphicsControl.PointCollections = cls;
  457. this.view.MyDrawGraphicsControl.FitControl();
  458. }
  459. private void OnClearData()
  460. {
  461. }
  462. }
  463. #endregion
  464. #region 数据类
  465. public class RecipeItem
  466. {
  467. public bool Selected { get; set; }
  468. public string Recipe { get; set; }
  469. public string Guid { get; set; }
  470. public string RecipeRunGuid { get; set; }
  471. public string Chamber { get; set; }
  472. public string Status { get; set; }
  473. public DateTime StartTime { get; set; }
  474. public DateTime EndTime { get; set; }
  475. public string LotID { get; set; }
  476. public string SlotID { get; set; }
  477. }
  478. public class PdKeyData
  479. {
  480. public string Key { get; set; }
  481. public SolidColorBrush Color { get; set; }
  482. }
  483. #endregion
  484. }