SourceTimeSyncCommand.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Windows.Input;
  6. using Aitex.UI.Charting.ViewModel;
  7. using System.Windows;
  8. using Aitex.UI.Charting.Model;
  9. using Abt.Controls.SciChart;
  10. using Abt.Controls.SciChart.Model.DataSeries;
  11. using Aitex.UI.Charting.View;
  12. using DataAnalysisControl.Core;
  13. namespace Aitex.UI.Charting.Command
  14. {
  15. public class SourceTimeSyncCommand : ICommand
  16. {
  17. public SourceTimeSyncCommand(string dataSourceName, DateTime syncTimePoint, string syncStepName)
  18. {
  19. _dataSourceName = dataSourceName;
  20. _syncTimePoint = syncTimePoint;
  21. _syncStepName = syncStepName;
  22. }
  23. string _dataSourceName;
  24. DateTime _syncTimePoint;
  25. string _syncStepName;
  26. public bool CanExecute(object parameter)
  27. {
  28. return true;
  29. }
  30. #pragma warning disable 0067
  31. public event EventHandler CanExecuteChanged;
  32. #pragma warning restore 0067
  33. private void AsyncShowWarningMessageBox(string message, string title)
  34. {
  35. Application.Current.Dispatcher.BeginInvoke(new Action(() => MessageBox.Show(message, title, MessageBoxButton.OK, MessageBoxImage.Warning)));
  36. }
  37. public void Execute(object parameter)
  38. {
  39. string reason = string.Empty;
  40. try
  41. {
  42. var commonViewModel = (CommonViewModel)parameter;
  43. //判断新加入数据是否合理
  44. if (!commonViewModel.DataSources.Keys.Contains(_dataSourceName))
  45. {
  46. reason = string.Format("数据源{0}不存在", _dataSourceName);
  47. //AsyncShowWarningMessageBox(reason, "警告");
  48. return;
  49. }
  50. //至少要含有2个数据源同步才可以开始实现数据同步功能
  51. List<IDataSource> syncSources = new List<IDataSource>();
  52. foreach (var source in commonViewModel.DataSourceList)
  53. {
  54. if (source.SyncPoint != null)
  55. {
  56. if (source.SyncPoint.StepTime != DateTime.MinValue)
  57. {
  58. syncSources.Add(source);
  59. }
  60. else
  61. {
  62. source.TimeMove = new TimeSpan(0);
  63. //重新更新charting的数据
  64. foreach (MyLineSeries line in commonViewModel.RenderableSeries)
  65. {
  66. if (line.DataSource == source)
  67. {
  68. var dataSet = line.DataSeries as XyDataSeries<DateTime, float>;
  69. if (dataSet != null)
  70. {
  71. var timeList = new List<DateTime>();
  72. var dataList = new List<float>();
  73. for (int m = 0; m < line.Points.TimeStamp.Count; m++)
  74. {
  75. timeList.Add(line.Points.TimeStamp[m]);
  76. dataList.Add((float)(line.Points.RawData[m] * line.Factor + line.Offset));
  77. }
  78. if (timeList.Count > 0)
  79. {
  80. dataSet.Clear();
  81. dataSet.Append(timeList, dataList);
  82. }
  83. }
  84. }
  85. }
  86. }
  87. }
  88. }
  89. if (syncSources.Count >= 2)
  90. {
  91. var firstSource = syncSources[0];
  92. for (int i = 1; i < syncSources.Count; i++)
  93. {
  94. var curSource = syncSources[i];
  95. var timeMove = firstSource.SyncPoint.StepTime - curSource.SyncPoint.StepTime;
  96. curSource.TimeMove = timeMove;
  97. //重新更新charting的数据
  98. foreach (MyLineSeries line in commonViewModel.RenderableSeries)
  99. {
  100. if (line.DataSource == curSource)
  101. {
  102. var dataSet = line.DataSeries as XyDataSeries<DateTime, float>;
  103. var timeList = new List<DateTime>();
  104. var dataList = new List<float>();
  105. for (int m = 0; m < line.Points.TimeStamp.Count; m++)
  106. {
  107. timeList.Add(line.Points.TimeStamp[m] + curSource.TimeMove);
  108. dataList.Add((float)(line.Points.RawData[m] * line.Factor + line.Offset));
  109. }
  110. if (timeList.Count > 0)
  111. {
  112. dataSet.Clear();
  113. dataSet.Append(timeList, dataList);
  114. }
  115. }
  116. }
  117. }
  118. }
  119. commonViewModel.InvokePropertyChanged();
  120. }
  121. catch (Exception ex)
  122. {
  123. reason = ex.Message;
  124. CONTEXT.WriteLog(ex, string.Format("执行数据源{0}时间同步功能发生失败", _dataSourceName));
  125. AsyncShowWarningMessageBox(reason, "警告");
  126. }
  127. }
  128. }
  129. }