HamBook – Blame information for rev 54
?pathlinks?
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; |
||
1 | office | 9 | using System.Reflection; |
54 | office | 10 | using System.Text; |
1 | office | 11 | using System.Threading; |
12 | using System.Threading.Tasks; |
||
13 | using System.Windows.Forms; |
||
14 | using HamBook.Properties; |
||
54 | office | 15 | using HamBook.Radios; |
1 | office | 16 | using HamBook.Radios.Generic; |
54 | office | 17 | using HamBook.Utilities; |
7 | office | 18 | using HamBook.Utilities.Controls; |
54 | office | 19 | using HamBook.Utilities.Serialization; |
20 | using NetSparkleUpdater; |
||
21 | using NetSparkleUpdater.Enums; |
||
22 | using NetSparkleUpdater.SignatureVerifiers; |
||
23 | using NetSparkleUpdater.UI.WinForms; |
||
9 | office | 24 | using RJCP.IO.Ports; |
54 | office | 25 | using Serilog; |
26 | using PowerState = HamBook.Radios.Generic.PowerState; |
||
1 | office | 27 | |
28 | namespace HamBook |
||
29 | { |
||
30 | public partial class Form1 : Form |
||
31 | { |
||
54 | office | 32 | private readonly CancellationToken _cancellationToken; |
33 | private readonly CancellationTokenSource _cancellationTokenSource; |
||
34 | private readonly ScheduledContinuation _changedConfigurationContinuation; |
||
35 | |||
36 | private readonly ConcurrentDictionary<string, MemoryChannel> _memoryChannelStore = |
||
37 | new ConcurrentDictionary<string, MemoryChannel>(); |
||
38 | |||
39 | private readonly LogMemorySink _memorySink; |
||
40 | private readonly SparkleUpdater _sparkle; |
||
41 | private readonly ScheduledContinuation _squelchScheduledContinuation; |
||
42 | private AboutForm _aboutForm; |
||
43 | private BandScan _bandScan; |
||
44 | private CatAssemblies _catAssemblies; |
||
45 | private MemoryBanks _memoryBanks; |
||
46 | private MemoryOrganizerForm _memoryOrganizerForm; |
||
47 | private MemoryTune _memoryTune; |
||
48 | private MenuForm _menuForm; |
||
25 | office | 49 | private ScheduledContinuation _powerScheduledContinuation; |
9 | office | 50 | private SerialPortStream _serialPort; |
1 | office | 51 | private SettingsForm _settingsForm; |
10 | office | 52 | private SpectrogramForm _spectrogramForm; |
54 | office | 53 | private string _storedMemoryChannelLocation; |
54 | private string _storedMemoryChannelTagText; |
||
55 | private CancellationToken _tagTickerCancellationToken; |
||
1 | office | 56 | |
15 | office | 57 | private CancellationTokenSource _tagTickerCancellationTokenSource; |
58 | private Task _tickerTask; |
||
59 | private volatile bool _tickerTaskRunning; |
||
54 | office | 60 | private MemoryChannel _tickerTextMemoryChannel; |
61 | private ViewLogsForm _viewLogsForm; |
||
15 | office | 62 | |
1 | office | 63 | private Form1() |
64 | { |
||
65 | _cancellationTokenSource = new CancellationTokenSource(); |
||
66 | _cancellationToken = _cancellationTokenSource.Token; |
||
67 | |||
68 | _changedConfigurationContinuation = new ScheduledContinuation(); |
||
3 | office | 69 | |
25 | office | 70 | _squelchScheduledContinuation = new ScheduledContinuation(); |
71 | _powerScheduledContinuation = new ScheduledContinuation(); |
||
1 | office | 72 | } |
73 | |||
74 | public Form1(Mutex mutex) : this() |
||
75 | { |
||
76 | InitializeComponent(); |
||
77 | |||
78 | _memorySink = new LogMemorySink(); |
||
79 | |||
80 | Log.Logger = new LoggerConfiguration() |
||
81 | .MinimumLevel.Debug() |
||
82 | .WriteTo.Conditional(condition => MemorySinkEnabled, configureSink => configureSink.Sink(_memorySink)) |
||
83 | .WriteTo.File(Path.Combine(Constants.UserApplicationDirectory, "Logs", $"{Constants.AssemblyName}.log"), |
||
84 | rollingInterval: RollingInterval.Day) |
||
85 | .CreateLogger(); |
||
86 | |||
87 | // Start application update. |
||
88 | var manifestModuleName = Assembly.GetEntryAssembly().ManifestModule.FullyQualifiedName; |
||
89 | var icon = Icon.ExtractAssociatedIcon(manifestModuleName); |
||
90 | |||
91 | _sparkle = new SparkleUpdater("https://hambook.grimore.org/update/appcast.xml", |
||
92 | new Ed25519Checker(SecurityMode.Strict, "LonrgxVjSF0GnY4hzwlRJnLkaxnDn2ikdmOifILzLJY=")) |
||
93 | { |
||
94 | UIFactory = new UIFactory(icon), |
||
95 | RelaunchAfterUpdate = true |
||
96 | }; |
||
97 | } |
||
98 | |||
54 | office | 99 | private Configuration.Configuration Configuration { get; set; } |
100 | |||
101 | public bool MemorySinkEnabled { get; set; } |
||
102 | |||
1 | office | 103 | private async void Form1_Load(object sender, EventArgs e) |
104 | { |
||
13 | office | 105 | _sparkle.StartLoop(true, true); |
106 | |||
1 | office | 107 | Configuration = await LoadConfiguration(); |
108 | |||
50 | office | 109 | _memoryBanks = MemoryBanks.Create(Configuration.Radio); |
45 | office | 110 | |
7 | office | 111 | _serialPort = InitializeSerialPort(Configuration); |
1 | office | 112 | |
7 | office | 113 | _catAssemblies = InitializeAssemblies(_serialPort); |
1 | office | 114 | |
50 | office | 115 | // Initialize power state. |
1 | office | 116 | try |
117 | { |
||
9 | office | 118 | switch (await _catAssemblies.CatReadAsync<PowerState>("PS", new object[] { }, _cancellationToken)) |
1 | office | 119 | { |
120 | case PowerState.ON: |
||
7 | office | 121 | Log.Information(Resources.Attempting_to_initialize_radio); |
54 | office | 122 | if (!await InitializeRadio()) return; |
7 | office | 123 | |
124 | Log.Information(Resources.Initializing_GUI); |
||
1 | office | 125 | break; |
126 | } |
||
127 | } |
||
54 | office | 128 | catch (Exception exception) |
1 | office | 129 | { |
130 | Log.Error(exception, Resources.Failed_to_read_power_state); |
||
131 | } |
||
26 | office | 132 | |
50 | office | 133 | // Initialize memory banks. |
41 | office | 134 | var memoryBankQueue = new ConcurrentQueue<string>(); |
26 | office | 135 | var memoryBankTaskCompletionSource = new TaskCompletionSource<bool>(); |
136 | |||
137 | async void IdleHandler(object idleHandlerSender, EventArgs idleHandlerArgs) |
||
138 | { |
||
139 | await memoryBankTaskCompletionSource.Task; |
||
140 | |||
141 | try |
||
142 | { |
||
143 | if (!memoryBankQueue.TryDequeue(out var memoryBank)) |
||
144 | { |
||
54 | office | 145 | Application.Idle -= IdleHandler; |
26 | office | 146 | |
147 | return; |
||
148 | } |
||
149 | |||
41 | office | 150 | scrollableToolStripComboBox12.Items.Add(memoryBank); |
26 | office | 151 | |
54 | office | 152 | var memoryChannel = MemoryChannel.Create(Configuration.Radio); |
26 | office | 153 | |
154 | try |
||
155 | { |
||
54 | office | 156 | memoryChannel = await _catAssemblies.CatReadAsync<MemoryChannel>("MT", |
157 | new object[] { memoryBank }, _cancellationToken); |
||
158 | |||
41 | office | 159 | scrollableToolStripComboBox5.Items.Add(memoryBank); |
26 | office | 160 | } |
54 | office | 161 | catch (Exception exception) |
26 | office | 162 | { |
163 | Log.Warning(exception, Resources.Could_not_read_memory_bank); |
||
164 | |||
165 | return; |
||
166 | } |
||
167 | |||
168 | _memoryChannelStore.TryAdd(memoryBank, memoryChannel); |
||
169 | } |
||
170 | catch (Exception exception) |
||
171 | { |
||
172 | Log.Error(exception, Resources.Could_not_update_data_grid_view); |
||
173 | } |
||
174 | } |
||
175 | |||
54 | office | 176 | Application.Idle += IdleHandler; |
26 | office | 177 | try |
178 | { |
||
54 | office | 179 | foreach (var memoryBank in _memoryBanks.GetMemoryBanks()) memoryBankQueue.Enqueue(memoryBank); |
26 | office | 180 | |
181 | memoryBankTaskCompletionSource.TrySetResult(true); |
||
182 | } |
||
183 | catch (Exception exception) |
||
184 | { |
||
54 | office | 185 | Application.Idle -= IdleHandler; |
26 | office | 186 | |
38 | office | 187 | Log.Error(exception, Resources.Unable_to_read_memory_banks); |
26 | office | 188 | } |
50 | office | 189 | |
190 | // Initialize lock state. |
||
191 | try |
||
192 | { |
||
54 | office | 193 | var lockState = |
194 | await _catAssemblies.CatReadAsync<LockState>("LK", new object[] { }, _cancellationToken); |
||
195 | switch (lockState) |
||
50 | office | 196 | { |
197 | case LockState.OFF: |
||
198 | lockToolStripMenuItem.Checked = false; |
||
199 | break; |
||
200 | case LockState.ON: |
||
201 | lockToolStripMenuItem.Checked = true; |
||
202 | break; |
||
203 | } |
||
204 | } |
||
205 | catch (Exception exception) |
||
206 | { |
||
207 | Log.Error(exception, Resources.Failed_to_read_lock_state); |
||
208 | } |
||
1 | office | 209 | } |
210 | |||
9 | office | 211 | private async void quitToolStripMenuItem_Click(object sender, EventArgs e) |
1 | office | 212 | { |
54 | office | 213 | if (_bandScan != null) |
9 | office | 214 | { |
39 | office | 215 | _bandScan.Stop(); |
9 | office | 216 | _bandScan = null; |
217 | } |
||
218 | |||
39 | office | 219 | if (_memoryTune != null) |
220 | { |
||
221 | _memoryTune.Stop(); |
||
222 | _memoryTune = null; |
||
223 | } |
||
224 | |||
14 | office | 225 | // Save configuration on quit. |
226 | await SaveConfiguration(); |
||
227 | |||
1 | office | 228 | Close(); |
229 | } |
||
230 | |||
231 | private void viewLogsToolStripMenuItem_Click(object sender, EventArgs e) |
||
232 | { |
||
54 | office | 233 | if (_viewLogsForm != null) return; |
1 | office | 234 | |
235 | _viewLogsForm = new ViewLogsForm(this, _memorySink, _cancellationToken); |
||
236 | _viewLogsForm.Closing += ViewLogsForm_Closing; |
||
237 | _viewLogsForm.Show(); |
||
238 | } |
||
239 | |||
240 | private void ViewLogsForm_Closing(object sender, CancelEventArgs e) |
||
241 | { |
||
54 | office | 242 | if (_viewLogsForm == null) return; |
1 | office | 243 | |
244 | _viewLogsForm.Closing -= ViewLogsForm_Closing; |
||
245 | _viewLogsForm.Close(); |
||
246 | _viewLogsForm = null; |
||
247 | } |
||
248 | |||
249 | private void aboutToolStripMenuItem_Click(object sender, EventArgs e) |
||
250 | { |
||
54 | office | 251 | if (_aboutForm != null) return; |
1 | office | 252 | |
253 | _aboutForm = new AboutForm(_cancellationToken); |
||
254 | _aboutForm.Closing += AboutForm_Closing; |
||
255 | _aboutForm.Show(); |
||
256 | } |
||
257 | |||
258 | private void AboutForm_Closing(object sender, CancelEventArgs e) |
||
259 | { |
||
54 | office | 260 | if (_aboutForm == null) return; |
1 | office | 261 | |
262 | _aboutForm.Dispose(); |
||
263 | _aboutForm = null; |
||
264 | } |
||
265 | |||
266 | private void settingsToolStripMenuItem_Click(object sender, EventArgs e) |
||
267 | { |
||
54 | office | 268 | if (_settingsForm != null) return; |
1 | office | 269 | |
270 | _settingsForm = new SettingsForm(Configuration, _cancellationToken); |
||
271 | _settingsForm.Closing += SettingsForm_Closing; |
||
272 | _settingsForm.Show(); |
||
273 | } |
||
274 | |||
275 | private void SettingsForm_Closing(object sender, CancelEventArgs e) |
||
276 | { |
||
54 | office | 277 | if (_settingsForm == null) return; |
1 | office | 278 | |
54 | office | 279 | if (_settingsForm.SaveOnClose) |
1 | office | 280 | // Commit the configuration. |
281 | _changedConfigurationContinuation.Schedule(TimeSpan.FromSeconds(1), |
||
54 | office | 282 | async () => |
283 | { |
||
1 | office | 284 | await SaveConfiguration(); |
285 | |||
54 | office | 286 | if (_bandScan != null) |
11 | office | 287 | { |
39 | office | 288 | _bandScan.Stop(); |
11 | office | 289 | _bandScan = null; |
290 | } |
||
291 | |||
39 | office | 292 | if (_memoryTune != null) |
293 | { |
||
294 | _memoryTune.Stop(); |
||
295 | _memoryTune = null; |
||
296 | } |
||
11 | office | 297 | |
54 | office | 298 | _memoryBanks = MemoryBanks.Create(Configuration.Radio); |
45 | office | 299 | |
1 | office | 300 | Miscellaneous.LaunchOnBootSet(Configuration.LaunchOnBoot); |
301 | |||
7 | office | 302 | _serialPort = InitializeSerialPort(Configuration); |
1 | office | 303 | |
7 | office | 304 | _catAssemblies = InitializeAssemblies(_serialPort); |
305 | |||
306 | try |
||
307 | { |
||
54 | office | 308 | switch (await _catAssemblies.CatReadAsync<PowerState>("PS", new object[] { }, |
309 | _cancellationToken)) |
||
7 | office | 310 | { |
311 | case PowerState.ON: |
||
312 | Log.Information(Resources.Attempting_to_initialize_radio); |
||
54 | office | 313 | if (!await InitializeRadio()) return; |
7 | office | 314 | Log.Information(Resources.Initializing_GUI); |
315 | break; |
||
316 | } |
||
317 | } |
||
318 | catch (Exception exception) |
||
319 | { |
||
320 | Log.Error(exception, Resources.Failed_to_read_power_state); |
||
321 | } |
||
1 | office | 322 | }, _cancellationToken); |
323 | |||
324 | _settingsForm.Dispose(); |
||
325 | _settingsForm = null; |
||
326 | } |
||
327 | |||
328 | public async Task SaveConfiguration() |
||
329 | { |
||
330 | if (!Directory.Exists(Constants.UserApplicationDirectory)) |
||
331 | Directory.CreateDirectory(Constants.UserApplicationDirectory); |
||
332 | |||
333 | switch (await Serialization.Serialize(Configuration, Constants.ConfigurationFile, "Configuration", |
||
334 | "<!ATTLIST Configuration xmlns:xsi CDATA #IMPLIED xsi:noNamespaceSchemaLocation CDATA #IMPLIED>", |
||
335 | CancellationToken.None)) |
||
336 | { |
||
3 | office | 337 | case SerializationSuccess<Configuration.Configuration> configuration: |
16 | office | 338 | Log.Information(Resources.Configuration_serialized_successfully); |
1 | office | 339 | break; |
340 | case SerializationFailure serializationFailure: |
||
16 | office | 341 | Log.Warning(serializationFailure.Exception.Message, Resources.Configuration_failed_to_serialize); |
1 | office | 342 | break; |
343 | } |
||
344 | } |
||
345 | |||
346 | public static async Task<Configuration.Configuration> LoadConfiguration() |
||
347 | { |
||
348 | if (!Directory.Exists(Constants.UserApplicationDirectory)) |
||
349 | Directory.CreateDirectory(Constants.UserApplicationDirectory); |
||
350 | |||
351 | var deserializationResult = |
||
352 | await Serialization.Deserialize<Configuration.Configuration>(Constants.ConfigurationFile, |
||
353 | Constants.ConfigurationNamespace, Constants.ConfigurationXsd, CancellationToken.None); |
||
354 | |||
355 | switch (deserializationResult) |
||
356 | { |
||
357 | case SerializationSuccess<Configuration.Configuration> serializationSuccess: |
||
358 | return serializationSuccess.Result; |
||
359 | case SerializationFailure serializationFailure: |
||
16 | office | 360 | Log.Warning(serializationFailure.Exception, Resources.Configuration_failed_to_deserialize); |
1 | office | 361 | return new Configuration.Configuration(); |
362 | default: |
||
363 | return new Configuration.Configuration(); |
||
364 | } |
||
365 | } |
||
366 | |||
9 | office | 367 | private async Task<bool> InitializeRadio() |
1 | office | 368 | { |
369 | try |
||
370 | { |
||
54 | office | 371 | await _catAssemblies.CatWriteAsync<InformationState>("AI", new object[] { InformationState.OFF }, |
372 | _cancellationToken); |
||
9 | office | 373 | |
11 | office | 374 | return await _catAssemblies.CatReadAsync<bool>("ID", new object[] { }, _cancellationToken); |
1 | office | 375 | } |
54 | office | 376 | catch (Exception exception) |
1 | office | 377 | { |
7 | office | 378 | Log.Error(exception, Resources.Unable_to_initialize_radio); |
11 | office | 379 | |
7 | office | 380 | return false; |
1 | office | 381 | } |
7 | office | 382 | } |
383 | |||
9 | office | 384 | private CatAssemblies InitializeAssemblies(SerialPortStream serialPort) |
7 | office | 385 | { |
54 | office | 386 | if (_catAssemblies != null) |
1 | office | 387 | { |
7 | office | 388 | _catAssemblies.Dispose(); |
389 | _catAssemblies = null; |
||
1 | office | 390 | } |
391 | |||
7 | office | 392 | return new CatAssemblies(serialPort, Configuration.Radio); |
393 | } |
||
394 | |||
9 | office | 395 | private SerialPortStream InitializeSerialPort(Configuration.Configuration configuration) |
7 | office | 396 | { |
397 | if (_serialPort != null) |
||
1 | office | 398 | { |
54 | office | 399 | if (_serialPort.IsOpen) _serialPort.Close(); |
26 | office | 400 | |
7 | office | 401 | _serialPort.Dispose(); |
402 | _serialPort = null; |
||
1 | office | 403 | } |
404 | |||
7 | office | 405 | // Set up serial connection. |
54 | office | 406 | var serialPort = new SerialPortStream(configuration.Port, configuration.Speed, configuration.DataBits, |
407 | configuration.Parity, configuration.StopBits); |
||
7 | office | 408 | serialPort.ReadTimeout = configuration.SerialPortTimeout.Read; |
409 | serialPort.WriteTimeout = configuration.SerialPortTimeout.Write; |
||
410 | serialPort.Handshake = configuration.Handshake; |
||
9 | office | 411 | serialPort.Encoding = Encoding.ASCII; |
1 | office | 412 | |
54 | office | 413 | Log.Information( |
414 | $"{Resources.Initialized_serial_port} {configuration.Port} {configuration.Speed} {configuration.Parity} {configuration.DataBits} {configuration.StopBits}"); |
||
7 | office | 415 | |
416 | return serialPort; |
||
1 | office | 417 | } |
418 | |||
419 | private async void updateToolStripMenuItem_Click(object sender, EventArgs e) |
||
420 | { |
||
421 | // Manually check for updates, this will not show a ui |
||
422 | var result = await _sparkle.CheckForUpdatesQuietly(); |
||
54 | office | 423 | if (result.Status == UpdateStatus.UpdateAvailable) |
1 | office | 424 | { |
425 | // if update(s) are found, then we have to trigger the UI to show it gracefully |
||
426 | _sparkle.ShowUpdateNeededUI(); |
||
427 | return; |
||
428 | } |
||
429 | |||
430 | MessageBox.Show(Resources.No_updates_available_at_this_time, Resources.HamBook, MessageBoxButtons.OK, |
||
431 | MessageBoxIcon.Asterisk, |
||
432 | MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly, false); |
||
433 | } |
||
434 | |||
435 | private async void onToolStripMenuItem_Click(object sender, EventArgs e) |
||
436 | { |
||
437 | try |
||
438 | { |
||
54 | office | 439 | await _catAssemblies.CatSetAsync<PowerState, bool>("PS", new object[] { PowerState.ON }, |
440 | _cancellationToken); |
||
441 | } |
||
442 | catch (Exception exception) |
||
1 | office | 443 | { |
444 | Log.Error(exception, Resources.Failed_to_set_power_state); |
||
445 | } |
||
446 | } |
||
447 | |||
448 | private async void offToolStripMenuItem_Click(object sender, EventArgs e) |
||
449 | { |
||
450 | try |
||
451 | { |
||
54 | office | 452 | await _catAssemblies.CatSetAsync<PowerState, bool>("PS", new object[] { PowerState.OFF }, |
453 | _cancellationToken); |
||
454 | } |
||
455 | catch (Exception exception) |
||
1 | office | 456 | { |
457 | Log.Error(exception, Resources.Failed_to_set_power_state); |
||
458 | } |
||
459 | } |
||
460 | |||
29 | office | 461 | private async void scrollableToolStripComboBox2_MouseWheel(object sender, MouseEventArgs e) |
1 | office | 462 | { |
27 | office | 463 | var toolStripComboBox = (ScrollableToolStripComboBox)sender; |
464 | if (int.TryParse(toolStripComboBox.Text, out var frequency)) |
||
1 | office | 465 | { |
27 | office | 466 | switch (Math.Sign(e.Delta)) |
3 | office | 467 | { |
27 | office | 468 | case -1: |
469 | frequency = frequency - Configuration.Navigation.FrequencyStep; |
||
470 | break; |
||
471 | case 1: |
||
472 | frequency = frequency + Configuration.Navigation.FrequencyStep; |
||
473 | break; |
||
474 | } |
||
5 | office | 475 | |
27 | office | 476 | try |
3 | office | 477 | { |
54 | office | 478 | using (var soundPlayer = new SoundPlayer(Assembly.GetExecutingAssembly() |
479 | .GetManifestResourceStream("HamBook.Effects.pot.wav"))) |
||
27 | office | 480 | { |
54 | office | 481 | if (await _catAssemblies.CatSetAsync<int, bool>("FB", new object[] { frequency }, |
482 | _cancellationToken)) |
||
29 | office | 483 | { |
484 | toolStripComboBox.Text = $"{frequency}"; |
||
27 | office | 485 | |
54 | office | 486 | if (Configuration.Navigation.MouseScrollSound) soundPlayer.Play(); |
29 | office | 487 | } |
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); |
||
15 | office | 810 | if (!success) |
811 | { |
||
812 | Log.Error(Resources.Error_while_restoring_memory_text); |
||
813 | } |
||
814 | } |
||
54 | office | 815 | catch (Exception exception) |
15 | office | 816 | { |
817 | Log.Error(exception, Resources.Error_while_restoring_memory_text); |
||
818 | } |
||
17 | office | 819 | finally |
820 | { |
||
821 | _tickerTaskRunning = false; |
||
822 | } |
||
15 | office | 823 | } |
824 | |||
825 | private async Task CycleText(Queue<char> characterQueue) |
||
826 | { |
||
827 | _tickerTaskRunning = true; |
||
828 | try |
||
829 | { |
||
830 | do |
||
831 | { |
||
832 | var text = string.Join("", characterQueue.OfType<char>()); |
||
833 | |||
834 | _tickerTextMemoryChannel.Text = text; |
||
835 | |||
54 | office | 836 | await _catAssemblies.CatWriteAsync<MemoryChannel>("MT", new object[] { _tickerTextMemoryChannel }, |
837 | _cancellationToken); |
||
15 | office | 838 | |
839 | var x = characterQueue.Dequeue(); |
||
840 | characterQueue.Enqueue(x); |
||
841 | |||
842 | await Task.Delay(250); |
||
843 | } while (!_tagTickerCancellationToken.IsCancellationRequested); |
||
54 | office | 844 | } |
845 | catch (Exception exception) |
||
15 | office | 846 | { |
847 | Log.Error(exception, Resources.Error_while_cycling_text); |
||
848 | } |
||
849 | } |
||
19 | office | 850 | |
27 | office | 851 | private async void scrollableToolStripComboBox5_SelectedIndexChanged(object sender, EventArgs e) |
19 | office | 852 | { |
38 | office | 853 | var toolStripComboBox = (ScrollableToolStripComboBox)sender; |
854 | |||
41 | office | 855 | var channel = toolStripComboBox.Text; |
856 | |||
857 | if (!string.IsNullOrEmpty(channel)) |
||
38 | office | 858 | if (_memoryChannelStore.TryGetValue(channel, out var memoryChannel)) |
859 | try |
||
27 | office | 860 | { |
54 | office | 861 | using (var soundPlayer = new SoundPlayer(Assembly.GetExecutingAssembly() |
862 | .GetManifestResourceStream("HamBook.Effects.pot.wav"))) |
||
27 | office | 863 | { |
54 | office | 864 | await _catAssemblies.CatWriteAsync<string>("MC", new object[] { channel }, |
865 | _cancellationToken); |
||
19 | office | 866 | |
27 | office | 867 | scrollableToolStripComboBox1.Text = $"{memoryChannel.Frequency}"; |
24 | office | 868 | |
27 | office | 869 | toolStripMenuItem27.Text = memoryChannel.Text; |
24 | office | 870 | |
27 | office | 871 | soundPlayer.Play(); |
872 | } |
||
26 | office | 873 | } |
38 | office | 874 | catch (Exception exception) |
875 | { |
||
876 | Log.Error(exception, Resources.Failed_to_set_memory_channel); |
||
877 | } |
||
19 | office | 878 | } |
20 | office | 879 | |
35 | office | 880 | private async void powerToolStripMenuItem_DropDownOpening(object sender, EventArgs e) |
20 | office | 881 | { |
882 | var toolStripMenuItem = toolStripMenuItem11; |
||
883 | |||
884 | try |
||
885 | { |
||
886 | switch (await _catAssemblies.CatReadAsync<PowerState>("PS", new object[] { }, _cancellationToken)) |
||
887 | { |
||
888 | case PowerState.ON: |
||
889 | toolStripMenuItem.Text = Resources.On; |
||
890 | break; |
||
891 | case PowerState.OFF: |
||
892 | toolStripMenuItem.Text = Resources.Off; |
||
893 | break; |
||
894 | } |
||
895 | } |
||
896 | catch (Exception exception) |
||
897 | { |
||
898 | Log.Error(exception, Resources.Failed_to_read_power_state); |
||
899 | } |
||
900 | } |
||
23 | office | 901 | |
902 | private async void toolStripMenuItem17_Click(object sender, EventArgs e) |
||
903 | { |
||
904 | try |
||
905 | { |
||
54 | office | 906 | await _catAssemblies.CatWriteAsync<TunerState>("AC", new object[] { TunerState.TUNER_ON }, |
907 | _cancellationToken); |
||
23 | office | 908 | |
54 | office | 909 | await _catAssemblies.CatWriteAsync<TunerState>("AC", new object[] { TunerState.TUNING_START }, |
910 | _cancellationToken); |
||
23 | office | 911 | } |
912 | catch (Exception exception) |
||
913 | { |
||
914 | Log.Error(exception, Resources.Failed_tuning_current_frequency); |
||
915 | } |
||
916 | } |
||
24 | office | 917 | |
918 | private async void toolStripMenuItem22_CheckStateChanged(object sender, EventArgs e) |
||
919 | { |
||
920 | var toolStripMenuItem = (ToolStripMenuItem)sender; |
||
921 | try |
||
922 | { |
||
923 | if (toolStripMenuItem.Checked) |
||
924 | { |
||
54 | office | 925 | await _catAssemblies.CatWriteAsync<IpoState>("PA", new object[] { IpoState.IPO }, |
926 | _cancellationToken); |
||
24 | office | 927 | return; |
928 | } |
||
929 | |||
930 | await _catAssemblies.CatWriteAsync<IpoState>("PA", new object[] { IpoState.AMP }, _cancellationToken); |
||
931 | } |
||
932 | catch (Exception exception) |
||
933 | { |
||
934 | Log.Error(exception, Resources.Failed_setting_IPO); |
||
935 | } |
||
936 | } |
||
937 | |||
938 | private async void toolStripMenuItem23_CheckStateChanged(object sender, EventArgs e) |
||
939 | { |
||
940 | var toolStripMenuItem = (ToolStripMenuItem)sender; |
||
941 | |||
942 | try |
||
943 | { |
||
944 | if (toolStripMenuItem.Checked) |
||
945 | { |
||
54 | office | 946 | await _catAssemblies.CatWriteAsync<TunerState>("AC", new object[] { TunerState.TUNER_ON }, |
947 | _cancellationToken); |
||
24 | office | 948 | |
949 | return; |
||
950 | } |
||
951 | |||
54 | office | 952 | await _catAssemblies.CatWriteAsync<TunerState>("AC", new object[] { TunerState.TUNER_OFF }, |
953 | _cancellationToken); |
||
24 | office | 954 | } |
955 | catch (Exception exception) |
||
956 | { |
||
957 | Log.Error(exception, Resources.Failed_setting_the_tuner_state); |
||
958 | } |
||
959 | } |
||
960 | |||
961 | private async void toolStripMenuItem24_Click(object sender, EventArgs e) |
||
962 | { |
||
963 | try |
||
964 | { |
||
54 | office | 965 | await _catAssemblies.CatWriteAsync<TunerState>("AC", new object[] { TunerState.TUNER_ON }, |
966 | _cancellationToken); |
||
39 | office | 967 | |
968 | do |
||
969 | { |
||
970 | await Task.Delay(TimeSpan.FromSeconds(1), _cancellationToken); |
||
971 | |||
972 | try |
||
973 | { |
||
54 | office | 974 | var tuneState = |
975 | await _catAssemblies.CatReadAsync<TunerState>("AC", new object[] { }, _cancellationToken); |
||
39 | office | 976 | |
54 | office | 977 | if (tuneState == TunerState.TUNER_ON) break; |
39 | office | 978 | } |
979 | catch (Exception) |
||
980 | { |
||
981 | // retry |
||
982 | } |
||
983 | } while (!_cancellationToken.IsCancellationRequested); |
||
984 | |||
54 | office | 985 | await _catAssemblies.CatWriteAsync<TunerState>("AC", new object[] { TunerState.TUNING_START }, |
986 | _cancellationToken); |
||
24 | office | 987 | } |
54 | office | 988 | catch (Exception exception) |
24 | office | 989 | { |
39 | office | 990 | Log.Error(exception, Resources.Could_not_start_tuning); |
24 | office | 991 | } |
992 | } |
||
25 | office | 993 | |
35 | office | 994 | private void toolStripMenuItem21_DropDownOpening(object sender, EventArgs e) |
25 | office | 995 | { |
996 | Task.Delay(TimeSpan.FromSeconds(1), _cancellationToken).ContinueWith(async task => |
||
997 | { |
||
998 | try |
||
999 | { |
||
1000 | var ac = await _catAssemblies.CatReadAsync<TunerState>("AC", new object[] { }, _cancellationToken); |
||
1001 | |||
1002 | contextMenuStrip1.InvokeIfRequired(contextMenuStrip => |
||
1003 | { |
||
1004 | switch (ac) |
||
1005 | { |
||
1006 | case TunerState.TUNING_START: |
||
1007 | case TunerState.TUNER_ON: |
||
1008 | toolStripMenuItem23.Checked = true; |
||
1009 | break; |
||
1010 | case TunerState.TUNER_OFF: |
||
1011 | toolStripMenuItem23.Checked = false; |
||
1012 | break; |
||
1013 | } |
||
1014 | }); |
||
1015 | } |
||
1016 | catch (Exception exception) |
||
1017 | { |
||
1018 | Log.Error(exception, Resources.Failed_to_read_the_tuner_state); |
||
1019 | } |
||
1020 | |||
1021 | try |
||
1022 | { |
||
1023 | var pa = await _catAssemblies.CatReadAsync<IpoState>("PA", new object[] { }, _cancellationToken); |
||
1024 | |||
1025 | contextMenuStrip1.InvokeIfRequired(contextMenuStrip => |
||
1026 | { |
||
1027 | switch (pa) |
||
1028 | { |
||
1029 | case IpoState.AMP: |
||
1030 | toolStripMenuItem22.Checked = false; |
||
1031 | break; |
||
1032 | case IpoState.IPO: |
||
1033 | toolStripMenuItem22.Checked = true; |
||
1034 | break; |
||
1035 | } |
||
1036 | }); |
||
1037 | } |
||
1038 | catch (Exception exception) |
||
1039 | { |
||
1040 | Log.Error(exception, Resources.Failed_to_read_IPO); |
||
1041 | } |
||
1042 | }, _cancellationToken); |
||
1043 | } |
||
1044 | |||
29 | office | 1045 | private async void scrollableToolStripComboBox1_MouseWheel(object sender, MouseEventArgs e) |
25 | office | 1046 | { |
1047 | var toolStripComboBox = (ScrollableToolStripComboBox)sender; |
||
27 | office | 1048 | if (int.TryParse(toolStripComboBox.Text, out var frequency)) |
1049 | { |
||
1050 | switch (Math.Sign(e.Delta)) |
||
1051 | { |
||
1052 | case -1: |
||
1053 | frequency = frequency - Configuration.Navigation.FrequencyStep; |
||
1054 | break; |
||
1055 | case 1: |
||
1056 | frequency = frequency + Configuration.Navigation.FrequencyStep; |
||
1057 | break; |
||
1058 | } |
||
25 | office | 1059 | |
27 | office | 1060 | try |
1061 | { |
||
54 | office | 1062 | using (var soundPlayer = new SoundPlayer(Assembly.GetExecutingAssembly() |
1063 | .GetManifestResourceStream("HamBook.Effects.pot.wav"))) |
||
25 | office | 1064 | { |
54 | office | 1065 | if (await _catAssemblies.CatSetAsync<int, bool>("FA", new object[] { frequency }, |
1066 | _cancellationToken)) |
||
29 | office | 1067 | { |
1068 | toolStripComboBox.Text = $"{frequency}"; |
||
25 | office | 1069 | |
54 | office | 1070 | if (Configuration.Navigation.MouseScrollSound) soundPlayer.Play(); |
29 | office | 1071 | } |
25 | office | 1072 | } |
27 | office | 1073 | } |
1074 | catch (Exception exception) |
||
1075 | { |
||
1076 | Log.Error(exception, Resources.Failed_to_set_VFO_A_frequency); |
||
1077 | } |
||
1078 | } |
||
1079 | } |
||
25 | office | 1080 | |
29 | office | 1081 | private async void scrollableToolStripComboBox1_KeyPress(object sender, KeyPressEventArgs e) |
27 | office | 1082 | { |
29 | office | 1083 | switch (e.KeyChar) |
27 | office | 1084 | { |
29 | office | 1085 | case (char)Keys.Enter: |
25 | office | 1086 | |
54 | office | 1087 | |
29 | office | 1088 | var toolStripComboBox = (ScrollableToolStripComboBox)sender; |
27 | office | 1089 | |
29 | office | 1090 | if (int.TryParse(toolStripComboBox.Text, out var frequency)) |
1091 | try |
||
1092 | { |
||
54 | office | 1093 | using (var soundPlayer = new SoundPlayer(Assembly.GetExecutingAssembly() |
1094 | .GetManifestResourceStream("HamBook.Effects.pot.wav"))) |
||
29 | office | 1095 | { |
54 | office | 1096 | if (await _catAssemblies.CatSetAsync<int, bool>("FA", new object[] { frequency }, |
1097 | _cancellationToken)) |
||
29 | office | 1098 | { |
32 | office | 1099 | e.Handled = true; |
1100 | |||
54 | office | 1101 | if (Configuration.Navigation.MouseScrollSound) soundPlayer.Play(); |
29 | office | 1102 | } |
1103 | } |
||
1104 | } |
||
1105 | catch (Exception exception) |
||
1106 | { |
||
1107 | Log.Error(exception, Resources.Failed_to_set_VFO_A_frequency); |
||
1108 | } |
||
54 | office | 1109 | |
29 | office | 1110 | break; |
27 | office | 1111 | } |
25 | office | 1112 | } |
1113 | |||
37 | office | 1114 | private void scrollableToolStripComboBox11_SelectedIndexChanged(object sender, EventArgs e) |
25 | office | 1115 | { |
1116 | var toolStripComboBox = (ScrollableToolStripComboBox)sender; |
||
1117 | |||
1118 | _squelchScheduledContinuation.Schedule(TimeSpan.FromSeconds(1), () => |
||
1119 | { |
||
1120 | contextMenuStrip1.InvokeIfRequired(async contextMenuStrip1 => |
||
1121 | { |
||
1122 | if (int.TryParse(toolStripComboBox.Text, out var squelch)) |
||
1123 | try |
||
1124 | { |
||
1125 | await _catAssemblies.CatWriteAsync<int>("SQ", new object[] { squelch }, _cancellationToken); |
||
1126 | |||
1127 | toolStripComboBox.Text = $"{squelch}"; |
||
1128 | |||
27 | office | 1129 | Log.Information($"{Resources.Squelch_set} {squelch}"); |
25 | office | 1130 | } |
1131 | catch (Exception exception) |
||
1132 | { |
||
1133 | Log.Error(exception, Resources.Failed_to_set_squelch); |
||
1134 | } |
||
1135 | }); |
||
1136 | }, _cancellationToken); |
||
1137 | } |
||
27 | office | 1138 | |
1139 | private async void toolStripComboBox1_SelectedIndexChanged(object sender, EventArgs e) |
||
1140 | { |
||
1141 | var toolStripComboBox = (ToolStripComboBox)sender; |
||
43 | office | 1142 | |
1143 | var radioMode = RadioMode.Create(Configuration.Radio, toolStripComboBox.Text); |
||
1144 | |||
1145 | try |
||
27 | office | 1146 | { |
43 | office | 1147 | await _catAssemblies.CatSetAsync<RadioMode, bool>("MD", new object[] { radioMode }, _cancellationToken); |
27 | office | 1148 | } |
43 | office | 1149 | catch (Exception exception) |
1150 | { |
||
1151 | Log.Error(exception, Resources.Failed_to_set_radio_mode, radioMode); |
||
1152 | } |
||
27 | office | 1153 | } |
1154 | |||
34 | office | 1155 | private async void scrollableToolStripComboBox7_SelectedIndexChanged(object sender, EventArgs e) |
27 | office | 1156 | { |
1157 | var toolStripComboBox = (ScrollableToolStripComboBox)sender; |
||
1158 | |||
34 | office | 1159 | if (int.TryParse(toolStripComboBox.Text, out var amplification)) |
1160 | try |
||
27 | office | 1161 | { |
54 | office | 1162 | if (await _catAssemblies.CatSetAsync<int, bool>("PC", new object[] { amplification }, |
1163 | _cancellationToken)) |
||
34 | office | 1164 | { |
1165 | toolStripComboBox.Text = $"{amplification}"; |
||
1166 | |||
1167 | Log.Information($"{Resources.Amplification_set} {amplification}W"); |
||
1168 | } |
||
1169 | } |
||
1170 | catch (Exception exception) |
||
1171 | { |
||
1172 | Log.Error(exception, Resources.Failed_to_set_amplification); |
||
1173 | } |
||
1174 | } |
||
1175 | |||
1176 | private async void scrollableToolStripComboBox7_KeyPress(object sender, KeyPressEventArgs e) |
||
1177 | { |
||
1178 | switch (e.KeyChar) |
||
1179 | { |
||
1180 | case (char)Keys.Enter: |
||
1181 | var toolStripComboBox = (ScrollableToolStripComboBox)sender; |
||
1182 | |||
27 | office | 1183 | if (int.TryParse(toolStripComboBox.Text, out var amplification)) |
1184 | try |
||
1185 | { |
||
54 | office | 1186 | if (await _catAssemblies.CatSetAsync<int, bool>("PC", new object[] { amplification }, |
1187 | _cancellationToken)) |
||
34 | office | 1188 | { |
1189 | toolStripComboBox.Text = $"{amplification}"; |
||
27 | office | 1190 | |
34 | office | 1191 | Log.Information($"{Resources.Amplification_set} {amplification}W"); |
1192 | |||
1193 | e.Handled = true; |
||
1194 | } |
||
27 | office | 1195 | } |
1196 | catch (Exception exception) |
||
1197 | { |
||
1198 | Log.Error(exception, Resources.Failed_to_set_amplification); |
||
1199 | } |
||
54 | office | 1200 | |
34 | office | 1201 | break; |
1202 | } |
||
27 | office | 1203 | } |
1204 | |||
39 | office | 1205 | private void toolStripMenuItem30_Click(object sender, EventArgs e) |
33 | office | 1206 | { |
1207 | if (!int.TryParse(scrollableToolStripComboBox9.Text, out var start) |
||
1208 | || !int.TryParse(scrollableToolStripComboBox10.Text, out var stop)) |
||
1209 | return; |
||
29 | office | 1210 | |
54 | office | 1211 | if (!int.TryParse(scrollableToolStripComboBox3.Text, out var pause)) pause = 5; |
33 | office | 1212 | |
54 | office | 1213 | if (!int.TryParse(scrollableToolStripComboBox4.Text, out var step)) step = 5000; |
33 | office | 1214 | |
54 | office | 1215 | if (!int.TryParse(scrollableToolStripComboBox6.Text, out var scanDetectPause)) scanDetectPause = 10; |
33 | office | 1216 | |
1217 | if (_bandScan != null) |
||
1218 | { |
||
39 | office | 1219 | _bandScan.Stop(); |
33 | office | 1220 | _bandScan = null; |
1221 | } |
||
1222 | |||
1223 | _bandScan = new BandScan(_catAssemblies, start, stop, _serialPort, Configuration); |
||
1224 | |||
1225 | if (toolStripMenuItem14.Checked) |
||
1226 | { |
||
1227 | _bandScan.Start(step, pause, scanDetectPause, toolStripMenuItem16.Checked); |
||
1228 | |||
1229 | return; |
||
1230 | } |
||
1231 | |||
36 | office | 1232 | _bandScan.Start(step, pause, 0, toolStripMenuItem16.Checked); |
33 | office | 1233 | } |
35 | office | 1234 | |
1235 | private async void toolStripMenuItem32_Click(object sender, EventArgs e) |
||
1236 | { |
||
1237 | var toolStripMenuItem = toolStripMenuItem31; |
||
1238 | |||
1239 | try |
||
1240 | { |
||
54 | office | 1241 | if (await _catAssemblies.CatSetAsync<TxState, bool>("TX", new object[] { TxState.ON }, |
1242 | _cancellationToken)) |
||
35 | office | 1243 | { |
1244 | toolStripMenuItem.Text = Resources.On; |
||
1245 | } |
||
1246 | } |
||
1247 | catch (Exception exception) |
||
1248 | { |
||
1249 | Log.Error(exception, Resources.Failed_to_set_PTT_state); |
||
1250 | } |
||
1251 | } |
||
1252 | |||
1253 | private async void toolStripMenuItem33_Click(object sender, EventArgs e) |
||
1254 | { |
||
1255 | var toolStripMenuItem = toolStripMenuItem31; |
||
1256 | |||
1257 | try |
||
1258 | { |
||
54 | office | 1259 | if (await _catAssemblies.CatSetAsync<TxState, bool>("TX", new object[] { TxState.OFF }, |
1260 | _cancellationToken)) |
||
35 | office | 1261 | { |
1262 | toolStripMenuItem.Text = Resources.Off; |
||
1263 | } |
||
1264 | } |
||
1265 | catch (Exception exception) |
||
1266 | { |
||
1267 | Log.Error(exception, Resources.Failed_to_set_PTT_state); |
||
1268 | } |
||
1269 | } |
||
1270 | |||
1271 | private async void toolStripMenuItem26_DropDownOpening(object sender, EventArgs e) |
||
1272 | { |
||
1273 | var toolStripMenuItem = toolStripMenuItem31; |
||
1274 | |||
1275 | try |
||
1276 | { |
||
1277 | switch (await _catAssemblies.CatReadAsync<TxState>("TX", new object[] { }, _cancellationToken)) |
||
1278 | { |
||
1279 | case TxState.ON: |
||
1280 | toolStripMenuItem.Text = Resources.On; |
||
1281 | break; |
||
1282 | case TxState.OFF: |
||
1283 | toolStripMenuItem.Text = Resources.Off; |
||
1284 | break; |
||
1285 | } |
||
1286 | } |
||
1287 | catch (Exception exception) |
||
1288 | { |
||
1289 | Log.Error(exception, Resources.Failed_to_read_PTT_state); |
||
1290 | } |
||
1291 | } |
||
37 | office | 1292 | |
1293 | private async void scrollableToolStripComboBox8_SelectedIndexChanged(object sender, EventArgs e) |
||
1294 | { |
||
1295 | var toolStripComboBox = (ScrollableToolStripComboBox)sender; |
||
1296 | |||
1297 | if (SplitState.TryParse(toolStripComboBox.Text, out var splitState)) |
||
1298 | try |
||
1299 | { |
||
54 | office | 1300 | if (await _catAssemblies.CatSetAsync<int, bool>("ST", new object[] { splitState }, |
1301 | _cancellationToken)) Log.Information($"{Resources.Split_state_set} {splitState}W"); |
||
37 | office | 1302 | } |
1303 | catch (Exception exception) |
||
1304 | { |
||
1305 | Log.Error(exception, Resources.Failed_to_set_split_state); |
||
1306 | } |
||
1307 | } |
||
38 | office | 1308 | |
1309 | private async void toolStripMenuItem35_Click(object sender, EventArgs e) |
||
1310 | { |
||
1311 | var scrollableToolStripComboBox = scrollableToolStripComboBox12; |
||
1312 | |||
41 | office | 1313 | var channel = scrollableToolStripComboBox.Text; |
1314 | if (!string.IsNullOrEmpty(channel)) |
||
38 | office | 1315 | { |
1316 | if (!_memoryChannelStore.TryGetValue(channel, out var memoryChannel)) |
||
1317 | { |
||
46 | office | 1318 | memoryChannel = MemoryChannel.Create(Configuration.Radio); |
41 | office | 1319 | memoryChannel.CurrentLocation = channel; |
54 | office | 1320 | memoryChannel.MemoryRadioMode = |
1321 | MemoryRadioMode.Create(Configuration.Radio, toolStripComboBox1.Text); |
||
38 | office | 1322 | } |
1323 | |||
1324 | if (memoryChannel.Tag = !string.IsNullOrEmpty(toolStripTextBox1.Text)) |
||
54 | office | 1325 | memoryChannel.Text = $"{toolStripTextBox1.Text,-12}"; |
38 | office | 1326 | |
1327 | if (int.TryParse(scrollableToolStripComboBox1.Text, out var frequency)) |
||
1328 | { |
||
1329 | memoryChannel.Frequency = frequency; |
||
1330 | |||
1331 | try |
||
1332 | { |
||
54 | office | 1333 | if (await _catAssemblies.CatSetAsync<MemoryChannel, bool>("MT", new object[] { memoryChannel }, |
1334 | _cancellationToken)) Log.Information(Resources.Stored_VFO_A_to_memory); |
||
38 | office | 1335 | } |
1336 | catch (Exception exception) |
||
1337 | { |
||
1338 | Log.Error(exception, Resources.Failed_to_save_VFO_A_to_memory); |
||
1339 | } |
||
1340 | } |
||
1341 | } |
||
1342 | } |
||
1343 | |||
1344 | private void scrollableToolStripComboBox12_SelectedIndexChanged(object sender, EventArgs e) |
||
1345 | { |
||
1346 | var toolStripComboBox = (ScrollableToolStripComboBox)sender; |
||
1347 | |||
41 | office | 1348 | var channel = toolStripComboBox.Text; |
1349 | if (!string.IsNullOrEmpty(channel)) |
||
38 | office | 1350 | { |
1351 | if (_memoryChannelStore.TryGetValue(channel, out var memoryChannel)) |
||
1352 | { |
||
1353 | toolStripTextBox1.Text = memoryChannel.Text; |
||
1354 | return; |
||
1355 | } |
||
1356 | |||
1357 | toolStripTextBox1.Text = string.Empty; |
||
1358 | } |
||
1359 | } |
||
1360 | |||
1361 | private async void toolStripMenuItem20_Click(object sender, EventArgs e) |
||
1362 | { |
||
1363 | try |
||
1364 | { |
||
1365 | var frequency = await _catAssemblies.CatReadAsync<int>("FA", new object[] { }, _cancellationToken); |
||
1366 | |||
1367 | await _catAssemblies.CatWriteAsync<int>("FA", new object[] { frequency }, _cancellationToken); |
||
1368 | |||
1369 | await _catAssemblies.CatWriteAsync("SV", new object[] { }, _cancellationToken); |
||
1370 | } |
||
1371 | catch (Exception exception) |
||
1372 | { |
||
1373 | Log.Error(exception, Resources.Unable_to_swap_VFO_A_and_VFO_B); |
||
1374 | } |
||
1375 | } |
||
50 | office | 1376 | |
1377 | private async void lockToolStripMenuItem_CheckStateChanged(object sender, EventArgs e) |
||
1378 | { |
||
1379 | var toolStripMenuItem = (ToolStripMenuItem)sender; |
||
1380 | try |
||
1381 | { |
||
1382 | LockState state; |
||
1383 | switch (toolStripMenuItem.Checked) |
||
1384 | { |
||
1385 | case true: |
||
1386 | state = LockState.ON; |
||
1387 | break; |
||
1388 | default: |
||
1389 | state = LockState.OFF; |
||
1390 | break; |
||
1391 | } |
||
1392 | |||
54 | office | 1393 | if (!await _catAssemblies.CatSetAsync<LockState, bool>("LK", new object[] { state }, |
1394 | _cancellationToken)) Log.Error(Resources.Failed_to_set_lock_state); |
||
50 | office | 1395 | } |
1396 | catch (Exception exception) |
||
1397 | { |
||
1398 | Log.Error(exception, Resources.Failed_to_read_lock_state); |
||
1399 | } |
||
1400 | } |
||
54 | office | 1401 | |
1402 | private void menuToolStripMenuItem_Click(object sender, EventArgs e) |
||
1403 | { |
||
1404 | if (_menuForm != null) return; |
||
1405 | |||
1406 | _menuForm = new MenuForm(Configuration, _catAssemblies, _cancellationToken); |
||
1407 | _menuForm.Closing += MenuForm_Closing; |
||
1408 | _menuForm.Show(); |
||
1409 | } |
||
1410 | |||
1411 | private void MenuForm_Closing(object sender, CancelEventArgs e) |
||
1412 | { |
||
1413 | if (_menuForm == null) return; |
||
1414 | |||
1415 | _menuForm.Dispose(); |
||
1416 | _menuForm = null; |
||
1417 | } |
||
1 | office | 1418 | } |
54 | office | 1419 | } |