-
Notifications
You must be signed in to change notification settings - Fork 0
/
LogSearch.cs
598 lines (502 loc) · 22.1 KB
/
LogSearch.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
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Windows.Forms;
using System.IO;
namespace StreamReader1
{
public partial class LogSearch : Form
{
LogServiceUtility _logService = new LogServiceUtility();
LogSearchData _logSearchData = new LogSearchData();
List<string> _oriDate = new List<string>();
List<string> _oriTime = new List<string>();
List<string> _oriAttribute = new List<string>();
List<string> _oriSignal = new List<string>();
List<string> _lstDate = new List<string>();
List<string> _lstTime = new List<string>();
List<string> _lstAttribute = new List<string>();
List<string> _lstSignal = new List<string>();
List<string> _lstResult = new List<string>();
public LogSearch()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
lstShow.Columns.Add("日期", 60);
lstShow.Columns.Add("時間", 60);
lstShow.Columns.Add("屬性", 60);
lstShow.Columns.Add("訊號", 700);
}
private void openFile_Click(object sender, EventArgs e)
{
lstShow.Items.Clear();
openFileDialog1.Filter = @"LOG|*.log";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
try
{
filename = openFileDialog1.FileName;
}
catch (Exception)
{
MessageBox.Show(@"請選取適當格式", @"注意");
}
}
//清空上次讀檔產生的數據
_oriDate.Clear();
_oriTime.Clear();
_oriAttribute.Clear();
_oriSignal.Clear();
StreamReader sr = new StreamReader(filename);
string data;
do
{
data = sr.ReadLine(); // 讀取一行文字資料
if (string.IsNullOrEmpty(data)) break; // 若資料讀取完畢,跳離迴圈
_oriDate.Add(data.Substring(0, 8)); //切割日期
_oriTime.Add(data.Substring(9, 8)); //切割時間
_oriAttribute.Add(data.Substring(18, 8));//切割屬性
_oriSignal.Add(data.Substring(34)); //切割訊號
} while (true);
sr.Close();
DrawListview();
}
private void btn_search_Click(object sender, EventArgs e)
{
lstShow.Items.Clear();
_lstResult.Clear();
if ((string) cbb_attr.SelectedItem == "Received")
{
if(string.IsNullOrEmpty((string)cbb_case.SelectedItem))
DrawListview((string) cbb_attr.SelectedItem);
else
{
DrawListview((string)cbb_attr.SelectedItem, (string)cbb_case.SelectedItem);
}
}
else if ((string) cbb_attr.SelectedItem == "Send")
{
if (string.IsNullOrEmpty((string)cbb_case.SelectedItem))
DrawListview((string)cbb_attr.SelectedItem);
else
{
DrawListview((string)cbb_attr.SelectedItem, (string)cbb_case.SelectedItem);
}
}
else
{
DrawListview();
}
MessageBox.Show(@"符合Fuct選取條件的數目:"+_lstResult.Count);
}
private void cbb_case_SelectedIndexChanged(object sender, EventArgs e)
{
if (cbb_case.SelectedIndex == 0)
{
cbb_fuct.Items.Clear();
cbb_fuct.Items.AddRange(_logSearchData.Case0);
cbb_type.Items.Clear();
cbb_type.Items.AddRange(_logSearchData.Type0);
}
else if (cbb_case.SelectedIndex == 1)
{
cbb_fuct.Items.Clear();
cbb_fuct.Items.AddRange(_logSearchData.Case1);
cbb_type.Items.Clear();
cbb_type.Items.AddRange(_logSearchData.Type1);
}
else if (cbb_case.SelectedIndex == 2)
{
cbb_fuct.Items.Clear();
cbb_fuct.Items.AddRange(_logSearchData.Case2);
cbb_type.Items.Clear();
cbb_type.Items.AddRange(_logSearchData.Type2);
}
else if (cbb_case.SelectedIndex == 3)
{
cbb_fuct.Items.Clear();
cbb_fuct.Items.AddRange(_logSearchData.Case3);
cbb_type.Items.Clear();
cbb_type.Items.AddRange(_logSearchData.Type3);
}
else if (cbb_case.SelectedIndex == 4)
{
cbb_fuct.Items.Clear();
cbb_fuct.Items.AddRange(_logSearchData.Case4);
cbb_type.Items.Clear();
cbb_type.Items.AddRange(_logSearchData.Type4);
}
else
{
cbb_type.Items.Clear();
cbb_type.Items.AddRange(_logSearchData.Type4);
}
}
private void lstShow_SelectedIndexChanged(object sender, EventArgs e)
{
if (lstShow.SelectedIndices.Count > 0)
{
//分割訊號並放入Com_Tx_Buffer陣列中
char[] separators = { ' ', '\n', '\r', '\t' };
var Com_Tx_Buffer = _lstSignal[lstShow.SelectedIndices[0]].ToString().Split(separators, StringSplitOptions.RemoveEmptyEntries);
int Com_Packet_Type = 1;
switch (Convert.ToInt32(Com_Tx_Buffer[1]))
{
case 0x00:
break;
case Global.COM_SW_RESET:
break;
case Global.COM_NORMAL_MODE:
Com_Packet_Type = 0;
break;
case Global.COM_CALIBRATION_MODE:
Com_Packet_Type = 0;
break;
case Global.COM_CONSOLE_STATUS:
Com_Packet_Type = 1;
break;
case Global.COM_SET_PARAMETER:
Com_Packet_Type = 1;
break;
case Global.COM_CLEAR_COUNTER:
Com_Packet_Type = 1;
break;
case Global.COM_POLL_CALIBRATION_1:
Com_Packet_Type = 2;
break;
case Global.COM_POLL_CALIBRATION_2:
Com_Packet_Type = 3;
break;
case Global.COM_SET_CALIBRATION_1:
Com_Packet_Type = 2;
break;
case Global.COM_SET_CALIBRATION_2:
Com_Packet_Type = 3;
break;
case Global.COM_SET_MICROPULSE:
Com_Packet_Type = 4;
break;
case Global.COM_SAVE_CALIBRATION:
Com_Packet_Type = 1;
break;
case Global.COM_SAVE_XMODEM:
Com_Packet_Type = 1;
break;
case Global.COM_BURST_MODE:
Com_Packet_Type = 1;
break;
case Global.COM_BURST_MODE_DISABLE:
Com_Packet_Type = 1;
break;
case Global.COM_CLEAR_FIRED_COUNTER:
Com_Packet_Type = 1;
break;
case Global.COM_FLASH_RESET:
break;
case Global.DIAG_POWER_UP:
break;
case Global.DIAG_POWER_DOWN:
break;
default:
Com_Packet_Type = 1;
break;
}
switch (Com_Packet_Type)
{
case 0:
_logService.CreateSignalDataLabel(panel1, _logSearchData.Case0, Com_Tx_Buffer, _logSearchData.Cg0, _logSearchData.Cb0);
dec_case.Text = "0";
break;
case 1:
fill_default: ;
_logService.CreateSignalDataLabel(panel1, _logSearchData.Case1, Com_Tx_Buffer, _logSearchData.Cg1, _logSearchData.Cb1);
dec_case.Text = "1";
break;
case 2:
_logService.CreateSignalDataLabel(panel1, _logSearchData.Case2, Com_Tx_Buffer, _logSearchData.Cg2, _logSearchData.Cb2);
dec_case.Text = "2";
break;
case 3:
_logService.CreateSignalDataLabel(panel1, _logSearchData.Case3, Com_Tx_Buffer, _logSearchData.Cg3, _logSearchData.Cb3);
dec_case.Text = "3";
break;
case 4:
_logService.CreateSignalDataLabel(panel1, _logSearchData.Case4, Com_Tx_Buffer, _logSearchData.Cg4, _logSearchData.Cb4);
dec_case.Text = "4";
break;
default:
goto fill_default;
}
}
}
/// <summary>
/// 判斷搜尋的Fuction.
/// </summary>
/// <param name="nameCount">The name count.</param>
/// <param name="packet">The packet.</param>
/// <param name="cg">The cg.</param>
/// <param name="cb">The cb.</param>
/// <param name="chk">選擇的Fuct索引值</param>
/// <param name="judge">判斷大於、小於、等於</param>
/// <param name="txt">使用者數入搜尋資料</param>
private void Judgefuct(int nameCount, string[] packet, int[] cg, int[] cb, int chk, string judge, string txt)
{
/*chk代表訊號的特定值(index) 過濾使用oriList 確認為顯示資料轉存到lstList中 每次逐個幾確認並且增加*/
//判斷是否選取fuct
if (chk > -1)
{
//存放訊號值 依照不同CASE訊號值長度也不同
string[] content = new string[nameCount];
/*分析每個封包並轉成訊號值放入content[]中*/
int jump = 2;
for (int i = 0; i < nameCount; i++)
{
for (int j = jump; j < 46; j++)
{
if (cg.Contains(j))
{
content[i] = _logService.Cgsignal(packet[j]);
jump++;
}
else if (cb.Contains(j))
{
content[i] = _logService.Cbsignal(packet[j], packet[j + 1]);
jump = jump + 2;
}
break;
}
}
/*判斷每個訊號裡面的指定fuct是否符合搜尋條件*/
int storeIndex = 0;
//大於
if (judge == ">")
{
if (int.Parse(content[chk]) > int.Parse(txt))
{
storeIndex = Convert.ToInt32(_lstDate.Count.ToString()) - 1;
_lstResult.Add(storeIndex.ToString());
}
}
//等於
else if (judge == "=")
{
if (int.Parse(content[chk]) == int.Parse(txt))
{
storeIndex = Convert.ToInt32(_lstDate.Count.ToString()) - 1;
_lstResult.Add(storeIndex.ToString());
}
}
//小於
else if (judge == "<")
{
if (int.Parse(content[chk]) < int.Parse(txt))
{
storeIndex = Convert.ToInt32(_lstDate.Count.ToString()) - 1;
_lstResult.Add(storeIndex.ToString());
}
}
}
}
#region 繪製ListView
/// <summary>
/// 重新繪製Listview
/// </summary>
private void DrawListview()
{
//清空lst數據
_lstDate.Clear();
_lstTime.Clear();
_lstAttribute.Clear();
_lstSignal.Clear();
//導入lstShow裡面
lstShow.BeginUpdate();
for (int i = 0; i < Convert.ToInt32(_oriDate.Count.ToString()); i++)
{
Draw(i);
}
lstShow.EndUpdate();
}
/// <summary>
/// 重新繪製Listview
/// </summary>
/// <param name="attribuate">The attribuate. (string)</param>
private void DrawListview(string attribuate)
{
//清空lst數據
_lstDate.Clear();
_lstTime.Clear();
_lstAttribute.Clear();
_lstSignal.Clear();
lstShow.BeginUpdate();
for (int i = 0; i < Convert.ToInt32(_oriDate.Count.ToString()); i++)
{
if (_oriAttribute[i].Contains(attribuate))
{
Draw(i);
}
}
lstShow.EndUpdate();
}
/// <summary>
/// 重新繪製Listview
/// </summary>
/// <param name="attribuate">The attribuate. (string)</param>
/// <param name="group">The case. (string)</param>
private void DrawListview(string attribuate, string group)
{
//清空lst數據
_lstDate.Clear();
_lstTime.Clear();
_lstAttribute.Clear();
_lstSignal.Clear();
lstShow.BeginUpdate();
for (int i = 0; i < Convert.ToInt32(_oriDate.Count.ToString()); i++)
{
if (_oriAttribute[i].Contains(attribuate))
{
var Com_Tx_Buffer = SplitSignal(_oriSignal[i]);
int chk = Convert.ToInt32(Com_Tx_Buffer[1]);
switch (group)
{
case "case 0":
if (chk == Global.COM_NORMAL_MODE || chk == Global.COM_CALIBRATION_MODE)
{
Draw(i);
}
Judgefuct(_logSearchData.Case0.Length, Com_Tx_Buffer, _logSearchData.Cg0, _logSearchData.Cb0, cbb_fuct.SelectedIndex, (string)cbb_judge.SelectedItem, txt_num.Text);
break;
case "case 1":
if (chk != Global.COM_NORMAL_MODE && chk != Global.COM_CALIBRATION_MODE && chk != Global.COM_POLL_CALIBRATION_1 && chk != Global.COM_SET_CALIBRATION_1 &&
chk != Global.COM_POLL_CALIBRATION_2 && chk != Global.COM_SET_CALIBRATION_2 && chk != Global.COM_SET_MICROPULSE)
{
Draw(i);
}
Judgefuct(_logSearchData.Case1.Length, Com_Tx_Buffer, _logSearchData.Cg1, _logSearchData.Cb1, cbb_fuct.SelectedIndex, (string)cbb_judge.SelectedItem, txt_num.Text);
break;
case "case 2":
if (chk == Global.COM_POLL_CALIBRATION_1 || chk == Global.COM_SET_CALIBRATION_1)
{
Draw(i);
}
Judgefuct(_logSearchData.Case2.Length, Com_Tx_Buffer, _logSearchData.Cg2, _logSearchData.Cb2, cbb_fuct.SelectedIndex, (string)cbb_judge.SelectedItem, txt_num.Text);
break;
case "case 3":
if (chk == Global.COM_POLL_CALIBRATION_2 || chk == Global.COM_SET_CALIBRATION_2)
{
Draw(i);
}
Judgefuct(_logSearchData.Case3.Length, Com_Tx_Buffer, _logSearchData.Cg3, _logSearchData.Cb3, cbb_fuct.SelectedIndex, (string)cbb_judge.SelectedItem, txt_num.Text);
break;
case "case 4":
if (chk == Global.COM_SET_MICROPULSE)
{
Draw(i);
}
Judgefuct(_logSearchData.Case4.Length, Com_Tx_Buffer, _logSearchData.Cg4, _logSearchData.Cb4, cbb_fuct.SelectedIndex, (string)cbb_judge.SelectedItem, txt_num.Text);
break;
}
}
}
lstShow.EndUpdate();
}
/// <summary>
/// 繪製Listview裡面的項目
/// </summary>
/// <param name="i">需要繪製的個數 (int)</param>
private void Draw(int i)
{
//宣告一個ListViewItem物件
ListViewItem lvi = new ListViewItem(_oriDate[i]);
lvi.SubItems.Add(_oriTime[i]);
lvi.SubItems.Add(_oriAttribute[i]);
lvi.SubItems.Add(_oriSignal[i]);
lstShow.Items.Add(lvi);//新增項目
_lstDate.Add(_oriDate[i]);
_lstTime.Add(_oriTime[i]);
_lstAttribute.Add(_oriAttribute[i]);
_lstSignal.Add(_oriSignal[i]);
}
#endregion
#region 分割訊號
/// <summary>
/// 分割singal放入Com_Tx_Buffer[]裡面 共會有48個.
/// </summary>
/// <param name="signal">The signal.</param>
/// <returns></returns>
private string[] SplitSignal(string signal)
{
//分割訊號並放入Com_Tx_Buffer陣列中
char[] separators = { ' ', '\n', '\r', '\t' };
var Com_Tx_Buffer = signal.Split(separators, StringSplitOptions.RemoveEmptyEntries);
return Com_Tx_Buffer;
}
#endregion
private void lstShow_DrawItem(object sender, DrawListViewItemEventArgs e)
{
ListView listView = new ListView();
listView = (ListView)sender;
e.DrawBackground();
e.Graphics.DrawString(listView.Items[e.ItemIndex].Text, e.Item.Font, Brushes.Black, e.Bounds, StringFormat.GenericDefault);
for (int i = 0; i < _lstResult.Count; i++)
{
if (e.ItemIndex.ToString() == _lstResult[i])
{
bool selected = ((e.State & ListViewItemStates.Selected) == ListViewItemStates.Selected);
//搜尋過後尚未選取發生的事件
if(!selected)
{
e.Graphics.FillRectangle(new SolidBrush(Color.Silver), e.Bounds);
e.Graphics.DrawString(listView.Items[e.ItemIndex].Text, e.Item.Font, Brushes.Black, e.Bounds, StringFormat.GenericDefault);
}
//搜尋過選取時發生的事件
else
{
}
}
//搜尋過後不符合的項目想發生的事件往這寫
}
if (((e.State & ListViewItemStates.Selected) == ListViewItemStates.Selected))
{
////設定指定物件填充背景的大小
//Rectangle r = new Rectangle(e.Bounds.Left + 4, e.Bounds.Top, TextRenderer.MeasureText(e.Item.Text, e.Item.Font).Width, e.Bounds.Height);
////設定填充的顏色和無間
//e.Graphics.FillRectangle(SystemBrushes.Highlight, r);
////設定物件中的字體顏色
//e.Item.ForeColor = SystemColors.HighlightText;
//e.DrawText();
e.Graphics.FillRectangle(SystemBrushes.Highlight, e.Bounds);
e.Graphics.DrawString(listView.Items[e.ItemIndex].Text, e.Item.Font, Brushes.White, e.Bounds, StringFormat.GenericDefault);
}
e.DrawFocusRectangle();
}
private void lstShow_DrawColumnHeader(object sender, DrawListViewColumnHeaderEventArgs e)
{
e.DrawDefault = true;
}
private void lstShow_DrawSubItem(object sender, DrawListViewSubItemEventArgs e)
{
if (e.ColumnIndex > 0)
{
e.DrawBackground();
string searchTerm = "";
int index = e.SubItem.Text.IndexOf(searchTerm);
if (index >= 0)
{
string sBefore = e.SubItem.Text.Substring(0, index);
Size bounds = new Size(e.Bounds.Width, e.Bounds.Height);
Size s1 = TextRenderer.MeasureText(e.Graphics, sBefore, this.Font, bounds);
Size s2 = TextRenderer.MeasureText(e.Graphics, searchTerm, this.Font, bounds);
Rectangle rect = new Rectangle(e.Bounds.X + s1.Width, e.Bounds.Y, s2.Width, e.Bounds.Height);
e.Graphics.SetClip(e.Bounds);
e.Graphics.FillRectangle(new SolidBrush(Color.Yellow), rect);
e.Graphics.ResetClip();
}
e.DrawText();
}
}
}
}