ProcessHistoryViewModel.cs 20 KB

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