diff --git a/Service/Service.csproj b/Service/Service.csproj index c6e3835..e2023b3 100644 --- a/Service/Service.csproj +++ b/Service/Service.csproj @@ -11,9 +11,9 @@ - + - + diff --git a/StatusWindow/App.xaml.cs b/StatusWindow/App.xaml.cs index 503bf5e..4266d44 100644 --- a/StatusWindow/App.xaml.cs +++ b/StatusWindow/App.xaml.cs @@ -8,7 +8,7 @@ namespace HardwareMonitorStatusWindow.StatusWindow; public partial class App { - private List _windowSourceList; + private List? _windowSourceList; protected override void OnStartup(StartupEventArgs e) { @@ -30,7 +30,7 @@ public partial class App protected override void OnExit(ExitEventArgs e) { - _windowSourceList.ForEach(ws => ws.Dispose()); + _windowSourceList?.ForEach(ws => ws.Dispose()); base.OnExit(e); } diff --git a/StatusWindow/Data.cs b/StatusWindow/Data.cs index 3848542..b120b31 100644 --- a/StatusWindow/Data.cs +++ b/StatusWindow/Data.cs @@ -6,15 +6,16 @@ using System.Collections.ObjectModel; using System.Linq; using System.Text.Json; using System.Threading.Tasks; +using Serilog; namespace HardwareMonitorStatusWindow.StatusWindow; internal static class Data { - private static PipeClient _pipeClient; - private static IEnumerable _hardware; + private static PipeClient? _pipeClient; + private static IEnumerable? _hardware; - internal static ObservableCollection SensorEntries { get; set; } + internal static ObservableCollection SensorEntries { get; set; } = []; internal static async Task LoadComputer() { @@ -25,7 +26,7 @@ internal static class Data } catch (Exception exception) { - + Log.Error(exception, ""); } } @@ -36,14 +37,14 @@ internal static class Data internal static void CloseComputer() { - _pipeClient.Dispose(); + _pipeClient?.Dispose(); } - internal static IList ComputerHardware => _hardware.ToList(); + internal static IList ComputerHardware => _hardware?.ToList() ?? []; internal static void Load() { - SensorEntries = JsonSerializer.Deserialize>(Settings.Default.Sensors); + SensorEntries = JsonSerializer.Deserialize>(Settings.Default.Sensors) ?? []; } internal static void Save() diff --git a/StatusWindow/DataErrorDictionary.cs b/StatusWindow/DataErrorDictionary.cs index ed6f4d4..3bdcf63 100644 --- a/StatusWindow/DataErrorDictionary.cs +++ b/StatusWindow/DataErrorDictionary.cs @@ -7,15 +7,18 @@ namespace HardwareMonitorStatusWindow.StatusWindow; internal class DataErrorDictionary : Dictionary> { - public event EventHandler ErrorsChanged; + public event EventHandler? ErrorsChanged; private void OnErrorsChanged(string propertyName) { ErrorsChanged?.Invoke(this, new DataErrorsChangedEventArgs(propertyName)); } - public IEnumerable GetErrors(string propertyName) + public IEnumerable? GetErrors(string? propertyName) { + if (propertyName == null) + return null; + return TryGetValue(propertyName, out var value) ? value : null; } diff --git a/StatusWindow/SensorEntry.cs b/StatusWindow/SensorEntry.cs index 22b9ab3..8a50eeb 100644 --- a/StatusWindow/SensorEntry.cs +++ b/StatusWindow/SensorEntry.cs @@ -11,15 +11,14 @@ namespace HardwareMonitorStatusWindow.StatusWindow; public class SensorEntry : INotifyDataErrorInfo, INotifyPropertyChanged { - private readonly DataErrorDictionary _dataErrorDictionary; + private readonly DataErrorDictionary _dataErrorDictionary = new(); public SensorEntry() { - _dataErrorDictionary = new DataErrorDictionary(); _dataErrorDictionary.ErrorsChanged += DataErrorDictionaryErrorsChanged; } - public string Label + public string? Label { get; set @@ -31,7 +30,7 @@ public class SensorEntry : INotifyDataErrorInfo, INotifyPropertyChanged } } - public string HardwareId + public string? HardwareId { get; set @@ -41,7 +40,7 @@ public class SensorEntry : INotifyDataErrorInfo, INotifyPropertyChanged } } - public string SensorId + public string? SensorId { get; set @@ -52,7 +51,7 @@ public class SensorEntry : INotifyDataErrorInfo, INotifyPropertyChanged } [JsonIgnore] - public Hardware? Hardware => Data.ComputerHardware.FirstOrDefault(h => h.Identifier.ToString() == HardwareId); + public Hardware? Hardware => Data.ComputerHardware?.FirstOrDefault(h => h.Identifier.ToString() == HardwareId); [JsonIgnore] public Sensor? Sensor => Hardware?.Sensors.FirstOrDefault(s => s.Identifier.ToString() == SensorId); @@ -60,19 +59,22 @@ public class SensorEntry : INotifyDataErrorInfo, INotifyPropertyChanged [JsonIgnore] public bool HasErrors => _dataErrorDictionary.Any(); - public IEnumerable GetErrors(string propertyName) + public IEnumerable GetErrors(string? propertyName) { - return _dataErrorDictionary.GetErrors(propertyName); + if (string.IsNullOrWhiteSpace(propertyName)) + throw new InvalidOperationException(); + + return _dataErrorDictionary.GetErrors(propertyName) ?? Enumerable.Empty(); } - public event EventHandler ErrorsChanged; + public event EventHandler? ErrorsChanged; - private void DataErrorDictionaryErrorsChanged(object sender, DataErrorsChangedEventArgs e) + private void DataErrorDictionaryErrorsChanged(object? sender, DataErrorsChangedEventArgs e) { ErrorsChanged?.Invoke(this, new DataErrorsChangedEventArgs(e.PropertyName)); } - private bool ValidateLabel(string newValue) + private bool ValidateLabel(string? newValue) { _dataErrorDictionary.ClearErrors(nameof(Label)); @@ -84,14 +86,14 @@ public class SensorEntry : INotifyDataErrorInfo, INotifyPropertyChanged return false; } - public event PropertyChangedEventHandler PropertyChanged; + public event PropertyChangedEventHandler? PropertyChanged; - protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null) + protected virtual void OnPropertyChanged([CallerMemberName] string? propertyName = null) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } - protected bool SetField(ref T field, T value, [CallerMemberName] string propertyName = null) + protected bool SetField(ref T field, T value, [CallerMemberName] string? propertyName = null) { if (EqualityComparer.Default.Equals(field, value)) return false; field = value; diff --git a/StatusWindow/SettingsWindow/HardwareSettingsPanel.xaml b/StatusWindow/SettingsWindow/HardwareSettingsPanel.xaml index dfe9596..7db11df 100644 --- a/StatusWindow/SettingsWindow/HardwareSettingsPanel.xaml +++ b/StatusWindow/SettingsWindow/HardwareSettingsPanel.xaml @@ -58,12 +58,12 @@ - - @@ -75,12 +75,12 @@ - - diff --git a/StatusWindow/SettingsWindow/HardwareSettingsPanel.xaml.cs b/StatusWindow/SettingsWindow/HardwareSettingsPanel.xaml.cs index d5ea377..2f2163c 100644 --- a/StatusWindow/SettingsWindow/HardwareSettingsPanel.xaml.cs +++ b/StatusWindow/SettingsWindow/HardwareSettingsPanel.xaml.cs @@ -7,7 +7,7 @@ namespace HardwareMonitorStatusWindow.StatusWindow.SettingsWindow; public partial class HardwareSettingsPanel { - private CollectionViewSource _collectionViewSource; + private CollectionViewSource? _collectionViewSource; public HardwareSettingsPanel() { @@ -105,7 +105,7 @@ public partial class HardwareSettingsPanel SensorDataGrid.SelectedItems.CopyTo(selectedItems, 0); foreach (var sensorEntry in selectedItems) - Data.SensorEntries.Remove(sensorEntry); + Data.SensorEntries?.Remove(sensorEntry); SetSensorButtonStates(); } diff --git a/StatusWindow/SettingsWindow/SensorWindow.xaml.cs b/StatusWindow/SettingsWindow/SensorWindow.xaml.cs index b83d182..98be7e7 100644 --- a/StatusWindow/SettingsWindow/SensorWindow.xaml.cs +++ b/StatusWindow/SettingsWindow/SensorWindow.xaml.cs @@ -12,7 +12,7 @@ public partial class SensorWindow InitializeComponent(); } - public bool? Display(SensorEntry sensorEntry, Window owner) + public bool? Display(SensorEntry sensorEntry, Window? owner) { DataContext = sensorEntry; diff --git a/StatusWindow/SettingsWindow/UpdateSettingsPanel.xaml.cs b/StatusWindow/SettingsWindow/UpdateSettingsPanel.xaml.cs index b393e54..6aa3fab 100644 --- a/StatusWindow/SettingsWindow/UpdateSettingsPanel.xaml.cs +++ b/StatusWindow/SettingsWindow/UpdateSettingsPanel.xaml.cs @@ -1,6 +1,5 @@ using System.Windows; using System.Windows.Input; -using HardwareMonitorStatusWindow.StatusWindow; namespace HardwareMonitorStatusWindow.StatusWindow.SettingsWindow; diff --git a/StatusWindow/StatusWindow.csproj b/StatusWindow/StatusWindow.csproj index e523247..1ffdbe1 100644 --- a/StatusWindow/StatusWindow.csproj +++ b/StatusWindow/StatusWindow.csproj @@ -28,10 +28,10 @@ - + - + diff --git a/StatusWindow/WindowSource.cs b/StatusWindow/WindowSource.cs index a03f431..f38e306 100644 --- a/StatusWindow/WindowSource.cs +++ b/StatusWindow/WindowSource.cs @@ -2,6 +2,7 @@ using ChrisKaczor.Wpf.Windows.FloatingStatusWindow; using HardwareMonitorStatusWindow.Service; using HardwareMonitorStatusWindow.StatusWindow.SettingsWindow; +using LibreHardwareMonitor.PawnIo; using Microsoft.Win32.TaskScheduler; using Serilog; using System; @@ -23,13 +24,24 @@ 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(); + if (!PawnIo.IsInstalled) + { + Log.Information("PawnIO not installed"); + } + else + { + var pawnIoVersion = PawnIo.Version; + + Log.Information("PawnIO installed: {version}", pawnIoVersion); + } + Log.Information("Checking for task name: {name}", HardwareMonitorService.ScheduledTaskName); var existingTask = taskService.FindTask(HardwareMonitorService.ScheduledTaskName); @@ -231,6 +243,9 @@ internal class WindowSource : IWindowSource, IDisposable foreach (var sensorEntry in Data.SensorEntries) { + if (sensorEntry.Sensor == null) + continue; + if (text.Length > 0) text.AppendLine();