Files
HardwareMonitorStatusWindow/StatusWindow/SettingsWindow/SensorWindow.xaml.cs
Chris Kaczor 5bb26c1c3f
All checks were successful
Deploy to Gitea Releases / deploy-to-gitea-releases (push) Successful in 48s
Improve startup
2026-02-27 16:06:23 -05:00

135 lines
4.3 KiB
C#

using System;
using ChrisKaczor.Wpf.Validation;
using HardwareMonitorStatusWindow.Service;
using System.Linq;
using System.Threading.Tasks;
using System.Timers;
using System.Windows;
using System.Windows.Threading;
namespace HardwareMonitorStatusWindow.StatusWindow.SettingsWindow;
public partial class SensorWindow
{
private readonly DispatcherTimer _timer = new();
public SensorWindow()
{
InitializeComponent();
}
public async Task<bool?> Display(SensorEntry sensorEntry, Window? owner)
{
DataContext = sensorEntry;
await Data.RefreshComputer();
HardwareTypeComboBox.ItemsSource = Data.ComputerHardware.Where(h => h.Sensors.Any()).DistinctBy(h => h.Type).Select(s => new HardwareTypeItem(s.Type)).OrderBy(s => s.Name);
var hardware = Data.ComputerHardware.FirstOrDefault(h => h.Identifier.ToString() == sensorEntry.HardwareId);
var sensor = hardware?.Sensors.FirstOrDefault(s => s.Identifier.ToString() == sensorEntry.SensorId);
HardwareTypeComboBox.SelectedValue = hardware?.Type;
HardwareComboBox.SelectedItem = hardware;
SensorTypeComboBox.SelectedValue = sensor?.Type;
SensorComboBox.SelectedItem = sensor;
Title = string.IsNullOrWhiteSpace(sensorEntry.Label) ? StatusWindow.Resources.SensorWindowAdd : StatusWindow.Resources.SensorWindowEdit;
Owner = owner;
_timer.Interval = TimeSpan.FromSeconds(1);
_timer.Tick += TimerOnTick;
_timer.Start();
return ShowDialog();
}
private void TimerOnTick(object? sender, EventArgs e)
{
UpdateCurrentSensorValue();
}
private void HandleOkayButtonClick(object sender, RoutedEventArgs e)
{
if (!this.IsValid())
return;
var sensorEntry = (SensorEntry)DataContext;
var hardware = (Hardware)HardwareComboBox.SelectedItem;
sensorEntry.HardwareId = hardware.Identifier;
var sensor = (Sensor)SensorComboBox.SelectedItem;
sensorEntry.SensorId = sensor.Identifier;
if (!Data.SensorEntries.Contains(sensorEntry))
Data.SensorEntries.Add(sensorEntry);
Data.Save();
DialogResult = true;
Close();
}
private void HardwareTypeComboBox_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
if (HardwareTypeComboBox.SelectedIndex == -1)
return;
var hardwareType = (HardwareTypeItem)HardwareTypeComboBox.SelectedItem;
HardwareComboBox.SelectedIndex = -1;
SensorTypeComboBox.SelectedIndex = -1;
SensorComboBox.SelectedIndex = -1;
HardwareComboBox.ItemsSource = Data.ComputerHardware.Where(h => h.Type == hardwareType.Value);
}
private void HardwareComboBox_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
if (HardwareComboBox.SelectedIndex == -1)
return;
var hardware = (Hardware)HardwareComboBox.SelectedItem;
SensorTypeComboBox.SelectedIndex = -1;
SensorComboBox.SelectedIndex = -1;
SensorTypeComboBox.ItemsSource = hardware.Sensors.DistinctBy(s => s.Type).Select(s => new SensorTypeItem(s.Type)).OrderBy(s => s.Name);
}
private void SensorTypeComboBox_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
if (SensorTypeComboBox.SelectedIndex == -1)
return;
var hardware = (Hardware)HardwareComboBox.SelectedItem;
var sensorType = (SensorTypeItem)SensorTypeComboBox.SelectedItem;
SensorComboBox.ItemsSource = hardware.Sensors.Where(s => s.Type == sensorType.Value);
}
private void SensorComboBox_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
UpdateCurrentSensorValue();
}
private void UpdateCurrentSensorValue()
{
var sensor = (Sensor)SensorComboBox.SelectedItem;
if (sensor?.Value == null)
{
CurrentValueTextBox.Text = string.Empty;
return;
}
var hardware = (Hardware)HardwareComboBox.SelectedItem;
var displaySensorEntry = new SensorEntry { HardwareId = hardware.Identifier, SensorId = sensor.Identifier };
CurrentValueTextBox.Text = string.Format(displaySensorEntry.SensorValueFormat, displaySensorEntry.Sensor!.Value);
}
}