initial version with serial link
This commit is contained in:
@@ -1,86 +1,41 @@
|
||||
#nullable enable
|
||||
|
||||
using System;
|
||||
using System.Drawing;
|
||||
using System.Reflection;
|
||||
using System.Threading;
|
||||
using System.Windows.Forms;
|
||||
|
||||
namespace analog_system_monitor
|
||||
public class TrayApp : ApplicationContext
|
||||
{
|
||||
public class TrayApp : ApplicationContext
|
||||
private NotifyIcon trayIcon;
|
||||
private Telemetry telemetry;
|
||||
|
||||
public TrayApp()
|
||||
{
|
||||
private NotifyIcon trayIcon;
|
||||
private Thread workerThread;
|
||||
private bool running = true;
|
||||
telemetry = new Telemetry();
|
||||
telemetry.Initialize();
|
||||
|
||||
public TrayApp()
|
||||
trayIcon = new NotifyIcon()
|
||||
{
|
||||
trayIcon = new NotifyIcon()
|
||||
{
|
||||
Icon = LoadEmbeddedIcon(),
|
||||
Text = "Analog System Monitor",
|
||||
Visible = true,
|
||||
ContextMenuStrip = BuildMenu()
|
||||
};
|
||||
Icon = Icon.ExtractAssociatedIcon(Application.ExecutablePath),
|
||||
Visible = true,
|
||||
Text = "Telemetry Running"
|
||||
};
|
||||
|
||||
workerThread = new Thread(WorkerLoop)
|
||||
{
|
||||
IsBackground = true
|
||||
};
|
||||
workerThread.Start();
|
||||
}
|
||||
var menu = new ContextMenuStrip();
|
||||
menu.Items.Add("Exit", null, OnExit);
|
||||
trayIcon.ContextMenuStrip = menu;
|
||||
|
||||
private Icon LoadEmbeddedIcon()
|
||||
{
|
||||
var assembly = Assembly.GetExecutingAssembly();
|
||||
var resourceName = "analog_system_monitor.telemetry_icon.ico";
|
||||
// Start telemetry loop
|
||||
var timer = new System.Windows.Forms.Timer();
|
||||
timer.Interval = 1000;
|
||||
timer.Tick += (s, e) => telemetry.UpdateAndSend();
|
||||
timer.Start();
|
||||
}
|
||||
|
||||
using var stream = assembly.GetManifestResourceStream(resourceName);
|
||||
if (stream == null)
|
||||
{
|
||||
MessageBox.Show("Embedded icon not found: " + resourceName);
|
||||
return SystemIcons.Application;
|
||||
}
|
||||
|
||||
return new Icon(stream);
|
||||
}
|
||||
|
||||
private ContextMenuStrip BuildMenu()
|
||||
{
|
||||
var menu = new ContextMenuStrip();
|
||||
|
||||
var exitItem = new ToolStripMenuItem("Exit");
|
||||
exitItem.Click += (s, e) => ExitApp();
|
||||
|
||||
menu.Items.Add(exitItem);
|
||||
return menu;
|
||||
}
|
||||
|
||||
private void WorkerLoop()
|
||||
{
|
||||
var telemetry = new Telemetry();
|
||||
telemetry.Initialize();
|
||||
|
||||
while (running)
|
||||
{
|
||||
telemetry.UpdateAndSend();
|
||||
Thread.Sleep(telemetry.UpdateRateMs);
|
||||
}
|
||||
}
|
||||
|
||||
private void ExitApp()
|
||||
{
|
||||
running = false;
|
||||
|
||||
try
|
||||
{
|
||||
workerThread?.Join(500);
|
||||
}
|
||||
catch { }
|
||||
|
||||
trayIcon.Visible = false;
|
||||
trayIcon.Dispose();
|
||||
|
||||
Application.Exit();
|
||||
}
|
||||
private void OnExit(object? sender, EventArgs e)
|
||||
{
|
||||
telemetry.Dispose();
|
||||
trayIcon.Visible = false;
|
||||
Application.Exit();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user