All checks were successful
Deploy to Gitea Releases / deploy-to-gitea-releases (push) Successful in 52s
254 lines
6.8 KiB
C#
254 lines
6.8 KiB
C#
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();
|
|
|
|
Log.Information("Checking for task name: {name}", HardwareMonitorService.ScheduledTaskName);
|
|
|
|
var existingTask = taskService.FindTask(HardwareMonitorService.ScheduledTaskName);
|
|
|
|
Log.Information("Task: {existingTask}", existingTask);
|
|
|
|
if (existingTask == null)
|
|
{
|
|
var assembly = Assembly.GetExecutingAssembly();
|
|
|
|
var path = Path.GetDirectoryName(assembly.Location);
|
|
|
|
Log.Information("Service path: {path}", path);
|
|
|
|
if (path != null)
|
|
{
|
|
var fileName = Path.Combine(path, "HardwareMonitorService.exe");
|
|
|
|
Log.Information("Full service path: {fileName}", fileName);
|
|
|
|
var startInfo = new ProcessStartInfo
|
|
{
|
|
FileName = fileName,
|
|
Arguments = "--install",
|
|
UseShellExecute = true,
|
|
Verb = "runas"
|
|
};
|
|
|
|
Log.Information("Starting process");
|
|
|
|
Process.Start(startInfo);
|
|
}
|
|
}
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
// Ignored
|
|
Log.Error(exception, "");
|
|
}
|
|
|
|
_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
|
|
{
|
|
Log.Information("IsInstalled: {isInstalled}", UpdateCheck.IsInstalled);
|
|
|
|
if (!UpdateCheck.IsInstalled)
|
|
return false;
|
|
|
|
Log.Information("CheckVersionAtStartup: {checkVersionAtStartup}", Settings.Default.CheckVersionAtStartup);
|
|
|
|
if (!Settings.Default.CheckVersionAtStartup)
|
|
return false;
|
|
|
|
await _dispatcher.InvokeAsync(() => _floatingStatusWindow.SetText(Resources.CheckingForUpdate));
|
|
|
|
Log.Information("Checking for updates");
|
|
|
|
var newVersion = await UpdateCheck.UpdateManager.CheckForUpdatesAsync();
|
|
|
|
Log.Information("New version: {version}", newVersion);
|
|
|
|
if (newVersion == null)
|
|
return false;
|
|
|
|
await _dispatcher.InvokeAsync(() => _floatingStatusWindow.SetText(Resources.DownloadingUpdate));
|
|
|
|
Log.Information("Downloading update");
|
|
|
|
await UpdateCheck.UpdateManager.DownloadUpdatesAsync(newVersion);
|
|
|
|
await _dispatcher.InvokeAsync(() => _floatingStatusWindow.SetText(Resources.InstallingUpdate));
|
|
|
|
Log.Information("Installing update");
|
|
|
|
UpdateCheck.UpdateManager.ApplyUpdatesAndRestart(newVersion);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Log.Error(e, nameof(UpdateApp));
|
|
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
private async Task Start(bool hasUpdate)
|
|
{
|
|
Log.Information("Start: hasUpdate={hasUpdate}", hasUpdate);
|
|
|
|
if (hasUpdate)
|
|
return;
|
|
|
|
Log.Information("Load");
|
|
|
|
await Load();
|
|
|
|
Log.Information("Starting timer");
|
|
|
|
_timer.Elapsed += HandleTimerElapsed;
|
|
_timer.AutoReset = false;
|
|
_timer.Enabled = true;
|
|
}
|
|
|
|
private static async Task Load()
|
|
{
|
|
await Data.LoadComputer();
|
|
|
|
Data.Load();
|
|
}
|
|
|
|
private static 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();
|
|
}
|
|
}
|
|
} |