HamBook – Blame information for rev 3

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 using HamBook.Radios;
2 using HamBook.Utilities;
3 using HamBook.Utilities.Serialization;
4 using NetSparkleUpdater.Enums;
5 using NetSparkleUpdater.SignatureVerifiers;
6 using NetSparkleUpdater.UI.WinForms;
7 using NetSparkleUpdater;
8 using Serilog;
9 using System;
10 using System.ComponentModel;
11 using System.Drawing;
12 using System.IO;
13 using System.IO.Ports;
14 using System.Reflection;
15 using System.Threading;
16 using System.Threading.Tasks;
17 using System.Windows.Forms;
18 using HamBook.Properties;
19 using HamBook.Radios.Generic;
20 using PowerState = HamBook.Radios.Generic.PowerState;
21  
22 namespace HamBook
23 {
24 public partial class Form1 : Form
25 {
26 private ScheduledContinuation _changedConfigurationContinuation;
27 private Configuration.Configuration Configuration { get; set; }
28 private SerialPort _serialPort;
29 private LogMemorySink _memorySink;
30 private ViewLogsForm _viewLogsForm;
31 private AboutForm _aboutForm;
32 private SettingsForm _settingsForm;
33 private SparkleUpdater _sparkle;
34 private readonly CancellationToken _cancellationToken;
35 private readonly CancellationTokenSource _cancellationTokenSource;
36 private CatAssemblies _catAssemblies;
3 office 37 private CancellationTokenSource _scanningCancellationTokenSource;
38 private CancellationToken _scanningCancellationToken;
39 private BandScan _bandScan;
1 office 40  
41 public bool MemorySinkEnabled { get; set; }
42  
43 private Form1()
44 {
45 _cancellationTokenSource = new CancellationTokenSource();
46 _cancellationToken = _cancellationTokenSource.Token;
47  
48 _changedConfigurationContinuation = new ScheduledContinuation();
3 office 49  
1 office 50 }
51  
52 public Form1(Mutex mutex) : this()
53 {
54 InitializeComponent();
55  
56 _memorySink = new LogMemorySink();
57  
58 Log.Logger = new LoggerConfiguration()
59 .MinimumLevel.Debug()
60 .WriteTo.Conditional(condition => MemorySinkEnabled, configureSink => configureSink.Sink(_memorySink))
61 .WriteTo.File(Path.Combine(Constants.UserApplicationDirectory, "Logs", $"{Constants.AssemblyName}.log"),
62 rollingInterval: RollingInterval.Day)
63 .CreateLogger();
64  
65 // Start application update.
66 var manifestModuleName = Assembly.GetEntryAssembly().ManifestModule.FullyQualifiedName;
67 var icon = Icon.ExtractAssociatedIcon(manifestModuleName);
68  
69 _sparkle = new SparkleUpdater("https://hambook.grimore.org/update/appcast.xml",
70 new Ed25519Checker(SecurityMode.Strict, "LonrgxVjSF0GnY4hzwlRJnLkaxnDn2ikdmOifILzLJY="))
71 {
72 UIFactory = new UIFactory(icon),
73 RelaunchAfterUpdate = true
74 };
75 _sparkle.StartLoop(true, true);
76 }
77  
78 private async void Form1_Load(object sender, EventArgs e)
79 {
80 Configuration = await LoadConfiguration();
81  
82 // Set up serial connection.
83 _serialPort = new SerialPort(Configuration.Port, Configuration.Speed, Configuration.Parity, Configuration.DataBits, Configuration.StopBits);
84 //TODO: move to settings
85 //_serialPort.ReadTimeout = TimeSpan.FromSeconds(1).Milliseconds;
86  
87 _catAssemblies = new CatAssemblies(_serialPort, Configuration.Radio);
3 office 88 _catAssemblies.CatSet<InformationState>("AI", new object[] { InformationState.OFF });
1 office 89  
90 try
91 {
92 switch (await _catAssemblies.CatRead<PowerState>("PS", new object[] { }, _cancellationToken))
93 {
94 case PowerState.ON:
95 Initialize();
96 break;
97 }
98 }
99 catch(Exception exception)
100 {
101 Log.Error(exception, Resources.Failed_to_read_power_state);
102 }
103 }
104  
105 private void quitToolStripMenuItem_Click(object sender, EventArgs e)
106 {
107 Close();
108 }
109  
110 private void viewLogsToolStripMenuItem_Click(object sender, EventArgs e)
111 {
112 if (_viewLogsForm != null)
113 {
114 return;
115 }
116  
117 _viewLogsForm = new ViewLogsForm(this, _memorySink, _cancellationToken);
118 _viewLogsForm.Closing += ViewLogsForm_Closing;
119 _viewLogsForm.Show();
120 }
121  
122 private void ViewLogsForm_Closing(object sender, CancelEventArgs e)
123 {
124 if (_viewLogsForm == null)
125 {
126 return;
127 }
128  
129 _viewLogsForm.Closing -= ViewLogsForm_Closing;
130 _viewLogsForm.Close();
131 _viewLogsForm = null;
132 }
133  
134 private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
135 {
136 if (_aboutForm != null)
137 {
138 return;
139 }
140  
141 _aboutForm = new AboutForm(_cancellationToken);
142 _aboutForm.Closing += AboutForm_Closing;
143 _aboutForm.Show();
144 }
145  
146 private void AboutForm_Closing(object sender, CancelEventArgs e)
147 {
148 if (_aboutForm == null)
149 {
150 return;
151 }
152  
153 _aboutForm.Dispose();
154 _aboutForm = null;
155 }
156  
157 private void settingsToolStripMenuItem_Click(object sender, EventArgs e)
158 {
159 if (_settingsForm != null)
160 {
161 return;
162 }
163  
164 _settingsForm = new SettingsForm(Configuration, _cancellationToken);
165 _settingsForm.Closing += SettingsForm_Closing;
166 _settingsForm.Show();
167 }
168  
169 private void SettingsForm_Closing(object sender, CancelEventArgs e)
170 {
171 if (_settingsForm == null)
172 {
173 return;
174 }
175  
176 if(_settingsForm.SaveOnClose)
177 {
178 // Commit the configuration.
179 _changedConfigurationContinuation.Schedule(TimeSpan.FromSeconds(1),
180 async () => {
181 await SaveConfiguration();
182  
183 Miscellaneous.LaunchOnBootSet(Configuration.LaunchOnBoot);
184  
185 _serialPort.Dispose();
186 _serialPort = new SerialPort(Configuration.Port, Configuration.Speed, Configuration.Parity, Configuration.DataBits, Configuration.StopBits);
187  
188 }, _cancellationToken);
189 }
190  
191 _settingsForm.Dispose();
192 _settingsForm = null;
193 }
194  
195 public async Task SaveConfiguration()
196 {
197 if (!Directory.Exists(Constants.UserApplicationDirectory))
198 {
199 Directory.CreateDirectory(Constants.UserApplicationDirectory);
200 }
201  
202 switch (await Serialization.Serialize(Configuration, Constants.ConfigurationFile, "Configuration",
203 "<!ATTLIST Configuration xmlns:xsi CDATA #IMPLIED xsi:noNamespaceSchemaLocation CDATA #IMPLIED>",
204 CancellationToken.None))
205 {
3 office 206 case SerializationSuccess<Configuration.Configuration> configuration:
1 office 207 Log.Information("Serialized configuration.");
208 break;
209 case SerializationFailure serializationFailure:
210 Log.Warning(serializationFailure.Exception.Message, "Failed to serialize configuration.");
211 break;
212 }
213 }
214  
215 public static async Task<Configuration.Configuration> LoadConfiguration()
216 {
217 if (!Directory.Exists(Constants.UserApplicationDirectory))
218 {
219 Directory.CreateDirectory(Constants.UserApplicationDirectory);
220 }
221  
222 var deserializationResult =
223 await Serialization.Deserialize<Configuration.Configuration>(Constants.ConfigurationFile,
224 Constants.ConfigurationNamespace, Constants.ConfigurationXsd, CancellationToken.None);
225  
226 switch (deserializationResult)
227 {
228 case SerializationSuccess<Configuration.Configuration> serializationSuccess:
229 return serializationSuccess.Result;
230 case SerializationFailure serializationFailure:
231 Log.Warning(serializationFailure.Exception, "Failed to load configuration.");
232 return new Configuration.Configuration();
233 default:
234 return new Configuration.Configuration();
235 }
236 }
237  
238 private void Initialize()
239 {
240 try
241 {
242 var mode = _catAssemblies.CatRead<RadioMode>("MD", new object[] { });
3 office 243 toolStripComboBox1.Text = mode;
1 office 244 }
245 catch(Exception exception)
246 {
247 Log.Error(exception, Resources.Failed_to_read_radio_mode);
248 }
249  
250 try
251 {
252 var fa = _catAssemblies.CatRead<int>("FA", new object[] { });
3 office 253 scrollableToolStripComboBox1.Text = $"{fa}";
1 office 254 }
255 catch(Exception exception)
256 {
257 Log.Error(exception, Resources.Failed_to_read_VFO_A);
258 }
259  
260 try
261 {
262 var fb = _catAssemblies.CatRead<int>("FB", new object[] { });
3 office 263 scrollableToolStripComboBox2.Text = $"{fb}";
1 office 264 }
265 catch (Exception exception)
266 {
267 Log.Error(exception, Resources.Failed_to_read_VFO_B);
268 }
269  
270  
271 }
272  
273 private async void updateToolStripMenuItem_Click(object sender, EventArgs e)
274 {
275 // Manually check for updates, this will not show a ui
276 var result = await _sparkle.CheckForUpdatesQuietly();
277 if (result.Status == NetSparkleUpdater.Enums.UpdateStatus.UpdateAvailable)
278 {
279 // if update(s) are found, then we have to trigger the UI to show it gracefully
280 _sparkle.ShowUpdateNeededUI();
281 return;
282 }
283  
284 MessageBox.Show(Resources.No_updates_available_at_this_time, Resources.HamBook, MessageBoxButtons.OK,
285 MessageBoxIcon.Asterisk,
286 MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly, false);
287 }
288  
289 private void toolStripComboBox1_SelectedIndexChanged(object sender, EventArgs e)
290 {
291 var toolStripComboBox = (ToolStripComboBox)sender;
3 office 292 if(RadioMode.TryParse(toolStripComboBox.Text, out var radioMode))
1 office 293 {
3 office 294 try
1 office 295 {
3 office 296 _catAssemblies.CatSet<RadioMode>("MD", new object[] { radioMode });
1 office 297 }
3 office 298 catch (Exception exception)
299 {
300 Log.Error(exception, Resources.Failed_to_set_radio_mode, radioMode);
301 }
1 office 302 }
303 }
304  
305 private async void onToolStripMenuItem_Click(object sender, EventArgs e)
306 {
307 try
308 {
309 await _catAssemblies.CatSet<PowerState>("PS", new object[] { PowerState.ON }, _cancellationToken);
310 Initialize();
311 }
312 catch(Exception exception)
313 {
314 Log.Error(exception, Resources.Failed_to_set_power_state);
315 }
316 }
317  
318 private async void offToolStripMenuItem_Click(object sender, EventArgs e)
319 {
320 try
321 {
322 await _catAssemblies.CatSet<PowerState>("PS", new object[] { PowerState.OFF }, _cancellationToken);
323 }
324 catch(Exception exception)
325 {
326 Log.Error(exception, Resources.Failed_to_set_power_state);
327 }
328 }
329  
3 office 330 private void toolStripComboBox2_MouseWheel(object sender, MouseEventArgs e)
1 office 331 {
3 office 332 var toolStripComboBox = (ToolStripComboBox)sender;
333 if (int.TryParse(toolStripComboBox.Text, out var frequency))
1 office 334 {
3 office 335 switch(Math.Sign(e.Delta))
336 {
337 case -1:
338 frequency = frequency - 100;
339 break;
340 case 1:
341 frequency = frequency + 100;
342 break;
343 }
344  
345 try
346 {
347 _catAssemblies.CatSet<int>("FA", new object[] { frequency });
348 toolStripComboBox.Text = $"{frequency}";
349 }
350 catch (Exception exception)
351 {
352 Log.Error(exception, Resources.Failed_to_set_VFO_A_frequency);
353 }
1 office 354 }
355 }
356  
3 office 357 private void scrollableToolStripComboBox1_MouseWheel(object sender, MouseEventArgs e)
1 office 358 {
3 office 359 var toolStripComboBox = (ToolStripComboBox)sender;
360 if (int.TryParse(toolStripComboBox.Text, out var frequency))
1 office 361 {
3 office 362 switch (Math.Sign(e.Delta))
363 {
364 case -1:
365 frequency = frequency - 100;
366 break;
367 case 1:
368 frequency = frequency + 100;
369 break;
370 }
371  
1 office 372 try
373 {
3 office 374 _catAssemblies.CatSet<int>("FB", new object[] { frequency });
375 toolStripComboBox.Text = $"{frequency}";
1 office 376 }
377 catch (Exception exception)
378 {
3 office 379 Log.Error(exception, Resources.Failed_to_set_VFO_B_frequency);
1 office 380 }
381 }
382 }
383  
3 office 384 private void scrollableToolStripComboBox1_TextChanged(object sender, EventArgs e)
1 office 385 {
3 office 386 var toolStripComboBox = (ToolStripComboBox)sender;
387 if (int.TryParse(toolStripComboBox.Text, out var frequency))
1 office 388 {
3 office 389 try
390 {
391 _catAssemblies.CatSet<int>("FA", new object[] { frequency });
392 }
393 catch (Exception exception)
394 {
395 Log.Error(exception, Resources.Failed_to_set_VFO_A_frequency);
396 }
1 office 397 }
398 }
399  
3 office 400 private void scrollableToolStripComboBox2_TextChanged(object sender, EventArgs e)
1 office 401 {
3 office 402 var toolStripComboBox = (ToolStripComboBox)sender;
403 if (int.TryParse(toolStripComboBox.Text, out var frequency))
1 office 404 {
405 try
406 {
407 _catAssemblies.CatSet<int>("FB", new object[] { frequency });
408 }
409 catch (Exception exception)
410 {
411 Log.Error(exception, Resources.Failed_to_set_VFO_B_frequency);
412 }
3 office 413 }
414 }
415  
416 private void toolStripMenuItem1_Click(object sender, EventArgs e)
417 {
418 if (_bandScan == null)
419 {
1 office 420 return;
421 }
422  
3 office 423 _bandScan.Stop();
424 _bandScan = null;
1 office 425 }
3 office 426  
427 private void metersToolStripMenuItem1_Click(object sender, EventArgs e)
428 {
429 if(_bandScan != null)
430 {
431 _bandScan.Stop();
432 }
433  
434 _bandScan = new BandScan(_catAssemblies, 028000000, 029700000, _serialPort);
435 _bandScan.Start();
436 }
437  
438 private void metersToolStripMenuItem_Click(object sender, EventArgs e)
439 {
440 if (_bandScan != null)
441 {
442 _bandScan.Stop();
443 }
444  
445 _bandScan = new BandScan(_catAssemblies, 026965000, 027405000, _serialPort);
446 _bandScan.Start();
447 }
448  
449 private void metersToolStripMenuItem2_Click(object sender, EventArgs e)
450 {
451 if (_bandScan != null)
452 {
453 _bandScan.Stop();
454 }
455  
456 _bandScan = new BandScan(_catAssemblies, 024920000, 024990000, _serialPort);
457 _bandScan.Start();
458 }
459  
460 private void metersToolStripMenuItem3_Click(object sender, EventArgs e)
461 {
462 if (_bandScan != null)
463 {
464 _bandScan.Stop();
465 }
466  
467 _bandScan = new BandScan(_catAssemblies, 021000000, 021450000, _serialPort);
468 _bandScan.Start();
469 }
470  
471 private void metersToolStripMenuItem4_Click(object sender, EventArgs e)
472 {
473 if (_bandScan != null)
474 {
475 _bandScan.Stop();
476 }
477  
478 _bandScan = new BandScan(_catAssemblies, 018068000, 018168000, _serialPort);
479 _bandScan.Start();
480 }
481  
482 private void metersToolStripMenuItem5_Click(object sender, EventArgs e)
483 {
484 if (_bandScan != null)
485 {
486 _bandScan.Stop();
487 }
488  
489 _bandScan = new BandScan(_catAssemblies, 014000000, 014350000, _serialPort);
490 _bandScan.Start();
491 }
492  
493 private void metersToolStripMenuItem6_Click(object sender, EventArgs e)
494 {
495 if (_bandScan != null)
496 {
497 _bandScan.Stop();
498 }
499  
500 _bandScan = new BandScan(_catAssemblies, 010100000, 010150000, _serialPort);
501 _bandScan.Start();
502 }
503  
504 private void metersToolStripMenuItem7_Click(object sender, EventArgs e)
505 {
506 if (_bandScan != null)
507 {
508 _bandScan.Stop();
509 }
510  
511 _bandScan = new BandScan(_catAssemblies, 007000000, 007300000, _serialPort);
512 _bandScan.Start();
513 }
514  
515 private void metersToolStripMenuItem8_Click(object sender, EventArgs e)
516 {
517 if (_bandScan != null)
518 {
519 _bandScan.Stop();
520 }
521  
522 _bandScan = new BandScan(_catAssemblies, 050000000, 054000000, _serialPort);
523 _bandScan.Start();
524 }
1 office 525 }
526 }