DataSourceExportingDialog.xaml.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Data;
  8. using System.Windows.Documents;
  9. using System.Windows.Input;
  10. using System.Windows.Media;
  11. using System.Windows.Media.Imaging;
  12. using System.Windows.Shapes;
  13. using Aitex.UI.Charting.ViewModel;
  14. using System.Threading;
  15. using DataAnalysisControl.Core;
  16. using Aitex.UI.Charting.Model;
  17. using Aitex.DataAnalysis.Core;
  18. namespace Aitex.UI.Charting.View
  19. {
  20. /// <summary>
  21. /// Interaction logic for DataSourceExportingDialog.xaml
  22. /// </summary>
  23. public partial class DataSourceExportingDialog : Window
  24. {
  25. public DataSourceExportingDialog(string sourceName, string fileName)
  26. {
  27. InitializeComponent();
  28. _sourceName = sourceName;
  29. _viewModel = CommonViewModel.Instance;
  30. if (!_viewModel.DataSources.ContainsKey(_sourceName))
  31. {
  32. MessageBox.Show(string.Format(DataAnalysisControl.Properties.Resource.DataSourceExportingDialog_DataSourceExportingDialog_data_source_not_exist, _sourceName));
  33. return;
  34. }
  35. _source = _viewModel.DataSources[_sourceName];
  36. txt_Info.Content = string.Format(DataAnalysisControl.Properties.Resource.DataSeriesExportingDialog_DataSeriesExportingDialog_exporting,
  37. _source.ChamberName,
  38. _source.BeginTime.ToString("yyyy/MM/dd HH:mm:ss"),
  39. _source.EndTime.ToString("yyyy/MM/dd HH:mm:ss"));
  40. txt_Completed.Content = "1%";
  41. progressBar1.Value = 1;
  42. _archieveFilePath = fileName;
  43. _thread = new Thread(new ThreadStart(Task_Export));
  44. _thread.Name = "数据源导出线程";
  45. _thread.IsBackground = true;
  46. _thread.Start();
  47. }
  48. CommonViewModel _viewModel;
  49. string _sourceName;
  50. Model.IDataSource _source;
  51. string _archieveFilePath;
  52. Thread _thread;
  53. private void Task_Export()
  54. {
  55. try
  56. {
  57. if (_source is Aitex.UI.Charting.Model.PostgreSqlDataSource)
  58. {
  59. // Process open file dialog box results
  60. ArchivedFile archived = new ArchivedFile();
  61. archived.AliasList = new Dictionary<string, List<string>>();
  62. archived.BeginTime = _source.BeginTime;
  63. archived.ChamId = _source.ChamberName;
  64. archived.WaferDisplayIndex = _source.WaferDisplayIndex;
  65. archived.Datas = _source.Datas;
  66. archived.Description = _source.Description;
  67. archived.EndTime = _source.EndTime;
  68. List<KeyValuePair<DateTime, string>> recipeInfo = new List<KeyValuePair<DateTime, string>>();
  69. foreach (var item in _source.RecipeSteps)
  70. recipeInfo.Add(new KeyValuePair<DateTime, string>(item.StepTime, item.StepName));
  71. archived.RecipeSteps = recipeInfo;
  72. foreach (var item in archived.Datas)
  73. {
  74. item.Value.RawData.Clear();
  75. item.Value.TimeStamp.Clear();
  76. }
  77. List<DateTime> timeDataList = new List<DateTime>();
  78. var dataNameList = archived.Datas.Keys.ToList();
  79. DateTime dt = archived.BeginTime;
  80. float totalCount = (int)((archived.EndTime - archived.BeginTime).TotalHours * 6 + 0.5);
  81. float curCount = 0;
  82. do
  83. {
  84. DateTime nextTime = dt + new TimeSpan(0, 10, 0);
  85. if (nextTime > archived.EndTime)
  86. nextTime = archived.EndTime;
  87. if (nextTime >= dt)
  88. {
  89. Dictionary<string, DataItem> qData;
  90. var isSucc = _viewModel.GetDbData(archived.ChamId, dt, nextTime, dataNameList, out qData);
  91. if (!isSucc)
  92. {
  93. MessageBox.Show(string.Format(DataAnalysisControl.Properties.Resource.DataSourceExportingDialog_Task_Export_get_data_error,
  94. dt.ToString("yyyy/MM/dd HH:mm:ss"),
  95. nextTime.ToString("yyyy/MM/dd HH:mm:ss"),
  96. archived.ChamId), "Export", MessageBoxButton.OK, MessageBoxImage.Error);
  97. return;
  98. }
  99. else
  100. {
  101. bool insertTime = false;
  102. foreach (var dataName in qData.Keys)
  103. {
  104. archived.Datas[dataName].RawData.AddRange(qData[dataName].RawData);
  105. //archived.Datas[dataName].TimeStamp.AddRange(qData[dataName].TimeStamp);
  106. if (!insertTime)
  107. {
  108. insertTime = true;
  109. timeDataList.AddRange(qData[dataName].TimeStamp);
  110. }
  111. }
  112. }
  113. qData.Clear();
  114. }
  115. dt += new TimeSpan(0, 10, 0);
  116. curCount++;
  117. txt_Completed.Dispatcher.BeginInvoke(new Action<double>((o) =>
  118. {
  119. if (o > 1) o = 1;
  120. txt_Completed.Content = string.Format("{0}%", (o * 100).ToString("F1"));
  121. }), curCount / totalCount);
  122. progressBar1.Dispatcher.BeginInvoke(new Action<double>((o) =>
  123. {
  124. if (o > 1) o = 1;
  125. progressBar1.Value = o * 100;
  126. }), curCount / totalCount);
  127. }
  128. while (dt < archived.EndTime);
  129. //use a same time table to reduce data file size
  130. if (archived.Datas.Count > 0)
  131. {
  132. foreach (var item in archived.Datas)
  133. {
  134. item.Value.TimeStamp = timeDataList;
  135. }
  136. }
  137. // Open document
  138. ObjectSerializer.SerializeObjectToBinaryFile(_archieveFilePath, archived);
  139. if (archived.Datas != null)
  140. archived.Datas.Clear();
  141. GC.Collect();
  142. MessageBox.Show(DataAnalysisControl.Properties.Resource.DataSourceExportingDialog_Task_Export_succeed + _archieveFilePath, "Export", MessageBoxButton.OK, MessageBoxImage.Information);
  143. }
  144. else if(_source is ArchievedFileDataSource)
  145. {
  146. var source = _source as ArchievedFileDataSource;
  147. ObjectSerializer.SerializeObjectToBinaryFile(_archieveFilePath, source.ArchivedFile);
  148. MessageBox.Show(DataAnalysisControl.Properties.Resource.DataSourceExportingDialog_Task_Export_succeed + _archieveFilePath, "Export", MessageBoxButton.OK, MessageBoxImage.Information);
  149. }
  150. }
  151. catch (Exception ex)
  152. {
  153. CONTEXT.WriteLog(ex, string.Format("导出数据源{0}发生异常", _sourceName));
  154. MessageBox.Show(string.Format(DataAnalysisControl.Properties.Resource.DataSeriesExportingDialog_Task_Export_export_error, ex.Message), "Export", MessageBoxButton.OK, MessageBoxImage.Error);
  155. }
  156. finally
  157. {
  158. this.Dispatcher.BeginInvoke(new Action(() => { Close(); }));
  159. }
  160. }
  161. private void btnAbort_Click(object sender, RoutedEventArgs e)
  162. {
  163. if(_thread != null)
  164. _thread.Abort();
  165. Close();
  166. }
  167. }
  168. }