HamBook – Blame information for rev 35

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 using HamBook.Radios;
2 using HamBook.Utilities;
3 using HamBook.Utilities.Serialization;
4 using NetSparkleUpdater.Enums;
5 using NetSparkleUpdater.SignatureVerifiers;
6 using NetSparkleUpdater.UI.WinForms;
7 using NetSparkleUpdater;
8 using Serilog;
9 using System;
10 using System.ComponentModel;
11 using System.Drawing;
12 using System.IO;
13 using System.IO.Ports;
14 using System.Reflection;
15 using System.Threading;
16 using System.Threading.Tasks;
17 using System.Windows.Forms;
18 using HamBook.Properties;
19 using HamBook.Radios.Generic;
20 using PowerState = HamBook.Radios.Generic.PowerState;
5 office 21 using System.Media;
7 office 22 using HamBook.Utilities.Controls;
9 office 23 using RJCP.IO.Ports;
24 using System.Text;
12 office 25 using System.Collections.ObjectModel;
26 using Configuration;
27 using System.Collections.Generic;
15 office 28 using NAudio.Utils;
29 using System.Linq;
30 using static System.Net.Mime.MediaTypeNames;
25 office 31 using System.Diagnostics;
32 using Newtonsoft.Json.Linq;
26 office 33 using System.Collections.Concurrent;
34 using Org.BouncyCastle.Math.Field;
1 office 35  
36 namespace HamBook
37 {
38 public partial class Form1 : Form
39 {
40 private ScheduledContinuation _changedConfigurationContinuation;
25 office 41 private ScheduledContinuation _squelchScheduledContinuation;
42 private ScheduledContinuation _powerScheduledContinuation;
43  
1 office 44 private Configuration.Configuration Configuration { get; set; }
9 office 45 private SerialPortStream _serialPort;
1 office 46 private LogMemorySink _memorySink;
47 private ViewLogsForm _viewLogsForm;
48 private AboutForm _aboutForm;
49 private SettingsForm _settingsForm;
50 private SparkleUpdater _sparkle;
51 private readonly CancellationToken _cancellationToken;
52 private readonly CancellationTokenSource _cancellationTokenSource;
53 private CatAssemblies _catAssemblies;
3 office 54 private BandScan _bandScan;
10 office 55 private SpectrogramForm _spectrogramForm;
15 office 56 private MemoryOrganizerForm _memoryOrganizerForm;
1 office 57  
15 office 58 private CancellationTokenSource _tagTickerCancellationTokenSource;
59 private CancellationToken _tagTickerCancellationToken;
60 private string _storedMemoryChannelTagText;
61 private string _storedMemoryChannelLocation;
62 private MemoryChannel _tickerTextMemoryChannel;
63 private Task _tickerTask;
64 private volatile bool _tickerTaskRunning;
65  
26 office 66 private ConcurrentDictionary<int, MemoryChannel> _memoryChannelStore = new ConcurrentDictionary<int, MemoryChannel>();
67  
1 office 68 public bool MemorySinkEnabled { get; set; }
69  
70 private Form1()
71 {
72 _cancellationTokenSource = new CancellationTokenSource();
73 _cancellationToken = _cancellationTokenSource.Token;
74  
75 _changedConfigurationContinuation = new ScheduledContinuation();
3 office 76  
25 office 77 _squelchScheduledContinuation = new ScheduledContinuation();
78 _powerScheduledContinuation = new ScheduledContinuation();
79  
1 office 80 }
81  
82 public Form1(Mutex mutex) : this()
83 {
84 InitializeComponent();
85  
86 _memorySink = new LogMemorySink();
87  
88 Log.Logger = new LoggerConfiguration()
89 .MinimumLevel.Debug()
90 .WriteTo.Conditional(condition => MemorySinkEnabled, configureSink => configureSink.Sink(_memorySink))
91 .WriteTo.File(Path.Combine(Constants.UserApplicationDirectory, "Logs", $"{Constants.AssemblyName}.log"),
92 rollingInterval: RollingInterval.Day)
93 .CreateLogger();
94  
95 // Start application update.
96 var manifestModuleName = Assembly.GetEntryAssembly().ManifestModule.FullyQualifiedName;
97 var icon = Icon.ExtractAssociatedIcon(manifestModuleName);
98  
99 _sparkle = new SparkleUpdater("https://hambook.grimore.org/update/appcast.xml",
100 new Ed25519Checker(SecurityMode.Strict, "LonrgxVjSF0GnY4hzwlRJnLkaxnDn2ikdmOifILzLJY="))
101 {
102 UIFactory = new UIFactory(icon),
103 RelaunchAfterUpdate = true
104 };
105 }
106  
107 private async void Form1_Load(object sender, EventArgs e)
108 {
13 office 109 _sparkle.StartLoop(true, true);
110  
1 office 111 Configuration = await LoadConfiguration();
112  
7 office 113 _serialPort = InitializeSerialPort(Configuration);
1 office 114  
7 office 115 _catAssemblies = InitializeAssemblies(_serialPort);
1 office 116  
117 try
118 {
9 office 119 switch (await _catAssemblies.CatReadAsync<PowerState>("PS", new object[] { }, _cancellationToken))
1 office 120 {
121 case PowerState.ON:
7 office 122 Log.Information(Resources.Attempting_to_initialize_radio);
9 office 123 if(!await InitializeRadio())
7 office 124 {
125 return;
126 }
127  
128 Log.Information(Resources.Initializing_GUI);
1 office 129 break;
130 }
131 }
132 catch(Exception exception)
133 {
134 Log.Error(exception, Resources.Failed_to_read_power_state);
135 }
26 office 136  
137 var memoryBankQueue = new ConcurrentQueue<int>();
138 var memoryBankTaskCompletionSource = new TaskCompletionSource<bool>();
139  
140 async void IdleHandler(object idleHandlerSender, EventArgs idleHandlerArgs)
141 {
142 await memoryBankTaskCompletionSource.Task;
143  
144 try
145 {
146 if (!memoryBankQueue.TryDequeue(out var memoryBank))
147 {
148 System.Windows.Forms.Application.Idle -= IdleHandler;
149  
150 return;
151 }
152  
153 var location = $"{memoryBank:000}";
154  
155 MemoryChannel memoryChannel = new MemoryChannel();
156  
157 try
158 {
159 memoryChannel = await _catAssemblies.CatReadAsync<MemoryChannel>("MT", new object[] { location }, _cancellationToken);
160  
161 scrollableToolStripComboBox5.Items.Add(location);
162  
163 }
164 catch(Exception exception)
165 {
166 Log.Warning(exception, Resources.Could_not_read_memory_bank);
167  
168 return;
169 }
170  
171 _memoryChannelStore.TryAdd(memoryBank, memoryChannel);
172  
173 }
174 catch (Exception exception)
175 {
176 Log.Error(exception, Resources.Could_not_update_data_grid_view);
177 }
178 }
179  
180 System.Windows.Forms.Application.Idle += IdleHandler;
181 try
182 {
183 foreach (var memoryBank in Enumerable.Range(1, 99))
184 {
185 memoryBankQueue.Enqueue(memoryBank);
186 }
187  
188 memoryBankTaskCompletionSource.TrySetResult(true);
189 }
190 catch (Exception exception)
191 {
192 System.Windows.Forms.Application.Idle -= IdleHandler;
193  
194 Log.Error(exception, Resources.Unable_to_create_memory_banks);
195 }
1 office 196 }
197  
9 office 198 private async void quitToolStripMenuItem_Click(object sender, EventArgs e)
1 office 199 {
9 office 200 if(_bandScan != null)
201 {
202 await _bandScan.Stop();
203 _bandScan = null;
204 }
205  
14 office 206 // Save configuration on quit.
207 await SaveConfiguration();
208  
1 office 209 Close();
210 }
211  
212 private void viewLogsToolStripMenuItem_Click(object sender, EventArgs e)
213 {
214 if (_viewLogsForm != null)
215 {
216 return;
217 }
218  
219 _viewLogsForm = new ViewLogsForm(this, _memorySink, _cancellationToken);
220 _viewLogsForm.Closing += ViewLogsForm_Closing;
221 _viewLogsForm.Show();
222 }
223  
224 private void ViewLogsForm_Closing(object sender, CancelEventArgs e)
225 {
226 if (_viewLogsForm == null)
227 {
228 return;
229 }
230  
231 _viewLogsForm.Closing -= ViewLogsForm_Closing;
232 _viewLogsForm.Close();
233 _viewLogsForm = null;
234 }
235  
236 private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
237 {
238 if (_aboutForm != null)
239 {
240 return;
241 }
242  
243 _aboutForm = new AboutForm(_cancellationToken);
244 _aboutForm.Closing += AboutForm_Closing;
245 _aboutForm.Show();
246 }
247  
248 private void AboutForm_Closing(object sender, CancelEventArgs e)
249 {
250 if (_aboutForm == null)
251 {
252 return;
253 }
254  
255 _aboutForm.Dispose();
256 _aboutForm = null;
257 }
258  
259 private void settingsToolStripMenuItem_Click(object sender, EventArgs e)
260 {
261 if (_settingsForm != null)
262 {
263 return;
264 }
265  
266 _settingsForm = new SettingsForm(Configuration, _cancellationToken);
267 _settingsForm.Closing += SettingsForm_Closing;
268 _settingsForm.Show();
269 }
270  
271 private void SettingsForm_Closing(object sender, CancelEventArgs e)
272 {
273 if (_settingsForm == null)
274 {
275 return;
276 }
277  
278 if(_settingsForm.SaveOnClose)
279 {
280 // Commit the configuration.
281 _changedConfigurationContinuation.Schedule(TimeSpan.FromSeconds(1),
282 async () => {
283 await SaveConfiguration();
284  
11 office 285 if(_bandScan != null)
286 {
287 await _bandScan.Stop();
288 _bandScan = null;
289 }
290  
291  
1 office 292 Miscellaneous.LaunchOnBootSet(Configuration.LaunchOnBoot);
293  
7 office 294 _serialPort = InitializeSerialPort(Configuration);
1 office 295  
7 office 296 _catAssemblies = InitializeAssemblies(_serialPort);
297  
298 try
299 {
9 office 300 switch (await _catAssemblies.CatReadAsync<PowerState>("PS", new object[] { }, _cancellationToken))
7 office 301 {
302 case PowerState.ON:
303 Log.Information(Resources.Attempting_to_initialize_radio);
9 office 304 if (!await InitializeRadio())
7 office 305 {
306 return;
307 }
308 Log.Information(Resources.Initializing_GUI);
309 break;
310 }
311 }
312 catch (Exception exception)
313 {
314 Log.Error(exception, Resources.Failed_to_read_power_state);
315 }
316  
1 office 317 }, _cancellationToken);
318 }
319  
320 _settingsForm.Dispose();
321 _settingsForm = null;
322 }
323  
324 public async Task SaveConfiguration()
325 {
326 if (!Directory.Exists(Constants.UserApplicationDirectory))
327 {
328 Directory.CreateDirectory(Constants.UserApplicationDirectory);
329 }
330  
331 switch (await Serialization.Serialize(Configuration, Constants.ConfigurationFile, "Configuration",
332 "<!ATTLIST Configuration xmlns:xsi CDATA #IMPLIED xsi:noNamespaceSchemaLocation CDATA #IMPLIED>",
333 CancellationToken.None))
334 {
3 office 335 case SerializationSuccess<Configuration.Configuration> configuration:
16 office 336 Log.Information(Resources.Configuration_serialized_successfully);
1 office 337 break;
338 case SerializationFailure serializationFailure:
16 office 339 Log.Warning(serializationFailure.Exception.Message, Resources.Configuration_failed_to_serialize);
1 office 340 break;
341 }
342 }
343  
344 public static async Task<Configuration.Configuration> LoadConfiguration()
345 {
346 if (!Directory.Exists(Constants.UserApplicationDirectory))
347 {
348 Directory.CreateDirectory(Constants.UserApplicationDirectory);
349 }
350  
351 var deserializationResult =
352 await Serialization.Deserialize<Configuration.Configuration>(Constants.ConfigurationFile,
353 Constants.ConfigurationNamespace, Constants.ConfigurationXsd, CancellationToken.None);
354  
355 switch (deserializationResult)
356 {
357 case SerializationSuccess<Configuration.Configuration> serializationSuccess:
358 return serializationSuccess.Result;
359 case SerializationFailure serializationFailure:
16 office 360 Log.Warning(serializationFailure.Exception, Resources.Configuration_failed_to_deserialize);
1 office 361 return new Configuration.Configuration();
362 default:
363 return new Configuration.Configuration();
364 }
365 }
366  
9 office 367 private async Task<bool> InitializeRadio()
1 office 368 {
369 try
370 {
9 office 371 await _catAssemblies.CatWriteAsync<InformationState>("AI", new object[] { InformationState.OFF }, _cancellationToken);
372  
11 office 373 return await _catAssemblies.CatReadAsync<bool>("ID", new object[] { }, _cancellationToken);
1 office 374 }
375 catch(Exception exception)
376 {
7 office 377 Log.Error(exception, Resources.Unable_to_initialize_radio);
11 office 378  
7 office 379 return false;
1 office 380 }
7 office 381 }
382  
9 office 383 private CatAssemblies InitializeAssemblies(SerialPortStream serialPort)
7 office 384 {
385 if(_catAssemblies != null)
1 office 386 {
7 office 387 _catAssemblies.Dispose();
388 _catAssemblies = null;
1 office 389 }
390  
7 office 391 return new CatAssemblies(serialPort, Configuration.Radio);
392 }
393  
9 office 394 private SerialPortStream InitializeSerialPort(Configuration.Configuration configuration)
7 office 395 {
396 if (_serialPort != null)
1 office 397 {
7 office 398 if (_serialPort.IsOpen)
399 {
400 _serialPort.Close();
401 }
26 office 402  
7 office 403 _serialPort.Dispose();
404 _serialPort = null;
1 office 405 }
406  
7 office 407 // Set up serial connection.
9 office 408 var serialPort = new SerialPortStream(configuration.Port, configuration.Speed, configuration.DataBits, configuration.Parity, configuration.StopBits);
7 office 409 serialPort.ReadTimeout = configuration.SerialPortTimeout.Read;
410 serialPort.WriteTimeout = configuration.SerialPortTimeout.Write;
411 serialPort.Handshake = configuration.Handshake;
9 office 412 serialPort.Encoding = Encoding.ASCII;
1 office 413  
7 office 414 Log.Information($"{Resources.Initialized_serial_port} {configuration.Port} {configuration.Speed} {configuration.Parity} {configuration.DataBits} {configuration.StopBits}");
415  
416 return serialPort;
1 office 417 }
418  
419 private async void updateToolStripMenuItem_Click(object sender, EventArgs e)
420 {
421 // Manually check for updates, this will not show a ui
422 var result = await _sparkle.CheckForUpdatesQuietly();
423 if (result.Status == NetSparkleUpdater.Enums.UpdateStatus.UpdateAvailable)
424 {
425 // if update(s) are found, then we have to trigger the UI to show it gracefully
426 _sparkle.ShowUpdateNeededUI();
427 return;
428 }
429  
430 MessageBox.Show(Resources.No_updates_available_at_this_time, Resources.HamBook, MessageBoxButtons.OK,
431 MessageBoxIcon.Asterisk,
432 MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly, false);
433 }
434  
435 private async void onToolStripMenuItem_Click(object sender, EventArgs e)
436 {
437 try
438 {
15 office 439 await _catAssemblies.CatSetAsync<PowerState, bool>("PS", new object[] { PowerState.ON }, _cancellationToken);
1 office 440 }
441 catch(Exception exception)
442 {
443 Log.Error(exception, Resources.Failed_to_set_power_state);
444 }
445 }
446  
447 private async void offToolStripMenuItem_Click(object sender, EventArgs e)
448 {
449 try
450 {
15 office 451 await _catAssemblies.CatSetAsync<PowerState, bool>("PS", new object[] { PowerState.OFF }, _cancellationToken);
1 office 452 }
453 catch(Exception exception)
454 {
455 Log.Error(exception, Resources.Failed_to_set_power_state);
456 }
457 }
458  
29 office 459 private async void scrollableToolStripComboBox2_MouseWheel(object sender, MouseEventArgs e)
1 office 460 {
27 office 461 var toolStripComboBox = (ScrollableToolStripComboBox)sender;
462 if (int.TryParse(toolStripComboBox.Text, out var frequency))
1 office 463 {
27 office 464 switch (Math.Sign(e.Delta))
3 office 465 {
27 office 466 case -1:
467 frequency = frequency - Configuration.Navigation.FrequencyStep;
468 break;
469 case 1:
470 frequency = frequency + Configuration.Navigation.FrequencyStep;
471 break;
472 }
5 office 473  
27 office 474 try
3 office 475 {
27 office 476 using (var soundPlayer = new SoundPlayer(Assembly.GetExecutingAssembly().GetManifestResourceStream("HamBook.Effects.pot.wav")))
477 {
29 office 478 if (await _catAssemblies.CatSetAsync<int, bool>("FB", new object[] { frequency }, _cancellationToken))
479 {
480 toolStripComboBox.Text = $"{frequency}";
27 office 481  
32 office 482 if (Configuration.Navigation.MouseScrollSound)
483 {
484 soundPlayer.Play();
485 }
29 office 486 }
27 office 487 }
3 office 488 }
489 catch (Exception exception)
490 {
27 office 491 Log.Error(exception, Resources.Failed_to_set_VFO_B_frequency);
3 office 492 }
1 office 493 }
494 }
495  
29 office 496 private async void scrollableToolStripComboBox2_KeyPress(object sender, KeyPressEventArgs e)
1 office 497 {
29 office 498 switch (e.KeyChar)
1 office 499 {
29 office 500 case (char)Keys.Enter:
501 var toolStripComboBox = (ScrollableToolStripComboBox)sender;
27 office 502  
29 office 503 if (int.TryParse(toolStripComboBox.Text, out var frequency))
27 office 504 {
29 office 505 try
506 {
507 using (var soundPlayer = new SoundPlayer(Assembly.GetExecutingAssembly().GetManifestResourceStream("HamBook.Effects.pot.wav")))
508 {
509 if (await _catAssemblies.CatSetAsync<int, bool>("FB", new object[] { frequency }, _cancellationToken))
510 {
511 e.Handled = true;
27 office 512  
32 office 513 if (Configuration.Navigation.MouseScrollSound)
514 {
515 soundPlayer.Play();
516 }
29 office 517 }
518 }
519 }
520 catch (Exception exception)
521 {
522 Log.Error(exception, Resources.Failed_to_set_VFO_B_frequency);
523 }
524 }
525 break;
27 office 526 }
3 office 527 }
528  
9 office 529 private async void toolStripMenuItem1_Click(object sender, EventArgs e)
3 office 530 {
531 if (_bandScan == null)
532 {
1 office 533 return;
534 }
535  
9 office 536 await _bandScan.Stop();
3 office 537 _bandScan = null;
1 office 538 }
3 office 539  
9 office 540 private async void scanToolStripMenuItem_Click(object sender, EventArgs e)
3 office 541 {
5 office 542 if (!(sender is ToolStripMenuItem toolStripMenuItem) ||
543 !int.TryParse(toolStripMenuItem.Tag.ToString(), out var meters))
3 office 544 {
5 office 545 return;
3 office 546 }
547  
5 office 548 if (!int.TryParse(scrollableToolStripComboBox3.Text, out var pause))
3 office 549 {
5 office 550 pause = 5;
3 office 551 }
552  
5 office 553 if(!int.TryParse(scrollableToolStripComboBox4.Text, out var step))
3 office 554 {
5 office 555 step = 5000;
3 office 556 }
557  
21 office 558 if(!int.TryParse(scrollableToolStripComboBox6.Text, out var scanDetectPause))
559 {
560 scanDetectPause = 10;
561 }
562  
5 office 563 if (!Configuration.Definitions.TryGetBand(meters, out var band))
3 office 564 {
5 office 565 return;
3 office 566 }
567  
568 if (_bandScan != null)
569 {
9 office 570 await _bandScan.Stop();
571 _bandScan = null;
3 office 572 }
573  
21 office 574 _bandScan = new BandScan(_catAssemblies, (int)band.Min, (int)band.Max, _serialPort, Configuration);
9 office 575  
21 office 576 if (toolStripMenuItem14.Checked)
577 {
23 office 578 _bandScan.Start(step, pause, scanDetectPause, toolStripMenuItem16.Checked);
21 office 579  
580 return;
581 }
582  
23 office 583 _bandScan.Start(step, pause, toolStripMenuItem16.Checked);
3 office 584 }
585  
35 office 586 private void modeToolStripMenuItem_DropDownOpening(object sender, EventArgs e)
3 office 587 {
11 office 588 Task.Delay(TimeSpan.FromSeconds(1), _cancellationToken).ContinueWith(async task =>
7 office 589 {
11 office 590 try
591 {
592 var mode = await _catAssemblies.CatReadAsync<RadioMode>("MD", new object[] { }, _cancellationToken);
7 office 593  
11 office 594 contextMenuStrip1.InvokeIfRequired(contextMenuStrip =>
595 {
596 toolStripComboBox1.Text = mode;
597 });
598 }
599 catch (Exception exception)
600 {
601 Log.Error(exception, Resources.Failed_to_read_radio_mode);
602 }
7 office 603  
11 office 604 try
605 {
606 var fa = await _catAssemblies.CatReadAsync<int>("FA", new object[] { }, _cancellationToken);
7 office 607  
11 office 608 contextMenuStrip1.InvokeIfRequired(contextMenuStrip =>
609 {
29 office 610 scrollableToolStripComboBox1.Text = $"{fa}";
11 office 611  
612 });
613  
614 }
615 catch (Exception exception)
616 {
617 Log.Error(exception, Resources.Failed_to_read_VFO_A);
618 }
619  
620 try
621 {
622 var fb = await _catAssemblies.CatReadAsync<int>("FB", new object[] { }, _cancellationToken);
623  
624 contextMenuStrip1.InvokeIfRequired(contextMenuStrip =>
625 {
29 office 626 scrollableToolStripComboBox2.Text = $"{fb}";
11 office 627  
628 });
629 }
630 catch (Exception exception)
631 {
632 Log.Error(exception, Resources.Failed_to_read_VFO_B);
633 }
19 office 634  
635 try
636 {
637 var mc = await _catAssemblies.CatReadAsync<int>("MC", new object[] { }, _cancellationToken);
638  
639 contextMenuStrip1.InvokeIfRequired(contextMenuStrip =>
640 {
26 office 641 scrollableToolStripComboBox5.Text = $"{mc:000}";
19 office 642  
26 office 643 if(_memoryChannelStore.TryGetValue(mc, out var memoryChannel))
644 {
645 toolStripMenuItem27.Text = memoryChannel.Text;
646 }
647  
19 office 648 });
649 }
650 catch (Exception exception)
651 {
652 Log.Error(exception, Resources.Failed_to_read_memory_channel);
653 }
24 office 654  
655 try
656 {
657 var pc = await _catAssemblies.CatReadAsync<int>("PC", new object[] { }, _cancellationToken);
658  
659 contextMenuStrip1.InvokeIfRequired(contextMenuStrip =>
660 {
661 scrollableToolStripComboBox7.Text = $"{pc}";
662  
663 });
664 }
665 catch (Exception exception)
666 {
25 office 667 Log.Error(exception, Resources.Failed_to_read_power_state);
24 office 668 }
35 office 669  
670 try
671 {
672 var sq = await _catAssemblies.CatReadAsync<int>("SQ", new object[] { }, _cancellationToken);
673  
674 contextMenuStrip1.InvokeIfRequired(contextMenuStrip =>
675 {
676 scrollableToolStripComboBox11.Text = $"{sq}";
677  
678 });
679 }
680 catch (Exception exception)
681 {
682 Log.Error(exception, Resources.Failed_to_read_squelch);
683 }
11 office 684 }, _cancellationToken);
3 office 685 }
10 office 686  
687 private void spectrogramToolStripMenuItem_Click(object sender, EventArgs e)
688 {
689 if (_spectrogramForm != null)
690 {
691 return;
692 }
693  
694 _spectrogramForm = new SpectrogramForm(Configuration, _cancellationToken);
695 _spectrogramForm.Closing += SpectrogramForm_Closing;
696 _spectrogramForm.Show();
697 }
698  
699 private void SpectrogramForm_Closing(object sender, CancelEventArgs e)
700 {
701 if (_spectrogramForm == null)
702 {
703 return;
704 }
705  
706 _spectrogramForm.Dispose();
707 _spectrogramForm = null;
14 office 708  
709 // Commit the configuration.
710 _changedConfigurationContinuation.Schedule(TimeSpan.FromSeconds(1),
711 async () =>
712 {
713 await SaveConfiguration();
714 }, _cancellationToken);
10 office 715 }
15 office 716  
717 private void toolStripMenuItem3_Click(object sender, EventArgs e)
718 {
719 if (_memoryOrganizerForm != null)
720 {
721 return;
722 }
723  
724 _memoryOrganizerForm = new MemoryOrganizerForm(Configuration, _catAssemblies, _cancellationToken);
725 _memoryOrganizerForm.Closing += MemoryOrganizerForm_Closing;
726 _memoryOrganizerForm.Show();
727 }
728  
729 private void MemoryOrganizerForm_Closing(object sender, CancelEventArgs e)
730 {
731 if (_memoryOrganizerForm == null)
732 {
733 return;
734 }
735  
736 _memoryOrganizerForm.Dispose();
737 _memoryOrganizerForm = null;
738  
739 }
740  
741 private async void toolStripMenuItem4_Click(object sender, EventArgs e)
742 {
743 if (_tickerTaskRunning)
744 {
745 return;
746 }
747  
748 var toolStripTextBox = (ToolStripTextBox)toolStripTextBox6;
749  
750 try
751 {
752 var result = await _catAssemblies.CatReadAsync<MemoryChannel>("MT", new object[] { "001" }, _cancellationToken);
753  
754 _tickerTextMemoryChannel = await _catAssemblies.CatReadAsync<MemoryChannel>("MT", new object[] { $"{result.CurrentLocation}" }, _cancellationToken);
755  
756 _storedMemoryChannelTagText = _tickerTextMemoryChannel.Text;
757 _storedMemoryChannelLocation = _tickerTextMemoryChannel.CurrentLocation;
758 }
759 catch(Exception exception)
760 {
761 Log.Error(exception, Resources.Could_not_read_memory_bank);
762 }
763  
764 var tickerText = $"{toolStripTextBox.Text,-12}";
765  
766 _tagTickerCancellationTokenSource = new CancellationTokenSource();
767 _tagTickerCancellationToken = _tagTickerCancellationTokenSource.Token;
768  
769 var characterQueue = new Queue<char>(12);
770 foreach(var i in Enumerable.Range(0, 12))
771 {
772 var x = tickerText.ElementAtOrDefault(i);
773  
774 if(x == default)
775 {
776 characterQueue.Enqueue(' ');
777 continue;
778 }
779  
780 characterQueue.Enqueue(x);
781 }
782  
783 #pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
784 _tickerTask = Task.Run(() => CycleText(characterQueue), _cancellationToken);
785 #pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
786 }
787  
788 private async void toolStripMenuItem5_Click(object sender, EventArgs e)
789 {
790 if(!_tickerTaskRunning)
791 {
792 return;
793 }
794  
795 _tagTickerCancellationTokenSource.Cancel();
796 if (_tickerTask != null)
797 {
798 await _tickerTask;
799 _tickerTask = null;
800 }
801  
802 try
803 {
804 _tickerTextMemoryChannel.CurrentLocation = $"{_storedMemoryChannelLocation:000}";
17 office 805 _tickerTextMemoryChannel.Text = $"{_storedMemoryChannelTagText, -12}";
15 office 806  
807 var success = await _catAssemblies.CatSetAsync<MemoryChannel, bool>("MT", new object[] { _tickerTextMemoryChannel }, _cancellationToken);
808 if (!success)
809 {
810 Log.Error(Resources.Error_while_restoring_memory_text);
811  
812 return;
813 }
814 }
815 catch(Exception exception)
816 {
817 Log.Error(exception, Resources.Error_while_restoring_memory_text);
818 }
17 office 819 finally
820 {
821 _tickerTaskRunning = false;
822 }
15 office 823 }
824  
825 private async Task CycleText(Queue<char> characterQueue)
826 {
827 _tickerTaskRunning = true;
828 try
829 {
830 do
831 {
832 var text = string.Join("", characterQueue.OfType<char>());
833  
834 _tickerTextMemoryChannel.Text = text;
835  
836 await _catAssemblies.CatWriteAsync<MemoryChannel>("MT", new object[] { _tickerTextMemoryChannel }, _cancellationToken);
837  
838 var x = characterQueue.Dequeue();
839 characterQueue.Enqueue(x);
840  
841 await Task.Delay(250);
842  
843 } while (!_tagTickerCancellationToken.IsCancellationRequested);
844 }
845 catch(Exception exception)
846 {
847 Log.Error(exception, Resources.Error_while_cycling_text);
848 }
849 }
19 office 850  
27 office 851 private async void scrollableToolStripComboBox5_SelectedIndexChanged(object sender, EventArgs e)
19 office 852 {
27 office 853 using (var soundPlayer = new SoundPlayer(Assembly.GetExecutingAssembly().GetManifestResourceStream("HamBook.Effects.pot.wav")))
19 office 854 {
27 office 855 var toolStripComboBox = (ToolStripComboBox)sender;
856 if (int.TryParse(toolStripComboBox.Text, out var channel))
19 office 857 {
27 office 858 if (_memoryChannelStore.TryGetValue(channel, out var memoryChannel))
859 {
860 try
861 {
862 await _catAssemblies.CatWriteAsync<int>("MC", new object[] { channel }, _cancellationToken);
19 office 863  
27 office 864 scrollableToolStripComboBox1.Text = $"{memoryChannel.Frequency}";
24 office 865  
27 office 866 toolStripMenuItem27.Text = memoryChannel.Text;
24 office 867  
27 office 868 soundPlayer.Play();
869 }
870 catch (Exception exception)
871 {
872 Log.Error(exception, Resources.Failed_to_set_memory_channel);
873 }
26 office 874 }
19 office 875 }
876 }
877 }
20 office 878  
35 office 879 private async void powerToolStripMenuItem_DropDownOpening(object sender, EventArgs e)
20 office 880 {
881 var toolStripMenuItem = toolStripMenuItem11;
882  
883 try
884 {
885 switch (await _catAssemblies.CatReadAsync<PowerState>("PS", new object[] { }, _cancellationToken))
886 {
887 case PowerState.ON:
888 toolStripMenuItem.Text = Resources.On;
889 break;
890 case PowerState.OFF:
891 toolStripMenuItem.Text = Resources.Off;
892 break;
893 }
894 }
895 catch (Exception exception)
896 {
897 Log.Error(exception, Resources.Failed_to_read_power_state);
898 }
899 }
23 office 900  
901 private async void toolStripMenuItem17_Click(object sender, EventArgs e)
902 {
903 try
904 {
905 await _catAssemblies.CatWriteAsync<TunerState>("AC", new object[] { TunerState.TUNER_ON }, _cancellationToken);
906  
907 await _catAssemblies.CatWriteAsync<TunerState>("AC", new object[] { TunerState.TUNING_START }, _cancellationToken);
908 }
909 catch (Exception exception)
910 {
911 Log.Error(exception, Resources.Failed_tuning_current_frequency);
912 }
913 }
24 office 914  
915 private async void toolStripMenuItem22_CheckStateChanged(object sender, EventArgs e)
916 {
917 var toolStripMenuItem = (ToolStripMenuItem)sender;
918 try
919 {
920 if (toolStripMenuItem.Checked)
921 {
922 await _catAssemblies.CatWriteAsync<IpoState>("PA", new object[] { IpoState.IPO }, _cancellationToken);
923 return;
924 }
925  
926 await _catAssemblies.CatWriteAsync<IpoState>("PA", new object[] { IpoState.AMP }, _cancellationToken);
927 }
928 catch (Exception exception)
929 {
930 Log.Error(exception, Resources.Failed_setting_IPO);
931 }
932 }
933  
934 private async void toolStripMenuItem23_CheckStateChanged(object sender, EventArgs e)
935 {
936 var toolStripMenuItem = (ToolStripMenuItem)sender;
937  
938 try
939 {
940 if (toolStripMenuItem.Checked)
941 {
942 await _catAssemblies.CatWriteAsync<TunerState>("AC", new object[] { TunerState.TUNER_ON }, _cancellationToken);
943  
944 return;
945 }
946  
947 await _catAssemblies.CatWriteAsync<TunerState>("AC", new object[] { TunerState.TUNER_OFF }, _cancellationToken);
948 }
949 catch (Exception exception)
950 {
951 Log.Error(exception, Resources.Failed_setting_the_tuner_state);
952 }
953 }
954  
955 private async void toolStripMenuItem24_Click(object sender, EventArgs e)
956 {
957 try
958 {
959 await _catAssemblies.CatWriteAsync<TunerState>("AC", new object[] { TunerState.TUNING_START }, _cancellationToken);
960 }
961 catch(Exception exception)
962 {
963  
964 }
965 }
25 office 966  
35 office 967 private void toolStripMenuItem21_DropDownOpening(object sender, EventArgs e)
25 office 968 {
969 Task.Delay(TimeSpan.FromSeconds(1), _cancellationToken).ContinueWith(async task =>
970 {
971 try
972 {
973 var ac = await _catAssemblies.CatReadAsync<TunerState>("AC", new object[] { }, _cancellationToken);
974  
975 contextMenuStrip1.InvokeIfRequired(contextMenuStrip =>
976 {
977 switch (ac)
978 {
979 case TunerState.TUNING_START:
980 case TunerState.TUNER_ON:
981 toolStripMenuItem23.Checked = true;
982 break;
983 case TunerState.TUNER_OFF:
984 toolStripMenuItem23.Checked = false;
985 break;
986 }
987  
988  
989 });
990 }
991 catch (Exception exception)
992 {
993 Log.Error(exception, Resources.Failed_to_read_the_tuner_state);
994 }
995  
996 try
997 {
998 var pa = await _catAssemblies.CatReadAsync<IpoState>("PA", new object[] { }, _cancellationToken);
999  
1000 contextMenuStrip1.InvokeIfRequired(contextMenuStrip =>
1001 {
1002 switch (pa)
1003 {
1004 case IpoState.AMP:
1005 toolStripMenuItem22.Checked = false;
1006 break;
1007 case IpoState.IPO:
1008 toolStripMenuItem22.Checked = true;
1009 break;
1010 }
1011  
1012  
1013 });
1014 }
1015 catch (Exception exception)
1016 {
1017 Log.Error(exception, Resources.Failed_to_read_IPO);
1018 }
1019 }, _cancellationToken);
1020 }
1021  
35 office 1022 private async void toolStripMenuItem19_DropDownOpening(object sender, EventArgs e)
25 office 1023 {
35 office 1024  
25 office 1025 }
1026  
29 office 1027 private async void scrollableToolStripComboBox1_MouseWheel(object sender, MouseEventArgs e)
25 office 1028 {
1029 var toolStripComboBox = (ScrollableToolStripComboBox)sender;
27 office 1030 if (int.TryParse(toolStripComboBox.Text, out var frequency))
1031 {
1032 switch (Math.Sign(e.Delta))
1033 {
1034 case -1:
1035 frequency = frequency - Configuration.Navigation.FrequencyStep;
1036 break;
1037 case 1:
1038 frequency = frequency + Configuration.Navigation.FrequencyStep;
1039 break;
1040 }
25 office 1041  
27 office 1042 try
1043 {
29 office 1044 using (var soundPlayer = new SoundPlayer(Assembly.GetExecutingAssembly().GetManifestResourceStream("HamBook.Effects.pot.wav")))
25 office 1045 {
29 office 1046 if (await _catAssemblies.CatSetAsync<int, bool>("FA", new object[] { frequency }, _cancellationToken))
1047 {
1048 toolStripComboBox.Text = $"{frequency}";
25 office 1049  
32 office 1050 if (Configuration.Navigation.MouseScrollSound)
1051 {
1052 soundPlayer.Play();
1053 }
29 office 1054 }
25 office 1055 }
27 office 1056 }
1057 catch (Exception exception)
1058 {
1059 Log.Error(exception, Resources.Failed_to_set_VFO_A_frequency);
1060 }
1061 }
1062 }
25 office 1063  
29 office 1064 private async void scrollableToolStripComboBox1_KeyPress(object sender, KeyPressEventArgs e)
27 office 1065 {
29 office 1066 switch (e.KeyChar)
27 office 1067 {
29 office 1068 case (char)Keys.Enter:
32 office 1069  
25 office 1070  
29 office 1071 var toolStripComboBox = (ScrollableToolStripComboBox)sender;
27 office 1072  
29 office 1073 if (int.TryParse(toolStripComboBox.Text, out var frequency))
27 office 1074 {
29 office 1075 try
1076 {
1077 using (var soundPlayer = new SoundPlayer(Assembly.GetExecutingAssembly().GetManifestResourceStream("HamBook.Effects.pot.wav")))
1078 {
1079 if (await _catAssemblies.CatSetAsync<int, bool>("FA", new object[] { frequency }, _cancellationToken))
1080 {
32 office 1081 e.Handled = true;
1082  
1083 if (Configuration.Navigation.MouseScrollSound)
1084 {
1085 soundPlayer.Play();
1086 }
29 office 1087 }
1088 }
1089 }
1090 catch (Exception exception)
1091 {
1092 Log.Error(exception, Resources.Failed_to_set_VFO_A_frequency);
1093 }
1094 }
1095 break;
27 office 1096 }
25 office 1097 }
1098  
27 office 1099 private void scrollableToolStripComboBox8_SelectedIndexChanged(object sender, EventArgs e)
25 office 1100 {
1101 var toolStripComboBox = (ScrollableToolStripComboBox)sender;
1102  
1103 _squelchScheduledContinuation.Schedule(TimeSpan.FromSeconds(1), () =>
1104 {
1105 contextMenuStrip1.InvokeIfRequired(async contextMenuStrip1 =>
1106 {
1107 if (int.TryParse(toolStripComboBox.Text, out var squelch))
1108 {
1109  
1110 try
1111 {
1112 await _catAssemblies.CatWriteAsync<int>("SQ", new object[] { squelch }, _cancellationToken);
1113  
1114 toolStripComboBox.Text = $"{squelch}";
1115  
27 office 1116 Log.Information($"{Resources.Squelch_set} {squelch}");
25 office 1117 }
1118 catch (Exception exception)
1119 {
1120 Log.Error(exception, Resources.Failed_to_set_squelch);
1121 }
1122 }
1123 });
1124 }, _cancellationToken);
1125 }
27 office 1126  
1127 private async void toolStripComboBox1_SelectedIndexChanged(object sender, EventArgs e)
1128 {
1129 var toolStripComboBox = (ToolStripComboBox)sender;
1130 if (RadioMode.TryParse(toolStripComboBox.Text, out var radioMode))
1131 {
1132 try
1133 {
1134 await _catAssemblies.CatSetAsync<RadioMode, bool>("MD", new object[] { radioMode }, _cancellationToken);
1135 }
1136 catch (Exception exception)
1137 {
1138 Log.Error(exception, Resources.Failed_to_set_radio_mode, radioMode);
1139 }
1140 }
1141 }
1142  
34 office 1143 private async void scrollableToolStripComboBox7_SelectedIndexChanged(object sender, EventArgs e)
27 office 1144 {
1145 var toolStripComboBox = (ScrollableToolStripComboBox)sender;
1146  
34 office 1147 if (int.TryParse(toolStripComboBox.Text, out var amplification))
27 office 1148 {
34 office 1149  
1150 try
27 office 1151 {
34 office 1152 if (await _catAssemblies.CatSetAsync<int, bool>("PC", new object[] { amplification }, _cancellationToken))
1153 {
1154  
1155 toolStripComboBox.Text = $"{amplification}";
1156  
1157 Log.Information($"{Resources.Amplification_set} {amplification}W");
1158 }
1159 }
1160 catch (Exception exception)
1161 {
1162 Log.Error(exception, Resources.Failed_to_set_amplification);
1163 }
1164 }
1165 }
1166  
1167 private async void scrollableToolStripComboBox7_KeyPress(object sender, KeyPressEventArgs e)
1168 {
1169 switch (e.KeyChar)
1170 {
1171 case (char)Keys.Enter:
1172 var toolStripComboBox = (ScrollableToolStripComboBox)sender;
1173  
27 office 1174 if (int.TryParse(toolStripComboBox.Text, out var amplification))
1175 {
1176  
1177 try
1178 {
34 office 1179 if (await _catAssemblies.CatSetAsync<int, bool>("PC", new object[] { amplification }, _cancellationToken))
1180 {
27 office 1181  
34 office 1182 toolStripComboBox.Text = $"{amplification}";
27 office 1183  
34 office 1184 Log.Information($"{Resources.Amplification_set} {amplification}W");
1185  
1186 e.Handled = true;
1187 }
27 office 1188 }
1189 catch (Exception exception)
1190 {
1191 Log.Error(exception, Resources.Failed_to_set_amplification);
1192 }
1193 }
34 office 1194 break;
1195 }
27 office 1196 }
1197  
33 office 1198 private async void toolStripMenuItem30_Click(object sender, EventArgs e)
1199 {
1200 if (!int.TryParse(scrollableToolStripComboBox9.Text, out var start)
1201 || !int.TryParse(scrollableToolStripComboBox10.Text, out var stop))
1202 {
1203 return;
1204 }
29 office 1205  
33 office 1206 if (!int.TryParse(scrollableToolStripComboBox3.Text, out var pause))
1207 {
1208 pause = 5;
1209 }
1210  
1211 if (!int.TryParse(scrollableToolStripComboBox4.Text, out var step))
1212 {
1213 step = 5000;
1214 }
1215  
1216 if (!int.TryParse(scrollableToolStripComboBox6.Text, out var scanDetectPause))
1217 {
1218 scanDetectPause = 10;
1219 }
1220  
1221 if (_bandScan != null)
1222 {
1223 await _bandScan.Stop();
1224 _bandScan = null;
1225 }
1226  
1227 _bandScan = new BandScan(_catAssemblies, start, stop, _serialPort, Configuration);
1228  
1229 if (toolStripMenuItem14.Checked)
1230 {
1231 _bandScan.Start(step, pause, scanDetectPause, toolStripMenuItem16.Checked);
1232  
1233 return;
1234 }
1235  
1236 _bandScan.Start(step, pause, toolStripMenuItem16.Checked);
1237 }
35 office 1238  
1239 private async void toolStripMenuItem32_Click(object sender, EventArgs e)
1240 {
1241 var toolStripMenuItem = toolStripMenuItem31;
1242  
1243 try
1244 {
1245 if (await _catAssemblies.CatSetAsync<TxState, bool>("TX", new object[] { TxState.ON }, _cancellationToken))
1246 {
1247 toolStripMenuItem.Text = Resources.On;
1248 return;
1249 }
1250 }
1251 catch (Exception exception)
1252 {
1253 Log.Error(exception, Resources.Failed_to_set_PTT_state);
1254 }
1255 }
1256  
1257 private async void toolStripMenuItem33_Click(object sender, EventArgs e)
1258 {
1259 var toolStripMenuItem = toolStripMenuItem31;
1260  
1261 try
1262 {
1263 if (await _catAssemblies.CatSetAsync<TxState, bool>("TX", new object[] { TxState.OFF }, _cancellationToken))
1264 {
1265 toolStripMenuItem.Text = Resources.Off;
1266 return;
1267 }
1268 }
1269 catch (Exception exception)
1270 {
1271 Log.Error(exception, Resources.Failed_to_set_PTT_state);
1272 }
1273 }
1274  
1275 private async void toolStripMenuItem26_DropDownOpening(object sender, EventArgs e)
1276 {
1277 var toolStripMenuItem = toolStripMenuItem31;
1278  
1279 try
1280 {
1281 switch (await _catAssemblies.CatReadAsync<TxState>("TX", new object[] { }, _cancellationToken))
1282 {
1283 case TxState.ON:
1284 toolStripMenuItem.Text = Resources.On;
1285 break;
1286 case TxState.OFF:
1287 toolStripMenuItem.Text = Resources.Off;
1288 break;
1289 }
1290 }
1291 catch (Exception exception)
1292 {
1293 Log.Error(exception, Resources.Failed_to_read_PTT_state);
1294 }
1295 }
1 office 1296 }
1297 }