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