HamBook – Blame information for rev 53

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