ProcessHistoryViewModel.cs 18 KB

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