HamBook – Blame information for rev 31

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  
29 office 482 soundPlayer.Play();
483 }
27 office 484 }
3 office 485 }
486 catch (Exception exception)
487 {
27 office 488 Log.Error(exception, Resources.Failed_to_set_VFO_B_frequency);
3 office 489 }
1 office 490 }
491 }
492  
29 office 493 private async void scrollableToolStripComboBox2_KeyPress(object sender, KeyPressEventArgs e)
1 office 494 {
29 office 495 switch (e.KeyChar)
1 office 496 {
29 office 497 case (char)Keys.Enter:
498 var toolStripComboBox = (ScrollableToolStripComboBox)sender;
27 office 499  
29 office 500 if (int.TryParse(toolStripComboBox.Text, out var frequency))
27 office 501 {
29 office 502 try
503 {
504 using (var soundPlayer = new SoundPlayer(Assembly.GetExecutingAssembly().GetManifestResourceStream("HamBook.Effects.pot.wav")))
505 {
506 if (await _catAssemblies.CatSetAsync<int, bool>("FB", new object[] { frequency }, _cancellationToken))
507 {
508 e.Handled = true;
27 office 509  
29 office 510 soundPlayer.Play();
511 }
512 }
513 }
514 catch (Exception exception)
515 {
516 Log.Error(exception, Resources.Failed_to_set_VFO_B_frequency);
517 }
518 }
519 break;
27 office 520 }
3 office 521 }
522  
9 office 523 private async void toolStripMenuItem1_Click(object sender, EventArgs e)
3 office 524 {
525 if (_bandScan == null)
526 {
1 office 527 return;
528 }
529  
9 office 530 await _bandScan.Stop();
3 office 531 _bandScan = null;
1 office 532 }
3 office 533  
9 office 534 private async void scanToolStripMenuItem_Click(object sender, EventArgs e)
3 office 535 {
5 office 536 if (!(sender is ToolStripMenuItem toolStripMenuItem) ||
537 !int.TryParse(toolStripMenuItem.Tag.ToString(), out var meters))
3 office 538 {
5 office 539 return;
3 office 540 }
541  
5 office 542 if (!int.TryParse(scrollableToolStripComboBox3.Text, out var pause))
3 office 543 {
5 office 544 pause = 5;
3 office 545 }
546  
5 office 547 if(!int.TryParse(scrollableToolStripComboBox4.Text, out var step))
3 office 548 {
5 office 549 step = 5000;
3 office 550 }
551  
21 office 552 if(!int.TryParse(scrollableToolStripComboBox6.Text, out var scanDetectPause))
553 {
554 scanDetectPause = 10;
555 }
556  
5 office 557 if (!Configuration.Definitions.TryGetBand(meters, out var band))
3 office 558 {
5 office 559 return;
3 office 560 }
561  
562 if (_bandScan != null)
563 {
9 office 564 await _bandScan.Stop();
565 _bandScan = null;
3 office 566 }
567  
21 office 568 _bandScan = new BandScan(_catAssemblies, (int)band.Min, (int)band.Max, _serialPort, Configuration);
9 office 569  
23 office 570 //toolStripMenuItem16
21 office 571 if (toolStripMenuItem14.Checked)
572 {
23 office 573 _bandScan.Start(step, pause, scanDetectPause, toolStripMenuItem16.Checked);
21 office 574  
575 return;
576 }
577  
23 office 578 _bandScan.Start(step, pause, toolStripMenuItem16.Checked);
3 office 579 }
580  
11 office 581 private void modeToolStripMenuItem_DropDownOpened(object sender, EventArgs e)
3 office 582 {
11 office 583 Task.Delay(TimeSpan.FromSeconds(1), _cancellationToken).ContinueWith(async task =>
7 office 584 {
11 office 585 try
586 {
587 var mode = await _catAssemblies.CatReadAsync<RadioMode>("MD", new object[] { }, _cancellationToken);
7 office 588  
11 office 589 contextMenuStrip1.InvokeIfRequired(contextMenuStrip =>
590 {
591 toolStripComboBox1.Text = mode;
592 });
593 }
594 catch (Exception exception)
595 {
596 Log.Error(exception, Resources.Failed_to_read_radio_mode);
597 }
7 office 598  
11 office 599 try
600 {
601 var fa = await _catAssemblies.CatReadAsync<int>("FA", new object[] { }, _cancellationToken);
7 office 602  
11 office 603 contextMenuStrip1.InvokeIfRequired(contextMenuStrip =>
604 {
29 office 605 scrollableToolStripComboBox1.Text = $"{fa}";
11 office 606  
607 });
608  
609 }
610 catch (Exception exception)
611 {
612 Log.Error(exception, Resources.Failed_to_read_VFO_A);
613 }
614  
615 try
616 {
617 var fb = await _catAssemblies.CatReadAsync<int>("FB", new object[] { }, _cancellationToken);
618  
619 contextMenuStrip1.InvokeIfRequired(contextMenuStrip =>
620 {
29 office 621 scrollableToolStripComboBox2.Text = $"{fb}";
11 office 622  
623 });
624 }
625 catch (Exception exception)
626 {
627 Log.Error(exception, Resources.Failed_to_read_VFO_B);
628 }
19 office 629  
630 try
631 {
632 var mc = await _catAssemblies.CatReadAsync<int>("MC", new object[] { }, _cancellationToken);
633  
634 contextMenuStrip1.InvokeIfRequired(contextMenuStrip =>
635 {
26 office 636 scrollableToolStripComboBox5.Text = $"{mc:000}";
19 office 637  
26 office 638 if(_memoryChannelStore.TryGetValue(mc, out var memoryChannel))
639 {
640 toolStripMenuItem27.Text = memoryChannel.Text;
641 }
642  
19 office 643 });
644 }
645 catch (Exception exception)
646 {
647 Log.Error(exception, Resources.Failed_to_read_memory_channel);
648 }
24 office 649  
650 try
651 {
652 var pc = await _catAssemblies.CatReadAsync<int>("PC", new object[] { }, _cancellationToken);
653  
654 contextMenuStrip1.InvokeIfRequired(contextMenuStrip =>
655 {
656 scrollableToolStripComboBox7.Text = $"{pc}";
657  
658 });
659 }
660 catch (Exception exception)
661 {
25 office 662 Log.Error(exception, Resources.Failed_to_read_power_state);
24 office 663 }
11 office 664 }, _cancellationToken);
3 office 665 }
10 office 666  
667 private void spectrogramToolStripMenuItem_Click(object sender, EventArgs e)
668 {
669 if (_spectrogramForm != null)
670 {
671 return;
672 }
673  
674 _spectrogramForm = new SpectrogramForm(Configuration, _cancellationToken);
675 _spectrogramForm.Closing += SpectrogramForm_Closing;
676 _spectrogramForm.Show();
677 }
678  
679 private void SpectrogramForm_Closing(object sender, CancelEventArgs e)
680 {
681 if (_spectrogramForm == null)
682 {
683 return;
684 }
685  
686 _spectrogramForm.Dispose();
687 _spectrogramForm = null;
14 office 688  
689 // Commit the configuration.
690 _changedConfigurationContinuation.Schedule(TimeSpan.FromSeconds(1),
691 async () =>
692 {
693 await SaveConfiguration();
694 }, _cancellationToken);
10 office 695 }
15 office 696  
697 private void toolStripMenuItem3_Click(object sender, EventArgs e)
698 {
699 if (_memoryOrganizerForm != null)
700 {
701 return;
702 }
703  
704 _memoryOrganizerForm = new MemoryOrganizerForm(Configuration, _catAssemblies, _cancellationToken);
705 _memoryOrganizerForm.Closing += MemoryOrganizerForm_Closing;
706 _memoryOrganizerForm.Show();
707 }
708  
709 private void MemoryOrganizerForm_Closing(object sender, CancelEventArgs e)
710 {
711 if (_memoryOrganizerForm == null)
712 {
713 return;
714 }
715  
716 _memoryOrganizerForm.Dispose();
717 _memoryOrganizerForm = null;
718  
719 }
720  
721 private async void toolStripMenuItem4_Click(object sender, EventArgs e)
722 {
723 if (_tickerTaskRunning)
724 {
725 return;
726 }
727  
728 var toolStripTextBox = (ToolStripTextBox)toolStripTextBox6;
729  
730 try
731 {
732 var result = await _catAssemblies.CatReadAsync<MemoryChannel>("MT", new object[] { "001" }, _cancellationToken);
733  
734 _tickerTextMemoryChannel = await _catAssemblies.CatReadAsync<MemoryChannel>("MT", new object[] { $"{result.CurrentLocation}" }, _cancellationToken);
735  
736 _storedMemoryChannelTagText = _tickerTextMemoryChannel.Text;
737 _storedMemoryChannelLocation = _tickerTextMemoryChannel.CurrentLocation;
738 }
739 catch(Exception exception)
740 {
741 Log.Error(exception, Resources.Could_not_read_memory_bank);
742 }
743  
744 var tickerText = $"{toolStripTextBox.Text,-12}";
745  
746 _tagTickerCancellationTokenSource = new CancellationTokenSource();
747 _tagTickerCancellationToken = _tagTickerCancellationTokenSource.Token;
748  
749 var characterQueue = new Queue<char>(12);
750 foreach(var i in Enumerable.Range(0, 12))
751 {
752 var x = tickerText.ElementAtOrDefault(i);
753  
754 if(x == default)
755 {
756 characterQueue.Enqueue(' ');
757 continue;
758 }
759  
760 characterQueue.Enqueue(x);
761 }
762  
763 #pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
764 _tickerTask = Task.Run(() => CycleText(characterQueue), _cancellationToken);
765 #pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
766 }
767  
768 private async void toolStripMenuItem5_Click(object sender, EventArgs e)
769 {
770 if(!_tickerTaskRunning)
771 {
772 return;
773 }
774  
775 _tagTickerCancellationTokenSource.Cancel();
776 if (_tickerTask != null)
777 {
778 await _tickerTask;
779 _tickerTask = null;
780 }
781  
782 try
783 {
784 _tickerTextMemoryChannel.CurrentLocation = $"{_storedMemoryChannelLocation:000}";
17 office 785 _tickerTextMemoryChannel.Text = $"{_storedMemoryChannelTagText, -12}";
15 office 786  
787 var success = await _catAssemblies.CatSetAsync<MemoryChannel, bool>("MT", new object[] { _tickerTextMemoryChannel }, _cancellationToken);
788 if (!success)
789 {
790 Log.Error(Resources.Error_while_restoring_memory_text);
791  
792 return;
793 }
794 }
795 catch(Exception exception)
796 {
797 Log.Error(exception, Resources.Error_while_restoring_memory_text);
798 }
17 office 799 finally
800 {
801 _tickerTaskRunning = false;
802 }
15 office 803 }
804  
805 private async Task CycleText(Queue<char> characterQueue)
806 {
807 _tickerTaskRunning = true;
808 try
809 {
810 do
811 {
812 var text = string.Join("", characterQueue.OfType<char>());
813  
814 _tickerTextMemoryChannel.Text = text;
815  
816 await _catAssemblies.CatWriteAsync<MemoryChannel>("MT", new object[] { _tickerTextMemoryChannel }, _cancellationToken);
817  
818 var x = characterQueue.Dequeue();
819 characterQueue.Enqueue(x);
820  
821 await Task.Delay(250);
822  
823 } while (!_tagTickerCancellationToken.IsCancellationRequested);
824 }
825 catch(Exception exception)
826 {
827 Log.Error(exception, Resources.Error_while_cycling_text);
828 }
829 }
19 office 830  
27 office 831 private async void scrollableToolStripComboBox5_SelectedIndexChanged(object sender, EventArgs e)
19 office 832 {
27 office 833 using (var soundPlayer = new SoundPlayer(Assembly.GetExecutingAssembly().GetManifestResourceStream("HamBook.Effects.pot.wav")))
19 office 834 {
27 office 835 var toolStripComboBox = (ToolStripComboBox)sender;
836 if (int.TryParse(toolStripComboBox.Text, out var channel))
19 office 837 {
27 office 838 if (_memoryChannelStore.TryGetValue(channel, out var memoryChannel))
839 {
840 try
841 {
842 await _catAssemblies.CatWriteAsync<int>("MC", new object[] { channel }, _cancellationToken);
19 office 843  
27 office 844 scrollableToolStripComboBox1.Text = $"{memoryChannel.Frequency}";
24 office 845  
27 office 846 toolStripMenuItem27.Text = memoryChannel.Text;
24 office 847  
27 office 848 soundPlayer.Play();
849 }
850 catch (Exception exception)
851 {
852 Log.Error(exception, Resources.Failed_to_set_memory_channel);
853 }
26 office 854 }
19 office 855 }
856 }
857 }
20 office 858  
859 private async void powerToolStripMenuItem_DropDownOpened(object sender, EventArgs e)
860 {
861 var toolStripMenuItem = toolStripMenuItem11;
862  
863 try
864 {
865 switch (await _catAssemblies.CatReadAsync<PowerState>("PS", new object[] { }, _cancellationToken))
866 {
867 case PowerState.ON:
868 toolStripMenuItem.Text = Resources.On;
869 break;
870 case PowerState.OFF:
871 toolStripMenuItem.Text = Resources.Off;
872 break;
873 }
874 }
875 catch (Exception exception)
876 {
877 Log.Error(exception, Resources.Failed_to_read_power_state);
878 }
879 }
23 office 880  
881 private async void toolStripMenuItem17_Click(object sender, EventArgs e)
882 {
883 try
884 {
885 await _catAssemblies.CatWriteAsync<TunerState>("AC", new object[] { TunerState.TUNER_ON }, _cancellationToken);
886  
887 await _catAssemblies.CatWriteAsync<TunerState>("AC", new object[] { TunerState.TUNING_START }, _cancellationToken);
888 }
889 catch (Exception exception)
890 {
891 Log.Error(exception, Resources.Failed_tuning_current_frequency);
892 }
893 }
24 office 894  
895 private async void toolStripMenuItem22_CheckStateChanged(object sender, EventArgs e)
896 {
897 var toolStripMenuItem = (ToolStripMenuItem)sender;
898 try
899 {
900 if (toolStripMenuItem.Checked)
901 {
902 await _catAssemblies.CatWriteAsync<IpoState>("PA", new object[] { IpoState.IPO }, _cancellationToken);
903 return;
904 }
905  
906 await _catAssemblies.CatWriteAsync<IpoState>("PA", new object[] { IpoState.AMP }, _cancellationToken);
907 }
908 catch (Exception exception)
909 {
910 Log.Error(exception, Resources.Failed_setting_IPO);
911 }
912 }
913  
914 private async void toolStripMenuItem23_CheckStateChanged(object sender, EventArgs e)
915 {
916 var toolStripMenuItem = (ToolStripMenuItem)sender;
917  
918 try
919 {
920 if (toolStripMenuItem.Checked)
921 {
922 await _catAssemblies.CatWriteAsync<TunerState>("AC", new object[] { TunerState.TUNER_ON }, _cancellationToken);
923  
924 return;
925 }
926  
927 await _catAssemblies.CatWriteAsync<TunerState>("AC", new object[] { TunerState.TUNER_OFF }, _cancellationToken);
928 }
929 catch (Exception exception)
930 {
931 Log.Error(exception, Resources.Failed_setting_the_tuner_state);
932 }
933 }
934  
935 private async void toolStripMenuItem24_Click(object sender, EventArgs e)
936 {
937 try
938 {
939 await _catAssemblies.CatWriteAsync<TunerState>("AC", new object[] { TunerState.TUNING_START }, _cancellationToken);
940 }
941 catch(Exception exception)
942 {
943  
944 }
945 }
25 office 946  
947 private void toolStripMenuItem21_DropDownOpened(object sender, EventArgs e)
948 {
949 Task.Delay(TimeSpan.FromSeconds(1), _cancellationToken).ContinueWith(async task =>
950 {
951 try
952 {
953 var ac = await _catAssemblies.CatReadAsync<TunerState>("AC", new object[] { }, _cancellationToken);
954  
955 contextMenuStrip1.InvokeIfRequired(contextMenuStrip =>
956 {
957 switch (ac)
958 {
959 case TunerState.TUNING_START:
960 case TunerState.TUNER_ON:
961 toolStripMenuItem23.Checked = true;
962 break;
963 case TunerState.TUNER_OFF:
964 toolStripMenuItem23.Checked = false;
965 break;
966 }
967  
968  
969 });
970 }
971 catch (Exception exception)
972 {
973 Log.Error(exception, Resources.Failed_to_read_the_tuner_state);
974 }
975  
976 try
977 {
978 var pa = await _catAssemblies.CatReadAsync<IpoState>("PA", new object[] { }, _cancellationToken);
979  
980 contextMenuStrip1.InvokeIfRequired(contextMenuStrip =>
981 {
982 switch (pa)
983 {
984 case IpoState.AMP:
985 toolStripMenuItem22.Checked = false;
986 break;
987 case IpoState.IPO:
988 toolStripMenuItem22.Checked = true;
989 break;
990 }
991  
992  
993 });
994 }
995 catch (Exception exception)
996 {
997 Log.Error(exception, Resources.Failed_to_read_IPO);
998 }
999 }, _cancellationToken);
1000 }
1001  
1002 private async void toolStripMenuItem19_DropDownOpened(object sender, EventArgs e)
1003 {
1004 try
1005 {
1006 var sq = await _catAssemblies.CatReadAsync<int>("SQ", new object[] { }, _cancellationToken);
1007  
1008 contextMenuStrip1.InvokeIfRequired(contextMenuStrip =>
1009 {
1010 scrollableToolStripComboBox8.Text = $"{sq}";
1011  
1012 });
1013 }
1014 catch (Exception exception)
1015 {
1016 Log.Error(exception, Resources.Failed_to_read_squelch);
1017 }
1018 }
1019  
29 office 1020 private async void scrollableToolStripComboBox1_MouseWheel(object sender, MouseEventArgs e)
25 office 1021 {
1022 var toolStripComboBox = (ScrollableToolStripComboBox)sender;
27 office 1023 if (int.TryParse(toolStripComboBox.Text, out var frequency))
1024 {
1025 switch (Math.Sign(e.Delta))
1026 {
1027 case -1:
1028 frequency = frequency - Configuration.Navigation.FrequencyStep;
1029 break;
1030 case 1:
1031 frequency = frequency + Configuration.Navigation.FrequencyStep;
1032 break;
1033 }
25 office 1034  
27 office 1035 try
1036 {
29 office 1037 using (var soundPlayer = new SoundPlayer(Assembly.GetExecutingAssembly().GetManifestResourceStream("HamBook.Effects.pot.wav")))
25 office 1038 {
29 office 1039 if (await _catAssemblies.CatSetAsync<int, bool>("FA", new object[] { frequency }, _cancellationToken))
1040 {
1041 toolStripComboBox.Text = $"{frequency}";
25 office 1042  
29 office 1043 soundPlayer.Play();
1044 }
25 office 1045 }
27 office 1046 }
1047 catch (Exception exception)
1048 {
1049 Log.Error(exception, Resources.Failed_to_set_VFO_A_frequency);
1050 }
1051 }
1052 }
25 office 1053  
29 office 1054 private async void scrollableToolStripComboBox1_KeyPress(object sender, KeyPressEventArgs e)
27 office 1055 {
29 office 1056 switch (e.KeyChar)
27 office 1057 {
29 office 1058 case (char)Keys.Enter:
1059 e.Handled = true;
25 office 1060  
29 office 1061 var toolStripComboBox = (ScrollableToolStripComboBox)sender;
27 office 1062  
29 office 1063 if (int.TryParse(toolStripComboBox.Text, out var frequency))
27 office 1064 {
29 office 1065 try
1066 {
1067 using (var soundPlayer = new SoundPlayer(Assembly.GetExecutingAssembly().GetManifestResourceStream("HamBook.Effects.pot.wav")))
1068 {
1069 if (await _catAssemblies.CatSetAsync<int, bool>("FA", new object[] { frequency }, _cancellationToken))
1070 {
1071  
1072 soundPlayer.Play();
1073 }
1074 }
1075 }
1076 catch (Exception exception)
1077 {
1078 Log.Error(exception, Resources.Failed_to_set_VFO_A_frequency);
1079 }
1080 }
1081 break;
27 office 1082 }
25 office 1083 }
1084  
27 office 1085 private void scrollableToolStripComboBox8_SelectedIndexChanged(object sender, EventArgs e)
25 office 1086 {
1087 var toolStripComboBox = (ScrollableToolStripComboBox)sender;
1088  
1089 _squelchScheduledContinuation.Schedule(TimeSpan.FromSeconds(1), () =>
1090 {
1091 contextMenuStrip1.InvokeIfRequired(async contextMenuStrip1 =>
1092 {
1093 if (int.TryParse(toolStripComboBox.Text, out var squelch))
1094 {
1095  
1096 try
1097 {
1098 await _catAssemblies.CatWriteAsync<int>("SQ", new object[] { squelch }, _cancellationToken);
1099  
1100 toolStripComboBox.Text = $"{squelch}";
1101  
27 office 1102 Log.Information($"{Resources.Squelch_set} {squelch}");
25 office 1103 }
1104 catch (Exception exception)
1105 {
1106 Log.Error(exception, Resources.Failed_to_set_squelch);
1107 }
1108 }
1109 });
1110 }, _cancellationToken);
1111 }
27 office 1112  
1113 private async void toolStripComboBox1_SelectedIndexChanged(object sender, EventArgs e)
1114 {
1115 var toolStripComboBox = (ToolStripComboBox)sender;
1116 if (RadioMode.TryParse(toolStripComboBox.Text, out var radioMode))
1117 {
1118 try
1119 {
1120 await _catAssemblies.CatSetAsync<RadioMode, bool>("MD", new object[] { radioMode }, _cancellationToken);
1121 }
1122 catch (Exception exception)
1123 {
1124 Log.Error(exception, Resources.Failed_to_set_radio_mode, radioMode);
1125 }
1126 }
1127 }
1128  
1129 private void scrollableToolStripComboBox7_SelectedIndexChanged(object sender, EventArgs e)
1130 {
1131 var toolStripComboBox = (ScrollableToolStripComboBox)sender;
1132  
1133 _powerScheduledContinuation.Schedule(TimeSpan.FromSeconds(1), () =>
1134 {
1135 contextMenuStrip1.InvokeIfRequired(async contextMenuStrip1 =>
1136 {
1137 if (int.TryParse(toolStripComboBox.Text, out var amplification))
1138 {
1139  
1140 try
1141 {
1142 await _catAssemblies.CatWriteAsync<int>("PC", new object[] { amplification }, _cancellationToken);
1143  
1144 toolStripComboBox.Text = $"{amplification}";
1145  
1146 Log.Information($"{Resources.Amplification_set} {amplification}W");
1147 }
1148 catch (Exception exception)
1149 {
1150 Log.Error(exception, Resources.Failed_to_set_amplification);
1151 }
1152 }
1153 });
1154 }, _cancellationToken);
1155 }
1156  
29 office 1157  
1 office 1158 }
1159 }