Shuxia Ning
2024-11-06 adf8dc1c7cae1b12f486dcdb3d7daf4a5a59ec52
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
using IStation.Unit;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
 
namespace IStation.WinFrmUI.Monitor
{
    public partial class DataComparePage : DocumentPage
    {
        public DataComparePage()
        {
            InitializeComponent();
            this.PageTitle.Caption = "数据比较";
            this.monitorDataSourcesTreeList.FocusedChangedEvent += MonitorDataSourcesTreeList1_FocusedChangedEvent; ;
            this.multiMonitorPointTreeList1.CheckChangedEvent += MultiMonitorPointTreeList1_CheckChangedEvent;
        }
 
 
        private class CurrentViewModel
        {
            public long ID { get; set; }
            public long SignalID { get; set; }
            public string Name { get; set; }
            public string AxisYTitle { get; set; }
            public Color Color { get; set; }
        }
 
 
        private Model.MonitorDataSources _dataSource = null;
        private BLL.MonitorDataSet _bll = new BLL.MonitorDataSet();
        private Dictionary<long, CurrentViewModel> _timeViewDict = null;
 
 
        /// <summary>
        /// 初始化数据
        /// </summary>
        public override void InitialDataSource()
        {
            _timeViewDict = new Dictionary<long, CurrentViewModel>();
            this.timeValueSwiftPlotChartView1.ClearSeries();
            this.timeValueSwiftPlotChartView1.SetTimeAxisX(DevExpress.XtraCharts.DateTimeMeasureUnit.Minute);
            this.monitorDataSourcesTreeList.SetBindingData();
            this.multiMonitorPointTreeList1.SetBindingData();
        }
 
        private void MonitorDataSourcesTreeList1_FocusedChangedEvent(Model.MonitorDataSources obj)
        {
            SetBindingData(obj);
        }
 
 
        private void MultiMonitorPointTreeList1_CheckChangedEvent(Model.MonitorPointExSignalWithSignalType monitorPoint, System.Drawing.Color color, bool cek)
        {
            SetBindingData(_dataSource, monitorPoint, color, cek);
        }
 
        private void SetBindingData(Model.MonitorDataSources monitorDataSources)
        {
            _dataSource = monitorDataSources;
            
            if (_dataSource == null)
            {
                this.timeValueSwiftPlotChartView1.ClearSeries();
            }
            else
            {
                var year = new BLL.MonitorDataSetSummary().GetMaxMinTime(_dataSource.ID,out DateTime min,out DateTime max); 
                if ((max - min).TotalDays < 2)
                {
                    this.timeValueSwiftPlotChartView1.SetTimeAxisX(DevExpress.XtraCharts.DateTimeMeasureUnit.Second);
                }
                else
                {
                    this.timeValueSwiftPlotChartView1.SetTimeAxisX(DevExpress.XtraCharts.DateTimeMeasureUnit.Minute);
                }
 
                _currentMonitorDataSourcesId = _dataSource.ID;
                this.timeValueSwiftPlotChartView1.ClearSeries();
                foreach (var item in _timeViewDict)
                {
                    var timeValues = new List<TimeValue>();
                    var packet = _bll.GetSignalRecordPacket(_dataSource.ID, item.Value.ID, item.Value.SignalID);
                    if (packet != null)
                    {
                        var records = packet.RecordList;
                        if (records != null && records.Any())
                        {
                            timeValues = records.Where(x => x.Value != IStation.Error.Default && x.Value != IStation.Error.Abnormal).
                                Select(x => new TimeValue(x.Time, Math.Round(x.Value, 4))).ToList();
                        }
                    } 
                    this.timeValueSwiftPlotChartView1.AddSwiftPlotSeries(item.Value.ID.ToString(), item.Value.Color, item.Value.Name, item.Value.AxisYTitle, timeValues);
                }
            }
        }
 
        private long _currentMonitorDataSourcesId = 0;
        private void SetBindingData(Model.MonitorDataSources scene, Model.MonitorPointExSignalWithSignalType monitorPoint, System.Drawing.Color color, bool cek)
        {
            _dataSource = scene;
            if (_dataSource == null)
            {
                this.timeValueSwiftPlotChartView1.ClearSeries();
            }
            else
            {
                if (_currentMonitorDataSourcesId != _dataSource.ID)
                {
                    SetBindingData(_dataSource);
                }
 
                if (cek)
                {
                    if (_timeViewDict.ContainsKey(monitorPoint.MonitorPointID))
                        return;
                    var timeValues = new List<TimeValue>();
                    var packet = _bll.GetSignalRecordPacket(_dataSource.ID, monitorPoint.MonitorPointID, monitorPoint.SignalID);
                    if (packet != null)
                    {
                        var records = packet.RecordList;
                        if (records != null && records.Any())
                        {
                            timeValues = records.Where(x => x.Value != IStation.Error.Default && x.Value != IStation.Error.Abnormal).
                                Select(x => new TimeValue(x.Time, Math.Round(x.Value, 4))).ToList(); 
                        }
 
                    }
                    var axisYTitle = monitorPoint.UnitValue;
                    if (int.TryParse(monitorPoint.UnitValue, out int unitValue))
                    {
                        var dict = UnitHelper.GetEnUnitDict(monitorPoint.UnitType);
                        if (dict != null)
                            axisYTitle = dict[unitValue];
                    }
 
                    _timeViewDict.Add(monitorPoint.MonitorPointID, new CurrentViewModel()
                    {
                        ID = monitorPoint.MonitorPointID,
                        SignalID = monitorPoint.SignalID,
                        Name = monitorPoint.Name,
                        AxisYTitle = axisYTitle,
                        Color = color
                    });
                    this.timeValueSwiftPlotChartView1.AddSwiftPlotSeries(monitorPoint.MonitorPointID.ToString(), color, monitorPoint.Name, axisYTitle, timeValues);
                }
                else
                {
                    if (!_timeViewDict.ContainsKey(monitorPoint.MonitorPointID))
                        return;
                    _timeViewDict.Remove(monitorPoint.MonitorPointID);
                    this.timeValueSwiftPlotChartView1.RemoveSeries(monitorPoint.MonitorPointID.ToString());
                }
            }
        }
 
 
 
 
        // 刷新数据
        private void barBtnRefresh_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            InitialDataSource();
        }
 
 
 
        private void barBtnExport_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            var ids = _timeViewDict.Select(x => x.Key).ToList();
            ExportTestHelper.Export(_dataSource.ID, ids, 2024, 10);
        }
    }
}