Initial WIP commit
Some checks failed
Deploy to Gitea Releases / deploy-to-gitea-releases (push) Failing after 9s
Some checks failed
Deploy to Gitea Releases / deploy-to-gitea-releases (push) Failing after 9s
This commit is contained in:
235
StatusWindow/WindowSource.cs
Normal file
235
StatusWindow/WindowSource.cs
Normal file
@@ -0,0 +1,235 @@
|
||||
using ChrisKaczor.Wpf.Windows;
|
||||
using ChrisKaczor.Wpf.Windows.FloatingStatusWindow;
|
||||
using HardwareMonitorStatusWindow.Service;
|
||||
using HardwareMonitorStatusWindow.StatusWindow.SettingsWindow;
|
||||
using Microsoft.Win32.TaskScheduler;
|
||||
using Serilog;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Timers;
|
||||
using System.Windows.Threading;
|
||||
using Task = System.Threading.Tasks.Task;
|
||||
using Timer = System.Timers.Timer;
|
||||
|
||||
namespace HardwareMonitorStatusWindow.StatusWindow;
|
||||
|
||||
internal class WindowSource : IWindowSource, IDisposable
|
||||
{
|
||||
private readonly FloatingStatusWindow _floatingStatusWindow;
|
||||
private readonly Timer _timer;
|
||||
private readonly Dispatcher _dispatcher;
|
||||
|
||||
internal WindowSource()
|
||||
{
|
||||
try
|
||||
{
|
||||
using var taskService = new TaskService();
|
||||
|
||||
var existingTask = taskService.FindTask(HardwareMonitorService.ScheduledTaskName);
|
||||
|
||||
if (existingTask == null)
|
||||
{
|
||||
var assembly = Assembly.GetExecutingAssembly();
|
||||
|
||||
var path = Path.GetDirectoryName(assembly.Location);
|
||||
|
||||
if (path != null)
|
||||
{
|
||||
var fileName = Path.Combine(path, "Service.exe");
|
||||
|
||||
var startInfo = new ProcessStartInfo
|
||||
{
|
||||
FileName = fileName,
|
||||
Arguments = "--install",
|
||||
UseShellExecute = true,
|
||||
Verb = "runas"
|
||||
};
|
||||
|
||||
Process.Start(startInfo);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
// Ignored
|
||||
}
|
||||
|
||||
_floatingStatusWindow = new FloatingStatusWindow(this);
|
||||
_floatingStatusWindow.SetText(Resources.Loading);
|
||||
|
||||
_dispatcher = Dispatcher.CurrentDispatcher;
|
||||
|
||||
_timer = new Timer(5000);
|
||||
|
||||
Task.Factory.StartNew(UpdateApp).ContinueWith(t => Start(t.Result.Result));
|
||||
}
|
||||
|
||||
private async Task<bool> UpdateApp()
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!UpdateCheck.IsInstalled)
|
||||
return false;
|
||||
|
||||
if (!Settings.Default.CheckVersionAtStartup)
|
||||
return false;
|
||||
|
||||
Log.Logger.Information("Checking for update");
|
||||
|
||||
await _dispatcher.InvokeAsync(() => _floatingStatusWindow.SetText(Resources.CheckingForUpdate));
|
||||
|
||||
var newVersion = await UpdateCheck.UpdateManager.CheckForUpdatesAsync();
|
||||
|
||||
if (newVersion == null)
|
||||
return false;
|
||||
|
||||
Log.Logger.Information("Downloading update");
|
||||
|
||||
await _dispatcher.InvokeAsync(() => _floatingStatusWindow.SetText(Resources.DownloadingUpdate));
|
||||
|
||||
await UpdateCheck.UpdateManager.DownloadUpdatesAsync(newVersion);
|
||||
|
||||
Log.Logger.Information("Installing update");
|
||||
|
||||
await _dispatcher.InvokeAsync(() => _floatingStatusWindow.SetText(Resources.InstallingUpdate));
|
||||
|
||||
UpdateCheck.UpdateManager.ApplyUpdatesAndRestart(newVersion);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Log.Logger.Error(e, nameof(UpdateApp));
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private async Task Start(bool hasUpdate)
|
||||
{
|
||||
Log.Logger.Information("Start: hasUpdate={hasUpdate}", hasUpdate);
|
||||
|
||||
if (hasUpdate)
|
||||
return;
|
||||
|
||||
Log.Logger.Information("Load");
|
||||
|
||||
await Load();
|
||||
|
||||
Log.Logger.Information("Starting timer");
|
||||
|
||||
_timer.Elapsed += HandleTimerElapsed;
|
||||
_timer.AutoReset = false;
|
||||
_timer.Enabled = true;
|
||||
}
|
||||
|
||||
private static async Task Load()
|
||||
{
|
||||
await Data.LoadComputer();
|
||||
|
||||
Data.Load();
|
||||
}
|
||||
|
||||
private void Save()
|
||||
{
|
||||
Data.Save();
|
||||
}
|
||||
|
||||
private void HandleTimerElapsed(object? sender, ElapsedEventArgs e)
|
||||
{
|
||||
Refresh();
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_timer.Enabled = false;
|
||||
_timer.Dispose();
|
||||
|
||||
Data.CloseComputer();
|
||||
|
||||
_floatingStatusWindow.Save();
|
||||
_floatingStatusWindow.Dispose();
|
||||
}
|
||||
|
||||
public Guid Id => Guid.Parse("0DB9393F-2710-40A5-A27B-34568696C61A");
|
||||
|
||||
public string Name => Resources.ApplicationName;
|
||||
|
||||
public System.Drawing.Icon Icon => Resources.ApplicationIcon;
|
||||
|
||||
public bool HasSettingsMenu => true;
|
||||
|
||||
public bool HasAboutMenu => false;
|
||||
|
||||
public void ShowAbout()
|
||||
{
|
||||
}
|
||||
|
||||
public void ShowSettings()
|
||||
{
|
||||
var categoryPanels = new List<CategoryPanelBase>
|
||||
{
|
||||
new GeneralSettingsPanel(),
|
||||
new HardwareSettingsPanel(),
|
||||
new UpdateSettingsPanel(),
|
||||
new AboutSettingsPanel()
|
||||
};
|
||||
|
||||
var settingsWindow = new CategoryWindow(categoryPanels, Resources.SettingsTitle, Resources.CloseButtonText);
|
||||
|
||||
settingsWindow.ShowDialog();
|
||||
|
||||
Save();
|
||||
}
|
||||
|
||||
public bool HasRefreshMenu => true;
|
||||
|
||||
public void Refresh()
|
||||
{
|
||||
using (var taskService = new TaskService())
|
||||
{
|
||||
var existingTask = taskService.FindTask(HardwareMonitorService.ScheduledTaskName);
|
||||
|
||||
if (existingTask == null)
|
||||
{
|
||||
_dispatcher.Invoke(() => _floatingStatusWindow.SetText(Resources.ServiceNotInstalled));
|
||||
return;
|
||||
}
|
||||
|
||||
if (existingTask.State != TaskState.Running)
|
||||
{
|
||||
_dispatcher.Invoke(() => _floatingStatusWindow.SetText(Resources.ServiceNotStarted));
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
var text = new StringBuilder();
|
||||
|
||||
Data.RefreshComputer();
|
||||
|
||||
foreach (var sensorEntry in Data.SensorEntries)
|
||||
{
|
||||
if (text.Length > 0)
|
||||
text.AppendLine();
|
||||
|
||||
text.Append($"{sensorEntry.Label}: {string.Format(sensorEntry.SensorValueFormat, sensorEntry.Sensor.Value)}");
|
||||
}
|
||||
|
||||
_dispatcher.Invoke(() => _floatingStatusWindow.SetText(text.ToString()));
|
||||
|
||||
_timer.Start();
|
||||
}
|
||||
|
||||
public string WindowSettings
|
||||
{
|
||||
get => Settings.Default.WindowSettings;
|
||||
set
|
||||
{
|
||||
Settings.Default.WindowSettings = value;
|
||||
Settings.Default.Save();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user