ProcessHistoryViewModel.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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. private ProcessHistoryView view;
  29. List<string> keys = new List<string>();
  30. RealtimeProvider _provider = new RealtimeProvider();
  31. private ObservableCollection<ParameterNode> _ParameterNodes;
  32. DispatcherTimer timer = new DispatcherTimer();
  33. public List<string> RecipesAdd=new List<string>();
  34. DateTime currentTime;
  35. private DelegateCommand<object> _LoadCommandPD;
  36. public DelegateCommand<object> LoadCommandPD =>
  37. _LoadCommandPD ?? (_LoadCommandPD = new DelegateCommand<object>(OnLoadPd));
  38. private DelegateCommand _SearchRecipeCommand;
  39. public DelegateCommand SearchRecipeCommand =>
  40. _SearchRecipeCommand ?? (_SearchRecipeCommand = new DelegateCommand(SearchRecipe));
  41. //private string _starttime;
  42. //public string starttime
  43. //{ get { return _starttime; } set { SetProperty(ref _starttime, value); } }
  44. // public string starttime { get; set; }
  45. public List<HistoryDataItem> ProcessData { get; set; }
  46. //public List<Recipeslist> CheboxRecipes = new List<Recipeslist>();
  47. public ProcessHistoryViewModel()
  48. {
  49. ProcessChartData = new ProcessDataChartDataItem(60000);
  50. ParameterNodes = _provider.GetParameters();
  51. Recipes = new ObservableCollection<RecipeItem>();
  52. timer.Interval = TimeSpan.FromSeconds(0.5);
  53. CheboxRecipes = new List<Recipeslist>();
  54. //CheboxRecipes.Add(new Recipeslist { BoxName = "test" });
  55. // CheboxRecipes.Add(new Recipeslist { BoxName = "test2" });
  56. //CheboxRecipes.Add(new Recipeslist { BoxName = "test3" });
  57. //starttime = "1999-10-01";
  58. }
  59. public List<Recipeslist> CheboxRecipes { get; set; }
  60. public ProcessDataChartDataItem ProcessChartData
  61. {
  62. get;
  63. set;
  64. }
  65. private void OnLoadPd(Object eventView)
  66. {
  67. this.view = (ProcessHistoryView)eventView;
  68. this.view.wfTimeFrom.Value = DateTime.Today;
  69. this.view.wfTimeTo.Value = new DateTime(DateTime.Today.Year, DateTime.Today.Month, DateTime.Today.Day, 23, 59, 59, 999);
  70. this.LoadRecipeCheckBox();
  71. }
  72. private void LoadRecipeCheckBox()
  73. {
  74. string path= Path.Combine(QueryDataClient.Instance.Service.GetData("GetRTPath").ToString(), "Recipes","PMA");
  75. if (Directory.Exists(path)) {
  76. string[] dir = Directory.GetFiles(path);
  77. for (int i = 0; i < dir.Length; i++)
  78. {
  79. CheboxRecipes.Add(new Recipeslist { BoxName = Path.GetFileName(dir[i])});
  80. }
  81. }
  82. }
  83. //Path.GetFileName(dir[i]).ToString()
  84. public DateTime StartDateTime { get; set; }
  85. public DateTime EndDateTime { get; set; }
  86. public string SelectedValuePM { get; set; }
  87. public string RecipeName { get; set; }
  88. public class RecipeItem
  89. {
  90. public bool Selected { get; set; }
  91. public string Recipe { get; set; }
  92. public string Guid { get; set; }
  93. public string RecipeRunGuid { get; set; }
  94. public string Chamber { get; set; }
  95. public string Status { get; set; }
  96. public string StartTime { get; set; }
  97. public string EndTime { get; set; }
  98. public string LotID { get; set; }
  99. public string SlotID { get; set; }
  100. }
  101. public class Recipeslist
  102. {
  103. public string BoxName { get; set; }
  104. }
  105. public class RecipeAdd
  106. {
  107. public string Recipesname { get; set; }
  108. }
  109. public ObservableCollection<RecipeItem> Recipes { get; set; }
  110. public ObservableCollection<ParameterNode> ParameterNodes
  111. {
  112. get { return _ParameterNodes; }
  113. set { SetProperty(ref _ParameterNodes, value); }
  114. }
  115. public void SearchRecipe()
  116. {
  117. //if (MenuPermission != 3) return;
  118. Console.WriteLine("er");
  119. this.StartDateTime = this.view.wfTimeFrom.Value;
  120. this.EndDateTime = this.view.wfTimeTo.Value;
  121. Recipes.Clear();
  122. try
  123. {
  124. 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")}' ";
  125. if (!string.IsNullOrEmpty(SelectedValuePM))
  126. {
  127. string[] pms = SelectedValuePM.Split(',');
  128. if (pms.Length > 0)
  129. {
  130. sql += " and (FALSE ";
  131. foreach (var pm in pms)
  132. {
  133. sql += $" OR \"process_in\"='{pm}' ";
  134. }
  135. sql += " ) ";
  136. }
  137. }
  138. if (!string.IsNullOrEmpty(RecipeName))
  139. {
  140. sql += string.Format(" and lower(\"recipe_name\") like '%{0}%'", RecipeName.ToLower());
  141. }
  142. sql += " order by \"process_begin_time\" ASC;";
  143. DataTable dbData = QueryDataClient.Instance.Service.QueryData(sql);
  144. Application.Current.Dispatcher.BeginInvoke(new Action(() =>
  145. {
  146. if (dbData == null || dbData.Rows.Count == 0) return;
  147. for (int i = 0; i < dbData.Rows.Count; i++)
  148. {
  149. RecipeItem item = new RecipeItem();
  150. item.Recipe = dbData.Rows[i]["recipe_name"].ToString();
  151. item.Guid = dbData.Rows[i]["guid"].ToString();
  152. item.RecipeRunGuid = dbData.Rows[i]["wafer_data_guid"].ToString();
  153. item.Chamber = dbData.Rows[i]["process_in"].ToString();
  154. item.Status = dbData.Rows[i]["process_status"].ToString();
  155. item.SlotID = dbData.Rows[i]["slot_id"].ToString();
  156. item.LotID = dbData.Rows[i]["lot_id"].ToString();
  157. if (!dbData.Rows[i]["process_begin_time"].Equals(DBNull.Value))
  158. item.StartTime = ((DateTime)dbData.Rows[i]["process_begin_time"]).ToString("yyyy-MM-dd HH:mm:ss.fff");
  159. if (!dbData.Rows[i]["process_end_time"].Equals(DBNull.Value))
  160. item.EndTime = ((DateTime)dbData.Rows[i]["process_end_time"]).ToString("yyyy-MM-dd HH:mm:ss.fff");
  161. Recipes.Add(item);
  162. }
  163. }));
  164. }
  165. catch (Exception e)
  166. {
  167. LOG.WriteExeption(e);
  168. }
  169. }
  170. private void CalKeys(ParameterNode parameterNode)
  171. {
  172. if (parameterNode.ChildNodes.Count > 0)
  173. {
  174. foreach (var item in parameterNode.ChildNodes)
  175. {
  176. CalKeys(item);
  177. }
  178. }
  179. else
  180. {
  181. if (parameterNode.Selected == true)
  182. {
  183. keys.Add(parameterNode.Name);
  184. }
  185. }
  186. }
  187. private Dictionary<string, List<HistoryDataItem>> GetData(List<string> keys, DateTime from, DateTime to)
  188. {
  189. string sql = "select time AS InternalTimeStamp";
  190. foreach (var dataId in keys)
  191. {
  192. sql += "," + string.Format("\"{0}\"", dataId);
  193. }
  194. sql += string.Format(" from \"{0}\" where time > {1} and time <= {2} order by time asc",
  195. from.ToString("yyyyMMdd") + "." + "Data", from.Ticks, to.Ticks);
  196. DataTable dataTable = QueryDataClient.Instance.Service.QueryData(sql);
  197. Dictionary<string, List<HistoryDataItem>> historyData = new Dictionary<string, List<HistoryDataItem>>();
  198. if (dataTable == null || dataTable.Rows.Count == 0)
  199. return null;
  200. DateTime dt = new DateTime();
  201. Dictionary<int, string> colName = new Dictionary<int, string>();
  202. for (int colNo = 0; colNo < dataTable.Columns.Count; colNo++)
  203. {
  204. colName.Add(colNo, dataTable.Columns[colNo].ColumnName);
  205. historyData[dataTable.Columns[colNo].ColumnName] = new List<HistoryDataItem>();
  206. }
  207. for (int rowNo = 0; rowNo < dataTable.Rows.Count; rowNo++)
  208. {
  209. PointCollection points = new PointCollection();
  210. var row = dataTable.Rows[rowNo];
  211. for (int i = 0; i < dataTable.Columns.Count; i++)
  212. {
  213. HistoryDataItem data = new HistoryDataItem();
  214. if (i == 0)
  215. {
  216. long ticks = (long)row[i];
  217. dt = new DateTime(ticks);
  218. continue;
  219. }
  220. else
  221. {
  222. string dataId = colName[i];
  223. if (row[i] is DBNull || row[i] == null)
  224. {
  225. data.dateTime = dt;
  226. data.dbName = colName[i];
  227. data.value = 0;
  228. }
  229. else if (row[i] is bool)
  230. {
  231. data.dateTime = dt;
  232. data.dbName = colName[i];
  233. data.value = (bool)row[i] ? 1 : 0;
  234. }
  235. else
  236. {
  237. data.dateTime = dt;
  238. data.dbName = colName[i];
  239. data.value = float.Parse(row[i].ToString());
  240. }
  241. }
  242. historyData[data.dbName].Add(data);
  243. }
  244. }
  245. foreach (var item in historyData)
  246. {
  247. item.Value.Sort((x, y) => DateTime.Compare(x.dateTime, y.dateTime));
  248. }
  249. return historyData;
  250. }
  251. public void UpdateData(RecipeItem dataLog)
  252. {
  253. if (dataLog == null)
  254. {
  255. this.view.MyDrawGraphicsControl.ClearPlotPoints();
  256. return;
  257. }
  258. keys.Clear();
  259. for (int i = 0; i < ParameterNodes.Count; i++)
  260. {
  261. CalKeys(ParameterNodes[i]);
  262. }
  263. if (keys.Count > 10)
  264. {
  265. WPFMessageBox.ShowWarning("最多显示10个数据");
  266. return;
  267. }
  268. Application.Current.Dispatcher.Invoke(new Action(() =>
  269. {
  270. string type = "A";
  271. string s = dataLog.Chamber;
  272. if (dataLog.Chamber == "PMA")
  273. {
  274. //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" };
  275. if ((bool)QueryDataClient.Instance.Service.GetConfig($"PMA.BiasRf.EnableBiasRF"))
  276. type = "A";
  277. }
  278. else if (dataLog.Chamber == "PMB")
  279. {
  280. // 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" };
  281. if ((bool)QueryDataClient.Instance.Service.GetConfig($"PMB.BiasRf.EnableBiasRF"))
  282. type = "B";
  283. }
  284. }));
  285. this.view.MyDrawGraphicsControl.ClearPlotPoints();
  286. DateTime dtFrom = Convert.ToDateTime(dataLog.StartTime);
  287. DateTime dtTo = dtFrom.AddMinutes(10);
  288. if (!string.IsNullOrEmpty(dataLog.EndTime))
  289. {
  290. dtTo = Convert.ToDateTime(dataLog.EndTime);
  291. }
  292. var result = GetData(keys.Distinct().ToList(), dtFrom, dtTo);
  293. //var result = GetData(keys.Distinct().ToList(), this.view.wfTimeFrom.Value, this.view.wfTimeTo.Value);
  294. List<PointCollection> cls = new List<PointCollection>();
  295. for (int i = 0; i < keys.Count; i++)
  296. {
  297. PointCollection points = new PointCollection();
  298. int k = 1;
  299. result[keys[i]].ForEach(point =>
  300. {
  301. points.Add(new Point() { X = point.dateTime.ToOADate(), Y = point.value });
  302. k += 1;
  303. });
  304. cls.Add(points);
  305. }
  306. this.view.MyDrawGraphicsControl.PointCollections = cls;
  307. this.view.MyDrawGraphicsControl.FitControl();
  308. }
  309. }
  310. }