HamBook – Blame information for rev 57

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