alielie
2025-05-20 99df058a665ea04065a23e729ec79a2f23f79881
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
using HMI.Util;
using Plugin.TestResult_WPF.Constant;
using Plugin.TestResult_WPF.Model;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IO;
using System.Runtime.CompilerServices;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
 
namespace Plugin.TestResult_WPF
{
    /// <summary>
    /// SingleCell.xaml 的交互逻辑
    /// </summary>
    public partial class SingleCell : UserControl, INotifyPropertyChanged
    {
 
        #region ...
        public event PropertyChangedEventHandler PropertyChanged;
 
        public virtual void RaisePropertyChanged(string name)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
        }
 
        public void RaiseAndSetIfchanged<T>(ref T old, T @new, [CallerMemberName] string propertyName = null)
        {
            old = @new;
            if (propertyName != null) RaisePropertyChanged(propertyName);
        }
        #endregion
 
        private InfoWindow window;
        private int maxSize;
 
        public SingleCell()
        {
            DataContext = this;
            InitializeComponent();
        }
 
        public SingleCell(string parentName, int index, int maxSize, string iniFile)
        {
            this.parentName = parentName;
            this.index = index;
            this.maxSize = maxSize;
            this.countIniPath = iniFile;
            DataContext = this;
            InitializeComponent();
            LoadProbeCount();
        }
 
        private string _parentName;
        public string parentName
        {
            get { return _parentName; }
            set
            {
                RaiseAndSetIfchanged(ref _parentName, value);
                RaisePropertyChanged("title");
            }
        }
 
        private int _index;
 
        public int index
        {
            get { return _index; }
            set
            {
                RaiseAndSetIfchanged(ref _index, value);
                RaisePropertyChanged("title");
            }
        }
 
        public string title => $"{parentName}-{index + 1}";
 
        private string _status = CommonStatus.WAITING;
 
        public string tooltipContent
        {
            get
            {
                string dataStr = "";
                if (infos != null && infos.Count > 0)
                {
                    foreach (var info in infos)
                    {
                        dataStr += $"\r\nStep:{info.step},Result:{info.result}";
                    }
                }
                return $"{title}\r\n状态:{CommonStatus.GetStatusDescription(status)}{dataStr}";
            }
        }
 
        public string status
        {
            get { return _status; }
            set => RaiseAndSetIfchanged(ref _status, value);
        }
 
        private ConcurrentQueue<Info> queue = new ConcurrentQueue<Info>();
 
        private ObservableCollection<Info> _infos = new ObservableCollection<Info>();
 
        public ObservableCollection<Info> infos
        {
            get => _infos;
            set
            {
                RaiseAndSetIfchanged(ref _infos, value);
                RaisePropertyChanged("tooltipContent");
            }
        }
 
        private string countIniPath;
        /// <summary>
        /// 探针寿命
        /// </summary>
        private int probeCount;
        public int ProbeCount
        {
            get => probeCount;
            set
            {
                RaiseAndSetIfchanged(ref probeCount, value);
            }
        }
 
        public void LoadProbeCount()
        {
            string temp = IniFileHelper.ReadIniData($"{title}", "ProbeCount", "", countIniPath);
            if (!string.IsNullOrEmpty(temp))
            {
                int count = Convert.ToInt32(temp);
                ProbeCount = count;
            }
        }
 
        private void UpdateProbeCount(int count)
        {
            ProbeCount = count;
            IniFileHelper.WriteIniData($"{title}", "ProbeCount", ProbeCount.ToString(), countIniPath);
            if (window != null) window.UpdateProbeCount(ProbeCount);
        }
 
        private void UserControl_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            if (window == null)
            {
                window = new InfoWindow(title, infos, electricChart, ProbeCount);
                window.Closed += Window_Closed;
                window.ClearProbeCountEvent += Window_ClearProbeCountEvent;
                window.Owner = Window.GetWindow(this);
                window.Show();
            }
            else
            {
                window.Activate();
            }
        }
 
        private void Window_ClearProbeCountEvent(object sender, EventArgs e)
        {
            UpdateProbeCount(0);
        }
 
        private void Window_Closed(object sender, EventArgs e)
        {
            this.window = null;
        }
 
        public void SetStatus(string status)
        {
            this.status = status;
            if (status == "testing")
            {
                UpdateProbeCount(ProbeCount + 1);
            }
        }
 
        public void SetInfos(Info info)
        {
            if (queue.Count >= maxSize)
            {
                queue.TryDequeue(out var first);
                queue.Enqueue(info);
                this.infos = new ObservableCollection<Info>(queue);
                if (window != null) window.UpdateSource(this.infos);
            }
            else
            {
                queue.Enqueue(info);
                if (window != null)
                {
                    window.Dispatcher.Invoke(() =>
                    {
                        this.infos.Add(info);
                    });
                }
                else
                {
                    Dispatcher.Invoke(() =>
                    {
                        this.infos.Add(info);
                    });
                }
                return;
            }
        }
 
        public void ClearInfo()
        {
            queue = new ConcurrentQueue<Info>();
            var list = new ObservableCollection<Info>(queue);
            this.infos = list;
            if (window != null) window.UpdateSource(this.infos);
        }
 
        private Chart electricChart = new Chart("电流曲线(A)", false, 20);
        public void AddElectricData(double data)
        {
            electricChart.AddData(data);
        }
 
        public void ClearElectricData()
        {
            electricChart.Clear();
        }
 
        public void SaveElectricDataToCSV(string filePath)
        {
            try
            {
                List<double> datas = electricChart?.GetSingleData();
 
                FileInfo fi = new FileInfo(filePath);
                if (!fi.Directory.Exists)
                {
                    fi.Directory.Create();  //文件不存在,创建文件
                }
 
                using (FileStream fs = new FileStream(filePath, FileMode.Create, FileAccess.Write))
                using (StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.UTF8))
                {
                    string title = "电流数据(A)";
                    sw.WriteLine(title);
                    if (datas != null)
                    {
                        for (int i = 0; i < datas.Count; i++)
                        {
                            sw.WriteLine(datas[i].ToString());
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }
    }
}