HamBook – Blame information for rev 5

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