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