123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Data;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using System.Windows.Shapes;
- using Aitex.UI.Charting.ViewModel;
- using System.Threading;
- using DataAnalysisControl.Core;
- using Aitex.UI.Charting.Model;
- using Aitex.DataAnalysis.Core;
- namespace Aitex.UI.Charting.View
- {
- /// <summary>
- /// Interaction logic for DataSourceExportingDialog.xaml
- /// </summary>
- public partial class DataSourceExportingDialog : Window
- {
- public DataSourceExportingDialog(string sourceName, string fileName)
- {
- InitializeComponent();
- _sourceName = sourceName;
- _viewModel = CommonViewModel.Instance;
- if (!_viewModel.DataSources.ContainsKey(_sourceName))
- {
- MessageBox.Show(string.Format(DataAnalysisControl.Properties.Resource.DataSourceExportingDialog_DataSourceExportingDialog_data_source_not_exist, _sourceName));
- return;
- }
- _source = _viewModel.DataSources[_sourceName];
- txt_Info.Content = string.Format(DataAnalysisControl.Properties.Resource.DataSeriesExportingDialog_DataSeriesExportingDialog_exporting,
- _source.ChamberName,
- _source.BeginTime.ToString("yyyy/MM/dd HH:mm:ss"),
- _source.EndTime.ToString("yyyy/MM/dd HH:mm:ss"));
- txt_Completed.Content = "1%";
- progressBar1.Value = 1;
- _archieveFilePath = fileName;
- _thread = new Thread(new ThreadStart(Task_Export));
- _thread.Name = "数据源导出线程";
- _thread.IsBackground = true;
- _thread.Start();
- }
- CommonViewModel _viewModel;
- string _sourceName;
- Model.IDataSource _source;
- string _archieveFilePath;
- Thread _thread;
- private void Task_Export()
- {
- try
- {
- if (_source is Aitex.UI.Charting.Model.PostgreSqlDataSource)
- {
- // Process open file dialog box results
- ArchivedFile archived = new ArchivedFile();
- archived.AliasList = new Dictionary<string, List<string>>();
- archived.BeginTime = _source.BeginTime;
- archived.ChamId = _source.ChamberName;
- archived.WaferDisplayIndex = _source.WaferDisplayIndex;
- archived.Datas = _source.Datas;
- archived.Description = _source.Description;
- archived.EndTime = _source.EndTime;
- List<KeyValuePair<DateTime, string>> recipeInfo = new List<KeyValuePair<DateTime, string>>();
- foreach (var item in _source.RecipeSteps)
- recipeInfo.Add(new KeyValuePair<DateTime, string>(item.StepTime, item.StepName));
- archived.RecipeSteps = recipeInfo;
- foreach (var item in archived.Datas)
- {
- item.Value.RawData.Clear();
- item.Value.TimeStamp.Clear();
- }
- List<DateTime> timeDataList = new List<DateTime>();
- var dataNameList = archived.Datas.Keys.ToList();
- DateTime dt = archived.BeginTime;
- float totalCount = (int)((archived.EndTime - archived.BeginTime).TotalHours * 6 + 0.5);
- float curCount = 0;
- do
- {
- DateTime nextTime = dt + new TimeSpan(0, 10, 0);
- if (nextTime > archived.EndTime)
- nextTime = archived.EndTime;
- if (nextTime >= dt)
- {
- Dictionary<string, DataItem> qData;
-
- var isSucc = _viewModel.GetDbData(archived.ChamId, dt, nextTime, dataNameList, out qData);
- if (!isSucc)
- {
- MessageBox.Show(string.Format(DataAnalysisControl.Properties.Resource.DataSourceExportingDialog_Task_Export_get_data_error,
- dt.ToString("yyyy/MM/dd HH:mm:ss"),
- nextTime.ToString("yyyy/MM/dd HH:mm:ss"),
- archived.ChamId), "Export", MessageBoxButton.OK, MessageBoxImage.Error);
- return;
- }
- else
- {
- bool insertTime = false;
- foreach (var dataName in qData.Keys)
- {
- archived.Datas[dataName].RawData.AddRange(qData[dataName].RawData);
- //archived.Datas[dataName].TimeStamp.AddRange(qData[dataName].TimeStamp);
- if (!insertTime)
- {
- insertTime = true;
- timeDataList.AddRange(qData[dataName].TimeStamp);
- }
- }
- }
- qData.Clear();
- }
- dt += new TimeSpan(0, 10, 0);
- curCount++;
- txt_Completed.Dispatcher.BeginInvoke(new Action<double>((o) =>
- {
- if (o > 1) o = 1;
- txt_Completed.Content = string.Format("{0}%", (o * 100).ToString("F1"));
- }), curCount / totalCount);
- progressBar1.Dispatcher.BeginInvoke(new Action<double>((o) =>
- {
- if (o > 1) o = 1;
- progressBar1.Value = o * 100;
- }), curCount / totalCount);
- }
- while (dt < archived.EndTime);
- //use a same time table to reduce data file size
- if (archived.Datas.Count > 0)
- {
- foreach (var item in archived.Datas)
- {
- item.Value.TimeStamp = timeDataList;
- }
- }
- // Open document
- ObjectSerializer.SerializeObjectToBinaryFile(_archieveFilePath, archived);
- if (archived.Datas != null)
- archived.Datas.Clear();
- GC.Collect();
- MessageBox.Show(DataAnalysisControl.Properties.Resource.DataSourceExportingDialog_Task_Export_succeed + _archieveFilePath, "Export", MessageBoxButton.OK, MessageBoxImage.Information);
- }
- else if(_source is ArchievedFileDataSource)
- {
- var source = _source as ArchievedFileDataSource;
- ObjectSerializer.SerializeObjectToBinaryFile(_archieveFilePath, source.ArchivedFile);
- MessageBox.Show(DataAnalysisControl.Properties.Resource.DataSourceExportingDialog_Task_Export_succeed + _archieveFilePath, "Export", MessageBoxButton.OK, MessageBoxImage.Information);
- }
- }
- catch (Exception ex)
- {
- CONTEXT.WriteLog(ex, string.Format("导出数据源{0}发生异常", _sourceName));
- MessageBox.Show(string.Format(DataAnalysisControl.Properties.Resource.DataSeriesExportingDialog_Task_Export_export_error, ex.Message), "Export", MessageBoxButton.OK, MessageBoxImage.Error);
- }
- finally
- {
- this.Dispatcher.BeginInvoke(new Action(() => { Close(); }));
- }
- }
- private void btnAbort_Click(object sender, RoutedEventArgs e)
- {
- if(_thread != null)
- _thread.Abort();
- Close();
- }
- }
- }
|