ProcessDetailViewModel.cs 64 KB

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