ProcessDetailViewModel.cs 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524
  1. using Aitex.Core.RT.Log;
  2. using Aitex.Core.UI.ControlDataContext;
  3. using Aitex.Core.Util;
  4. using Caliburn.Micro;
  5. using Caliburn.Micro.Core;
  6. using MECF.Framework.Common.CommonData;
  7. using MECF.Framework.Common.ControlDataContext;
  8. using MECF.Framework.Common.DataCenter;
  9. using MECF.Framework.Common.Utilities;
  10. using MECF.Framework.UI.Client.CenterViews.Configs.SystemConfig;
  11. using MECF.Framework.UI.Client.CenterViews.DataLogs.ProcessHistory;
  12. using MECF.Framework.UI.Client.CenterViews.Dialogs;
  13. using MECF.Framework.UI.Client.ClientBase;
  14. using OpenSEMI.ClientBase;
  15. using SciChart.Charting.Visuals;
  16. using SciChart.Charting.Visuals.Annotations;
  17. using SciChart.Charting.Visuals.Axes;
  18. using SciChart.Charting.Visuals.RenderableSeries;
  19. using SciChart.Data.Model;
  20. using System;
  21. using System.Collections.Concurrent;
  22. using System.Collections.Generic;
  23. using System.Collections.ObjectModel;
  24. using System.Data;
  25. using System.Diagnostics;
  26. using System.Drawing;
  27. using System.Linq;
  28. using System.Threading;
  29. using System.Threading.Tasks;
  30. using System.Windows;
  31. using Action = System.Action;
  32. using Media = System.Windows.Media;
  33. using System.Windows.Controls;
  34. using SciChart.Charting.Model.ChartData;
  35. using MECF.Framework.UI.Client.Converter;
  36. using MECF.Framework.UI.Client.CenterViews.Operations.RealTime;
  37. using SciChart.Core.Extensions;
  38. using System.Linq.Expressions;
  39. using System.Reflection;
  40. namespace MECF.Framework.UI.Client.CenterViews.DataLogs.ProcessHistory
  41. {
  42. public class StepInfo
  43. {
  44. public DateTime StartTime { get; set; }
  45. public DateTime StepEndTime { get; set; }
  46. public DateTime EndTime { get; set; }
  47. public string StepNo { get; set; }
  48. public string StepName { get; set; }
  49. public double StepTime { get; set; }
  50. public bool IsChecked { get; set; } = true;
  51. public string SubRecipeStepNumber { get; set; }
  52. public string SubRecipeStepName { get; set; }
  53. public string SubRecipeStepTime { get; set; }
  54. public string SubRecipeLoopInfo { get; set; }
  55. public string TempCorrection { get; set; }
  56. public string TempPid { get; set; }
  57. }
  58. public class ProcessDetailViewModel<T> : ModuleUiViewModelBase where T : IComparable
  59. {
  60. public class TimeChartDataLine : ChartDataLine<T>
  61. {
  62. public string Module { get; set; }
  63. public DateTime StartTime { get; set; }
  64. public DateTime EndTime { get; set; }
  65. public TimeChartDataLine(string dataName, string module, DateTime startTime, DateTime endTime) : base(dataName)
  66. {
  67. StartTime = startTime;
  68. EndTime = endTime;
  69. Module = module;
  70. }
  71. }
  72. public class QueryIndexer
  73. {
  74. public TimeChartDataLine DataLine { get; set; }
  75. public List<string> DataList { get; set; }
  76. public DateTime TimeToken { get; set; }
  77. }
  78. public bool IsPermission { get => this.Permission == 3; }
  79. private byte _directionByte = 0x03;
  80. private ProcessDetailView view;
  81. /// <summary>
  82. /// 正在导出数据事件。
  83. /// </summary>
  84. private event EventHandler Exporting;
  85. private CancellationTokenSource _cancellationTokenSource;
  86. #region Property
  87. private const int MAX_PARAMETERS = 20;
  88. private Queue<Color> colorQueue = new Queue<Color>(new Color[]{Color.Red,Color.Orange,Color.Yellow,Color.Green,Color.Blue,Color.Pink,Color.Purple,Color.Aqua,Color.Bisque,Color.Brown,Color.BurlyWood,Color.CadetBlue,
  89. Color.CornflowerBlue,Color.DarkBlue,Color.DarkCyan,Color.DarkGray,Color.DarkGreen,Color.DarkKhaki,Color.DarkMagenta,Color.DarkOliveGreen, Color.DarkOrange,
  90. Color.DarkSeaGreen,Color.DarkSlateBlue,Color.DarkSlateGray,Color.DarkViolet,Color.DeepPink,Color.DeepSkyBlue,Color.DimGray, Color.DodgerBlue,Color.ForestGreen, Color.Gold,
  91. Color.Gray,Color.GreenYellow,Color.HotPink,Color.Indigo,Color.Khaki,Color.LightBlue,Color.LightCoral,Color.LightGreen, Color.LightPink,Color.LightSalmon,Color.LightSkyBlue,
  92. Color.LightSlateGray,Color.LightSteelBlue,Color.LimeGreen,Color.MediumOrchid,Color.MediumPurple,Color.MediumSeaGreen,Color.MediumSlateBlue,Color.MediumSpringGreen,
  93. Color.MediumTurquoise,Color.Moccasin,Color.NavajoWhite,Color.Olive,Color.OliveDrab,Color.OrangeRed,Color.Orchid,Color.PaleGoldenrod,Color.PaleGreen,
  94. Color.PeachPuff,Color.Peru,Color.Plum,Color.PowderBlue,Color.RosyBrown,Color.RoyalBlue,Color.SaddleBrown,Color.Salmon,Color.SeaGreen, Color.Sienna,
  95. Color.SkyBlue,Color.SlateBlue,Color.SlateGray,Color.SpringGreen,Color.Teal,Color.Aquamarine,Color.Tomato,Color.Turquoise,Color.Violet,Color.Wheat, Color.YellowGreen});
  96. private ObservableCollection<ParameterNode> _ParameterNodes;
  97. public ObservableCollection<ParameterNode> ParameterNodes
  98. {
  99. get { return _ParameterNodes; }
  100. set { _ParameterNodes = value; NotifyOfPropertyChange("ParameterNodes"); }
  101. }
  102. public List<ParameterNode> DoubleParameterNodes { get; set; }=new List<ParameterNode>();
  103. public ObservableCollection<IRenderableSeries> SelectedData { get; set; }
  104. public ObservableCollection<IRenderableSeries> SynSelectedData { get; set; }
  105. public List<ProcessHistoryLot> RecipeDatas { get; set; }
  106. private static object _lockSelection = new object();
  107. private AutoRange _autoRange;
  108. public AutoRange ChartAutoRange
  109. {
  110. get { return _autoRange; }
  111. set
  112. {
  113. _autoRange = value;
  114. NotifyOfPropertyChange(nameof(ChartAutoRange));
  115. }
  116. }
  117. private IRange _timeRange;
  118. public IRange VisibleRangeTime
  119. {
  120. get { return _timeRange; }
  121. set
  122. {
  123. _timeRange = value;
  124. NotifyOfPropertyChange(nameof(VisibleRangeTime));
  125. }
  126. }
  127. private IRange _VisibleRangeValue;
  128. public IRange VisibleRangeValue
  129. {
  130. get { return _VisibleRangeValue; }
  131. set { _VisibleRangeValue = value; NotifyOfPropertyChange(nameof(VisibleRangeValue)); }
  132. }
  133. private PeriodicJob _thread;
  134. private ConcurrentBag<QueryIndexer> _lstTokenTimeData = new ConcurrentBag<QueryIndexer>();
  135. private Dictionary<string, string> _processDetailDisplayDic = new Dictionary<string, string>();
  136. private string _resolution;
  137. private AnnotationCollection _annotations;
  138. public AnnotationCollection Annotations
  139. {
  140. get => _annotations;
  141. set
  142. {
  143. _annotations = value;
  144. InvokePropertyChanged(nameof(Annotations));
  145. }
  146. }
  147. //StartTime,EndTime,StepNo,StepName
  148. private List<StepInfo> _stepInfo = new List<StepInfo>();
  149. public List<StepInfo> StepInfo
  150. {
  151. get { return _stepInfo; }
  152. set { _stepInfo = value; this.NotifyOfPropertyChange(nameof(StepInfo)); }
  153. }
  154. public bool IsStepVisiable => RecipeDatas.Count == 1;
  155. public DateTime StepStartTime { get; set; }
  156. public DateTime StepEndTime { get; set; }
  157. private bool _isAdding;
  158. private string _stepName;
  159. public string StepNameDisPlay
  160. {
  161. get { return _stepName; }
  162. set
  163. {
  164. if (_stepName != value)
  165. {
  166. _stepName = value;
  167. NotifyOfPropertyChange(nameof(StepNameDisPlay));
  168. }
  169. }
  170. }
  171. private string _recipeNameDisPlay;
  172. public string RecipeNameDisPlay
  173. {
  174. get { return _recipeNameDisPlay; }
  175. set
  176. {
  177. if (_recipeNameDisPlay != value)
  178. {
  179. _recipeNameDisPlay = value;
  180. NotifyOfPropertyChange(nameof(RecipeNameDisPlay));
  181. }
  182. }
  183. }
  184. private DateTime _pointTime;
  185. public DateTime PointTime
  186. {
  187. get { return _pointTime; }
  188. set
  189. {
  190. if (_pointTime != value)
  191. {
  192. _pointTime = value;
  193. NotifyOfPropertyChange(nameof(PointTime));
  194. }
  195. }
  196. }
  197. private double _lastPointX;
  198. private string _direction = "XYDirection";
  199. public string Direction
  200. {
  201. get { return _direction; }
  202. set
  203. {
  204. if (value != _direction)
  205. {
  206. _direction = value;
  207. if (_direction == "XDirection") _directionByte = 1;
  208. else if (_direction == "YDirection") _directionByte = 2;
  209. else if (_direction == "XYDirection") _directionByte = 3;
  210. NotifyOfPropertyChange(nameof(Direction));
  211. }
  212. }
  213. }
  214. //public Tuple<DateTime,double> _timePairs;
  215. private MultiLineXAxisLabelProvider _mulitiLineLabel;
  216. public MultiLineXAxisLabelProvider MultiLineLabel
  217. {
  218. get { return _mulitiLineLabel; }
  219. set
  220. {
  221. if (_mulitiLineLabel != value)
  222. {
  223. _mulitiLineLabel = value;
  224. NotifyOfPropertyChange(nameof(MultiLineLabel));
  225. }
  226. }
  227. }
  228. private double _xScale = 1;
  229. public double XScale
  230. {
  231. get { return _xScale; }
  232. set
  233. {
  234. if (_xScale != value)
  235. {
  236. _xScale = value;
  237. NotifyOfPropertyChange(nameof(XScale));
  238. }
  239. }
  240. }
  241. private double _yScale = 0.1;
  242. public double YScale
  243. {
  244. get { return _yScale; }
  245. set
  246. {
  247. if (value != _yScale)
  248. {
  249. _yScale = value;
  250. NotifyOfPropertyChange(nameof(YScale));
  251. }
  252. }
  253. }
  254. #endregion
  255. private RealtimeProvider _realtimeProvider;
  256. #region Function
  257. public ProcessDetailViewModel(List<ProcessHistoryLot> recipes)
  258. {
  259. _realtimeProvider = new RealtimeProvider();
  260. DisplayName = "Process Detail";
  261. RecipeDatas = recipes;
  262. ParameterNodes = _realtimeProvider.GetParameters(out var dict);
  263. DoubleParameterNodes = _realtimeProvider.GetParameters(out dict).ToList();
  264. for (int j = 0; j < ParameterNodes.Count; j++)
  265. {
  266. ParameterNodes[j].IsVisibilityParentNode = Visibility.Hidden;
  267. DoubleParameterNodes[j].IsVisibilityParentNode = Visibility.Hidden;
  268. }
  269. _processDetailDisplayDic = dict;
  270. if (recipes == null || recipes.Count == 0)
  271. {
  272. return;
  273. }
  274. SelectedData = new ObservableCollection<IRenderableSeries>();
  275. SynSelectedData = new ObservableCollection<IRenderableSeries>();
  276. VisibleRangeValue = new DoubleRange(0, 10);
  277. // Annotations = new AnnotationCollection();
  278. _thread = new PeriodicJob(200, MonitorData, "ProcessDetail", true);
  279. try
  280. {
  281. RecipeDatas.ToList().ForEach(recipe =>
  282. {
  283. QueryStep(recipe.StartTime, recipe.EndTime);
  284. });
  285. if (StepInfo != null && StepInfo.Count > 0)
  286. {
  287. StepStartTime = StepInfo.FirstOrDefault().StartTime;
  288. StepEndTime = StepInfo.LastOrDefault().EndTime;
  289. }
  290. MultiLineLabel = new MultiLineXAxisLabelProvider(StepStartTime);
  291. }
  292. catch (Exception ex)
  293. {
  294. LOG.Info(ex.StackTrace + ":" + ex.Message);
  295. }
  296. }
  297. protected override void OnViewLoaded(object view)
  298. {
  299. base.OnViewLoaded(view);
  300. this.view = (ProcessDetailView)view;
  301. }
  302. protected override void OnActivate()
  303. {
  304. _resolution = "f1";
  305. if (QueryDataClient.Instance.Service.GetConfig($"System.DataHistoryResolution") != null)
  306. _resolution = $"f{QueryDataClient.Instance.Service.GetConfig($"System.DataHistoryResolution")}";
  307. base.OnActivate();
  308. }
  309. public void QueryStep(DateTime starTime, DateTime endTime)//, string recipeName去掉查询recipeName名称,有嵌套调用
  310. {
  311. starTime = starTime.AddMinutes(-1);
  312. endTime = endTime.AddMinutes(1);
  313. string sql = $"SELECT * FROM \"process_data\" where (\"process_begin_time\" >= '{starTime:yyyy/MM/dd HH:mm:ss.fff}' and \"process_end_time\" <= '{endTime:yyyy/MM/dd HH:mm:ss.fff}') order by \"process_begin_time\" DESC;";
  314. DataTable dbData = QueryDataClient.Instance.Service.QueryData(sql);
  315. if (dbData != null && dbData.Rows.Count > 0)
  316. {
  317. List<ProcessDataLot> ProcessDataLotList = new List<ProcessDataLot>();
  318. for (int i = 0; i < dbData.Rows.Count; i++)
  319. {
  320. ProcessDataLot item = new ProcessDataLot();
  321. item.GUID = dbData.Rows[i]["guid"].ToString();
  322. item.RecipeName = dbData.Rows[i]["recipe_name"].ToString().Trim('\\');
  323. item.ProcessStatus = dbData.Rows[i]["process_status"].ToString();
  324. item.WaferDataGUID = dbData.Rows[i]["wafer_data_guid"].ToString();
  325. item.ProcessIn = dbData.Rows[i]["process_in"].ToString();
  326. if (!dbData.Rows[i]["process_begin_time"].Equals(DBNull.Value))
  327. {
  328. item.ProcessBeginTime = (DateTime)dbData.Rows[i]["process_begin_time"];
  329. }
  330. if (!dbData.Rows[i]["process_end_time"].Equals(DBNull.Value))
  331. {
  332. item.ProcessEndTime = (DateTime)dbData.Rows[i]["process_end_time"];
  333. }
  334. ProcessDataLotList.Add(item);
  335. }
  336. //Annotations.Add(VerLine(Media.Brushes.Blue, ProcessDataLotList[0].ProcessBeginTime, Media.Brushes.Blue, $"{ProcessDataLotList[0].RecipeName}"));
  337. //Annotations.Add(VerLine(Media.Brushes.Blue, ProcessDataLotList[0].ProcessEndTime, Media.Brushes.Blue, $"Recipe End"));
  338. string sql2 = $"SELECT * FROM \"recipe_step_data\" where";
  339. sql2 += $" \"recipe_step_data\".\"process_data_guid\" = '{ProcessDataLotList[0].GUID.ToString()}'";
  340. sql2 += " order by \"step_begin_time\" ASC;";
  341. using (var table = QueryDataClient.Instance.Service.QueryData(sql2))
  342. {
  343. if (!(table == null || table.Rows.Count == 0))
  344. {
  345. for (int i = 0; i < table.Rows.Count; i++)
  346. {
  347. var item = table.Rows[i];
  348. DateTime StartTime = DateTime.MinValue;
  349. DateTime EndTime = DateTime.MinValue;
  350. double stepTime = 0;
  351. if (!item["step_begin_time"].Equals(DBNull.Value))
  352. StartTime = (DateTime)item["step_begin_time"];
  353. if (!item["step_end_time"].Equals(DBNull.Value))
  354. EndTime = (DateTime)item["step_end_time"];
  355. if (!item["step_time"].Equals(DBNull.Value))
  356. {
  357. stepTime = (float)item["step_time"];
  358. }
  359. string stepNo = item["step_number"].ToString();
  360. string stepName = item["step_name"].ToString();
  361. if (StartTime>DateTime.MinValue &&EndTime>DateTime.MinValue )
  362. {
  363. if (EndTime < StartTime.AddSeconds(stepTime))
  364. {
  365. EndTime = StartTime.AddSeconds(stepTime);
  366. }
  367. else
  368. {
  369. if (_stepInfo.Any())//临时处理表中recipe_step_data step_end_time数据不能真实反映步次结束时间的问题
  370. {
  371. int lastIndex = _stepInfo.Count-1;
  372. if (_stepInfo[lastIndex].EndTime> StartTime) _stepInfo[lastIndex].EndTime= StartTime;
  373. }
  374. _stepInfo.Add(new StepInfo() { StartTime = StartTime, EndTime = EndTime, StepName = stepName, StepTime = stepTime, StepNo = stepNo });
  375. }
  376. }
  377. }
  378. }
  379. }
  380. }
  381. }
  382. public static VerticalLineAnnotation VerLine(Media.Brush stroke, DateTime x, Media.Brush color, string text)
  383. {
  384. var label = new AnnotationLabel()
  385. {
  386. LabelPlacement = LabelPlacement.TopRight,
  387. FontSize = 12,
  388. RotationAngle = 0,
  389. Foreground = color,
  390. Text = text
  391. };
  392. var line = new VerticalLineAnnotation()
  393. {
  394. Stroke = stroke,
  395. StrokeThickness = 1,
  396. X1 = x,
  397. IsEditable = false,
  398. VerticalAlignment = VerticalAlignment.Stretch,
  399. AnnotationLabels = new ObservableCollection<AnnotationLabel>() { label },
  400. };
  401. return line;
  402. }
  403. protected bool MonitorData()
  404. {
  405. try
  406. {
  407. if (_isAdding)
  408. return true;
  409. bool allUpdated = true;
  410. lock (_lockSelection)
  411. {
  412. foreach (var item in _lstTokenTimeData)
  413. {
  414. DateTime timeFrom = item.TimeToken;
  415. if (timeFrom >= item.DataLine.EndTime)
  416. continue;
  417. allUpdated = false;
  418. ChartAutoRange = AutoRange.Always;
  419. DateTime timeTo = timeFrom.AddMinutes(60);
  420. if (timeTo.DayOfYear > timeFrom.DayOfYear)
  421. timeTo = new DateTime(timeFrom.Year, timeFrom.Month, timeFrom.Day).AddDays(1);
  422. if (timeTo > item.DataLine.EndTime)
  423. timeTo = item.DataLine.EndTime;
  424. item.TimeToken = timeTo;
  425. GetData(item.DataList, timeFrom, timeTo, item.DataLine.Module, item.DataLine.DataSource);
  426. #region Update VisualMin、VisualMax、DataStatisticsInfo
  427. Application.Current.Dispatcher.BeginInvoke(new Action(() =>
  428. {
  429. double min = ((DoubleRange)VisibleRangeValue).Min;
  430. double max = ((DoubleRange)VisibleRangeValue).Max;
  431. int PointCount = 0;
  432. foreach (var selectedData in SelectedData)
  433. {
  434. var seriesItem = selectedData as ChartDataLine<T>;
  435. if (seriesItem == null)
  436. continue;
  437. double sumValue = 0;
  438. double minValue = 100000;
  439. double maxValue = 0;
  440. double averageValue = 0;
  441. double stdevValue = 0;
  442. double sigma3Value = 0;
  443. var pointValueList = seriesItem.Points.Select(x => x.Item2).ToList();
  444. for (int i = 0; i < pointValueList.Count; i++)
  445. {
  446. sumValue += pointValueList[i];
  447. if (pointValueList[i] < minValue)
  448. minValue = pointValueList[i];
  449. if (pointValueList[i] > maxValue)
  450. maxValue = pointValueList[i];
  451. }
  452. averageValue = pointValueList.Count==0?0: sumValue / pointValueList.Count;
  453. for (int i = 0; i < pointValueList.Count; i++)
  454. {
  455. stdevValue += Math.Pow(pointValueList[i] - averageValue, 2);
  456. }
  457. stdevValue = pointValueList.Count == 0 ? 0 : stdevValue / pointValueList.Count;
  458. sigma3Value = Math.Sqrt(stdevValue) * 3;
  459. (seriesItem.Tag as ParameterNode).AverageValue = averageValue.ToString("F2");
  460. (seriesItem.Tag as ParameterNode).MinValue = minValue.ToString("F2");
  461. (seriesItem.Tag as ParameterNode).MaxValue = maxValue.ToString("F2");
  462. (seriesItem.Tag as ParameterNode).Sigma3Value = sigma3Value.ToString("F2");
  463. min = minValue;
  464. if (maxValue != max)
  465. max = maxValue * 1.1;
  466. PointCount = pointValueList.Count;
  467. }
  468. VisibleRangeValue = new DoubleRange(min, max);
  469. }));
  470. #endregion
  471. }
  472. }
  473. if (allUpdated)
  474. {
  475. lock (_lockSelection)
  476. {
  477. while (_lstTokenTimeData.Count > 0)
  478. {
  479. _lstTokenTimeData.TryTake(out _);
  480. }
  481. ChartAutoRange = AutoRange.Never;
  482. }
  483. }
  484. }
  485. catch (Exception ex)
  486. {
  487. LOG.Error(ex.Message);
  488. }
  489. return true;
  490. }
  491. private void GetData(List<string> keys, DateTime from, DateTime to, string module, string dataSource)
  492. {
  493. string sql = "select time AS InternalTimeStamp";
  494. foreach (var dataKey in keys)
  495. {
  496. var dataId = _processDetailDisplayDic.ContainsKey(dataKey) ? _processDetailDisplayDic[dataKey] : dataKey;
  497. sql += "," + string.Format("\"{0}\"", dataId);
  498. }
  499. sql += string.Format(" from \"{0}\" where time > {1} and time <= {2} order by time asc",
  500. from.ToString("yyyyMMdd") + "." + module, from.Ticks, to.Ticks);
  501. DataTable dataTable = QueryDataClient.Instance.Service.QueryData(sql);
  502. Dictionary<string, List<HistoryDataItem>> historyData = new Dictionary<string, List<HistoryDataItem>>();
  503. if (dataTable == null || dataTable.Rows.Count == 0)
  504. return;
  505. DateTime dt = new DateTime();
  506. Dictionary<int, string> colName = new Dictionary<int, string>();
  507. for (int colNo = 0; colNo < dataTable.Columns.Count; colNo++)
  508. {
  509. colName.Add(colNo, dataTable.Columns[colNo].ColumnName);
  510. historyData[dataTable.Columns[colNo].ColumnName] = new List<HistoryDataItem>();
  511. }
  512. for (int rowNo = 0; rowNo < dataTable.Rows.Count; rowNo++)
  513. {
  514. var row = dataTable.Rows[rowNo];
  515. for (int i = 0; i < dataTable.Columns.Count; i++)
  516. {
  517. HistoryDataItem data = new HistoryDataItem();
  518. if (i == 0)
  519. {
  520. dt = new DateTime((long)row[i]);
  521. continue;
  522. }
  523. else
  524. {
  525. string dataId = colName[i];
  526. if (row[i] is DBNull || row[i] == null)
  527. {
  528. data.dateTime = dt;
  529. data.dbName = colName[i];
  530. data.value = 0;
  531. }
  532. else if (row[i] is bool)
  533. {
  534. data.dateTime = dt;
  535. data.dbName = colName[i];
  536. data.value = (bool)row[i] ? 1 : 0;
  537. }
  538. else
  539. {
  540. data.dateTime = dt;
  541. data.dbName = colName[i];
  542. var value = float.Parse(row[i].ToString()).ToString(_resolution);
  543. data.value = float.Parse(value);
  544. }
  545. }
  546. historyData[data.dbName].Add(data);
  547. }
  548. }
  549. Application.Current.Dispatcher.BeginInvoke(new Action(() =>
  550. {
  551. try
  552. {
  553. FillDataToChart(SelectedData,dataSource, historyData);
  554. FillDataToChart(SynSelectedData, dataSource, historyData);
  555. RefresCharView();
  556. }
  557. catch (Exception ex)
  558. {
  559. LOG.Write(ex);
  560. }
  561. }));
  562. }
  563. void FillDataToChart(ObservableCollection<IRenderableSeries> series,string dataSource, Dictionary<string, List<HistoryDataItem>> historyData)
  564. {
  565. var items = series.Where(x => (x as ChartDataLine<T>).DataSource == dataSource);
  566. if (items == null) return;
  567. foreach (var item in items)
  568. {
  569. var seriesItem = item as ChartDataLine<T>;
  570. if (seriesItem == null)
  571. continue;
  572. foreach (var data in historyData)
  573. {
  574. var dataKey = _processDetailDisplayDic.ContainsKey(seriesItem.DataName) ? _processDetailDisplayDic[seriesItem.DataName] : seriesItem.DataName;
  575. if (data.Key != dataKey)
  576. continue;
  577. seriesItem.Capacity += data.Value.Count;
  578. //DateTime beginTime = StepStartTime;
  579. for (int i = 0; i < data.Value.Count; i++)
  580. {
  581. var historyDataItem = data.Value[i];
  582. if (typeof(T) == typeof(double))
  583. {
  584. //xData = Math.Round((historyDataItem.dateTime - beginTime).TotalMilliseconds / 1000, 0, MidpointRounding.AwayFromZero);
  585. seriesItem.Append((T)Convert.ChangeType((historyDataItem.dateTime - StepStartTime).TotalMilliseconds / 1000d, typeof(T)), historyDataItem.value);
  586. }
  587. else
  588. seriesItem.Append((T)Convert.ChangeType(historyDataItem.dateTime, typeof(T)), historyDataItem.value);
  589. }
  590. }
  591. }
  592. }
  593. /// <summary>
  594. /// 获取时间列表
  595. /// </summary>
  596. /// <param name="from"></param>
  597. /// <param name="to"></param>
  598. /// <param name="module"></param>
  599. /// <param name="dataSource"></param>
  600. private void GetData(DateTime from, DateTime to, string module = "PM1")
  601. {
  602. string sql = "select time AS InternalTimeStamp,\"PM1.RecipeTotalElapseTime\"";
  603. sql += string.Format(" from \"{0}\" where time > {1} and time <= {2} order by time limt 1",
  604. from.ToString("yyyyMMdd") + "." + module, from.Ticks, to.Ticks);
  605. DataTable dataTable = QueryDataClient.Instance.Service.QueryData(sql);
  606. Dictionary<string, List<HistoryDataItem>> historyData = new Dictionary<string, List<HistoryDataItem>>();
  607. if (dataTable == null || dataTable.Rows.Count == 0)
  608. {
  609. return;
  610. }
  611. foreach (DataRow row in dataTable.Rows)
  612. {
  613. DateTime dt = new DateTime();
  614. dt = new DateTime((long)row[0]);
  615. var elapseTime = Convert.ToDouble(row[1]);
  616. }
  617. }
  618. private void SelectedDataChanged()
  619. {
  620. foreach (var item in SelectedData)
  621. {
  622. if (item.Stroke.Equals(System.Windows.Media.Color.FromArgb(255, 0, 0, 255)))
  623. {
  624. Color drawingColor = colorQueue.Peek();
  625. item.Stroke = System.Windows.Media.Color.FromRgb(drawingColor.R, drawingColor.G, drawingColor.B);
  626. colorQueue.Enqueue(colorQueue.Dequeue());
  627. }
  628. }
  629. }
  630. public void TxtMouseLeftButtonDown(ParameterNode node,ObservableCollection<IRenderableSeries> series)
  631. {
  632. node.Selected = !node.Selected;
  633. ParameterCheck(node,series);
  634. }
  635. public void ParameterCheck(ParameterNode node, ObservableCollection<IRenderableSeries> series)
  636. {
  637. bool result = RefreshTreeStatusToChild(node,series);
  638. if (!result)
  639. {
  640. node.Selected=!node.Selected;
  641. DialogBox.ShowWarning($"The max number of parameters is {MAX_PARAMETERS}.");
  642. }
  643. else
  644. RefreshTreeStatusToParent(node);
  645. }
  646. /// <summary>
  647. /// Refresh tree node status from current to children, and add data to SelectedData
  648. /// </summary>
  649. private bool RefreshTreeStatusToChild(ParameterNode node, ObservableCollection<IRenderableSeries> SelectedData)
  650. {
  651. if (node?.ChildNodes.Count > 0)
  652. {
  653. for (int i = 0; i < node.ChildNodes.Count; i++)
  654. {
  655. ParameterNode n = node.ChildNodes[i];
  656. n.Selected = node.Selected;
  657. if (!RefreshTreeStatusToChild(n, SelectedData))
  658. {
  659. n.Selected = !n.Selected;
  660. return false;
  661. }
  662. }
  663. }
  664. else //leaf node
  665. {
  666. bool lockTaken = false;
  667. try
  668. {
  669. var item = SelectedData?.FirstOrDefault(x => (x as ChartDataLine<T>).DataName == node.Name);
  670. bool isExist = item != null;
  671. if (isExist && !node.Selected)//删除
  672. {
  673. SelectedData.Remove(item);
  674. }
  675. else
  676. {
  677. if (!isExist && node.Selected)
  678. {
  679. if (SelectedData.Count >= MAX_PARAMETERS)
  680. {
  681. return false;
  682. }
  683. Monitor.TryEnter(_lockSelection, 1000, ref lockTaken);
  684. if (lockTaken)
  685. {
  686. var dataId = _processDetailDisplayDic.ContainsKey(node.Name) ? _processDetailDisplayDic[node.Name] : node.Name;
  687. var module = dataId.Split('.').ToList()[0];
  688. _isAdding = true;
  689. foreach (var recipe in RecipeDatas)
  690. {
  691. TimeChartDataLine line;
  692. if (IsStepVisiable)
  693. line = new TimeChartDataLine(node.Name.Replace("Tube", "PM1"), module, StepStartTime, StepEndTime);
  694. else
  695. line = new TimeChartDataLine(node.Name.Replace("Tube", "PM1"), module, recipe.StartTime, recipe.EndTime);
  696. line.DataSource = $"{recipe.BatchID}.{recipe.RecipeName}";
  697. line.Tag = node;
  698. if (isExist)
  699. {
  700. line.Stroke = item.Stroke;
  701. line.IsVisible = item.IsVisible;
  702. line.ClearData();
  703. SelectedData.Remove(item);
  704. }
  705. else
  706. SelectedData.Add(line);
  707. AddToMonitor(line);
  708. }
  709. _isAdding = false;
  710. if (!isExist)
  711. SelectedDataChanged();
  712. }
  713. }
  714. }
  715. }
  716. finally
  717. {
  718. if (lockTaken)
  719. {
  720. Monitor.Exit(_lockSelection);
  721. }
  722. }
  723. }
  724. return true;
  725. }
  726. private void AddToMonitor(TimeChartDataLine line)
  727. {
  728. QueryIndexer indexer = new QueryIndexer()
  729. {
  730. DataLine = line,
  731. DataList = new List<string>() { line.DataName },
  732. TimeToken = line.StartTime,
  733. };
  734. _lstTokenTimeData.Add(indexer);
  735. }
  736. /// <summary>
  737. /// Refresh tree node status from current to parent
  738. /// </summary>
  739. /// <param name="node"></param>
  740. /// <returns></returns>
  741. private void RefreshTreeStatusToParent(ParameterNode node)
  742. {
  743. if (node.ParentNode != null)
  744. {
  745. if (node.Selected)
  746. {
  747. bool flag = true;
  748. for (int i = 0; i < node.ParentNode.ChildNodes.Count; i++)
  749. {
  750. if (!node.ParentNode.ChildNodes[i].Selected)
  751. {
  752. flag = false; //as least one child is unselected
  753. break;
  754. }
  755. }
  756. if (flag)
  757. node.ParentNode.Selected = true;
  758. }
  759. else
  760. {
  761. node.ParentNode.Selected = false;
  762. }
  763. RefreshTreeStatusToParent(node.ParentNode);
  764. }
  765. }
  766. #region Parameter Grid Control
  767. public void DeleteAll()
  768. {
  769. //uncheck all tree nodes
  770. foreach (ChartDataLine<T> cp in SelectedData)
  771. {
  772. (cp.Tag as ParameterNode).Selected = false;
  773. RefreshTreeStatusToParent(cp.Tag as ParameterNode);
  774. }
  775. SelectedData.Clear();
  776. }
  777. private void SetParameterNode(ObservableCollection<ParameterNode> nodes, bool isChecked)
  778. {
  779. foreach (ParameterNode n in nodes)
  780. {
  781. n.Selected = isChecked;
  782. SetParameterNode(n.ChildNodes, isChecked);
  783. }
  784. }
  785. public void Delete(ChartDataLine<T> cp)
  786. {
  787. if (cp != null)
  788. {
  789. if (SelectedData.Contains(cp))
  790. {
  791. //uncheck tree node
  792. (cp.Tag as ParameterNode).Selected = false;
  793. RefreshTreeStatusToParent(cp.Tag as ParameterNode);
  794. SelectedData.Remove(cp);
  795. }
  796. }
  797. }
  798. public async void ExportAll()
  799. {
  800. try
  801. {
  802. if (SelectedData.Count == 0)
  803. {
  804. MessageBox.Show($"Please select the data you want to export.", "Export", MessageBoxButton.OK,
  805. MessageBoxImage.Warning);
  806. return;
  807. }
  808. Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
  809. dlg.DefaultExt = ".xlsx"; // Default file extension
  810. dlg.Filter = "Excel数据表格文件(*.xlsx)|*.xlsx"; // Filter files by extension
  811. dlg.FileName = $"{DisplayName}_{string.Join(",", RecipeDatas.Select(x => x.RecipeName.Replace('\\','.')).ToArray())}_{DateTime.Now:yyyyMMdd_HHmmss}";
  812. Nullable<bool> result = dlg.ShowDialog();// Show open file dialog box
  813. if (result == true) // Process open file dialog box results
  814. {
  815. Exporting?.Invoke(this, System.EventArgs.Empty);
  816. _cancellationTokenSource = new CancellationTokenSource();
  817. var sw = new Stopwatch();
  818. sw.Restart();
  819. System.Data.DataSet ds = new System.Data.DataSet();
  820. ds.Tables.Add(new System.Data.DataTable($"{DisplayName}_{DateTime.Now:yyyyMMdd_HHmmss}"));
  821. ds.Tables[0].Columns.Add("Recipe Info");
  822. ds.Tables[0].Columns[0].DataType = typeof(string);
  823. ds.Tables[0].Columns.Add("Date");
  824. ds.Tables[0].Columns[1].DataType = typeof(string);
  825. ds.Tables[0].Columns.Add("Time");
  826. ds.Tables[0].Columns[2].DataType = typeof(string);
  827. ds.Tables[0].Columns.Add("Step ID");
  828. ds.Tables[0].Columns[3].DataType = typeof(string);
  829. ds.Tables[0].Columns.Add("Step Name");
  830. ds.Tables[0].Columns[4].DataType = typeof(string);
  831. Dictionary<T, double[]> timeValue = new Dictionary<T, double[]>();
  832. await Task.Run(() =>
  833. {
  834. for (int i = 0; i < SelectedData.Count; i++)
  835. {
  836. List<Tuple<T, double>> data = (SelectedData[i] as ChartDataLine<T>).Points;
  837. foreach (var tuple in data)
  838. {
  839. if (!timeValue.ContainsKey(tuple.Item1))
  840. timeValue[tuple.Item1] = new double[SelectedData.Count];
  841. timeValue[tuple.Item1][i] = tuple.Item2;
  842. }
  843. ds.Tables[0].Columns.Add((SelectedData[i] as ChartDataLine<T>).DataName);
  844. ds.Tables[0].Columns[i + 5].DataType = typeof(double);
  845. }
  846. var recipeInfoRow = ds.Tables[0].NewRow();
  847. recipeInfoRow[0] = $"Recipe:{string.Join(",", RecipeDatas.Select(x => x.RecipeName).ToArray())}";
  848. ds.Tables[0].Rows.Add(recipeInfoRow);
  849. recipeInfoRow = ds.Tables[0].NewRow();
  850. recipeInfoRow[0] = $"Start Time";
  851. recipeInfoRow[1] = string.Join(",", RecipeDatas.Select(x => x.StartTime).ToArray());
  852. recipeInfoRow[2] = string.Join(",", RecipeDatas.Select(x => x.StartTime).ToArray());
  853. ds.Tables[0].Rows.Add(recipeInfoRow);
  854. recipeInfoRow = ds.Tables[0].NewRow();
  855. recipeInfoRow[0] = $"End Time";
  856. recipeInfoRow[1] = string.Join(",", RecipeDatas.Select(x => x.EndTime).ToArray());
  857. recipeInfoRow[2] = string.Join(",", RecipeDatas.Select(x => x.EndTime).ToArray());
  858. ds.Tables[0].Rows.Add(recipeInfoRow);
  859. QueryDataClientFromTable(ds);
  860. }, _cancellationTokenSource.Token).ContinueWith(t =>
  861. {
  862. if (t.IsCanceled || t.IsFaulted)
  863. return;
  864. });
  865. if (_cancellationTokenSource?.Token.IsCancellationRequested == true)
  866. return;
  867. if (!ExcelHelper.ExportToExcel(dlg.FileName, ds, out string reason))
  868. {
  869. MessageBox.Show($"Export failed, {reason}", "Export", MessageBoxButton.OK, MessageBoxImage.Warning);
  870. return;
  871. }
  872. MessageBox.Show($"Export succeed, file save as {dlg.FileName}", "Export", MessageBoxButton.OK, MessageBoxImage.Information);
  873. }
  874. }
  875. catch (Exception ex)
  876. {
  877. LOG.Write(ex);
  878. MessageBox.Show("Write failed," + ex.Message, "export failed", MessageBoxButton.OK, MessageBoxImage.Warning);
  879. }
  880. }
  881. public void DGSelectionChanged(object sender, EventArgs e)
  882. {
  883. var temp = (DataGrid)sender;
  884. var tempSelected = (IRenderableSeries)temp.SelectedItem;
  885. tempSelected.IsVisible = !tempSelected.IsVisible;
  886. }
  887. private void QueryDataClientFromTable(DataSet ds)
  888. {
  889. DateTime startTime = StepStartTime;
  890. DateTime endTime = StepEndTime;
  891. if (StepStartTime.Date == StepEndTime.Date)
  892. {
  893. SaveDataToTable(ds, startTime, endTime);
  894. }
  895. else
  896. {
  897. endTime = new DateTime(startTime.Year, startTime.Month, startTime.Day, 23, 59, 59);
  898. while (endTime < StepEndTime)
  899. {
  900. SaveDataToTable(ds, startTime, endTime);
  901. startTime = new DateTime(startTime.Year, startTime.Month, startTime.Day, 0, 0, 0).AddDays(1);
  902. endTime = new DateTime(startTime.Year, startTime.Month, startTime.Day, 23, 59, 59);
  903. }
  904. SaveDataToTable(ds, startTime, StepEndTime);
  905. }
  906. }
  907. private void SaveDataToTable(DataSet ds, DateTime startTime, DateTime endTime)
  908. {
  909. string stepID = "", stepName = "";
  910. string pmsql = "select time AS InternalTimeStamp";
  911. var pmColNames = _realtimeProvider.GetDBCol(startTime.ToString("yyyyMMdd") + "." + "PM1");
  912. List<string> keys = SelectedData.Select(x => (x as ChartDataLine<T>).DataName).ToList();
  913. DataTable pmDataTable = null;
  914. if (keys != null && keys.Count > 0)
  915. {
  916. List<string> pmList = new List<string>();
  917. foreach (var dataKey in keys)
  918. {
  919. var dataId = _processDetailDisplayDic.ContainsKey(dataKey) ? _processDetailDisplayDic[dataKey] : dataKey;
  920. var module = dataId.Split('.').ToList()[0];
  921. if (module.ToLower() == "pm1")
  922. {
  923. if (!pmColNames.Contains(dataId))
  924. {
  925. continue;
  926. }
  927. pmList.Add(dataId);
  928. pmsql += "," + string.Format("\"{0}\"", dataId);
  929. }
  930. }
  931. if (pmList.Count > 0)
  932. {
  933. pmsql += string.Format(" from \"{0}\" where time > {1} and time <= {2} order by time asc",
  934. startTime.ToString("yyyyMMdd") + "." + "PM1", startTime.Ticks, endTime.Ticks);
  935. pmDataTable = QueryDataClient.Instance.Service.QueryData(pmsql);
  936. if (pmDataTable == null)
  937. {
  938. MessageBox.Show($"Export failed, sql:{pmsql}", "Export", MessageBoxButton.OK, MessageBoxImage.Warning);
  939. return;
  940. }
  941. }
  942. }
  943. string systemsql = "select time AS InternalTimeStamp";
  944. var systemColNames = _realtimeProvider.GetDBCol(startTime.ToString("yyyyMMdd") + "." + "System");
  945. DataTable systemDataTable = null;
  946. if (keys != null && keys.Count > 0)
  947. {
  948. List<string> systemList = new List<string>();
  949. foreach (var dataKey in keys)
  950. {
  951. var dataId = _processDetailDisplayDic.ContainsKey(dataKey) ? _processDetailDisplayDic[dataKey] : dataKey;
  952. var module = dataId.Split('.').ToList()[0];
  953. if (module.ToLower() == "system")
  954. {
  955. if (!systemColNames.Contains(dataId))
  956. {
  957. continue;
  958. }
  959. systemsql += "," + string.Format("\"{0}\"", dataId);
  960. systemList.Add(dataId);
  961. }
  962. }
  963. if (systemList.Count > 0)
  964. {
  965. systemsql += string.Format(" from \"{0}\" where time > {1} and time <= {2} order by time asc",
  966. startTime.ToString("yyyyMMdd") + "." + "System", startTime.Ticks, endTime.Ticks);
  967. systemDataTable = QueryDataClient.Instance.Service.QueryData(systemsql);
  968. if (systemDataTable == null)
  969. {
  970. MessageBox.Show($"Export failed, sql:{systemsql}", "Export", MessageBoxButton.OK, MessageBoxImage.Warning);
  971. return;
  972. }
  973. }
  974. }
  975. int maxRow = 0;
  976. if ((pmDataTable == null || pmDataTable.Rows.Count == 0))
  977. {
  978. if (systemDataTable != null)
  979. {
  980. maxRow = systemDataTable.Rows.Count;
  981. }
  982. }
  983. else if ((systemDataTable == null || systemDataTable.Rows.Count == 0))
  984. {
  985. if (pmDataTable != null)
  986. {
  987. maxRow = pmDataTable.Rows.Count;
  988. }
  989. }
  990. else
  991. {
  992. maxRow = pmDataTable.Rows.Count > systemDataTable.Rows.Count ? systemDataTable.Rows.Count : pmDataTable.Rows.Count;
  993. }
  994. for (int i = 0; i < maxRow; i++)
  995. {
  996. var pmRow = pmDataTable != null ? pmDataTable.Rows[i] : null;
  997. var systemRow = systemDataTable != null ? systemDataTable.Rows[i] : null;
  998. //float timeIndex = 0;
  999. //float.TryParse(item.Key.ToString(), out timeIndex);
  1000. //DateTime dateTimeKey = startTime.AddMilliseconds(timeIndex * 1000); ;// DateTime.Parse(item.Key.ToString());
  1001. DateTime dateTimeKey = new DateTime(pmRow != null ? (long)pmRow[0] : (long)systemRow[0]);
  1002. var tempStepInfo = _stepInfo.FirstOrDefault(x => x.StartTime < dateTimeKey && x.EndTime > dateTimeKey);
  1003. if (tempStepInfo != null)// && dateTimeKey > _stepInfo[iIndex].StartTime
  1004. {
  1005. stepID = tempStepInfo.StepNo;
  1006. stepName = tempStepInfo.StepName;
  1007. }
  1008. var row = ds.Tables[0].NewRow();
  1009. row[1] = dateTimeKey.ToString("yyyy/MM/dd");
  1010. row[2] = dateTimeKey.ToString("HH:mm:ss:fff");
  1011. row[3] = stepID;
  1012. row[4] = stepName;
  1013. for (int j = 5; j < ds.Tables[0].Columns.Count; j++)
  1014. {
  1015. string colName = ds.Tables[0].Columns[j].ColumnName;
  1016. DataRow dataRow = null;
  1017. var dataId = _processDetailDisplayDic.ContainsKey(colName) ? _processDetailDisplayDic[colName] : colName;
  1018. var module = dataId.Split('.').ToList()[0];
  1019. int index = 0;
  1020. if (module == "PM1")
  1021. {
  1022. dataRow = pmRow;
  1023. foreach (var item in pmDataTable.Columns)
  1024. {
  1025. if (((DataColumn)item).ColumnName == dataId)
  1026. {
  1027. break;
  1028. }
  1029. index++;
  1030. }
  1031. }
  1032. else if (module == "System")
  1033. {
  1034. dataRow = systemRow;
  1035. foreach (var item in systemDataTable.Columns)
  1036. {
  1037. if (((DataColumn)item).ColumnName == dataId)
  1038. {
  1039. break;
  1040. }
  1041. index++;
  1042. }
  1043. }
  1044. if (dataRow[index] is DBNull || dataRow[index] == null)
  1045. {
  1046. row[j] = dataRow[index];
  1047. }
  1048. else if (dataRow[index] is bool)
  1049. {
  1050. row[j] = (bool)dataRow[index] ? 1 : 0;
  1051. }
  1052. else
  1053. {
  1054. var value = float.Parse(dataRow[index].ToString()).ToString(_resolution);
  1055. row[j] = float.Parse(value);
  1056. }
  1057. }
  1058. ds.Tables[0].Rows.Add(row);
  1059. }
  1060. }
  1061. public void Export(ChartDataLine<T> cp)
  1062. {
  1063. try
  1064. {
  1065. Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
  1066. dlg.DefaultExt = ".xlsx"; // Default file extension
  1067. dlg.Filter = "Excel数据表格文件(*.xlsx)|*.xlsx"; // Filter files by extension
  1068. dlg.FileName = $"{cp.DataName}_{DateTime.Now:yyyyMMdd_HHmmss}";
  1069. Nullable<bool> result = dlg.ShowDialog();// Show open file dialog box
  1070. if (result == true) // Process open file dialog box results
  1071. {
  1072. System.Data.DataSet ds = new System.Data.DataSet();
  1073. ds.Tables.Add(new System.Data.DataTable(cp.DataName));
  1074. ds.Tables[0].Columns.Add("Time");
  1075. ds.Tables[0].Columns[0].DataType = typeof(DateTime);
  1076. ds.Tables[0].Columns.Add(cp.DataName);
  1077. ds.Tables[0].Columns[1].DataType = typeof(double);
  1078. int i = 0;
  1079. foreach (var item in cp.Points)
  1080. {
  1081. var row = ds.Tables[0].NewRow();
  1082. row[0] =StepStartTime.AddSeconds(Convert.ToDouble(item.Item1));
  1083. row[1] = item.Item2;
  1084. ds.Tables[0].Rows.Add(row);
  1085. i++;
  1086. }
  1087. if (!ExcelHelper.ExportToExcel(dlg.FileName, ds, out string reason))
  1088. {
  1089. MessageBox.Show($"Export failed, {reason}", "Export", MessageBoxButton.OK, MessageBoxImage.Warning);
  1090. return;
  1091. }
  1092. MessageBox.Show($"Export succeed, file save as {dlg.FileName}", "Export", MessageBoxButton.OK, MessageBoxImage.Information);
  1093. }
  1094. }
  1095. catch (Exception ex)
  1096. {
  1097. LOG.Write(ex);
  1098. MessageBox.Show("Write failed," + ex.Message, "export failed", MessageBoxButton.OK, MessageBoxImage.Warning);
  1099. }
  1100. }
  1101. public void SelectColor(ChartDataLine<T> cp)
  1102. {
  1103. if (cp == null)
  1104. return;
  1105. var dlg = new System.Windows.Forms.ColorDialog();
  1106. if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  1107. {
  1108. cp.Stroke = new System.Windows.Media.Color() { A = dlg.Color.A, B = dlg.Color.B, G = dlg.Color.G, R = dlg.Color.R };
  1109. }
  1110. }
  1111. public void CloseCmd()
  1112. {
  1113. ((Window)GetView()).Close();
  1114. }
  1115. #endregion
  1116. #region SciChart Move
  1117. public void RefresCharView()
  1118. {
  1119. this.view.sciChart.ZoomExtents();
  1120. }
  1121. public void ArrowClick(string direction)
  1122. {
  1123. switch (direction)
  1124. {
  1125. case "Up":
  1126. if (VisibleRangeValue is DoubleRange uprange)
  1127. {
  1128. VisibleRangeValue = new DoubleRange(uprange.Min + YScale, uprange.Max + YScale);
  1129. }
  1130. break;
  1131. case "Down":
  1132. if (VisibleRangeValue is DoubleRange downrange)
  1133. {
  1134. VisibleRangeValue = new DoubleRange(downrange.Min - YScale, downrange.Max - YScale);
  1135. }
  1136. break;
  1137. case "Left":
  1138. if (VisibleRangeTime is DoubleRange leftrange)
  1139. {
  1140. VisibleRangeTime = new DoubleRange(leftrange.Min - XScale, leftrange.Max - XScale);
  1141. }
  1142. break;
  1143. case "Right":
  1144. if (VisibleRangeTime is DoubleRange range)
  1145. {
  1146. VisibleRangeTime = new DoubleRange(range.Min + XScale, range.Max + XScale);
  1147. }
  1148. break;
  1149. }
  1150. }
  1151. public void ZoomInClick()//放大
  1152. {
  1153. double scale = 0;
  1154. if ((_directionByte & 0x01) == 1)//x
  1155. {
  1156. //LOG.Info($"A ZoomInClick.X:scale{XScale}-{VisibleRangeTime.Min}: {VisibleRangeTime.Max}");
  1157. if (VisibleRangeTime is DoubleRange range)
  1158. {
  1159. if (range.Max - range.Min <= XScale) return;
  1160. if (range.Max - range.Min > 2 * XScale) scale = XScale;
  1161. else scale = XScale / 2;
  1162. VisibleRangeTime = new DoubleRange(range.Min + scale, range.Max - scale);
  1163. //LOG.Info($"ZoomInClick.X:scale{XScale}-{VisibleRangeTime.Min}: {VisibleRangeTime.Max}");
  1164. }
  1165. }
  1166. if ((_directionByte & 0x02) == 2)//y
  1167. {
  1168. //LOG.Info($"A ZoomInClick.Y:scale{YScale}-{VisibleRangeValue.Min}: {VisibleRangeValue.Max}");
  1169. if (VisibleRangeValue is DoubleRange range)
  1170. {
  1171. if (range.Max - range.Min <= YScale) return;
  1172. if (range.Max - range.Min > 2 * XScale) scale = YScale;
  1173. else scale = YScale / 2;
  1174. VisibleRangeValue = new DoubleRange(range.Min + scale, range.Max - scale);
  1175. //LOG.Info($"ZoomInClick.Y:scale{YScale}-{VisibleRangeValue.Min}: {VisibleRangeValue.Max}");
  1176. }
  1177. }
  1178. }
  1179. public void ZoomOutClick()//缩小
  1180. {
  1181. if ((_directionByte & 0x01) == 1)//x
  1182. {
  1183. //LOG.Info($"A ZoomOutClick.X:scale{XScale}-{VisibleRangeTime.Min}: {VisibleRangeTime.Max}");
  1184. if (VisibleRangeTime is DoubleRange range)
  1185. {
  1186. VisibleRangeTime = new DoubleRange(range.Min - XScale, range.Max + XScale);
  1187. //LOG.Info($"ZoomOutClick.X:scale{XScale}-{VisibleRangeTime.Min}: {VisibleRangeTime.Max}");
  1188. }
  1189. }
  1190. if ((_directionByte & 0x02) == 2)//y
  1191. {
  1192. //LOG.Info($"A ZoomOutClick.Y:scale{YScale}-{VisibleRangeValue.Min}: {VisibleRangeValue.Max}");
  1193. if (VisibleRangeValue is DoubleRange range)
  1194. {
  1195. VisibleRangeValue = new DoubleRange(range.Min - YScale, range.Max + YScale);
  1196. // LOG.Info($"ZoomOutClick.Y:scale{YScale}-{VisibleRangeValue.Min}: {VisibleRangeValue.Max}");
  1197. }
  1198. }
  1199. }
  1200. public void SelectStep()
  1201. {
  1202. ObservableCollection<Option> Items = new ObservableCollection<Option>();
  1203. foreach (var item in StepInfo)
  1204. {
  1205. Items.Add(new Option() { Name = $"{item.StepNo} {item.StepName}", Index = int.Parse(item.StepNo) });
  1206. }
  1207. ItemsSelectDialogViewModel itemsSelectDialogView = new ItemsSelectDialogViewModel();
  1208. itemsSelectDialogView.Items = Items;
  1209. itemsSelectDialogView.ColumnsCount = Items.Count / 10 + 1;
  1210. WindowManager windowManager = IoC.Get<IWindowManager>() as WindowManager;
  1211. if ((bool)(windowManager as WindowManager)?.ShowDialogWithNoWindow(itemsSelectDialogView))
  1212. {
  1213. var startItem = StepInfo.FirstOrDefault(step => step.StepNo == Items.FirstOrDefault(x => x.IsChecked)?.Index.ToString());
  1214. var endItem = StepInfo.FirstOrDefault(step => step.StepNo == Items.LastOrDefault(x => x.IsChecked)?.Index.ToString());
  1215. if (startItem == null)
  1216. {
  1217. DialogBox.ShowError("Please select begin step!");
  1218. return;
  1219. }
  1220. if (endItem == null)
  1221. {
  1222. DialogBox.ShowError("Please select end step!");
  1223. return;
  1224. }
  1225. if (Items.Where(x => x.IsChecked).Count() > 2)
  1226. {
  1227. DialogBox.ShowError("The selected step numbers must be 1 or 2.");
  1228. return;
  1229. }
  1230. StepStartTime = startItem.StartTime;
  1231. StepEndTime = endItem.EndTime;
  1232. List<ParameterNode> allCheckNode = new List<ParameterNode>();
  1233. ParameterNodes.ToList().ForEach(x =>
  1234. {
  1235. allCheckNode.AddRange(GetAllCheckedLeafNode(x));
  1236. });
  1237. allCheckNode.ForEach(x => { ParameterCheck(x,SelectedData); });
  1238. }
  1239. }
  1240. private List<ParameterNode> GetAllCheckedLeafNode(ParameterNode parameterNode)
  1241. {
  1242. List<ParameterNode> retNodes = new List<ParameterNode>();
  1243. if (parameterNode.ChildNodes != null && parameterNode.ChildNodes.Count > 0)
  1244. {
  1245. foreach (var item in parameterNode.ChildNodes)
  1246. {
  1247. retNodes.AddRange(GetAllCheckedLeafNode(item));
  1248. }
  1249. }
  1250. else
  1251. {
  1252. if (parameterNode.Selected)
  1253. retNodes.Add(parameterNode);
  1254. }
  1255. return retNodes;
  1256. }
  1257. #endregion
  1258. #endregion
  1259. public void GetCursorPoint(IEnumerable<SeriesInfo> infos)
  1260. {
  1261. if (infos.Any())
  1262. {
  1263. var seriesinfo = infos.First();
  1264. if (seriesinfo.XValue is double xvalue && _lastPointX != xvalue)
  1265. {
  1266. _lastPointX = xvalue;
  1267. PointTime = StepStartTime.AddSeconds(xvalue);
  1268. if (PointTime == DateTime.MinValue) return;
  1269. var step = _stepInfo.Find(r => r.StartTime <= PointTime && r.EndTime >= PointTime);
  1270. if (step == null)
  1271. {
  1272. LOG.Info($"couldn't find CurrentTime:{PointTime:yyyy/MM/dd HH:mm:ss.FFF} in steps");
  1273. }
  1274. else
  1275. {
  1276. StepNameDisPlay = $"{step.StepNo}:{step.StepName}";
  1277. LOG.Info($"CurrentTime:{PointTime:yyyy/MM/dd HH:mm:ss.FFF} {StepNameDisPlay}");
  1278. }
  1279. var name = (seriesinfo.RenderableSeries as ChartDataLine<T>).DataSource.Split('.','\\');
  1280. if (name.Length>0)
  1281. {
  1282. RecipeNameDisPlay = name.Last();
  1283. }
  1284. else RecipeNameDisPlay = string.Empty;
  1285. }
  1286. }
  1287. }
  1288. public void ScaleChange(string direction, double factor)
  1289. {
  1290. double t = 0;
  1291. if (direction == "X")
  1292. {
  1293. t = Math.Floor(_xScale * factor * 10) / 10d;//只保留3位小数
  1294. if (t >= 0.1) XScale = t;
  1295. else DialogBox.ShowWarning("already min value");
  1296. }
  1297. else if (direction == "Y")
  1298. {
  1299. t = Math.Floor(_yScale * factor * 1000) / 1000d;
  1300. if (t >= 0.001) YScale = t;
  1301. else DialogBox.ShowWarning("already min value");
  1302. }
  1303. }
  1304. }
  1305. public class ProcessDataLot : NotifiableItem
  1306. {
  1307. private string _guid;
  1308. public string GUID
  1309. {
  1310. get => _guid;
  1311. set
  1312. {
  1313. _guid = value;
  1314. InvokePropertyChanged(nameof(GUID));
  1315. }
  1316. }
  1317. private DateTime _processBeginTime;
  1318. public DateTime ProcessBeginTime
  1319. {
  1320. get => _processBeginTime;
  1321. set
  1322. {
  1323. _processBeginTime = value;
  1324. InvokePropertyChanged(nameof(ProcessBeginTime));
  1325. }
  1326. }
  1327. private DateTime _processEndTime;
  1328. public DateTime ProcessEndTime
  1329. {
  1330. get => _processEndTime;
  1331. set
  1332. {
  1333. _processEndTime = value;
  1334. InvokePropertyChanged(nameof(ProcessEndTime));
  1335. }
  1336. }
  1337. private string _recipeName;
  1338. public string RecipeName
  1339. {
  1340. get => _recipeName;
  1341. set
  1342. {
  1343. _recipeName = value;
  1344. InvokePropertyChanged(nameof(RecipeName));
  1345. }
  1346. }
  1347. private string _processStatus;
  1348. public string ProcessStatus
  1349. {
  1350. get => _processStatus;
  1351. set
  1352. {
  1353. _processStatus = value;
  1354. InvokePropertyChanged(nameof(ProcessStatus));
  1355. }
  1356. }
  1357. private string _waferDataguid;
  1358. public string WaferDataGUID
  1359. {
  1360. get => _waferDataguid;
  1361. set
  1362. {
  1363. _waferDataguid = value;
  1364. InvokePropertyChanged(nameof(WaferDataGUID));
  1365. }
  1366. }
  1367. private string _processIn;
  1368. public string ProcessIn
  1369. {
  1370. get => _processIn;
  1371. set
  1372. {
  1373. _processIn = value;
  1374. InvokePropertyChanged(nameof(ProcessIn));
  1375. }
  1376. }
  1377. private int _recipeSettingTime;
  1378. public int RecipeSettingTime
  1379. {
  1380. get => _recipeSettingTime;
  1381. set
  1382. {
  1383. _recipeSettingTime = value;
  1384. InvokePropertyChanged(nameof(RecipeSettingTime));
  1385. }
  1386. }
  1387. }
  1388. }