25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

60 lines
1.8 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.IO.Ports;
  5. using System.Windows.Forms;
  6. using ThunderboltTimeSync.Devices.Thunderbolt;
  7. using ThunderboltTimeSync.TimeProviders;
  8. using ThunderboltTimeSync.TimeProviders.Thunderbolt;
  9. namespace ThunderboltTimeSync {
  10. public partial class FormMain : Form {
  11. private static readonly Dictionary<LogLevel, Color> LOG_LEVEL_TO_COLOR = new Dictionary<LogLevel, Color>() {
  12. { LogLevel.Info, Color.Black },
  13. { LogLevel.Warning, Color.Yellow },
  14. { LogLevel.Error, Color.Red }
  15. };
  16. private ITimeProvider timeProvider;
  17. public FormMain() {
  18. // Check for admin rights
  19. // If running as admin:
  20. // Ask for COM port with dialog
  21. // Connect to COM port
  22. // When time message received:
  23. // If (time in UTC) AND (last time change was more than $MIN_UPDATE_INTERVAL ago) AND (error is less than $ERROR_THRESHOLD)
  24. // Change system time to GPS time
  25. // Else:
  26. // Display message to tell user to run as admin
  27. // Quit
  28. InitializeComponent();
  29. latestLogMessage.Text = "";
  30. ThunderboltSerialPort thunderboltSerialPort = new ThunderboltSerialPort(new SerialPort("COM3"));
  31. timeProvider = new ThunderboltTimeProvider(thunderboltSerialPort);
  32. timeProvider.TimeAvailable += (DateTime dateTime) => {
  33. Invoke(new Action(() => {
  34. labelTimestamps.Text += string.Format("{0} {1} @ {2}\n", dateTime.ToLongDateString(), dateTime.ToLongTimeString(), DateTime.Now.ToLongTimeString());
  35. }));
  36. };
  37. timeProvider.Log += (string message, LogLevel logLevel) => {
  38. Invoke(new Action(() => {
  39. latestLogMessage.Text = message;
  40. latestLogMessage.ForeColor = LOG_LEVEL_TO_COLOR[logLevel];
  41. }));
  42. };
  43. timeProvider.Start();
  44. }
  45. private void FormMain_FormClosing(object sender, FormClosingEventArgs e) {
  46. timeProvider.Stop();
  47. }
  48. }
  49. }