123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Windows.Input;
- using Aitex.UI.Charting.ViewModel;
- using System.Windows;
- using Aitex.UI.Charting.Model;
- using Abt.Controls.SciChart;
- using Abt.Controls.SciChart.Model.DataSeries;
- using Aitex.UI.Charting.View;
- using DataAnalysisControl.Core;
- namespace Aitex.UI.Charting.Command
- {
- public class SourceTimeSyncCommand : ICommand
- {
- public SourceTimeSyncCommand(string dataSourceName, DateTime syncTimePoint, string syncStepName)
- {
- _dataSourceName = dataSourceName;
- _syncTimePoint = syncTimePoint;
- _syncStepName = syncStepName;
- }
- string _dataSourceName;
- DateTime _syncTimePoint;
- string _syncStepName;
- public bool CanExecute(object parameter)
- {
- return true;
- }
- #pragma warning disable 0067
- public event EventHandler CanExecuteChanged;
- #pragma warning restore 0067
- private void AsyncShowWarningMessageBox(string message, string title)
- {
- Application.Current.Dispatcher.BeginInvoke(new Action(() => MessageBox.Show(message, title, MessageBoxButton.OK, MessageBoxImage.Warning)));
- }
- public void Execute(object parameter)
- {
- string reason = string.Empty;
- try
- {
- var commonViewModel = (CommonViewModel)parameter;
- //判断新加入数据是否合理
- if (!commonViewModel.DataSources.Keys.Contains(_dataSourceName))
- {
- reason = string.Format("数据源{0}不存在", _dataSourceName);
- //AsyncShowWarningMessageBox(reason, "警告");
- return;
- }
- //至少要含有2个数据源同步才可以开始实现数据同步功能
- List<IDataSource> syncSources = new List<IDataSource>();
- foreach (var source in commonViewModel.DataSourceList)
- {
- if (source.SyncPoint != null)
- {
- if (source.SyncPoint.StepTime != DateTime.MinValue)
- {
- syncSources.Add(source);
- }
- else
- {
- source.TimeMove = new TimeSpan(0);
- //重新更新charting的数据
- foreach (MyLineSeries line in commonViewModel.RenderableSeries)
- {
- if (line.DataSource == source)
- {
- var dataSet = line.DataSeries as XyDataSeries<DateTime, float>;
- if (dataSet != null)
- {
- var timeList = new List<DateTime>();
- var dataList = new List<float>();
- for (int m = 0; m < line.Points.TimeStamp.Count; m++)
- {
- timeList.Add(line.Points.TimeStamp[m]);
- dataList.Add((float)(line.Points.RawData[m] * line.Factor + line.Offset));
- }
- if (timeList.Count > 0)
- {
- dataSet.Clear();
- dataSet.Append(timeList, dataList);
- }
- }
- }
- }
- }
- }
- }
- if (syncSources.Count >= 2)
- {
- var firstSource = syncSources[0];
- for (int i = 1; i < syncSources.Count; i++)
- {
- var curSource = syncSources[i];
- var timeMove = firstSource.SyncPoint.StepTime - curSource.SyncPoint.StepTime;
- curSource.TimeMove = timeMove;
- //重新更新charting的数据
- foreach (MyLineSeries line in commonViewModel.RenderableSeries)
- {
- if (line.DataSource == curSource)
- {
- var dataSet = line.DataSeries as XyDataSeries<DateTime, float>;
- var timeList = new List<DateTime>();
- var dataList = new List<float>();
- for (int m = 0; m < line.Points.TimeStamp.Count; m++)
- {
- timeList.Add(line.Points.TimeStamp[m] + curSource.TimeMove);
- dataList.Add((float)(line.Points.RawData[m] * line.Factor + line.Offset));
- }
- if (timeList.Count > 0)
- {
- dataSet.Clear();
- dataSet.Append(timeList, dataList);
- }
- }
- }
- }
- }
- commonViewModel.InvokePropertyChanged();
- }
- catch (Exception ex)
- {
- reason = ex.Message;
- CONTEXT.WriteLog(ex, string.Format("执行数据源{0}时间同步功能发生失败", _dataSourceName));
- AsyncShowWarningMessageBox(reason, "警告");
- }
- }
- }
- }
|