-
Notifications
You must be signed in to change notification settings - Fork 2
/
rawDataForm.cs
457 lines (360 loc) · 13.6 KB
/
rawDataForm.cs
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
/*
* Yocto-Visualization, a free application to visualize Yoctopuce Sensors.
*
* Raw data window
*
*
* - - - - - - - - - License information: - - - - - - - - -
*
* Copyright (C) 2017 and beyond by Yoctopuce Sarl, Switzerland.
*
* Yoctopuce Sarl (hereafter Licensor) grants to you a perpetual
* non-exclusive license to use, modify, copy and integrate this
* file into your software for the sole purpose of interfacing
* with Yoctopuce products.
*
* You may reproduce and distribute copies of this file in
* source or object form, as long as the sole purpose of this
* code is to interface with Yoctopuce products. You must retain
* this notice in the distributed source file.
*
* You should refer to Yoctopuce General Terms and Conditions
* for additional information regarding your rights and
* obligations.
*
* THE SOFTWARE AND DOCUMENTATION ARE PROVIDED "AS IS" WITHOUT
* WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING
* WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO
* EVENT SHALL LICENSOR BE LIABLE FOR ANY INCIDENTAL, SPECIAL,
* INDIRECT OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA,
* COST OF PROCUREMENT OF SUBSTITUTE GOODS, TECHNOLOGY OR
* SERVICES, ANY CLAIMS BY THIRD PARTIES (INCLUDING BUT NOT
* LIMITED TO ANY DEFENSE THEREOF), ANY CLAIMS FOR INDEMNITY OR
* CONTRIBUTION, OR OTHER SIMILAR COSTS, WHETHER ASSERTED ON THE
* BASIS OF CONTRACT, TORT (INCLUDING NEGLIGENCE), BREACH OF
* WARRANTY, OR OTHERWISE.
*
*/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Windows.Forms;
namespace YoctoVisualisation
{
public partial class RawDataForm : Form
{
BackgroundWorker exportProcess;
bool refreshDisabled = false;
public RawDataForm()
{
InitializeComponent();
timer1.Enabled = true;
}
public void showData()
{
Show();
checkedListBox1.Items.Clear();
foreach (CustomYSensor s in sensorsManager.sensorList)
if (!(s is NullYSensor))
{
checkedListBox1.Items.Add(s);
}
refeshGridGeometry(null);
}
public void refeshGridGeometry(ItemCheckEventArgs e)
{
int sensCount = 0;
int persensor = 0;
List<CustomYSensor> slist = new List<CustomYSensor>();
for (int i = 0; i < checkedListBox1.Items.Count; i++)
{
bool toAdd = false;
if (checkedListBox1.GetItemChecked(i)) toAdd = true;
if (e != null)
{
if (e.Index == i)
toAdd = (e.NewValue == CheckState.Checked);
}
if (toAdd)
{
slist.Add((CustomYSensor)(checkedListBox1.Items[i]));
}
}
sensCount = slist.Count;
if (showMin.Checked) persensor++;
if (showAvg.Checked) persensor++;
if (showMax.Checked) persensor++;
dataGridView1.ColumnCount = persensor * sensCount;
dataGridView1.ColumnHeadersVisible = true;
dataGridView1.RowHeadersWidth = 200;
int n = 0;
for (int i = 0; i < slist.Count; i++)
{
string sName = slist[i].ToString();
if (showMin.Checked) dataGridView1.Columns[n++].Name = sName + "(min)";
if (showAvg.Checked) dataGridView1.Columns[n++].Name = sName + "(avg)";
if (showMax.Checked) dataGridView1.Columns[n++].Name = sName + "(max)";
}
for (int i = 0; i < dataGridView1.Columns.Count; i++)
{
dataGridView1.Columns[i].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
dataGridView1.Columns[i].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
}
RefreshContents(slist);
}
private void RefreshContents(List<CustomYSensor> slist)
{
dataGridView1.SuspendLayout();
List<int> indexes = new List<int>();
for (int i = 0; i < slist.Count; i++)
{
indexes.Add(slist[i].curData.Count - 1);
}
dataGridView1.Rows.Clear();
bool sMin = false;
bool sMax = false;
bool sAvg = false;
int toShow = 0;
if (showMin.Checked) { sMin = true; toShow++; }
if (showAvg.Checked) { sAvg = true; toShow++; }
if (showMax.Checked) { sMax = true; toShow++; }
int colcount = slist.Count * toShow;
double MaxTimeStamp = 0;
for (int i = 0; i < slist.Count; i++)
if (indexes[i] >= 0)
if (MaxTimeStamp < slist[i].curData[indexes[i]].DateTime)
MaxTimeStamp = slist[i].curData[indexes[i]].DateTime;
int rowcount = 0;
while ((MaxTimeStamp > 0) && (rowcount < constants.MAXRAWDATAROWS))
{
rowcount++;
string[] row = new string[colcount];
double nextTimeStamp = 0;
for (int i = 0; i < slist.Count; i++)
{
bool Shown = false;
if (indexes[i] >= 0)
{ if (slist[i].curData[indexes[i]].DateTime == MaxTimeStamp)
{
int n = 0;
if (sMin) { row[n + i * toShow] = slist[i].minData[indexes[i]].Value.ToString(); n++; }
if (sAvg) { row[n + i * toShow] = slist[i].curData[indexes[i]].Value.ToString(); n++; }
if (sMax) { row[n + i * toShow] = slist[i].maxData[indexes[i]].Value.ToString(); n++; }
Shown = true;
indexes[i]--;
}
if (indexes[i] >= 0)
{
if (slist[i].curData[indexes[i]].DateTime > nextTimeStamp)
nextTimeStamp = slist[i].curData[indexes[i]].DateTime;
}
}
if (!Shown)
{
int n = 0;
if (sMin) { row[n + i * toShow] = ""; n++; }
if (sAvg) { row[n + i * toShow] = ""; n++; }
if (sMax) { row[n + i * toShow] = ""; n++; }
}
// if (indexes[i] > 0)
// {
// double t = slist[i].minData[indexes[i]].DateTime;
// if ((t > nextTimeStamp) && (t < MaxTimeStamp)) nextTimeStamp = t;
// }
}
if (colcount > 0)
{
int idx = dataGridView1.Rows.Add(row);
dataGridView1.Rows[idx].HeaderCell.Value = constants.UnixTimeStampToDateTime(MaxTimeStamp).ToString("yyyy/MM/dd HH:mm:ss.ff");
}
MaxTimeStamp = nextTimeStamp;
}
if (toShow == 0) label1.Text = "No data shown, select at least Min, Avg or Max options in the top-right checkboxes";
else if ((rowcount) == 0) label1.Text = "No data shown, select at least one sensor in the list above.";
else if (rowcount < constants.MAXRAWDATAROWS) label1.Text = "Here are the last " + rowcount.ToString() + " data rows.";
else label1.Text = "Here are the last " + rowcount.ToString() + " data rows. Use the export feature to get the whole data set.";
CsvBtn.Enabled = ((rowcount > 0) && (toShow > 0));
dataGridView1.ResumeLayout();
}
private List<string> getCvsdata(List<CustomYSensor> slist, bool sMin, bool sAvg, bool sMax)
{
int toShow = 0;
List<int> indexes = new List<int>();
for (int i = 0; i < slist.Count; i++) indexes.Add(0);
if (sMin) toShow++;
if (sAvg) toShow++;
if (sMax) toShow++;
List<string> res = new List<string>();
string ColumnHeader = "Timestamp";
for (int i = 0; i < slist.Count; i++)
{
string sName = slist[i].ToString();
if (sMin) ColumnHeader += ";" + sName + "(min)";
if (sAvg) ColumnHeader += ";" + sName + "(avg)";
if (sMax) ColumnHeader += ";" + sName + "(max)";
}
res.Add(ColumnHeader);
int colcount = slist.Count * toShow;
double MinTimeStamp = Double.MaxValue;
for (int i = 0; i < slist.Count; i++)
if (indexes[i] < slist[i].curData.Count)
if (MinTimeStamp > slist[i].curData[indexes[i]].DateTime)
MinTimeStamp = slist[i].curData[indexes[i]].DateTime;
while ((MinTimeStamp < Double.MaxValue))
{
string line = "";
string[] row = new string[colcount];
double nextTimeStamp = Double.MaxValue;
for (int i = 0; i < slist.Count; i++)
{
bool Shown = false;
if (indexes[i] < slist[i].curData.Count)
{ if (slist[i].curData[indexes[i]].DateTime == MinTimeStamp)
{
if (sMin) line += ";" + slist[i].minData[indexes[i]].Value.ToString();
if (sAvg) line += ";" + slist[i].curData[indexes[i]].Value.ToString();
if (sMax) line += ";" + slist[i].maxData[indexes[i]].Value.ToString();
Shown = true;
indexes[i]++;
}
if (indexes[i] < slist[i].curData.Count)
if (slist[i].curData[indexes[i]].DateTime < nextTimeStamp)
nextTimeStamp = slist[i].curData[indexes[i]].DateTime;
}
if (!Shown)
{
if (sMin) line += " ; ";
if (sAvg) line += " ; ";
if (sMax) line += " ; ";
}
// if (indexes[i] < slist[i].curData.Count)
// {
// double t = slist[i].minData[indexes[i]].DateTime;
// if ((t < nextTimeStamp) && (t > MinTimeStamp)) nextTimeStamp = t;
// }
}
if (colcount > 0)
{
line = constants.UnixTimeStampToDateTime(MinTimeStamp).ToString("yyyy/MM/dd HH:mm:ss.ff") + line;
}
res.Add(line);
MinTimeStamp = nextTimeStamp;
}
return res;
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
private void CheckedChanged(object sender, EventArgs e)
{
refeshGridGeometry(null);
}
private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
{
if (!refreshDisabled) refeshGridGeometry(e);
}
private void button2_Click(object sender, EventArgs e)
{
}
private void RawDataForm_FormClosing(object sender, FormClosingEventArgs e)
{
if (e.CloseReason == CloseReason.UserClosing)
{
e.Cancel = true;
Hide();
}
}
private List<CustomYSensor> selection()
{
List<CustomYSensor> slist = new List<CustomYSensor>();
for (int i = 0; i < checkedListBox1.Items.Count; i++)
{
if (checkedListBox1.GetItemChecked(i))
slist.Add((CustomYSensor)(checkedListBox1.Items[i]));
}
return slist;
}
private void button2_Click_1(object sender, EventArgs e)
{
RefreshContents(selection());
}
protected void export_DoWork(object sender, DoWorkEventArgs e)
{
object[] p = (object[])e.Argument;
Boolean[] s = (Boolean[])(p[2]);
List<String> data = getCvsdata((List<CustomYSensor>)p[1], s[0], s[1], s[2]);
StreamWriter sw = File.CreateText((String)p[0]);
int n = 0;
foreach (string line in data)
{
n++;
sw.WriteLine(line);
((BackgroundWorker)sender).ReportProgress(((int)(50 + 50.0 * n / data.Count)));
}
sw.Close();
}
protected void export_Completed(object sender, RunWorkerCompletedEventArgs e)
{
progressPanel.Visible = false;
progressBar1.Visible = false;
}
protected void export_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
progressBar1.Value = e.ProgressPercentage;
}
private void button1_Click(object sender, EventArgs e)
{
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
progressPanel.Visible = true;
progressBar1.Visible = true;
progressBar1.Value = 0;
exportProcess = new BackgroundWorker();
exportProcess.WorkerReportsProgress = true;
exportProcess.DoWork += new DoWorkEventHandler(export_DoWork);
exportProcess.RunWorkerCompleted += new RunWorkerCompletedEventHandler(export_Completed);
exportProcess.ProgressChanged += new ProgressChangedEventHandler(export_ProgressChanged);
exportProcess.RunWorkerAsync(new object[] { saveFileDialog1.FileName, selection(), new Boolean[] { showMin.Checked, showAvg.Checked, showMax.Checked } });
}
}
private void timer1_Tick(object sender, EventArgs e)
{
double total = 0;
double count = 0;
for (int i = 0; i < checkedListBox1.Items.Count; i++)
{
if (checkedListBox1.GetItemChecked(i))
{
total += ((CustomYSensor)(checkedListBox1.Items[i])).getGetaLoadProgress(); ;
count++;
}
}
int percent = (int)(total / count);
if ((count == 0) || (total >= 100)) Text = "Raw data";
else Text = "Raw data (Loading from dataloggers " + percent.ToString() + "%)";
}
private void selectAllToolStripMenuItem_Click(object sender, EventArgs e)
{
refreshDisabled = true;
for (int i = 0; i < checkedListBox1.Items.Count; i++)
{
checkedListBox1.SetItemChecked(i, true);
}
refreshDisabled = false;
refeshGridGeometry(null);
}
private void selectNoneToolStripMenuItem_Click(object sender, EventArgs e)
{
refreshDisabled = true;
for (int i = 0; i < checkedListBox1.Items.Count; i++)
{
checkedListBox1.SetItemChecked(i, false);
}
refreshDisabled = false;
refeshGridGeometry(null);
}
}
}