HamBook – Blame information for rev 47

Subversion Repositories:
Rev:
Rev Author Line No. Line
15 office 1 using Configuration;
2 using HamBook.Properties;
3 using HamBook.Radios;
4 using HamBook.Radios.Generic;
41 office 5 using HamBook.Radios.Yaesu.FT_891;
16 office 6 using HamBook.Utilities.Serialization;
15 office 7 using Serilog;
8 using System;
9 using System.Collections;
10 using System.Collections.Concurrent;
11 using System.Collections.Generic;
12 using System.ComponentModel;
13 using System.Data;
14 using System.Diagnostics;
15 using System.Drawing;
16 using System.IO;
17 using System.Linq;
18 using System.Reflection;
47 office 19 using System.Runtime.CompilerServices;
15 office 20 using System.Text;
21 using System.Text.RegularExpressions;
22 using System.Threading;
23 using System.Threading.Tasks;
24 using System.Windows.Forms;
46 office 25 using MemoryChannel = HamBook.Radios.Generic.MemoryChannel;
15 office 26  
27 namespace HamBook
28 {
29 public partial class MemoryOrganizerForm : Form
30 {
31 private Configuration.Configuration Configuration { get; set; }
32  
33 private CatAssemblies _catAssemblies;
34 private CancellationToken _cancellationToken;
41 office 35 private Radios.Generic.MemoryBanks _memoryBanks;
22 office 36 private CancellationTokenSource _cancellationTokenSource;
37 private CancellationTokenSource _localCancellationTokenSource;
38 private CancellationToken _localCancellationToken;
15 office 39  
40 public MemoryOrganizerForm()
41 {
42 InitializeComponent();
22 office 43  
44 _localCancellationTokenSource = new CancellationTokenSource();
45 _localCancellationToken = _localCancellationTokenSource.Token;
15 office 46 }
47  
48 public MemoryOrganizerForm(Configuration.Configuration configuration, CatAssemblies catAssemblies, CancellationToken cancellationToken) : this()
49 {
50 Configuration = configuration;
51 _catAssemblies = catAssemblies;
22 office 52  
41 office 53 _cancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(_localCancellationToken, cancellationToken);
22 office 54 _cancellationToken = _cancellationTokenSource.Token;
41 office 55  
45 office 56 _memoryBanks = Radios.Generic.MemoryBanks.Create(Configuration.Radio);
15 office 57 }
58  
59 /// <summary>
60 /// Clean up any resources being used.
61 /// </summary>
62 /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
63 protected override void Dispose(bool disposing)
64 {
65 if (disposing && (components != null))
66 {
22 office 67 if (_cancellationTokenSource != null)
68 {
69 _cancellationTokenSource.Cancel();
70 }
71  
15 office 72 components.Dispose();
73 }
74 base.Dispose(disposing);
75 }
76  
77 private async void button1_Click(object sender, EventArgs e)
78 {
41 office 79 var rows = dataGridView1.Rows.OfType<DataGridViewRow>().OrderBy(row => row.Index).ToList();
22 office 80 var count = rows.Count;
81  
82 toolStripProgressBar1.Minimum = 0;
83 toolStripProgressBar1.Maximum = count;
84  
85 var progress = new Progress<DataGridViewRowProgress>(rowProgress =>
86 {
87 try
88 {
89 switch (rowProgress)
90 {
91 case DataGridViewRowProgressSuccess<MemoryChannel> rowProgressSuccess:
92  
47 office 93 switch(Configuration.Radio)
94 {
95 case "Yaesu FT-891":
96 var result = (Radios.Yaesu.FT_891.MemoryChannel)rowProgressSuccess.Data;
22 office 97  
47 office 98 rowProgress.Row.Cells["FrequencyColumn"].Value = result.Frequency;
99 rowProgress.Row.Cells["ClarifierDirectionColumn"].Value = (char)result.ClarifierDirection;
100 rowProgress.Row.Cells["ClarifierOffsetColumn"].Value = result.ClarifierOffset;
101 rowProgress.Row.Cells["ClarColumn"].Value = result.Clar;
102 rowProgress.Row.Cells["ModeColumn"].Value = result.MemoryRadioMode.Name;
103 rowProgress.Row.Cells["CtcssColumn"].Value = (string)result.Ctcss;
104 rowProgress.Row.Cells["PhaseColumn"].Value = (string)result.Phase;
105 rowProgress.Row.Cells["TagColumn"].Value = result.Tag;
106 rowProgress.Row.Cells["TextColumn"].Value = result.Text;
107 rowProgress.Row.Tag = rowProgressSuccess.Data;
108 break;
109 }
41 office 110  
111 toolStripStatusLabel1.Text = $"{Resources.Read_memory_bank} {rowProgress.Index + 1}";
22 office 112 break;
113 case DataGridViewRowProgressFailure rowProgressFailure:
114 Log.Error(rowProgressFailure.Exception, $"{Resources.Could_not_read_memory_bank}");
115  
41 office 116 toolStripStatusLabel1.Text = $"{Resources.Could_not_read_memory_bank} {rowProgress.Index + 1}";
22 office 117 break;
118 }
119  
120 toolStripProgressBar1.Increment(1);
121 statusStrip1.Update();
122 }
41 office 123 catch (Exception exception)
22 office 124 {
41 office 125 Log.Error(exception, Resources.Unexpected_error_while_reading_memory_bank);
22 office 126 }
127 });
128  
129 await Task.Run(() => ReadMemoryBanks(rows, progress, _cancellationToken), _cancellationToken);
130  
131 if (!_cancellationToken.IsCancellationRequested)
132 {
133 toolStripProgressBar1.Value = toolStripProgressBar1.Maximum;
134 toolStripStatusLabel1.Text = "Done.";
135 }
15 office 136 }
137  
138 private async void button2_Click(object sender, EventArgs e)
139 {
41 office 140 var rows = dataGridView1.Rows.OfType<DataGridViewRow>().OrderBy(row => row.Index).ToList();
22 office 141 var count = rows.Count;
142  
143 toolStripProgressBar1.Minimum = 0;
144 toolStripProgressBar1.Maximum = count;
145  
146 var progress = new Progress<DataGridViewRowProgress>(rowProgress =>
147 {
148 try
149 {
150 switch (rowProgress)
151 {
152 case DataGridViewRowProgressSuccess<bool> rowProgressSuccess:
153 var success = rowProgressSuccess.Data;
154  
41 office 155 if (success)
22 office 156 {
157  
158 toolStripStatusLabel1.Text =
41 office 159 $"{Resources.Wrote_memory_bank} {rowProgress.Index + 1}";
22 office 160 toolStripProgressBar1.Increment(1);
161 statusStrip1.Update();
162 return;
163 }
164  
41 office 165 Log.Error($"{Resources.Could_not_write_memory_bank}");
22 office 166 break;
167 case DataGridViewRowProgressFailure rowProgressFailure:
168 Log.Error(rowProgressFailure.Exception, $"{Resources.Could_not_write_memory_bank}");
169 break;
170 }
171  
41 office 172 toolStripStatusLabel1.Text =
173 $"{Resources.Could_not_write_memory_bank} {rowProgress.Index + 1}";
174 toolStripProgressBar1.Increment(1);
175 statusStrip1.Update();
22 office 176 }
41 office 177 catch (Exception exception)
178 {
179 Log.Error(exception, Resources.Unexpected_error_while_writing_memory_bank);
180 }
22 office 181  
182 });
183  
184 await Task.Run(() => WriteMemoryBanks(rows, progress, _cancellationToken), _cancellationToken);
185  
186 if (!_cancellationToken.IsCancellationRequested)
187 {
188 toolStripProgressBar1.Value = toolStripProgressBar1.Maximum;
189 toolStripStatusLabel1.Text = "Done.";
190 }
15 office 191 }
192  
193 private void MemoryOrganizerForm_Load(object sender, EventArgs e)
194 {
47 office 195 // Generate columns based on radio type.
196 switch(Configuration.Radio)
197 {
198 case "Yaesu FT-891":
199 dataGridView1.Columns.Add(new DataGridViewTextBoxColumn() { Name = "LocationColumn", HeaderText = "Location" });
200 dataGridView1.Columns.Add(new DataGridViewTextBoxColumn() { Name = "FrequencyColumn", HeaderText = "Frequency" });
201 var clarifierDropDownColumn = new DataGridViewComboBoxColumn() { Name = "ClarifierDirectionColumn", HeaderText = "Clarifier" };
202 clarifierDropDownColumn.Items.AddRange(
203 "+",
204 "-"
205 );
206 dataGridView1.Columns.Add(clarifierDropDownColumn);
207 dataGridView1.Columns.Add(new DataGridViewTextBoxColumn() { Name = "ClarifierOffsetColumn", HeaderText = "Offset" });
208 dataGridView1.Columns.Add(new DataGridViewCheckBoxColumn() { Name = "ClarColumn", HeaderText = "Clar" });
209 var modeComboBoxColumn = new DataGridViewComboBoxColumn() { Name = "ModeColumn", HeaderText = "Mode" };
210 modeComboBoxColumn.Items.AddRange(
211 "SSB_1",
212 "SSB_2",
213 "CW_1",
214 "FM",
215 "AM",
216 "RTTY_1",
217 "CW_2",
218 "DATA_1",
219 "RTTY_2",
220 "FM-N",
221 "DATA_2",
222 "AM-N"
223 );
224 dataGridView1.Columns.Add(modeComboBoxColumn);
225 var ctcssComboBoxColumn = new DataGridViewComboBoxColumn() { Name = "CtcssColumn", HeaderText = "CTCSS" };
226 ctcssComboBoxColumn.Items.AddRange(
227 "Off",
228 "Enc/Dec",
229 "Enc"
230 );
231 dataGridView1.Columns.Add(ctcssComboBoxColumn);
232 var phaseComboBoxColumn = new DataGridViewComboBoxColumn() { Name = "PhaseColumn", HeaderText = "Phase" };
233 phaseComboBoxColumn.Items.AddRange(
234 "Simplex",
235 "Plus Shift",
236 "Minus Shift"
237 );
238 dataGridView1.Columns.Add(phaseComboBoxColumn);
239 dataGridView1.Columns.Add(new DataGridViewCheckBoxColumn() { Name = "TagColumn", HeaderText = "Tag" });
240 dataGridView1.Columns.Add(new DataGridViewTextBoxColumn() { Name = "TextColumn", HeaderText = "Text" });
241 break;
242 }
243  
15 office 244 toolStripProgressBar1.Minimum = 0;
245 toolStripProgressBar1.Maximum = 98;
246  
41 office 247 var memoryBankQueue = new ConcurrentQueue<string>();
248 var memoryBankAddRowsTaskCompletionSource = new TaskCompletionSource<object>();
15 office 249  
250 async void IdleHandler(object idleHandlerSender, EventArgs idleHandlerArgs)
251 {
41 office 252 await memoryBankAddRowsTaskCompletionSource.Task;
15 office 253  
254 try
255 {
256 if (!memoryBankQueue.TryDequeue(out var memoryBank))
257 {
258 Application.Idle -= IdleHandler;
259  
260 dataGridView1.Sort(dataGridView1.Columns["LocationColumn"], ListSortDirection.Ascending);
261 toolStripStatusLabel1.Text = "Done.";
262  
263 return;
264 }
265  
266 var index = dataGridView1.Rows.Add();
267  
47 office 268 switch(Configuration.Radio)
269 {
270 case "Yaesu FT-891":
271 dataGridView1.Rows[index].Cells["LocationColumn"].Value = memoryBank;
272 dataGridView1.Rows[index].Cells["FrequencyColumn"].Value = default;
273 dataGridView1.Rows[index].Cells["ClarifierDirectionColumn"].Value = default;
274 dataGridView1.Rows[index].Cells["ClarifierOffsetColumn"].Value = default;
275 dataGridView1.Rows[index].Cells["ClarColumn"].Value = default;
276 dataGridView1.Rows[index].Cells["ModeColumn"].Value = default;
277 dataGridView1.Rows[index].Cells["CtcssColumn"].Value = default;
278 dataGridView1.Rows[index].Cells["PhaseColumn"].Value = default;
279 dataGridView1.Rows[index].Cells["TagColumn"].Value = default;
280 dataGridView1.Rows[index].Cells["TextColumn"].Value = default;
281 break;
282 }
15 office 283  
47 office 284 toolStripStatusLabel1.Text = $"{Resources.Created_memory_bank} {memoryBank}";
15 office 285 toolStripProgressBar1.Increment(1);
286 statusStrip1.Update();
287 }
288 catch (Exception exception)
289 {
290 Log.Error(exception, Resources.Could_not_update_data_grid_view);
291 }
292 }
293  
294 Application.Idle += IdleHandler;
295 try
296 {
41 office 297 foreach (var memoryBank in _memoryBanks.GetMemoryBanks())
15 office 298 {
299 memoryBankQueue.Enqueue(memoryBank);
300 }
301  
41 office 302 memoryBankAddRowsTaskCompletionSource.TrySetResult(new { });
15 office 303 }
304 catch (Exception exception)
305 {
306 Application.Idle -= IdleHandler;
307  
308 Log.Error(exception, Resources.Unable_to_create_memory_banks);
309 }
310 }
311  
312 private void DataGridView1_CurrentCellDirtyStateChanged(object sender, EventArgs e)
313 {
314 var dataGridView = (DataGridView)sender;
315  
316 if (dataGridView.CurrentCell is DataGridViewCheckBoxCell)
317 {
318 dataGridView.CommitEdit(DataGridViewDataErrorContexts.Commit);
319 }
320 }
321  
322 private void DataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
323 {
324 var dataGridView = (DataGridView)sender;
325  
326 dataGridView.CommitEdit(DataGridViewDataErrorContexts.Commit);
327 }
328  
329 private void DataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
330 {
331 var dataGridView = (DataGridView)sender;
332  
333 if (dataGridView.CurrentCell is DataGridViewCheckBoxCell)
334 {
335 if (dataGridView.CurrentCell.IsInEditMode)
336 {
337 if (dataGridView.IsCurrentCellDirty)
338 {
339 dataGridView.EndEdit();
340 }
341 }
342 }
343 }
344  
345 private void DataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
346 {
347 var dataGridView = (DataGridView)sender;
348  
349 if (e.RowIndex == -1 || e.ColumnIndex == -1)
350 {
351 return;
352 }
353  
354 switch (dataGridView.Columns[e.ColumnIndex].Name)
355 {
356 case "EnableColumn":
357 //ProcessEnable(dataGridView.Rows[e.RowIndex]);
358 break;
359 }
360 }
361  
362 private void DataGridView1_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e)
363 {
364 var dataGridView = (DataGridView)sender;
365  
366 if (e.RowIndex == -1 || e.ColumnIndex == -1 ||
367 !(dataGridView.Columns[e.ColumnIndex] is DataGridViewCheckBoxColumn))
368 {
369 return;
370 }
371  
372 dataGridView.EndEdit();
373 }
374  
375 private void DataGridView_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
376 {
377 var dataGridView = (DataGridView)sender;
378  
379 if (e.RowIndex == -1 || e.ColumnIndex == -1 ||
380 !(dataGridView.Columns[e.ColumnIndex] is DataGridViewCheckBoxColumn))
381 {
382 return;
383 }
384  
385 dataGridView.EndEdit();
386 }
387  
388 private void DataGridView1_DataError(object sender, DataGridViewDataErrorEventArgs e)
389 {
390 // @(-.-)@ -(o.o)- @(o_o)@
391 }
392  
393 private static List<DataGridViewRow> GetSelectedDataGridViewRows(DataGridView dataGridView)
394 {
395 return dataGridView.SelectedRows.OfType<DataGridViewRow>().OrderBy(row => row.Index).ToList();
396 }
397  
398 private async Task ReadMemoryBanks(IReadOnlyList<DataGridViewRow> rows, IProgress<DataGridViewRowProgress> progress,
399 CancellationToken cancellationToken)
400 {
401 var count = rows.Count;
402  
403 for (var i = 0; i < count && !cancellationToken.IsCancellationRequested; ++i)
404 {
405 try
406 {
41 office 407 var location = $"{rows[i].Cells["LocationColumn"].Value}";
15 office 408  
41 office 409 var result = await _catAssemblies.CatReadAsync<MemoryChannel>("MT", new object[] { location }, _cancellationToken);
15 office 410  
411 progress.Report(new DataGridViewRowProgressSuccess<MemoryChannel>(rows[i], i, result));
412 }
413 catch (UnexpectedRadioResponseException exception)
414 {
415 progress.Report(new DataGridViewRowProgressFailure(rows[i], i, exception));
416 }
417 catch (Exception exception)
418 {
419 progress.Report(new DataGridViewRowProgressFailure(rows[i], i, exception));
420 }
421 }
422 }
423  
424 private async Task WriteMemoryBanks(IReadOnlyList<DataGridViewRow> rows, IProgress<DataGridViewRowProgress> progress,
425 CancellationToken cancellationToken)
426 {
427 var count = rows.Count;
428  
429 for (var i = 0; i < count && !cancellationToken.IsCancellationRequested; ++i)
430 {
431 try
432 {
47 office 433 var success = false;
15 office 434  
47 office 435 switch(Configuration.Radio)
436 {
437 case "Yaesu FT-891":
438 var memoryChannel = (Radios.Yaesu.FT_891.MemoryChannel)MemoryChannel.Create(Configuration.Radio);
15 office 439  
47 office 440 memoryChannel.CurrentLocation = $"{rows[i].Cells["LocationColumn"].Value}";
441 memoryChannel.Frequency = int.Parse($"{rows[i].Cells["FrequencyColumn"].Value}");
442 memoryChannel.ClarifierDirection = char.Parse($"{rows[i].Cells["ClarifierDirectionColumn"].Value}");
443 memoryChannel.ClarifierOffset = int.Parse($"{rows[i].Cells["ClarifierOffsetColumn"].Value}");
444 memoryChannel.Clar = Convert.ToBoolean(rows[i].Cells["ClarColumn"].Value);
445 memoryChannel.MemoryRadioMode = Radios.Generic.MemoryRadioMode.Create(Configuration.Radio, $"{rows[i].Cells["ModeColumn"].Value}");
446 memoryChannel.Ctcss = new Ctcss($"{rows[i].Cells["CtcssColumn"].Value}");
447 memoryChannel.Phase = new Phase($"{rows[i].Cells["PhaseColumn"].Value}");
448 memoryChannel.Tag = Convert.ToBoolean(rows[i].Cells["TagColumn"].Value);
449 memoryChannel.Text = $"{rows[i].Cells["TextColumn"].Value,-12}";
15 office 450  
47 office 451 success = await _catAssemblies.CatSetAsync<MemoryChannel, bool>("MT", new object[] { memoryChannel }, _cancellationToken);
452 break;
453 }
454  
455 progress.Report(new DataGridViewRowProgressSuccess<bool>(rows[i], i, success));
15 office 456 }
457 catch (UnexpectedRadioResponseException exception)
458 {
459 progress.Report(new DataGridViewRowProgressFailure(rows[i], i, exception));
460 }
461 catch (Exception exception)
462 {
463 progress.Report(new DataGridViewRowProgressFailure(rows[i], i, exception));
464 }
465 }
466 }
16 office 467  
468 private void importToolStripMenuItem_Click(object sender, EventArgs e)
469 {
470 openFileDialog1.ShowDialog();
471 }
472  
473 private void exportToolStripMenuItem_Click(object sender, EventArgs e)
474 {
475 saveFileDialog1.ShowDialog();
476 }
477  
478 private async void saveFileDialog1_FileOk(object sender, CancelEventArgs e)
479 {
480 if (e.Cancel)
481 {
482 return;
483 }
484  
485 var fileName = saveFileDialog1.FileName;
26 office 486 var list = new List<MemoryChannelIndexed>();
16 office 487 foreach(var row in dataGridView1.Rows.OfType<DataGridViewRow>().OrderBy(row => row.Index))
488 {
46 office 489 if (row.Tag is Radios.Yaesu.FT_891.MemoryChannel memoryChannel)
16 office 490 {
26 office 491 var memoryChannelOrganizerBanks = new MemoryChannelIndexed(row.Index, memoryChannel);
16 office 492 list.Add(memoryChannelOrganizerBanks);
493 }
494 }
495  
496 var memoryBanks = list.ToArray();
497  
498 switch (await Serialization.Serialize(memoryBanks, fileName, "MemoryChannelOrganizerBank",
499 "<!ATTLIST MemoryChannelOrganizerBank xmlns:xsi CDATA #IMPLIED xsi:noNamespaceSchemaLocation CDATA #IMPLIED>",
500 CancellationToken.None))
501 {
26 office 502 case SerializationSuccess<MemoryChannelIndexed[]> configuration:
16 office 503 Log.Information(Resources.Serialized_memory_banks);
504 break;
505 case SerializationFailure serializationFailure:
506 Log.Warning(serializationFailure.Exception.Message, Resources.Failed_to_serialize_memory_banks);
507 break;
508 }
509 }
510  
511 private async void openFileDialog1_FileOk(object sender, CancelEventArgs e)
512 {
513 if(e.Cancel)
514 {
515 return;
516 }
517  
518 var fileName = openFileDialog1.FileName;
26 office 519 MemoryChannelIndexed[] memoryBanks = null;
16 office 520  
521 var deserializationResult =
26 office 522 await Serialization.Deserialize<MemoryChannelIndexed[]>(fileName,
45 office 523 "urn:hambook-memorychannelorganizerbank-schema", "MemoryChannelIndexed.xsd", CancellationToken.None);
16 office 524  
525 switch (deserializationResult)
526 {
26 office 527 case SerializationSuccess<MemoryChannelIndexed[]> serializationSuccess:
16 office 528 Log.Information(Resources.Deserialized_memory_banks);
529 memoryBanks = serializationSuccess.Result;
530 break;
531 case SerializationFailure serializationFailure:
532 Log.Warning(serializationFailure.Exception, Resources.Failed_to_deserialize_memory_banks);
533 return;
534 }
535  
536 toolStripProgressBar1.Minimum = 0;
537 toolStripProgressBar1.Maximum = 98;
538  
26 office 539 var memoryBankQueue = new ConcurrentQueue<MemoryChannelIndexed>();
16 office 540 var snapshotsQueuedTaskCompletionSource = new TaskCompletionSource<object>();
541  
542 async void IdleHandler(object idleHandlerSender, EventArgs idleHandlerArgs)
543 {
544 await snapshotsQueuedTaskCompletionSource.Task;
545  
546 try
547 {
548 if (!memoryBankQueue.TryDequeue(out var memoryBank))
549 {
550 Application.Idle -= IdleHandler;
551  
552 dataGridView1.Sort(dataGridView1.Columns["LocationColumn"], ListSortDirection.Ascending);
553 toolStripStatusLabel1.Text = "Done.";
554  
555 return;
556 }
557  
47 office 558 switch(Configuration.Radio)
559 {
560 case "Yaesu FT-891":
561 dataGridView1.Rows[memoryBank.Index].Cells["FrequencyColumn"].Value = memoryBank.MemoryChannel.Frequency;
562 dataGridView1.Rows[memoryBank.Index].Cells["ClarifierDirectionColumn"].Value = (char)memoryBank.MemoryChannel.ClarifierDirection;
563 dataGridView1.Rows[memoryBank.Index].Cells["ClarifierOffsetColumn"].Value = memoryBank.MemoryChannel.ClarifierOffset;
564 dataGridView1.Rows[memoryBank.Index].Cells["ClarColumn"].Value = memoryBank.MemoryChannel.Clar;
565 dataGridView1.Rows[memoryBank.Index].Cells["ModeColumn"].Value = memoryBank.MemoryChannel.MemoryRadioMode.Name;
566 dataGridView1.Rows[memoryBank.Index].Cells["CtcssColumn"].Value = (string)memoryBank.MemoryChannel.Ctcss;
567 dataGridView1.Rows[memoryBank.Index].Cells["PhaseColumn"].Value = (string)memoryBank.MemoryChannel.Phase;
568 dataGridView1.Rows[memoryBank.Index].Cells["TagColumn"].Value = memoryBank.MemoryChannel.Tag;
569 dataGridView1.Rows[memoryBank.Index].Cells["TextColumn"].Value = memoryBank.MemoryChannel.Text;
570 dataGridView1.Rows[memoryBank.Index].Tag = memoryBank.MemoryChannel;
571 break;
572 }
16 office 573  
574 toolStripProgressBar1.Increment(1);
575 statusStrip1.Update();
576 }
577 catch (Exception exception)
578 {
579 Log.Error(exception, Resources.Could_not_update_data_grid_view);
580 }
581 }
582  
583 Application.Idle += IdleHandler;
584 try
585 {
586 foreach (var memoryChannel in memoryBanks)
587 {
588 memoryBankQueue.Enqueue(memoryChannel);
589 }
590  
591 snapshotsQueuedTaskCompletionSource.TrySetResult(new { });
592 }
593 catch (Exception exception)
594 {
595 Application.Idle -= IdleHandler;
596  
597 Log.Error(exception, Resources.Unable_to_create_memory_banks);
598 }
599 }
22 office 600  
601 private void MemoryOrganizerForm_FormClosing(object sender, FormClosingEventArgs e)
602 {
603 if(_cancellationTokenSource != null)
604 {
605 _cancellationTokenSource.Cancel();
606 }
607 }
40 office 608  
41 office 609 private async void readToolStripMenuItem_Click(object sender, EventArgs e)
40 office 610 {
41 office 611 var rows = GetSelectedDataGridViewRows(dataGridView1);
612  
613 var count = rows.Count;
614  
615 toolStripProgressBar1.Minimum = 0;
616 toolStripProgressBar1.Maximum = count;
617  
618 var progress = new Progress<DataGridViewRowProgress>(rowProgress =>
619 {
620 try
621 {
622 switch (rowProgress)
623 {
47 office 624 case DataGridViewRowProgressSuccess<MemoryChannel> rowProgressSuccess:
625 switch(rowProgressSuccess.Data)
626 {
627 case Radios.Yaesu.FT_891.MemoryChannel memoryChannel:
628 rowProgress.Row.Cells["FrequencyColumn"].Value = memoryChannel.Frequency;
629 rowProgress.Row.Cells["ClarifierDirectionColumn"].Value = (char)memoryChannel.ClarifierDirection;
630 rowProgress.Row.Cells["ClarifierOffsetColumn"].Value = memoryChannel.ClarifierOffset;
631 rowProgress.Row.Cells["ClarColumn"].Value = memoryChannel.Clar;
632 rowProgress.Row.Cells["ModeColumn"].Value = memoryChannel.MemoryRadioMode.Name;
633 rowProgress.Row.Cells["CtcssColumn"].Value = (string)memoryChannel.Ctcss;
634 rowProgress.Row.Cells["PhaseColumn"].Value = (string)memoryChannel.Phase;
635 rowProgress.Row.Cells["TagColumn"].Value = memoryChannel.Tag;
636 rowProgress.Row.Cells["TextColumn"].Value = memoryChannel.Text;
637 rowProgress.Row.Tag = rowProgressSuccess.Data;
638 break;
639 }
41 office 640  
641 toolStripStatusLabel1.Text = $"{Resources.Read_memory_bank} {rowProgress.Index + 1}";
642 break;
643 case DataGridViewRowProgressFailure rowProgressFailure:
644 Log.Error(rowProgressFailure.Exception, $"{Resources.Could_not_read_memory_bank}");
645  
646 toolStripStatusLabel1.Text = $"{Resources.Could_not_read_memory_bank} {rowProgress.Index + 1}";
647 break;
648 }
649  
650 toolStripProgressBar1.Increment(1);
651 statusStrip1.Update();
652 }
653 catch (Exception exception)
654 {
655 Log.Error(exception, Resources.Unexpected_error_while_reading_memory_bank);
656 }
657 });
658  
659 await Task.Run(() => ReadMemoryBanks(rows, progress, _cancellationToken), _cancellationToken);
660  
661 if (!_cancellationToken.IsCancellationRequested)
662 {
663 toolStripProgressBar1.Value = toolStripProgressBar1.Maximum;
664 toolStripStatusLabel1.Text = "Done.";
665 }
40 office 666 }
41 office 667  
668 private async void writeToolStripMenuItem_Click(object sender, EventArgs e)
669 {
670 var rows = GetSelectedDataGridViewRows(dataGridView1);
671 var count = rows.Count;
672  
673 toolStripProgressBar1.Minimum = 0;
674 toolStripProgressBar1.Maximum = count;
675  
676 var progress = new Progress<DataGridViewRowProgress>(rowProgress =>
677 {
678 try
679 {
680 switch (rowProgress)
681 {
682 case DataGridViewRowProgressSuccess<bool> rowProgressSuccess:
683 var success = rowProgressSuccess.Data;
684  
685 if (success)
686 {
687  
688 toolStripStatusLabel1.Text =
689 $"{Resources.Wrote_memory_bank} {rowProgress.Index + 1}";
690 toolStripProgressBar1.Increment(1);
691 statusStrip1.Update();
692 return;
693 }
694  
695 Log.Error($"{Resources.Could_not_write_memory_bank}");
696 break;
697 case DataGridViewRowProgressFailure rowProgressFailure:
698 Log.Error(rowProgressFailure.Exception, $"{Resources.Could_not_write_memory_bank}");
699 break;
700 }
701  
702 toolStripStatusLabel1.Text =
703 $"{Resources.Could_not_write_memory_bank} {rowProgress.Index + 1}";
704 toolStripProgressBar1.Increment(1);
705 statusStrip1.Update();
706 }
707 catch (Exception exception)
708 {
709 Log.Error(exception, Resources.Unexpected_error_while_writing_memory_bank);
710 }
711  
712 });
713  
714 await Task.Run(() => WriteMemoryBanks(rows, progress, _cancellationToken), _cancellationToken);
715  
716 if (!_cancellationToken.IsCancellationRequested)
717 {
718 toolStripProgressBar1.Value = toolStripProgressBar1.Maximum;
719 toolStripStatusLabel1.Text = "Done.";
720 }
721 }
15 office 722 }
723 }