diff --git a/HSMonitor/ViewModels/Settings/ConnectionSettingsTabViewModel.cs b/HSMonitor/ViewModels/Settings/ConnectionSettingsTabViewModel.cs index c740939..484de37 100644 --- a/HSMonitor/ViewModels/Settings/ConnectionSettingsTabViewModel.cs +++ b/HSMonitor/ViewModels/Settings/ConnectionSettingsTabViewModel.cs @@ -1,14 +1,17 @@ using System.Collections.Generic; using System.IO.Ports; using System.Linq; +using HSMonitor.Models; using HSMonitor.Services; +using HSMonitor.Utils.Serial; +using HSMonitor.Utils.Usb.Serial; namespace HSMonitor.ViewModels.Settings; public class ConnectionSettingsTabViewModel : SettingsTabBaseViewModel { - private IEnumerable _availablePorts = SerialPort.GetPortNames(); - public IEnumerable AvailablePorts + private IEnumerable _availablePorts = Serial.GetPorts(); + public IEnumerable AvailablePorts { get => _availablePorts; private set @@ -35,10 +38,12 @@ private set 921600 }; - public string SelectedPort + public DeviceInfo SelectedDevice { - get => SettingsService.Settings.LastSelectedPort ?? "COM1"; - set => SettingsService.Settings.LastSelectedPort = value; + get => + AvailablePorts.FirstOrDefault(p => p.PortName == + (SettingsService.Settings.LastSelectedPort ?? "COM1")) ?? new DeviceInfo(); + set => SettingsService.Settings.LastSelectedPort = value.PortName; } public int SelectedBaudRate @@ -61,14 +66,12 @@ public int DeviceDisplayBrightness public void UpdateAvailablePorts() { - AvailablePorts = new List(); - AvailablePorts = SerialPort.GetPortNames(); + AvailablePorts = new List(); + AvailablePorts = Serial.GetPorts(); } - public ConnectionSettingsTabViewModel(SettingsService settingsService) + public ConnectionSettingsTabViewModel(SettingsService settingsService, Serial serial) : base(settingsService, 0, "Connection") { - if (SettingsService is {Settings: not null}) - SelectedPort = SettingsService.Settings.LastSelectedPort ?? (AvailablePorts.FirstOrDefault() ?? "COM1"); } } \ No newline at end of file diff --git a/HSMonitor/ViewModels/Settings/HardwareSettingsTabViewModel.cs b/HSMonitor/ViewModels/Settings/HardwareSettingsTabViewModel.cs index 0f1574f..676522b 100644 --- a/HSMonitor/ViewModels/Settings/HardwareSettingsTabViewModel.cs +++ b/HSMonitor/ViewModels/Settings/HardwareSettingsTabViewModel.cs @@ -2,6 +2,7 @@ using System.Linq; using HSMonitor.Services; using LibreHardwareMonitor.Hardware; +using LibreHardwareMonitor.Hardware.Cpu; namespace HSMonitor.ViewModels.Settings; @@ -9,47 +10,37 @@ public class HardwareSettingsTabViewModel : SettingsTabBaseViewModel { private readonly SettingsService _settingsService; - public IEnumerable Processors => HardwareMonitorService.GetProcessors(); + public List Processors => HardwareMonitorService.GetProcessors().ToList(); - public IEnumerable GraphicCards => HardwareMonitorService.GetGraphicCards(); + public List GraphicCards => HardwareMonitorService.GetGraphicCards().ToList(); public IHardware SelectedCpu { - get => HardwareMonitorService - .GetProcessors() - .FirstOrDefault(c => - c.Identifier - .ToString() - .Contains(_settingsService.Settings.CpuId ?? "")) - ?? HardwareMonitorService.GetProcessors().First(); - set => _settingsService.Settings.CpuId = - (HardwareMonitorService - .GetProcessors() - .FirstOrDefault(c => - c.Identifier - .ToString() - .Contains(value.Identifier.ToString())) - ?? HardwareMonitorService.GetProcessors().First()).Identifier - .ToString(); + get => + Processors + .FirstOrDefault(c => + c.Identifier.ToString()!.Contains(_settingsService.Settings.CpuId ?? "")) + ?? Processors.First(); + set => + _settingsService.Settings.CpuId = + (Processors + .FirstOrDefault(c => + c.Identifier.ToString()!.Contains(value.Identifier.ToString() ?? string.Empty)) + ?? Processors.First()).Identifier + .ToString(); } public IHardware SelectedGpu { - get => HardwareMonitorService - .GetGraphicCards() + get => GraphicCards .FirstOrDefault(c => - c.Identifier - .ToString() - .Contains(_settingsService.Settings.GpuId ?? "")) - ?? HardwareMonitorService.GetGraphicCards().First(); + c.Identifier.ToString()!.Contains(_settingsService.Settings.GpuId ?? "")) + ?? GraphicCards.First(); set => _settingsService.Settings.GpuId = - (HardwareMonitorService - .GetGraphicCards() + (GraphicCards .FirstOrDefault(c => - c.Identifier - .ToString() - .Contains(value.Identifier.ToString())) - ?? HardwareMonitorService.GetGraphicCards().First()).Identifier + c.Identifier.ToString()!.Contains(value.Identifier.ToString() ?? string.Empty)) + ?? GraphicCards.First()).Identifier .ToString(); } @@ -70,19 +61,19 @@ public HardwareSettingsTabViewModel(SettingsService settingsService) { _settingsService = settingsService; if (SettingsService is not {Settings: not null}) return; - SelectedCpu = HardwareMonitorService - .GetProcessors() - .FirstOrDefault(c => - c.Identifier - .ToString() - .Contains(_settingsService.Settings.CpuId ?? "")) - ?? HardwareMonitorService.GetProcessors().First(); - SelectedGpu = HardwareMonitorService - .GetGraphicCards() - .FirstOrDefault(c => - c.Identifier - .ToString() - .Contains(_settingsService.Settings.GpuId ?? "")) - ?? HardwareMonitorService.GetGraphicCards().First(); + if (Processors is {Count: > 0}) + { + SelectedCpu = Processors + .FirstOrDefault(c => + c.Identifier.ToString()!.Contains(_settingsService.Settings.CpuId ?? "")) + ?? Processors.First(); + } + if (GraphicCards is {Count: > 0}) + { + SelectedGpu = GraphicCards + .FirstOrDefault(c => + c.Identifier.ToString()!.Contains(_settingsService.Settings.GpuId ?? "")) + ?? GraphicCards.First(); + } } } \ No newline at end of file diff --git a/HSMonitor/Views/Settings/ConnectionSettingsTabView.xaml.cs b/HSMonitor/Views/Settings/ConnectionSettingsTabView.xaml.cs index 3c568cd..6478022 100644 --- a/HSMonitor/Views/Settings/ConnectionSettingsTabView.xaml.cs +++ b/HSMonitor/Views/Settings/ConnectionSettingsTabView.xaml.cs @@ -1,6 +1,4 @@ -using System.Text.RegularExpressions; -using System.Windows.Controls; -using System.Windows.Input; +using System.Windows.Controls; namespace HSMonitor.Views.Settings;