123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677 |
- using System;
- using System.Collections.Concurrent;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.Data;
- using System.Linq;
- using System.Windows;
- using Aitex.Core.RT.Log;
- using Aitex.Core.UI.ControlDataContext;
- using Aitex.Core.Util;
- using MECF.Framework.Common.CommonData;
- using MECF.Framework.Common.ControlDataContext;
- using MECF.Framework.Common.DataCenter;
- using MECF.Framework.Common.Utilities;
- using OpenSEMI.ClientBase;
- using SciChart.Charting.Visuals.Axes;
- using SciChart.Charting.Visuals.RenderableSeries;
- using SciChart.Data.Model;
- using VirgoUI.Client.Models.History.ProcessHistory;
- using VirgoUI.Client.Models.Operate.RealTime;
- using VirgoUI.Client.Models.Sys;
- namespace VirgoUI.Client.Models.History.DataHistory
- {
- public class TimeChartDataLine : ChartDataLine
- {
- public string Module { get; set; }
- public DateTime StartTime { get; set; }
- public DateTime EndTime { get; set; }
- public DateTime TokenTime { get; set; }
- private string[] _dbModules = {"PM2", "PM4", "PM6"};
- public TimeChartDataLine(string dataName, DateTime startTime, DateTime endTime) : base(dataName)
- {
- StartTime = startTime;
- EndTime = endTime;
- TokenTime = startTime;
- Module = "System";
- foreach (var dbModule in _dbModules)
- {
- if (dataName.StartsWith(dbModule))
- {
- Module = dbModule;
- break;
- }
- }
- }
- }
-
- public class DataViewModel : UiViewModelBase
- {
- private int MenuPermission;
- private class QueryIndexer
- {
- public DateTime TimeToken { get; set; }
- public List<string> DataList { get; set; }
- public string Module { get; set; }
- }
- #region Property
- private const int MAX_PARAMETERS = 20;
- private ObservableCollection<ParameterNode> _ParameterNodes;
- public ObservableCollection<ParameterNode> ParameterNodes
- {
- get { return _ParameterNodes; }
- set { _ParameterNodes = value; NotifyOfPropertyChange("ParameterNodes"); }
- }
- public ObservableCollection<IRenderableSeries> SelectedData { get; set; }
- private object _lockSelection = new object();
- private object _lockQueryCondition = new object();
- private AutoRange _autoRange;
- public AutoRange ChartAutoRange
- {
- get { return _autoRange; }
- set
- {
- _autoRange = value;
- NotifyOfPropertyChange(nameof(ChartAutoRange));
- }
- }
-
- private IRange _timeRange;
- public IRange VisibleRangeTime
- {
- get { return _timeRange; }
- set
- {
- _timeRange = value;
- NotifyOfPropertyChange(nameof(VisibleRangeTime));
- }
- }
- private IRange _VisibleRangeValue;
- public IRange VisibleRangeValue
- {
- get { return _VisibleRangeValue; }
- set { _VisibleRangeValue = value; NotifyOfPropertyChange(nameof(VisibleRangeValue)); }
- }
- public DateTime StartDateTime { get; set; }
- public DateTime EndDateTime { get; set; }
- public DateTime _queryDateTimeToken;
- //private bool _restart;
- private PeriodicJob _thread;
- ConcurrentBag<QueryIndexer> _lstTokenTimeData = new ConcurrentBag<QueryIndexer>();
- RealtimeProvider _provider = new RealtimeProvider();
- #endregion
- #region Function
- public DataViewModel()
- {
- MenuPermission = ClientApp.Instance.GetPermission("DataHistory");
- DisplayName = "Data History";
- SelectedData = new ObservableCollection<IRenderableSeries>();
- ParameterNodes = _provider.GetParameters();
- var now = DateTime.Now;
- StartDateTime = now.AddHours(-2);
- EndDateTime = now;
- _queryDateTimeToken = StartDateTime;
- VisibleRangeTime = new DateRange(DateTime.Now.AddMinutes(60), DateTime.Now.AddMinutes(-60));
- VisibleRangeValue = new DoubleRange(0, 10);
- _thread = new PeriodicJob(500, MonitorData, "RealTime", true);
- }
- protected override void OnActivate()
- {
- base.OnActivate();
- }
- protected bool MonitorData()
- {
- try
- {
- bool allUpdated = true;
- lock (_lockSelection)
- {
- foreach (var item in _lstTokenTimeData)
- {
- DateTime timeFrom = item.TimeToken;
- if (timeFrom >= EndDateTime)
- continue;
- allUpdated = false;
- Application.Current.Dispatcher.BeginInvoke(new Action(() => { ChartAutoRange = AutoRange.Always; }));
- DateTime timeTo = timeFrom.AddMinutes(60);
- if (timeTo > EndDateTime)
- timeTo = EndDateTime;
- item.TimeToken = timeTo;
- GetData(item.DataList, timeFrom, timeTo, item.Module);
-
- }
- }
- if (allUpdated)
- {
- lock (_lockSelection)
- {
- while (_lstTokenTimeData.Count > 0)
- {
- _lstTokenTimeData.TryTake(out _);
- }
- }
- Application.Current.Dispatcher.BeginInvoke(new Action(() => { ChartAutoRange = AutoRange.Never; }));
- }
- }
- catch (Exception ex)
- {
- LOG.Error(ex.Message);
- }
- return true;
- }
-
- public void Preset()
- {
- }
- public void Clear()
- {
- }
- private void GetData(List<string> keys, DateTime from, DateTime to, string module)
- {
- string sql = "select time AS InternalTimeStamp";
- foreach (var dataId in keys)
- {
- sql += "," + string.Format("\"{0}\"", dataId);
- }
- sql += string.Format(" from \"{0}\" where time > {1} and time <= {2} order by time asc",
- from.ToString("yyyyMMdd") + "." + "Data", from.Ticks, to.Ticks);
- DataTable dataTable = QueryDataClient.Instance.Service.QueryData(sql);
- Dictionary<string, List<HistoryDataItem>> historyData = new Dictionary<string, List<HistoryDataItem>>();
- if (dataTable == null || dataTable.Rows.Count == 0)
- return;
- DateTime dt = new DateTime();
- Dictionary<int, string> colName = new Dictionary<int, string>();
- for (int colNo = 0; colNo < dataTable.Columns.Count; colNo++)
- {
- colName.Add(colNo, dataTable.Columns[colNo].ColumnName);
- historyData[dataTable.Columns[colNo].ColumnName] = new List<HistoryDataItem>();
- }
- for (int rowNo = 0; rowNo < dataTable.Rows.Count; rowNo++)
- {
- var row = dataTable.Rows[rowNo];
- for (int i = 0; i < dataTable.Columns.Count; i++)
- {
- HistoryDataItem data = new HistoryDataItem();
- if (i == 0)
- {
- long ticks = (long)row[i];
- dt = new DateTime(ticks);
- continue;
- }
- else
- {
- string dataId = colName[i];
- if (row[i] is DBNull || row[i] == null)
- {
- data.dateTime = dt;
- data.dbName = colName[i];
- data.value = 0;
- }
- else if (row[i] is bool)
- {
- data.dateTime = dt;
- data.dbName = colName[i];
- data.value = (bool)row[i] ? 1 : 0;
- }
- else
- {
- data.dateTime = dt;
- data.dbName = colName[i];
- data.value = float.Parse(row[i].ToString());
- }
- }
- historyData[data.dbName].Add(data);
- }
- }
- Application.Current.Dispatcher.BeginInvoke(new Action(() =>
- {
- try
- {
- double min = ((DoubleRange)VisibleRangeValue).Min ;
- double max = ((DoubleRange)VisibleRangeValue).Max;
- foreach (var data in historyData)
- {
- foreach (var item in SelectedData)
- {
- var seriesItem = item as ChartDataLine;
- if (data.Key != seriesItem.DataName)
- continue;
- foreach (var historyDataItem in data.Value)
- {
- if (historyDataItem.value < min)
- min = historyDataItem.value;
- if (historyDataItem.value > max)
- max = historyDataItem.value;
- seriesItem.Append(historyDataItem.dateTime, historyDataItem.value);
- }
- }
- }
- VisibleRangeTime = new DateRange(StartDateTime.AddMinutes(-5), EndDateTime.AddMinutes(5));
- VisibleRangeValue = new DoubleRange(min,max);
-
- }
- catch (Exception ex)
- {
- LOG.Write(ex);
- }
- }));
- }
-
- private DataView view;
- public void Query()
- {
- if (MenuPermission != 3) return;
- this.StartDateTime = this.view.wfTimeFrom.Value;
- this.EndDateTime = this.view.wfTimeTo.Value;
- if (StartDateTime > EndDateTime)
- {
- MessageBox.Show("Time range invalid, start time should be early than end time");
- return;
- }
- ParameterNodes = _provider.GetParameters();
- VisibleRangeTime = new DateRange(StartDateTime.AddMinutes(-5), EndDateTime.AddMinutes(5));
- ChartAutoRange = AutoRange.Always;
- lock (_lockQueryCondition)
- {
- _queryDateTimeToken = StartDateTime;
- //_restart = true;
- while (!_lstTokenTimeData.IsEmpty)
- {
- _lstTokenTimeData.TryTake(out _);
- }
- foreach (var dataLine in SelectedData)
- {
- TimeChartDataLine line = dataLine as TimeChartDataLine;
- line.ClearData();
-
- QueryIndexer indexer = _lstTokenTimeData.FirstOrDefault(x => x.Module == line.Module);
- if (indexer == null)
- {
- indexer = new QueryIndexer()
- {
- DataList = new List<string>(),
- Module = line.Module,
- TimeToken = StartDateTime,
- };
- _lstTokenTimeData.Add(indexer);
- }
- indexer.DataList.Add(line.DataName);
- }
-
- }
- }
- public void Query(object parameter)
- {
- WaferHistoryRecipe waferHistoryRecipe = parameter as WaferHistoryRecipe;
- if (waferHistoryRecipe != null)
- {
- Application.Current.Dispatcher.BeginInvoke(new Action(() =>
- {
- if (waferHistoryRecipe.StartTime != DateTime.MinValue)
- this.view.wfTimeFrom.Value = waferHistoryRecipe.StartTime;
- if (waferHistoryRecipe.EndTime != DateTime.MinValue)
- this.view.wfTimeTo.Value = waferHistoryRecipe.EndTime;
- else
- this.view.wfTimeTo.Value = waferHistoryRecipe.StartTime.AddHours(1);
- //ParameterNodes.ForEachDo((x) =>
- //{
- // x.Selected = true;
- // ParameterCheck(x);
- //});
- Query();
- }));
- }
- }
- public void ParameterCheck(ParameterNode node)
- {
- bool result = RefreshTreeStatusToChild(node);
- if (!result)
- node.Selected = !node.Selected;
- else
- RefreshTreeStatusToParent(node);
- }
- /// <summary>
- /// Refresh tree node status from current to children, and add data to SelectedData
- /// </summary>
- private bool RefreshTreeStatusToChild(ParameterNode node)
- {
- if (node.ChildNodes.Count > 0)
- {
- for (int i = 0; i < node.ChildNodes.Count; i++)
- {
- ParameterNode n = node.ChildNodes[i];
- n.Selected = node.Selected;
- if (!RefreshTreeStatusToChild(n))
- {
- //uncheck left node
- for (int j = i; j < node.ChildNodes.Count; j++)
- {
- node.ChildNodes[j].Selected = !node.Selected;
- }
- node.Selected = !node.Selected;
- return false;
- }
- }
- }
- else //leaf node
- {
- lock (_lockSelection)
- {
- bool isExist = SelectedData.FirstOrDefault(x => (x as ChartDataLine).DataName == node.Name) != null;
- if (node.Selected && !isExist)
- {
- if (SelectedData.Count < MAX_PARAMETERS)
- {
- var line = new TimeChartDataLine(node.Name, StartDateTime, EndDateTime);
- line.Tag = node;
- SelectedData.Add(line);
- QueryIndexer indexer = _lstTokenTimeData.FirstOrDefault(x => x.Module == line.Module);
- if (indexer == null)
- {
- indexer = new QueryIndexer()
- {
- DataList = new List<string>(),
- Module = line.Module,
- TimeToken = StartDateTime,
- };
- _lstTokenTimeData.Add(indexer);
- }
- indexer.DataList.Add(line.DataName);
-
- }
- else
- {
- DialogBox.ShowWarning($"The max number of parameters is {MAX_PARAMETERS}.");
- return false;
- }
- }
- else if (!node.Selected && isExist)
- {
- //SelectedParameters.Remove(node.Name);
- var data = SelectedData.FirstOrDefault(d => (d as ChartDataLine).DataName == node.Name);
- SelectedData.Remove(data);
- }
- }
- }
- return true;
- }
- /// <summary>
- /// Refresh tree node status from current to parent
- /// </summary>
- /// <param name="node"></param>
- /// <returns></returns>
- private void RefreshTreeStatusToParent(ParameterNode node)
- {
- if (node.ParentNode != null)
- {
- if (node.Selected)
- {
- bool flag = true;
- for (int i = 0; i < node.ParentNode.ChildNodes.Count; i++)
- {
- if (!node.ParentNode.ChildNodes[i].Selected)
- {
- flag = false; //as least one child is unselected
- break;
- }
- }
- if (flag)
- node.ParentNode.Selected = true;
- }
- else
- {
- node.ParentNode.Selected = false;
- }
- RefreshTreeStatusToParent(node.ParentNode);
- }
- }
- #region Parameter Grid Control
- public void DeleteAll()
- {
- if (MenuPermission != 3) return;
- //uncheck all tree nodes
- foreach (ChartDataLine cp in SelectedData)
- {
- (cp.Tag as ParameterNode).Selected = false;
- RefreshTreeStatusToParent(cp.Tag as ParameterNode);
- }
- SelectedData.Clear();
- }
- private void SetParameterNode(ObservableCollection<ParameterNode> nodes, bool isChecked)
- {
- foreach (ParameterNode n in nodes)
- {
- n.Selected = isChecked;
- SetParameterNode(n.ChildNodes, isChecked);
- }
- }
- public void Delete(ChartDataLine cp)
- {
- if (MenuPermission != 3) return;
- if (cp != null && SelectedData.Contains(cp))
- {
- //uncheck tree node
- (cp.Tag as ParameterNode).Selected = false;
- RefreshTreeStatusToParent(cp.Tag as ParameterNode);
- SelectedData.Remove(cp);
- }
- }
- public void ExportAll()
- {
- if (MenuPermission != 3) return;
- try
- {
- Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
- dlg.DefaultExt = ".xlsx"; // Default file extension
- dlg.Filter = "Excel数据表格文件(*.xlsx)|*.xlsx"; // Filter files by extension
- dlg.FileName = $"Export_{DateTime.Now:yyyyMMdd_HHmmss}";
- Nullable<bool> result = dlg.ShowDialog();// Show open file dialog box
- if (result == true) // Process open file dialog box results
- {
- System.Data.DataSet ds = new System.Data.DataSet();
- ds.Tables.Add(new System.Data.DataTable($"Export_{DateTime.Now:yyyyMMdd_HHmmss}"));
- ds.Tables[0].Columns.Add("Time");
- ds.Tables[0].Columns[0].DataType = typeof(DateTime);
- lock (_lockSelection)
- {
- Dictionary<DateTime, double[]> timeValue = new Dictionary<DateTime, double[]>();
- for (int i = 0; i < SelectedData.Count; i++)
- {
- List<Tuple<DateTime, double>> data = (SelectedData[i] as ChartDataLine).Points;
- foreach (var tuple in data)
- {
- if (!timeValue.ContainsKey(tuple.Item1))
- timeValue[tuple.Item1] = new double[SelectedData.Count];
- timeValue[tuple.Item1][i] = tuple.Item2;
- }
- ds.Tables[0].Columns.Add((SelectedData[i] as ChartDataLine).DataName);
- ds.Tables[0].Columns[i + 1].DataType = typeof(double);
- }
- foreach (var item in timeValue)
- {
- var row = ds.Tables[0].NewRow();
- row[0] = item.Key;
- for (int j = 0; j < item.Value.Length; j++)
- {
- row[j + 1] = item.Value[j];
- }
- ds.Tables[0].Rows.Add(row);
- }
- }
- if (!ExcelHelper.ExportToExcel(dlg.FileName, ds, out string reason))
- {
- MessageBox.Show($"Export failed, {reason}", "Export", MessageBoxButton.OK, MessageBoxImage.Warning);
- return;
- }
- MessageBox.Show($"Export succeed, file save as {dlg.FileName}", "Export", MessageBoxButton.OK, MessageBoxImage.Information);
- }
- }
- catch (Exception ex)
- {
- LOG.Write(ex);
- MessageBox.Show("Write failed," + ex.Message, "export failed", MessageBoxButton.OK, MessageBoxImage.Warning);
- }
- }
- public void Export(ChartDataLine cp)
- {
- if (MenuPermission != 3) return;
- try
- {
- Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
- dlg.DefaultExt = ".xlsx"; // Default file extension
- dlg.Filter = "Excel数据表格文件(*.xlsx)|*.xlsx"; // Filter files by extension
- dlg.FileName = $"{cp.DataName}_{DateTime.Now:yyyyMMdd_HHmmss}";
- Nullable<bool> result = dlg.ShowDialog();// Show open file dialog box
- if (result == true) // Process open file dialog box results
- {
- System.Data.DataSet ds = new System.Data.DataSet();
- ds.Tables.Add(new System.Data.DataTable(cp.DataName));
- ds.Tables[0].Columns.Add("Time");
- ds.Tables[0].Columns[0].DataType = typeof(DateTime);
- ds.Tables[0].Columns.Add(cp.DataName);
- ds.Tables[0].Columns[1].DataType = typeof(double);
- foreach (var item in cp.Points)
- {
- var row = ds.Tables[0].NewRow();
- row[0] = item.Item1;
- row[1] = item.Item2;
- ds.Tables[0].Rows.Add(row);
- }
- if (!ExcelHelper.ExportToExcel(dlg.FileName, ds, out string reason))
- {
- MessageBox.Show($"Export failed, {reason}", "Export", MessageBoxButton.OK, MessageBoxImage.Warning);
- return;
- }
- MessageBox.Show($"Export succeed, file save as {dlg.FileName}", "Export", MessageBoxButton.OK, MessageBoxImage.Information);
- }
- }
- catch (Exception ex)
- {
- LOG.Write(ex);
- MessageBox.Show("Write failed," + ex.Message, "export failed", MessageBoxButton.OK, MessageBoxImage.Warning);
- }
- }
- public void SelectColor(ChartDataLine cp)
- {
- if (cp == null)
- return;
- var dlg = new System.Windows.Forms.ColorDialog();
- if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
- {
- cp.Stroke = new System.Windows.Media.Color() { A = dlg.Color.A, B = dlg.Color.B, G = dlg.Color.G, R = dlg.Color.R };
- }
- }
- #endregion
- #endregion
- protected override void OnViewLoaded(object _view)
- {
- base.OnViewLoaded(_view);
- this.view = (DataView)_view;
- this.view.wfTimeFrom.Value = this.StartDateTime;
- this.view.wfTimeTo.Value = this.EndDateTime;
- //this.AppendData();
- }
- }
- }
|