HamBook – Diff between revs 40 and 41

Subversion Repositories:
Rev:
Show entire fileIgnore whitespace
Rev 40 Rev 41
Line 1... Line 1...
1 using Configuration; 1 using Configuration;
2 using HamBook.Properties; 2 using HamBook.Properties;
3 using HamBook.Radios; 3 using HamBook.Radios;
4 using HamBook.Radios.Generic; 4 using HamBook.Radios.Generic;
-   5 using HamBook.Radios.Yaesu.FT_891;
5 using HamBook.Utilities.Serialization; 6 using HamBook.Utilities.Serialization;
6 using Serilog; 7 using Serilog;
7 using System; 8 using System;
8 using System.Collections; 9 using System.Collections;
9 using System.Collections.Concurrent; 10 using System.Collections.Concurrent;
Line 27... Line 28...
27 { 28 {
28 private Configuration.Configuration Configuration { get; set; } 29 private Configuration.Configuration Configuration { get; set; }
Line 29... Line 30...
29   30  
30 private CatAssemblies _catAssemblies; 31 private CatAssemblies _catAssemblies;
-   32 private CancellationToken _cancellationToken;
31 private CancellationToken _cancellationToken; 33 private Radios.Generic.MemoryBanks _memoryBanks;
32 private CancellationTokenSource _cancellationTokenSource; 34 private CancellationTokenSource _cancellationTokenSource;
33 private CancellationTokenSource _localCancellationTokenSource; 35 private CancellationTokenSource _localCancellationTokenSource;
Line 34... Line 36...
34 private CancellationToken _localCancellationToken; 36 private CancellationToken _localCancellationToken;
Line 44... Line 46...
44 public MemoryOrganizerForm(Configuration.Configuration configuration, CatAssemblies catAssemblies, CancellationToken cancellationToken) : this() 46 public MemoryOrganizerForm(Configuration.Configuration configuration, CatAssemblies catAssemblies, CancellationToken cancellationToken) : this()
45 { 47 {
46 Configuration = configuration; 48 Configuration = configuration;
47 _catAssemblies = catAssemblies; 49 _catAssemblies = catAssemblies;
Line 48... Line 50...
48   50  
49 _cancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(_cancellationToken, cancellationToken); 51 _cancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(_localCancellationToken, cancellationToken);
-   52 _cancellationToken = _cancellationTokenSource.Token;
-   53  
50 _cancellationToken = _cancellationTokenSource.Token; 54 _memoryBanks = Radios.Generic.MemoryBanks.Create(configuration.Radio);
Line 51... Line 55...
51 } 55 }
52   56  
53 /// <summary> 57 /// <summary>
Line 68... Line 72...
68 base.Dispose(disposing); 72 base.Dispose(disposing);
69 } 73 }
Line 70... Line 74...
70   74  
71 private async void button1_Click(object sender, EventArgs e) 75 private async void button1_Click(object sender, EventArgs e)
72 { 76 {
73 var rows = GetSelectedDataGridViewRows(dataGridView1); -  
74   77 var rows = dataGridView1.Rows.OfType<DataGridViewRow>().OrderBy(row => row.Index).ToList();
Line 75... Line 78...
75 var count = rows.Count; 78 var count = rows.Count;
76   79  
Line 95... Line 98...
95 rowProgress.Row.Cells["PhaseColumn"].Value = (string)result.Phase; 98 rowProgress.Row.Cells["PhaseColumn"].Value = (string)result.Phase;
96 rowProgress.Row.Cells["TagColumn"].Value = result.Tag; 99 rowProgress.Row.Cells["TagColumn"].Value = result.Tag;
97 rowProgress.Row.Cells["TextColumn"].Value = result.Text; 100 rowProgress.Row.Cells["TextColumn"].Value = result.Text;
98 rowProgress.Row.Tag = rowProgressSuccess.Data; 101 rowProgress.Row.Tag = rowProgressSuccess.Data;
Line -... Line 102...
-   102  
-   103  
99   104 toolStripStatusLabel1.Text = $"{Resources.Read_memory_bank} {rowProgress.Index + 1}";
100 break; 105 break;
101 case DataGridViewRowProgressFailure rowProgressFailure: 106 case DataGridViewRowProgressFailure rowProgressFailure:
Line -... Line 107...
-   107 Log.Error(rowProgressFailure.Exception, $"{Resources.Could_not_read_memory_bank}");
102 Log.Error(rowProgressFailure.Exception, $"{Resources.Could_not_read_memory_bank}"); 108  
103   109 toolStripStatusLabel1.Text = $"{Resources.Could_not_read_memory_bank} {rowProgress.Index + 1}";
Line 104... Line -...
104 break; -  
105 } -  
106   110 break;
107 toolStripStatusLabel1.Text = -  
108 $"{Resources.Read_memory_bank} {rowProgress.Index + 1}"; 111 }
109 toolStripProgressBar1.Increment(1); 112  
110   113 toolStripProgressBar1.Increment(1);
111 statusStrip1.Update(); 114 statusStrip1.Update();
112 } -  
-   115 }
113 catch(Exception) 116 catch (Exception exception)
114 { 117 {
Line 115... Line 118...
115   118 Log.Error(exception, Resources.Unexpected_error_while_reading_memory_bank);
Line 125... Line 128...
125 } 128 }
126 } 129 }
Line 127... Line 130...
127   130  
128 private async void button2_Click(object sender, EventArgs e) 131 private async void button2_Click(object sender, EventArgs e)
129 { 132 {
130 var rows = GetSelectedDataGridViewRows(dataGridView1); 133 var rows = dataGridView1.Rows.OfType<DataGridViewRow>().OrderBy(row => row.Index).ToList();
Line 131... Line 134...
131 var count = rows.Count; 134 var count = rows.Count;
132   135  
Line 140... Line 143...
140 switch (rowProgress) 143 switch (rowProgress)
141 { 144 {
142 case DataGridViewRowProgressSuccess<bool> rowProgressSuccess: 145 case DataGridViewRowProgressSuccess<bool> rowProgressSuccess:
143 var success = rowProgressSuccess.Data; 146 var success = rowProgressSuccess.Data;
Line 144... Line 147...
144   147  
145 if (!success) 148 if (success)
146 { -  
Line 147... Line 149...
147 Log.Error($"{Resources.Could_not_write_memory_bank}"); 149 {
148   150  
149 toolStripStatusLabel1.Text = 151 toolStripStatusLabel1.Text =
150 $"{Resources.Could_not_write_memory_bank} {rowProgress.Index + 1}"; 152 $"{Resources.Wrote_memory_bank} {rowProgress.Index + 1}";
151 toolStripProgressBar1.Increment(1); -  
152 statusStrip1.Update(); 153 toolStripProgressBar1.Increment(1);
153   154 statusStrip1.Update();
Line 154... Line -...
154 return; -  
155 } 155 return;
156   -  
157 toolStripStatusLabel1.Text = -  
158 $"{Resources.Wrote_memory_bank} {rowProgress.Index + 1}"; 156 }
159 toolStripProgressBar1.Increment(1); 157  
160 statusStrip1.Update(); 158 Log.Error($"{Resources.Could_not_write_memory_bank}");
161 break; -  
162 case DataGridViewRowProgressFailure rowProgressFailure: -  
163 Log.Error(rowProgressFailure.Exception, $"{Resources.Could_not_write_memory_bank}"); -  
164   -  
165 toolStripStatusLabel1.Text = -  
166 $"{Resources.Could_not_write_memory_bank} {rowProgress.Index + 1}"; 159 break;
167 toolStripProgressBar1.Increment(1); 160 case DataGridViewRowProgressFailure rowProgressFailure:
168 statusStrip1.Update(); -  
169 break; -  
170 } -  
Line -... Line 161...
-   161 Log.Error(rowProgressFailure.Exception, $"{Resources.Could_not_write_memory_bank}");
-   162 break;
-   163 }
-   164  
-   165 toolStripStatusLabel1.Text =
-   166 $"{Resources.Could_not_write_memory_bank} {rowProgress.Index + 1}";
-   167 toolStripProgressBar1.Increment(1);
-   168 statusStrip1.Update();
171 } 169 }
Line 172... Line 170...
172 catch(Exception) 170 catch (Exception exception)
Line 173... Line 171...
173 { 171 {
Line 188... Line 186...
188 private void MemoryOrganizerForm_Load(object sender, EventArgs e) 186 private void MemoryOrganizerForm_Load(object sender, EventArgs e)
189 { 187 {
190 toolStripProgressBar1.Minimum = 0; 188 toolStripProgressBar1.Minimum = 0;
191 toolStripProgressBar1.Maximum = 98; 189 toolStripProgressBar1.Maximum = 98;
Line 192... Line 190...
192   190  
193 var memoryBankQueue = new ConcurrentQueue<int>(); 191 var memoryBankQueue = new ConcurrentQueue<string>();
Line 194... Line 192...
194 var memoryBankReadTaskCompletionSource = new TaskCompletionSource<object>(); 192 var memoryBankAddRowsTaskCompletionSource = new TaskCompletionSource<object>();
195   193  
196 async void IdleHandler(object idleHandlerSender, EventArgs idleHandlerArgs) 194 async void IdleHandler(object idleHandlerSender, EventArgs idleHandlerArgs)
Line 197... Line 195...
197 { 195 {
198 await memoryBankReadTaskCompletionSource.Task; 196 await memoryBankAddRowsTaskCompletionSource.Task;
199   197  
200 try 198 try
Line 209... Line 207...
209 return; 207 return;
210 } 208 }
Line 211... Line 209...
211   209  
Line 212... Line 210...
212 var index = dataGridView1.Rows.Add(); 210 var index = dataGridView1.Rows.Add();
213   211  
214 dataGridView1.Rows[index].Cells["LocationColumn"].Value = $"{memoryBank:000}"; 212 dataGridView1.Rows[index].Cells["LocationColumn"].Value = memoryBank;
215 dataGridView1.Rows[index].Cells["FrequencyColumn"].Value = default; 213 dataGridView1.Rows[index].Cells["FrequencyColumn"].Value = default;
216 dataGridView1.Rows[index].Cells["ClarifierDirectionColumn"].Value = default; 214 dataGridView1.Rows[index].Cells["ClarifierDirectionColumn"].Value = default;
217 dataGridView1.Rows[index].Cells["ClarifierOffsetColumn"].Value = default; 215 dataGridView1.Rows[index].Cells["ClarifierOffsetColumn"].Value = default;
Line 235... Line 233...
235 } 233 }
Line 236... Line 234...
236   234  
237 Application.Idle += IdleHandler; 235 Application.Idle += IdleHandler;
238 try 236 try
239 { 237 {
240 foreach (var memoryBank in Enumerable.Range(1, 99)) 238 foreach (var memoryBank in _memoryBanks.GetMemoryBanks())
241 { 239 {
242 memoryBankQueue.Enqueue(memoryBank); 240 memoryBankQueue.Enqueue(memoryBank);
Line 243... Line 241...
243 } 241 }
244   242  
245 memoryBankReadTaskCompletionSource.TrySetResult(new { }); 243 memoryBankAddRowsTaskCompletionSource.TrySetResult(new { });
246 } 244 }
247 catch (Exception exception) 245 catch (Exception exception)
Line 345... Line 343...
345   343  
346 for (var i = 0; i < count && !cancellationToken.IsCancellationRequested; ++i) 344 for (var i = 0; i < count && !cancellationToken.IsCancellationRequested; ++i)
347 { 345 {
348 try 346 try
349 { 347 {
Line 350... Line -...
350 var location = int.Parse($"{rows[i].Cells["LocationColumn"].Value}"); -  
351   -  
352 var memoryBank = $"{location:000}"; 348 var location = $"{rows[i].Cells["LocationColumn"].Value}";
Line 353... Line 349...
353   349  
354 var result = await _catAssemblies.CatReadAsync<MemoryChannel>("MT", new object[] { memoryBank }, _cancellationToken); 350 var result = await _catAssemblies.CatReadAsync<MemoryChannel>("MT", new object[] { location }, _cancellationToken);
355   351  
356 progress.Report(new DataGridViewRowProgressSuccess<MemoryChannel>(rows[i], i, result)); 352 progress.Report(new DataGridViewRowProgressSuccess<MemoryChannel>(rows[i], i, result));
Line 373... Line 369...
373   369  
374 for (var i = 0; i < count && !cancellationToken.IsCancellationRequested; ++i) 370 for (var i = 0; i < count && !cancellationToken.IsCancellationRequested; ++i)
375 { 371 {
376 try 372 try
377 { 373 {
Line 378... Line -...
378 var location = int.Parse($"{rows[i].Cells["LocationColumn"].Value}"); -  
379   374 var location = $"{rows[i].Cells["LocationColumn"].Value}";
380 var currentLocation = $"{location:000}"; 375  
381 var frequency = int.Parse($"{rows[i].Cells["FrequencyColumn"].Value}"); 376 var frequency = int.Parse($"{rows[i].Cells["FrequencyColumn"].Value}");
382 var clarifierDirection = char.Parse($"{rows[i].Cells["ClarifierDirectionColumn"].Value}"); 377 var clarifierDirection = char.Parse($"{rows[i].Cells["ClarifierDirectionColumn"].Value}");
383 var clarifierOffset = int.Parse($"{rows[i].Cells["ClarifierOffsetColumn"].Value}"); 378 var clarifierOffset = int.Parse($"{rows[i].Cells["ClarifierOffsetColumn"].Value}");
Line 388... Line 383...
388 var tag = Convert.ToBoolean(rows[i].Cells["TagColumn"].Value); 383 var tag = Convert.ToBoolean(rows[i].Cells["TagColumn"].Value);
389 var text = $"{rows[i].Cells["TextColumn"].Value, -12}"; 384 var text = $"{rows[i].Cells["TextColumn"].Value, -12}";
Line 390... Line 385...
390   385  
391 var memoryChannel = new MemoryChannel 386 var memoryChannel = new MemoryChannel
392 { 387 {
393 CurrentLocation = currentLocation, 388 CurrentLocation = location,
394 Frequency = frequency, 389 Frequency = frequency,
395 ClarifierDirection = clarifierDirection, 390 ClarifierDirection = clarifierDirection,
396 ClarifierOffset = clarifierOffset, 391 ClarifierOffset = clarifierOffset,
397 Clar = clar, 392 Clar = clar,
Line 551... Line 546...
551 { 546 {
552 _cancellationTokenSource.Cancel(); 547 _cancellationTokenSource.Cancel();
553 } 548 }
554 } 549 }
Line 555... Line 550...
555   550  
556 private void DataGridView1_DefaultValuesNeeded(object sender, DataGridViewRowEventArgs e) 551 private async void readToolStripMenuItem_Click(object sender, EventArgs e)
-   552 {
-   553 var rows = GetSelectedDataGridViewRows(dataGridView1);
-   554  
-   555 var count = rows.Count;
-   556  
-   557 toolStripProgressBar1.Minimum = 0;
-   558 toolStripProgressBar1.Maximum = count;
-   559  
-   560 var progress = new Progress<DataGridViewRowProgress>(rowProgress =>
-   561 {
-   562 try
-   563 {
-   564 switch (rowProgress)
-   565 {
-   566 case DataGridViewRowProgressSuccess<MemoryChannel> rowProgressSuccess:
-   567 var result = rowProgressSuccess.Data;
-   568  
-   569 rowProgress.Row.Cells["FrequencyColumn"].Value = result.Frequency;
-   570 rowProgress.Row.Cells["ClarifierDirectionColumn"].Value = (char)result.ClarifierDirection;
-   571 rowProgress.Row.Cells["ClarifierOffsetColumn"].Value = result.ClarifierOffset;
-   572 rowProgress.Row.Cells["ClarColumn"].Value = result.Clar;
-   573 rowProgress.Row.Cells["ModeColumn"].Value = (string)result.MemoryRadioMode;
-   574 rowProgress.Row.Cells["CtcssColumn"].Value = (string)result.CtcssMode;
-   575 rowProgress.Row.Cells["PhaseColumn"].Value = (string)result.Phase;
-   576 rowProgress.Row.Cells["TagColumn"].Value = result.Tag;
-   577 rowProgress.Row.Cells["TextColumn"].Value = result.Text;
-   578 rowProgress.Row.Tag = rowProgressSuccess.Data;
-   579  
-   580  
-   581 toolStripStatusLabel1.Text = $"{Resources.Read_memory_bank} {rowProgress.Index + 1}";
-   582 break;
-   583 case DataGridViewRowProgressFailure rowProgressFailure:
-   584 Log.Error(rowProgressFailure.Exception, $"{Resources.Could_not_read_memory_bank}");
-   585  
-   586 toolStripStatusLabel1.Text = $"{Resources.Could_not_read_memory_bank} {rowProgress.Index + 1}";
-   587 break;
-   588 }
-   589  
-   590 toolStripProgressBar1.Increment(1);
-   591 statusStrip1.Update();
-   592 }
-   593 catch (Exception exception)
-   594 {
-   595 Log.Error(exception, Resources.Unexpected_error_while_reading_memory_bank);
-   596 }
-   597 });
-   598  
-   599 await Task.Run(() => ReadMemoryBanks(rows, progress, _cancellationToken), _cancellationToken);
-   600  
-   601 if (!_cancellationToken.IsCancellationRequested)
-   602 {
-   603 toolStripProgressBar1.Value = toolStripProgressBar1.Maximum;
-   604 toolStripStatusLabel1.Text = "Done.";
-   605 }
-   606 }
-   607  
-   608 private async void writeToolStripMenuItem_Click(object sender, EventArgs e)
-   609 {
557 { 610 var rows = GetSelectedDataGridViewRows(dataGridView1);
-   611 var count = rows.Count;
-   612  
-   613 toolStripProgressBar1.Minimum = 0;
-   614 toolStripProgressBar1.Maximum = count;
-   615  
-   616 var progress = new Progress<DataGridViewRowProgress>(rowProgress =>
-   617 {
-   618 try
-   619 {
-   620 switch (rowProgress)
-   621 {
-   622 case DataGridViewRowProgressSuccess<bool> rowProgressSuccess:
-   623 var success = rowProgressSuccess.Data;
-   624  
-   625 if (success)
-   626 {
-   627  
-   628 toolStripStatusLabel1.Text =
-   629 $"{Resources.Wrote_memory_bank} {rowProgress.Index + 1}";
-   630 toolStripProgressBar1.Increment(1);
-   631 statusStrip1.Update();
-   632 return;
-   633 }
-   634  
-   635 Log.Error($"{Resources.Could_not_write_memory_bank}");
-   636 break;
-   637 case DataGridViewRowProgressFailure rowProgressFailure:
-   638 Log.Error(rowProgressFailure.Exception, $"{Resources.Could_not_write_memory_bank}");
-   639 break;
-   640 }
-   641  
-   642 toolStripStatusLabel1.Text =
-   643 $"{Resources.Could_not_write_memory_bank} {rowProgress.Index + 1}";
-   644 toolStripProgressBar1.Increment(1);
-   645 statusStrip1.Update();
-   646 }
-   647 catch (Exception exception)
-   648 {
-   649 Log.Error(exception, Resources.Unexpected_error_while_writing_memory_bank);
-   650 }
-   651  
-   652 });
-   653  
-   654 await Task.Run(() => WriteMemoryBanks(rows, progress, _cancellationToken), _cancellationToken);
-   655  
-   656 if (!_cancellationToken.IsCancellationRequested)
-   657 {
-   658 toolStripProgressBar1.Value = toolStripProgressBar1.Maximum;
-   659 toolStripStatusLabel1.Text = "Done.";
558 var a = e.Row; 660 }
559 } 661 }
560 } 662 }