HamBook – Blame information for rev 52
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
15 | office | 1 | using Configuration; |
2 | using HamBook.Properties; |
||
3 | using HamBook.Radios; |
||
4 | using HamBook.Radios.Generic; |
||
51 | office | 5 | using HamBook.Utilities; |
6 | using HamBook.Utilities.Controls; |
||
16 | office | 7 | using HamBook.Utilities.Serialization; |
15 | office | 8 | using Serilog; |
9 | using System; |
||
10 | using System.Collections.Concurrent; |
||
11 | using System.Collections.Generic; |
||
12 | using System.ComponentModel; |
||
13 | using System.Data; |
||
14 | using System.Linq; |
||
51 | office | 15 | using System.Media; |
16 | using System.Reflection; |
||
17 | using System.Runtime.Remoting.Channels; |
||
49 | office | 18 | using System.Threading; |
15 | office | 19 | using System.Threading.Tasks; |
20 | using System.Windows.Forms; |
||
46 | office | 21 | using MemoryChannel = HamBook.Radios.Generic.MemoryChannel; |
15 | office | 22 | |
23 | namespace HamBook |
||
24 | { |
||
25 | public partial class MemoryOrganizerForm : Form |
||
26 | { |
||
27 | private Configuration.Configuration Configuration { get; set; } |
||
28 | |||
29 | private CatAssemblies _catAssemblies; |
||
30 | private CancellationToken _cancellationToken; |
||
41 | office | 31 | private Radios.Generic.MemoryBanks _memoryBanks; |
22 | office | 32 | private CancellationTokenSource _cancellationTokenSource; |
33 | private CancellationTokenSource _localCancellationTokenSource; |
||
34 | private CancellationToken _localCancellationToken; |
||
52 | office | 35 | private List<DataGridViewRow> _clipboardRows; |
15 | office | 36 | |
37 | public MemoryOrganizerForm() |
||
38 | { |
||
39 | InitializeComponent(); |
||
22 | office | 40 | |
41 | _localCancellationTokenSource = new CancellationTokenSource(); |
||
42 | _localCancellationToken = _localCancellationTokenSource.Token; |
||
15 | office | 43 | } |
44 | |||
45 | public MemoryOrganizerForm(Configuration.Configuration configuration, CatAssemblies catAssemblies, CancellationToken cancellationToken) : this() |
||
46 | { |
||
47 | Configuration = configuration; |
||
48 | _catAssemblies = catAssemblies; |
||
22 | office | 49 | |
41 | office | 50 | _cancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(_localCancellationToken, cancellationToken); |
22 | office | 51 | _cancellationToken = _cancellationTokenSource.Token; |
41 | office | 52 | |
45 | office | 53 | _memoryBanks = Radios.Generic.MemoryBanks.Create(Configuration.Radio); |
15 | office | 54 | } |
55 | |||
56 | /// <summary> |
||
57 | /// Clean up any resources being used. |
||
58 | /// </summary> |
||
59 | /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> |
||
60 | protected override void Dispose(bool disposing) |
||
61 | { |
||
62 | if (disposing && (components != null)) |
||
63 | { |
||
22 | office | 64 | if (_cancellationTokenSource != null) |
65 | { |
||
66 | _cancellationTokenSource.Cancel(); |
||
67 | } |
||
68 | |||
15 | office | 69 | components.Dispose(); |
70 | } |
||
71 | base.Dispose(disposing); |
||
72 | } |
||
73 | |||
74 | private async void button1_Click(object sender, EventArgs e) |
||
75 | { |
||
41 | office | 76 | var rows = dataGridView1.Rows.OfType<DataGridViewRow>().OrderBy(row => row.Index).ToList(); |
22 | office | 77 | var count = rows.Count; |
78 | |||
79 | toolStripProgressBar1.Minimum = 0; |
||
80 | toolStripProgressBar1.Maximum = count; |
||
81 | |||
82 | var progress = new Progress<DataGridViewRowProgress>(rowProgress => |
||
83 | { |
||
84 | try |
||
85 | { |
||
86 | switch (rowProgress) |
||
87 | { |
||
88 | case DataGridViewRowProgressSuccess<MemoryChannel> rowProgressSuccess: |
||
89 | |||
47 | office | 90 | switch(Configuration.Radio) |
91 | { |
||
92 | case "Yaesu FT-891": |
||
93 | var result = (Radios.Yaesu.FT_891.MemoryChannel)rowProgressSuccess.Data; |
||
22 | office | 94 | |
47 | office | 95 | rowProgress.Row.Cells["FrequencyColumn"].Value = result.Frequency; |
96 | rowProgress.Row.Cells["ClarifierDirectionColumn"].Value = (char)result.ClarifierDirection; |
||
97 | rowProgress.Row.Cells["ClarifierOffsetColumn"].Value = result.ClarifierOffset; |
||
98 | rowProgress.Row.Cells["ClarColumn"].Value = result.Clar; |
||
99 | rowProgress.Row.Cells["ModeColumn"].Value = result.MemoryRadioMode.Name; |
||
100 | rowProgress.Row.Cells["CtcssColumn"].Value = (string)result.Ctcss; |
||
101 | rowProgress.Row.Cells["PhaseColumn"].Value = (string)result.Phase; |
||
102 | rowProgress.Row.Cells["TagColumn"].Value = result.Tag; |
||
103 | rowProgress.Row.Cells["TextColumn"].Value = result.Text; |
||
104 | rowProgress.Row.Tag = rowProgressSuccess.Data; |
||
105 | break; |
||
106 | } |
||
41 | office | 107 | |
108 | toolStripStatusLabel1.Text = $"{Resources.Read_memory_bank} {rowProgress.Index + 1}"; |
||
22 | office | 109 | break; |
110 | case DataGridViewRowProgressFailure rowProgressFailure: |
||
111 | Log.Error(rowProgressFailure.Exception, $"{Resources.Could_not_read_memory_bank}"); |
||
112 | |||
41 | office | 113 | toolStripStatusLabel1.Text = $"{Resources.Could_not_read_memory_bank} {rowProgress.Index + 1}"; |
22 | office | 114 | break; |
115 | } |
||
116 | |||
117 | toolStripProgressBar1.Increment(1); |
||
118 | statusStrip1.Update(); |
||
119 | } |
||
41 | office | 120 | catch (Exception exception) |
22 | office | 121 | { |
41 | office | 122 | Log.Error(exception, Resources.Unexpected_error_while_reading_memory_bank); |
22 | office | 123 | } |
124 | }); |
||
125 | |||
126 | await Task.Run(() => ReadMemoryBanks(rows, progress, _cancellationToken), _cancellationToken); |
||
127 | |||
128 | if (!_cancellationToken.IsCancellationRequested) |
||
129 | { |
||
130 | toolStripProgressBar1.Value = toolStripProgressBar1.Maximum; |
||
131 | toolStripStatusLabel1.Text = "Done."; |
||
132 | } |
||
15 | office | 133 | } |
134 | |||
135 | private async void button2_Click(object sender, EventArgs e) |
||
136 | { |
||
41 | office | 137 | var rows = dataGridView1.Rows.OfType<DataGridViewRow>().OrderBy(row => row.Index).ToList(); |
22 | office | 138 | var count = rows.Count; |
139 | |||
140 | toolStripProgressBar1.Minimum = 0; |
||
141 | toolStripProgressBar1.Maximum = count; |
||
142 | |||
143 | var progress = new Progress<DataGridViewRowProgress>(rowProgress => |
||
144 | { |
||
145 | try |
||
146 | { |
||
147 | switch (rowProgress) |
||
148 | { |
||
149 | case DataGridViewRowProgressSuccess<bool> rowProgressSuccess: |
||
150 | var success = rowProgressSuccess.Data; |
||
151 | |||
41 | office | 152 | if (success) |
22 | office | 153 | { |
154 | |||
155 | toolStripStatusLabel1.Text = |
||
41 | office | 156 | $"{Resources.Wrote_memory_bank} {rowProgress.Index + 1}"; |
22 | office | 157 | toolStripProgressBar1.Increment(1); |
158 | statusStrip1.Update(); |
||
159 | return; |
||
160 | } |
||
161 | |||
41 | office | 162 | Log.Error($"{Resources.Could_not_write_memory_bank}"); |
22 | office | 163 | break; |
164 | case DataGridViewRowProgressFailure rowProgressFailure: |
||
165 | Log.Error(rowProgressFailure.Exception, $"{Resources.Could_not_write_memory_bank}"); |
||
166 | break; |
||
167 | } |
||
168 | |||
41 | office | 169 | toolStripStatusLabel1.Text = |
170 | $"{Resources.Could_not_write_memory_bank} {rowProgress.Index + 1}"; |
||
171 | toolStripProgressBar1.Increment(1); |
||
172 | statusStrip1.Update(); |
||
22 | office | 173 | } |
41 | office | 174 | catch (Exception exception) |
175 | { |
||
176 | Log.Error(exception, Resources.Unexpected_error_while_writing_memory_bank); |
||
177 | } |
||
22 | office | 178 | |
179 | }); |
||
180 | |||
181 | await Task.Run(() => WriteMemoryBanks(rows, progress, _cancellationToken), _cancellationToken); |
||
182 | |||
183 | if (!_cancellationToken.IsCancellationRequested) |
||
184 | { |
||
185 | toolStripProgressBar1.Value = toolStripProgressBar1.Maximum; |
||
186 | toolStripStatusLabel1.Text = "Done."; |
||
187 | } |
||
15 | office | 188 | } |
189 | |||
190 | private void MemoryOrganizerForm_Load(object sender, EventArgs e) |
||
191 | { |
||
47 | office | 192 | // Generate columns based on radio type. |
193 | switch(Configuration.Radio) |
||
194 | { |
||
195 | case "Yaesu FT-891": |
||
52 | office | 196 | dataGridView1.Columns.Add(new DataGridViewTextBoxColumn() { Name = "LocationColumn", HeaderText = "Location", ReadOnly = true }); |
47 | office | 197 | dataGridView1.Columns.Add(new DataGridViewTextBoxColumn() { Name = "FrequencyColumn", HeaderText = "Frequency" }); |
198 | var clarifierDropDownColumn = new DataGridViewComboBoxColumn() { Name = "ClarifierDirectionColumn", HeaderText = "Clarifier" }; |
||
199 | clarifierDropDownColumn.Items.AddRange( |
||
200 | "+", |
||
201 | "-" |
||
202 | ); |
||
203 | dataGridView1.Columns.Add(clarifierDropDownColumn); |
||
204 | dataGridView1.Columns.Add(new DataGridViewTextBoxColumn() { Name = "ClarifierOffsetColumn", HeaderText = "Offset" }); |
||
205 | dataGridView1.Columns.Add(new DataGridViewCheckBoxColumn() { Name = "ClarColumn", HeaderText = "Clar" }); |
||
206 | var modeComboBoxColumn = new DataGridViewComboBoxColumn() { Name = "ModeColumn", HeaderText = "Mode" }; |
||
51 | office | 207 | foreach (var name in Radios.Yaesu.FT_891.MemoryRadioMode.Names) |
208 | { |
||
209 | modeComboBoxColumn.Items.Add( |
||
210 | name |
||
211 | ); |
||
212 | } |
||
213 | |||
47 | office | 214 | dataGridView1.Columns.Add(modeComboBoxColumn); |
215 | var ctcssComboBoxColumn = new DataGridViewComboBoxColumn() { Name = "CtcssColumn", HeaderText = "CTCSS" }; |
||
216 | ctcssComboBoxColumn.Items.AddRange( |
||
217 | "Off", |
||
218 | "Enc/Dec", |
||
219 | "Enc" |
||
220 | ); |
||
221 | dataGridView1.Columns.Add(ctcssComboBoxColumn); |
||
222 | var phaseComboBoxColumn = new DataGridViewComboBoxColumn() { Name = "PhaseColumn", HeaderText = "Phase" }; |
||
223 | phaseComboBoxColumn.Items.AddRange( |
||
224 | "Simplex", |
||
225 | "Plus Shift", |
||
226 | "Minus Shift" |
||
227 | ); |
||
228 | dataGridView1.Columns.Add(phaseComboBoxColumn); |
||
229 | dataGridView1.Columns.Add(new DataGridViewCheckBoxColumn() { Name = "TagColumn", HeaderText = "Tag" }); |
||
230 | dataGridView1.Columns.Add(new DataGridViewTextBoxColumn() { Name = "TextColumn", HeaderText = "Text" }); |
||
231 | break; |
||
232 | } |
||
233 | |||
15 | office | 234 | toolStripProgressBar1.Minimum = 0; |
235 | toolStripProgressBar1.Maximum = 98; |
||
236 | |||
41 | office | 237 | var memoryBankQueue = new ConcurrentQueue<string>(); |
238 | var memoryBankAddRowsTaskCompletionSource = new TaskCompletionSource<object>(); |
||
15 | office | 239 | |
240 | async void IdleHandler(object idleHandlerSender, EventArgs idleHandlerArgs) |
||
241 | { |
||
41 | office | 242 | await memoryBankAddRowsTaskCompletionSource.Task; |
15 | office | 243 | |
244 | try |
||
245 | { |
||
246 | if (!memoryBankQueue.TryDequeue(out var memoryBank)) |
||
247 | { |
||
248 | Application.Idle -= IdleHandler; |
||
249 | |||
250 | dataGridView1.Sort(dataGridView1.Columns["LocationColumn"], ListSortDirection.Ascending); |
||
251 | toolStripStatusLabel1.Text = "Done."; |
||
252 | |||
253 | return; |
||
254 | } |
||
255 | |||
256 | var index = dataGridView1.Rows.Add(); |
||
257 | |||
47 | office | 258 | switch(Configuration.Radio) |
259 | { |
||
260 | case "Yaesu FT-891": |
||
261 | dataGridView1.Rows[index].Cells["LocationColumn"].Value = memoryBank; |
||
262 | dataGridView1.Rows[index].Cells["FrequencyColumn"].Value = default; |
||
263 | dataGridView1.Rows[index].Cells["ClarifierDirectionColumn"].Value = default; |
||
264 | dataGridView1.Rows[index].Cells["ClarifierOffsetColumn"].Value = default; |
||
265 | dataGridView1.Rows[index].Cells["ClarColumn"].Value = default; |
||
266 | dataGridView1.Rows[index].Cells["ModeColumn"].Value = default; |
||
267 | dataGridView1.Rows[index].Cells["CtcssColumn"].Value = default; |
||
268 | dataGridView1.Rows[index].Cells["PhaseColumn"].Value = default; |
||
269 | dataGridView1.Rows[index].Cells["TagColumn"].Value = default; |
||
270 | dataGridView1.Rows[index].Cells["TextColumn"].Value = default; |
||
271 | break; |
||
272 | } |
||
15 | office | 273 | |
47 | office | 274 | toolStripStatusLabel1.Text = $"{Resources.Created_memory_bank} {memoryBank}"; |
15 | office | 275 | toolStripProgressBar1.Increment(1); |
276 | statusStrip1.Update(); |
||
277 | } |
||
278 | catch (Exception exception) |
||
279 | { |
||
280 | Log.Error(exception, Resources.Could_not_update_data_grid_view); |
||
281 | } |
||
282 | } |
||
283 | |||
284 | Application.Idle += IdleHandler; |
||
285 | try |
||
286 | { |
||
41 | office | 287 | foreach (var memoryBank in _memoryBanks.GetMemoryBanks()) |
15 | office | 288 | { |
289 | memoryBankQueue.Enqueue(memoryBank); |
||
290 | } |
||
291 | |||
41 | office | 292 | memoryBankAddRowsTaskCompletionSource.TrySetResult(new { }); |
15 | office | 293 | } |
294 | catch (Exception exception) |
||
295 | { |
||
296 | Application.Idle -= IdleHandler; |
||
297 | |||
298 | Log.Error(exception, Resources.Unable_to_create_memory_banks); |
||
299 | } |
||
300 | } |
||
301 | |||
302 | private void DataGridView1_CurrentCellDirtyStateChanged(object sender, EventArgs e) |
||
303 | { |
||
304 | var dataGridView = (DataGridView)sender; |
||
305 | |||
306 | if (dataGridView.CurrentCell is DataGridViewCheckBoxCell) |
||
307 | { |
||
308 | dataGridView.CommitEdit(DataGridViewDataErrorContexts.Commit); |
||
309 | } |
||
310 | } |
||
311 | |||
312 | private void DataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e) |
||
313 | { |
||
314 | var dataGridView = (DataGridView)sender; |
||
315 | |||
316 | dataGridView.CommitEdit(DataGridViewDataErrorContexts.Commit); |
||
317 | } |
||
318 | |||
319 | private void DataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e) |
||
320 | { |
||
321 | |||
322 | } |
||
323 | |||
324 | private void DataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e) |
||
325 | { |
||
326 | var dataGridView = (DataGridView)sender; |
||
327 | |||
328 | if (e.RowIndex == -1 || e.ColumnIndex == -1) |
||
329 | { |
||
330 | return; |
||
331 | } |
||
332 | |||
333 | switch (dataGridView.Columns[e.ColumnIndex].Name) |
||
334 | { |
||
335 | case "EnableColumn": |
||
336 | //ProcessEnable(dataGridView.Rows[e.RowIndex]); |
||
337 | break; |
||
338 | } |
||
339 | } |
||
340 | |||
341 | private void DataGridView1_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e) |
||
342 | { |
||
343 | var dataGridView = (DataGridView)sender; |
||
344 | |||
345 | if (e.RowIndex == -1 || e.ColumnIndex == -1 || |
||
346 | !(dataGridView.Columns[e.ColumnIndex] is DataGridViewCheckBoxColumn)) |
||
347 | { |
||
348 | return; |
||
349 | } |
||
350 | |||
351 | dataGridView.EndEdit(); |
||
352 | } |
||
353 | |||
354 | private void DataGridView_CellDoubleClick(object sender, DataGridViewCellEventArgs e) |
||
355 | { |
||
356 | var dataGridView = (DataGridView)sender; |
||
357 | |||
358 | if (e.RowIndex == -1 || e.ColumnIndex == -1 || |
||
359 | !(dataGridView.Columns[e.ColumnIndex] is DataGridViewCheckBoxColumn)) |
||
360 | { |
||
361 | return; |
||
362 | } |
||
363 | |||
364 | dataGridView.EndEdit(); |
||
365 | } |
||
366 | |||
367 | private void DataGridView1_DataError(object sender, DataGridViewDataErrorEventArgs e) |
||
368 | { |
||
369 | // @(-.-)@ -(o.o)- @(o_o)@ |
||
370 | } |
||
371 | |||
51 | office | 372 | private static IEnumerable<DataGridViewRow> GetSelectedDataGridViewRows(DataGridView dataGridView) |
15 | office | 373 | { |
51 | office | 374 | return dataGridView.SelectedRows.OfType<DataGridViewRow>().OrderBy(row => row.Index); |
15 | office | 375 | } |
376 | |||
377 | private async Task ReadMemoryBanks(IReadOnlyList<DataGridViewRow> rows, IProgress<DataGridViewRowProgress> progress, |
||
378 | CancellationToken cancellationToken) |
||
379 | { |
||
380 | var count = rows.Count; |
||
381 | |||
382 | for (var i = 0; i < count && !cancellationToken.IsCancellationRequested; ++i) |
||
383 | { |
||
384 | try |
||
385 | { |
||
41 | office | 386 | var location = $"{rows[i].Cells["LocationColumn"].Value}"; |
15 | office | 387 | |
41 | office | 388 | var result = await _catAssemblies.CatReadAsync<MemoryChannel>("MT", new object[] { location }, _cancellationToken); |
15 | office | 389 | |
390 | progress.Report(new DataGridViewRowProgressSuccess<MemoryChannel>(rows[i], i, result)); |
||
391 | } |
||
392 | catch (UnexpectedRadioResponseException exception) |
||
393 | { |
||
394 | progress.Report(new DataGridViewRowProgressFailure(rows[i], i, exception)); |
||
395 | } |
||
396 | catch (Exception exception) |
||
397 | { |
||
398 | progress.Report(new DataGridViewRowProgressFailure(rows[i], i, exception)); |
||
399 | } |
||
400 | } |
||
401 | } |
||
402 | |||
403 | private async Task WriteMemoryBanks(IReadOnlyList<DataGridViewRow> rows, IProgress<DataGridViewRowProgress> progress, |
||
404 | CancellationToken cancellationToken) |
||
405 | { |
||
406 | var count = rows.Count; |
||
407 | |||
408 | for (var i = 0; i < count && !cancellationToken.IsCancellationRequested; ++i) |
||
409 | { |
||
410 | try |
||
411 | { |
||
47 | office | 412 | var success = false; |
15 | office | 413 | |
47 | office | 414 | switch(Configuration.Radio) |
415 | { |
||
416 | case "Yaesu FT-891": |
||
417 | var memoryChannel = (Radios.Yaesu.FT_891.MemoryChannel)MemoryChannel.Create(Configuration.Radio); |
||
15 | office | 418 | |
47 | office | 419 | memoryChannel.CurrentLocation = $"{rows[i].Cells["LocationColumn"].Value}"; |
420 | memoryChannel.Frequency = int.Parse($"{rows[i].Cells["FrequencyColumn"].Value}"); |
||
421 | memoryChannel.ClarifierDirection = char.Parse($"{rows[i].Cells["ClarifierDirectionColumn"].Value}"); |
||
422 | memoryChannel.ClarifierOffset = int.Parse($"{rows[i].Cells["ClarifierOffsetColumn"].Value}"); |
||
423 | memoryChannel.Clar = Convert.ToBoolean(rows[i].Cells["ClarColumn"].Value); |
||
424 | memoryChannel.MemoryRadioMode = Radios.Generic.MemoryRadioMode.Create(Configuration.Radio, $"{rows[i].Cells["ModeColumn"].Value}"); |
||
425 | memoryChannel.Ctcss = new Ctcss($"{rows[i].Cells["CtcssColumn"].Value}"); |
||
426 | memoryChannel.Phase = new Phase($"{rows[i].Cells["PhaseColumn"].Value}"); |
||
427 | memoryChannel.Tag = Convert.ToBoolean(rows[i].Cells["TagColumn"].Value); |
||
428 | memoryChannel.Text = $"{rows[i].Cells["TextColumn"].Value,-12}"; |
||
15 | office | 429 | |
47 | office | 430 | success = await _catAssemblies.CatSetAsync<MemoryChannel, bool>("MT", new object[] { memoryChannel }, _cancellationToken); |
431 | break; |
||
432 | } |
||
433 | |||
434 | progress.Report(new DataGridViewRowProgressSuccess<bool>(rows[i], i, success)); |
||
15 | office | 435 | } |
436 | catch (UnexpectedRadioResponseException exception) |
||
437 | { |
||
438 | progress.Report(new DataGridViewRowProgressFailure(rows[i], i, exception)); |
||
439 | } |
||
440 | catch (Exception exception) |
||
441 | { |
||
442 | progress.Report(new DataGridViewRowProgressFailure(rows[i], i, exception)); |
||
443 | } |
||
444 | } |
||
445 | } |
||
16 | office | 446 | |
447 | private void importToolStripMenuItem_Click(object sender, EventArgs e) |
||
448 | { |
||
449 | openFileDialog1.ShowDialog(); |
||
450 | } |
||
451 | |||
452 | private void exportToolStripMenuItem_Click(object sender, EventArgs e) |
||
453 | { |
||
454 | saveFileDialog1.ShowDialog(); |
||
455 | } |
||
456 | |||
457 | private async void saveFileDialog1_FileOk(object sender, CancelEventArgs e) |
||
458 | { |
||
459 | if (e.Cancel) |
||
460 | { |
||
461 | return; |
||
462 | } |
||
463 | |||
464 | var fileName = saveFileDialog1.FileName; |
||
26 | office | 465 | var list = new List<MemoryChannelIndexed>(); |
16 | office | 466 | foreach(var row in dataGridView1.Rows.OfType<DataGridViewRow>().OrderBy(row => row.Index)) |
467 | { |
||
48 | office | 468 | if (row.Tag is MemoryChannel memoryChannel) |
16 | office | 469 | { |
26 | office | 470 | var memoryChannelOrganizerBanks = new MemoryChannelIndexed(row.Index, memoryChannel); |
16 | office | 471 | list.Add(memoryChannelOrganizerBanks); |
472 | } |
||
473 | } |
||
474 | |||
475 | var memoryBanks = list.ToArray(); |
||
476 | |||
477 | switch (await Serialization.Serialize(memoryBanks, fileName, "MemoryChannelOrganizerBank", |
||
478 | "<!ATTLIST MemoryChannelOrganizerBank xmlns:xsi CDATA #IMPLIED xsi:noNamespaceSchemaLocation CDATA #IMPLIED>", |
||
479 | CancellationToken.None)) |
||
480 | { |
||
26 | office | 481 | case SerializationSuccess<MemoryChannelIndexed[]> configuration: |
16 | office | 482 | Log.Information(Resources.Serialized_memory_banks); |
483 | break; |
||
484 | case SerializationFailure serializationFailure: |
||
485 | Log.Warning(serializationFailure.Exception.Message, Resources.Failed_to_serialize_memory_banks); |
||
486 | break; |
||
487 | } |
||
488 | } |
||
489 | |||
490 | private async void openFileDialog1_FileOk(object sender, CancelEventArgs e) |
||
491 | { |
||
492 | if(e.Cancel) |
||
493 | { |
||
494 | return; |
||
495 | } |
||
496 | |||
497 | var fileName = openFileDialog1.FileName; |
||
26 | office | 498 | MemoryChannelIndexed[] memoryBanks = null; |
16 | office | 499 | |
500 | var deserializationResult = |
||
26 | office | 501 | await Serialization.Deserialize<MemoryChannelIndexed[]>(fileName, |
45 | office | 502 | "urn:hambook-memorychannelorganizerbank-schema", "MemoryChannelIndexed.xsd", CancellationToken.None); |
16 | office | 503 | |
504 | switch (deserializationResult) |
||
505 | { |
||
26 | office | 506 | case SerializationSuccess<MemoryChannelIndexed[]> serializationSuccess: |
16 | office | 507 | Log.Information(Resources.Deserialized_memory_banks); |
508 | memoryBanks = serializationSuccess.Result; |
||
509 | break; |
||
510 | case SerializationFailure serializationFailure: |
||
511 | Log.Warning(serializationFailure.Exception, Resources.Failed_to_deserialize_memory_banks); |
||
512 | return; |
||
513 | } |
||
514 | |||
515 | toolStripProgressBar1.Minimum = 0; |
||
516 | toolStripProgressBar1.Maximum = 98; |
||
517 | |||
26 | office | 518 | var memoryBankQueue = new ConcurrentQueue<MemoryChannelIndexed>(); |
16 | office | 519 | var snapshotsQueuedTaskCompletionSource = new TaskCompletionSource<object>(); |
520 | |||
521 | async void IdleHandler(object idleHandlerSender, EventArgs idleHandlerArgs) |
||
522 | { |
||
523 | await snapshotsQueuedTaskCompletionSource.Task; |
||
524 | |||
525 | try |
||
526 | { |
||
527 | if (!memoryBankQueue.TryDequeue(out var memoryBank)) |
||
528 | { |
||
529 | Application.Idle -= IdleHandler; |
||
530 | |||
531 | dataGridView1.Sort(dataGridView1.Columns["LocationColumn"], ListSortDirection.Ascending); |
||
532 | toolStripStatusLabel1.Text = "Done."; |
||
533 | |||
534 | return; |
||
535 | } |
||
536 | |||
47 | office | 537 | switch(Configuration.Radio) |
538 | { |
||
539 | case "Yaesu FT-891": |
||
48 | office | 540 | var memoryChannel = (Radios.Yaesu.FT_891.MemoryChannel)memoryBank.MemoryChannel; |
541 | |||
47 | office | 542 | dataGridView1.Rows[memoryBank.Index].Cells["FrequencyColumn"].Value = memoryBank.MemoryChannel.Frequency; |
48 | office | 543 | dataGridView1.Rows[memoryBank.Index].Cells["ClarifierDirectionColumn"].Value = (char)memoryChannel.ClarifierDirection; |
544 | dataGridView1.Rows[memoryBank.Index].Cells["ClarifierOffsetColumn"].Value = memoryChannel.ClarifierOffset; |
||
545 | dataGridView1.Rows[memoryBank.Index].Cells["ClarColumn"].Value = memoryChannel.Clar; |
||
47 | office | 546 | dataGridView1.Rows[memoryBank.Index].Cells["ModeColumn"].Value = memoryBank.MemoryChannel.MemoryRadioMode.Name; |
48 | office | 547 | dataGridView1.Rows[memoryBank.Index].Cells["CtcssColumn"].Value = (string)memoryChannel.Ctcss; |
548 | dataGridView1.Rows[memoryBank.Index].Cells["PhaseColumn"].Value = (string)memoryChannel.Phase; |
||
47 | office | 549 | dataGridView1.Rows[memoryBank.Index].Cells["TagColumn"].Value = memoryBank.MemoryChannel.Tag; |
550 | dataGridView1.Rows[memoryBank.Index].Cells["TextColumn"].Value = memoryBank.MemoryChannel.Text; |
||
551 | dataGridView1.Rows[memoryBank.Index].Tag = memoryBank.MemoryChannel; |
||
552 | break; |
||
553 | } |
||
16 | office | 554 | |
555 | toolStripProgressBar1.Increment(1); |
||
556 | statusStrip1.Update(); |
||
557 | } |
||
558 | catch (Exception exception) |
||
559 | { |
||
560 | Log.Error(exception, Resources.Could_not_update_data_grid_view); |
||
561 | } |
||
562 | } |
||
563 | |||
564 | Application.Idle += IdleHandler; |
||
565 | try |
||
566 | { |
||
567 | foreach (var memoryChannel in memoryBanks) |
||
568 | { |
||
569 | memoryBankQueue.Enqueue(memoryChannel); |
||
570 | } |
||
571 | |||
572 | snapshotsQueuedTaskCompletionSource.TrySetResult(new { }); |
||
573 | } |
||
574 | catch (Exception exception) |
||
575 | { |
||
576 | Application.Idle -= IdleHandler; |
||
577 | |||
578 | Log.Error(exception, Resources.Unable_to_create_memory_banks); |
||
579 | } |
||
580 | } |
||
22 | office | 581 | |
582 | private void MemoryOrganizerForm_FormClosing(object sender, FormClosingEventArgs e) |
||
583 | { |
||
584 | if(_cancellationTokenSource != null) |
||
585 | { |
||
586 | _cancellationTokenSource.Cancel(); |
||
587 | } |
||
588 | } |
||
40 | office | 589 | |
41 | office | 590 | private async void readToolStripMenuItem_Click(object sender, EventArgs e) |
40 | office | 591 | { |
41 | office | 592 | var rows = GetSelectedDataGridViewRows(dataGridView1); |
593 | |||
51 | office | 594 | if(!rows.Any()) |
595 | { |
||
596 | return; |
||
597 | } |
||
41 | office | 598 | |
51 | office | 599 | var list = rows.ToList(); |
600 | |||
601 | var count = list.Count; |
||
602 | |||
41 | office | 603 | toolStripProgressBar1.Minimum = 0; |
604 | toolStripProgressBar1.Maximum = count; |
||
605 | |||
606 | var progress = new Progress<DataGridViewRowProgress>(rowProgress => |
||
607 | { |
||
608 | try |
||
609 | { |
||
610 | switch (rowProgress) |
||
611 | { |
||
47 | office | 612 | case DataGridViewRowProgressSuccess<MemoryChannel> rowProgressSuccess: |
613 | switch(rowProgressSuccess.Data) |
||
614 | { |
||
615 | case Radios.Yaesu.FT_891.MemoryChannel memoryChannel: |
||
616 | rowProgress.Row.Cells["FrequencyColumn"].Value = memoryChannel.Frequency; |
||
617 | rowProgress.Row.Cells["ClarifierDirectionColumn"].Value = (char)memoryChannel.ClarifierDirection; |
||
618 | rowProgress.Row.Cells["ClarifierOffsetColumn"].Value = memoryChannel.ClarifierOffset; |
||
619 | rowProgress.Row.Cells["ClarColumn"].Value = memoryChannel.Clar; |
||
620 | rowProgress.Row.Cells["ModeColumn"].Value = memoryChannel.MemoryRadioMode.Name; |
||
621 | rowProgress.Row.Cells["CtcssColumn"].Value = (string)memoryChannel.Ctcss; |
||
622 | rowProgress.Row.Cells["PhaseColumn"].Value = (string)memoryChannel.Phase; |
||
623 | rowProgress.Row.Cells["TagColumn"].Value = memoryChannel.Tag; |
||
624 | rowProgress.Row.Cells["TextColumn"].Value = memoryChannel.Text; |
||
625 | rowProgress.Row.Tag = rowProgressSuccess.Data; |
||
626 | break; |
||
627 | } |
||
41 | office | 628 | |
629 | toolStripStatusLabel1.Text = $"{Resources.Read_memory_bank} {rowProgress.Index + 1}"; |
||
630 | break; |
||
631 | case DataGridViewRowProgressFailure rowProgressFailure: |
||
632 | Log.Error(rowProgressFailure.Exception, $"{Resources.Could_not_read_memory_bank}"); |
||
633 | |||
634 | toolStripStatusLabel1.Text = $"{Resources.Could_not_read_memory_bank} {rowProgress.Index + 1}"; |
||
635 | break; |
||
636 | } |
||
637 | |||
638 | toolStripProgressBar1.Increment(1); |
||
639 | statusStrip1.Update(); |
||
640 | } |
||
641 | catch (Exception exception) |
||
642 | { |
||
643 | Log.Error(exception, Resources.Unexpected_error_while_reading_memory_bank); |
||
644 | } |
||
645 | }); |
||
646 | |||
51 | office | 647 | await Task.Run(() => ReadMemoryBanks(list, progress, _cancellationToken), _cancellationToken); |
41 | office | 648 | |
649 | if (!_cancellationToken.IsCancellationRequested) |
||
650 | { |
||
651 | toolStripProgressBar1.Value = toolStripProgressBar1.Maximum; |
||
652 | toolStripStatusLabel1.Text = "Done."; |
||
653 | } |
||
40 | office | 654 | } |
41 | office | 655 | |
656 | private async void writeToolStripMenuItem_Click(object sender, EventArgs e) |
||
657 | { |
||
658 | var rows = GetSelectedDataGridViewRows(dataGridView1); |
||
659 | |||
51 | office | 660 | if (!rows.Any()) |
661 | { |
||
662 | return; |
||
663 | } |
||
664 | |||
665 | var list = rows.ToList(); |
||
666 | |||
667 | var count = list.Count; |
||
668 | |||
41 | office | 669 | toolStripProgressBar1.Minimum = 0; |
670 | toolStripProgressBar1.Maximum = count; |
||
671 | |||
672 | var progress = new Progress<DataGridViewRowProgress>(rowProgress => |
||
673 | { |
||
674 | try |
||
675 | { |
||
676 | switch (rowProgress) |
||
677 | { |
||
678 | case DataGridViewRowProgressSuccess<bool> rowProgressSuccess: |
||
679 | var success = rowProgressSuccess.Data; |
||
680 | |||
681 | if (success) |
||
682 | { |
||
683 | |||
684 | toolStripStatusLabel1.Text = |
||
685 | $"{Resources.Wrote_memory_bank} {rowProgress.Index + 1}"; |
||
686 | toolStripProgressBar1.Increment(1); |
||
687 | statusStrip1.Update(); |
||
688 | return; |
||
689 | } |
||
690 | |||
691 | Log.Error($"{Resources.Could_not_write_memory_bank}"); |
||
692 | break; |
||
693 | case DataGridViewRowProgressFailure rowProgressFailure: |
||
694 | Log.Error(rowProgressFailure.Exception, $"{Resources.Could_not_write_memory_bank}"); |
||
695 | break; |
||
696 | } |
||
697 | |||
698 | toolStripStatusLabel1.Text = |
||
699 | $"{Resources.Could_not_write_memory_bank} {rowProgress.Index + 1}"; |
||
700 | toolStripProgressBar1.Increment(1); |
||
701 | statusStrip1.Update(); |
||
702 | } |
||
703 | catch (Exception exception) |
||
704 | { |
||
705 | Log.Error(exception, Resources.Unexpected_error_while_writing_memory_bank); |
||
706 | } |
||
707 | |||
708 | }); |
||
709 | |||
51 | office | 710 | await Task.Run(() => WriteMemoryBanks(list, progress, _cancellationToken), _cancellationToken); |
41 | office | 711 | |
712 | if (!_cancellationToken.IsCancellationRequested) |
||
713 | { |
||
714 | toolStripProgressBar1.Value = toolStripProgressBar1.Maximum; |
||
715 | toolStripStatusLabel1.Text = "Done."; |
||
716 | } |
||
717 | } |
||
51 | office | 718 | |
719 | private async void readFromVFOAToolStripMenuItem_Click(object sender, EventArgs e) |
||
720 | { |
||
721 | var rows = GetSelectedDataGridViewRows(dataGridView1); |
||
722 | |||
723 | if (!rows.Any()) |
||
724 | { |
||
725 | return; |
||
726 | } |
||
727 | |||
728 | var list = rows.ToList(); |
||
729 | |||
730 | try |
||
731 | { |
||
732 | foreach (var row in list) |
||
733 | { |
||
734 | var fa = await _catAssemblies.CatReadAsync<int>("FA", new object[] { }, _cancellationToken); |
||
735 | |||
736 | row.Cells["FrequencyColumn"].Value = $"{ fa }"; |
||
737 | } |
||
738 | |||
739 | } |
||
740 | catch (Exception exception) |
||
741 | { |
||
742 | Log.Error(exception, Resources.Failed_to_read_VFO_A); |
||
743 | } |
||
744 | } |
||
745 | |||
746 | private async void readFromVFOBToolStripMenuItem_Click(object sender, EventArgs e) |
||
747 | { |
||
748 | var rows = GetSelectedDataGridViewRows(dataGridView1); |
||
749 | |||
750 | if (!rows.Any()) |
||
751 | { |
||
752 | return; |
||
753 | } |
||
754 | |||
755 | var list = rows.ToList(); |
||
756 | |||
757 | try |
||
758 | { |
||
759 | foreach (var row in list) |
||
760 | { |
||
761 | var fb = await _catAssemblies.CatReadAsync<int>("FB", new object[] { }, _cancellationToken); |
||
762 | |||
763 | row.Cells["FrequencyColumn"].Value = $"{fb}"; |
||
764 | } |
||
765 | |||
766 | } |
||
767 | catch (Exception exception) |
||
768 | { |
||
769 | Log.Error(exception, Resources.Failed_to_read_VFO_B); |
||
770 | } |
||
771 | } |
||
772 | |||
773 | private async void memoryChannelToolStripMenuItem_Click(object sender, EventArgs e) |
||
774 | { |
||
775 | var rows = GetSelectedDataGridViewRows(dataGridView1); |
||
776 | |||
777 | if (!rows.Any()) |
||
778 | { |
||
779 | return; |
||
780 | } |
||
781 | |||
782 | var row = rows.First(); |
||
783 | |||
784 | try |
||
785 | { |
||
786 | using (var soundPlayer = new SoundPlayer(Assembly.GetExecutingAssembly().GetManifestResourceStream("HamBook.Effects.pot.wav"))) |
||
787 | { |
||
788 | await _catAssemblies.CatWriteAsync<string>("MC", new object[] { row.Cells["LocationColumn"].Value }, _cancellationToken); |
||
789 | |||
790 | soundPlayer.Play(); |
||
791 | } |
||
792 | } |
||
793 | catch (Exception exception) |
||
794 | { |
||
795 | Log.Error(exception, Resources.Failed_to_set_memory_channel); |
||
796 | } |
||
797 | } |
||
798 | |||
799 | private async void frequencyToolStripMenuItem_Click(object sender, EventArgs e) |
||
800 | { |
||
801 | var rows = GetSelectedDataGridViewRows(dataGridView1); |
||
802 | |||
803 | if (!rows.Any()) |
||
804 | { |
||
805 | return; |
||
806 | } |
||
807 | |||
808 | var row = rows.First(); |
||
809 | |||
810 | try |
||
811 | { |
||
812 | using (var soundPlayer = new SoundPlayer(Assembly.GetExecutingAssembly().GetManifestResourceStream("HamBook.Effects.pot.wav"))) |
||
813 | { |
||
814 | if (await _catAssemblies.CatSetAsync<int, bool>("FA", new object[] { row.Cells["FrequencyColumn"].Value }, _cancellationToken)) |
||
815 | { |
||
816 | if (Configuration.Navigation.MouseScrollSound) |
||
817 | { |
||
818 | soundPlayer.Play(); |
||
819 | } |
||
820 | } |
||
821 | } |
||
822 | } |
||
823 | catch (Exception exception) |
||
824 | { |
||
825 | Log.Error(exception, Resources.Failed_to_set_VFO_A_frequency); |
||
826 | } |
||
827 | } |
||
828 | |||
829 | private void dataGridView1_MouseDown(object sender, MouseEventArgs e) |
||
830 | { |
||
831 | var dataGridView = (DataGridView)sender; |
||
832 | |||
833 | var hitTest = dataGridView.HitTest(e.X, e.Y); |
||
834 | |||
835 | switch (e.Button) |
||
836 | { |
||
837 | case MouseButtons.Right: |
||
838 | if(GetSelectedDataGridViewRows(dataGridView).Count() > 1) |
||
839 | { |
||
840 | return; |
||
841 | } |
||
842 | |||
843 | dataGridView.ClearSelection(); |
||
844 | dataGridView.Rows[hitTest.RowIndex].Selected = true; |
||
845 | break; |
||
846 | } |
||
847 | } |
||
52 | office | 848 | |
849 | private IEnumerable<DataGridViewRow> CopySelectedRows(DataGridView dataGridView) |
||
850 | { |
||
851 | foreach (var row in GetSelectedDataGridViewRows(dataGridView)) |
||
852 | { |
||
853 | var clone = row.Clone() as DataGridViewRow; |
||
854 | |||
855 | for (var i = 0; i < row.Cells.Count; ++i) |
||
856 | { |
||
857 | if (row.Cells[i].ReadOnly) |
||
858 | { |
||
859 | continue; |
||
860 | } |
||
861 | |||
862 | clone.Cells[i].Value = row.Cells[i].Value; |
||
863 | } |
||
864 | |||
865 | yield return clone; |
||
866 | } |
||
867 | } |
||
868 | private void DeleteSelectedRows(DataGridView dataGridView) |
||
869 | { |
||
870 | foreach (var row in GetSelectedDataGridViewRows(dataGridView)) |
||
871 | { |
||
872 | for (var i = 0; i < row.Cells.Count; ++i) |
||
873 | { |
||
874 | if (row.Cells[i].ReadOnly) |
||
875 | { |
||
876 | continue; |
||
877 | } |
||
878 | |||
879 | row.Cells[i].Value = default; |
||
880 | } |
||
881 | } |
||
882 | } |
||
883 | |||
884 | |||
885 | private void PasteSelectedRows(DataGridView dataGridView, IEnumerable<DataGridViewRow> pasteRows) |
||
886 | { |
||
887 | var rows = pasteRows.ToList(); |
||
888 | if(rows.Count == 0) |
||
889 | { |
||
890 | return; |
||
891 | } |
||
892 | |||
893 | foreach (var row in GetSelectedDataGridViewRows(dataGridView)) |
||
894 | { |
||
895 | if (rows.Count == 0) |
||
896 | { |
||
897 | return; |
||
898 | } |
||
899 | |||
900 | var paste = rows[0]; |
||
901 | for (var i = 0; i < paste.Cells.Count; ++i) |
||
902 | { |
||
903 | if (row.Cells[i].ReadOnly) |
||
904 | { |
||
905 | continue; |
||
906 | } |
||
907 | row.Cells[i].Value = paste.Cells[i].Value; |
||
908 | } |
||
909 | |||
910 | rows.RemoveAt(0); |
||
911 | } |
||
912 | } |
||
913 | |||
914 | private IEnumerable<DataGridViewRow> CutSelectedRows(DataGridView dataGridView) |
||
915 | { |
||
916 | foreach (var row in GetSelectedDataGridViewRows(dataGridView)) |
||
917 | { |
||
918 | var clone = row.Clone() as DataGridViewRow; |
||
919 | |||
920 | for (var i = 0; i < row.Cells.Count; ++i) |
||
921 | { |
||
922 | if (row.Cells[i].ReadOnly) |
||
923 | { |
||
924 | continue; |
||
925 | } |
||
926 | |||
927 | clone.Cells[i].Value = row.Cells[i].Value; |
||
928 | |||
929 | row.Cells[i].Value = default; |
||
930 | } |
||
931 | |||
932 | yield return clone; |
||
933 | } |
||
934 | } |
||
935 | |||
936 | private void cutToolStripMenuItem1_Click(object sender, EventArgs e) |
||
937 | { |
||
938 | _clipboardRows = new List<DataGridViewRow>(); |
||
939 | foreach (var row in CutSelectedRows(dataGridView1)) |
||
940 | { |
||
941 | _clipboardRows.Add(row); |
||
942 | } |
||
943 | } |
||
944 | |||
945 | private void copyToolStripMenuItem1_Click(object sender, EventArgs e) |
||
946 | { |
||
947 | _clipboardRows = new List<DataGridViewRow>(); |
||
948 | foreach (var row in CopySelectedRows(dataGridView1)) |
||
949 | { |
||
950 | _clipboardRows.Add(row); |
||
951 | } |
||
952 | } |
||
953 | |||
954 | private void pasteToolStripMenuItem1_Click(object sender, EventArgs e) |
||
955 | { |
||
956 | PasteSelectedRows(dataGridView1, _clipboardRows); |
||
957 | } |
||
958 | |||
959 | private void deleteToolStripMenuItem_Click(object sender, EventArgs e) |
||
960 | { |
||
961 | DeleteSelectedRows(dataGridView1); |
||
962 | } |
||
15 | office | 963 | } |
964 | } |