using System; using System.Collections; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Runtime.CompilerServices; using System.Text.Json.Serialization; using HardwareMonitorStatusWindow.Service; namespace HardwareMonitorStatusWindow.StatusWindow; public class SensorEntry : INotifyDataErrorInfo, INotifyPropertyChanged { private readonly DataErrorDictionary _dataErrorDictionary; public SensorEntry() { _dataErrorDictionary = new DataErrorDictionary(); _dataErrorDictionary.ErrorsChanged += DataErrorDictionaryErrorsChanged; } public string Label { get; set { if (!ValidateLabel(value)) return; SetField(ref field, value); } } public string HardwareId { get; set { SetField(ref field, value); OnPropertyChanged(nameof(Hardware)); } } public string SensorId { get; set { SetField(ref field, value); OnPropertyChanged(nameof(Sensor)); } } [JsonIgnore] public Hardware? Hardware => Data.ComputerHardware.FirstOrDefault(h => h.Identifier.ToString() == HardwareId); [JsonIgnore] public Sensor? Sensor => Hardware?.Sensors.FirstOrDefault(s => s.Identifier.ToString() == SensorId); [JsonIgnore] public bool HasErrors => _dataErrorDictionary.Any(); public IEnumerable GetErrors(string propertyName) { return _dataErrorDictionary.GetErrors(propertyName); } public event EventHandler ErrorsChanged; private void DataErrorDictionaryErrorsChanged(object sender, DataErrorsChangedEventArgs e) { ErrorsChanged?.Invoke(this, new DataErrorsChangedEventArgs(e.PropertyName)); } private bool ValidateLabel(string newValue) { _dataErrorDictionary.ClearErrors(nameof(Label)); if (!string.IsNullOrWhiteSpace(newValue)) return true; _dataErrorDictionary.AddError(nameof(Label), "Label cannot be empty"); return false; } public event PropertyChangedEventHandler PropertyChanged; 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) { if (EqualityComparer.Default.Equals(field, value)) return false; field = value; OnPropertyChanged(propertyName); return true; } public string SensorValueFormat { get { return Sensor?.Type switch { SensorType.Voltage => "{0:F3} V", SensorType.Current => "{0:F3} A", SensorType.Clock => "{0:F1} MHz", SensorType.Load => "{0:F1} %", SensorType.Temperature => "{0:F1} °C", SensorType.Fan => "{0:F0} RPM", SensorType.Flow => "{0:F1} L/h", SensorType.Control => "{0:F1} %", SensorType.Level => "{0:F1} %", SensorType.Power => "{0:F1} W", SensorType.Data => "{0:F1} GB", SensorType.SmallData => "{0:F1} MB", SensorType.Factor => "{0:F3}", SensorType.Frequency => "{0:F1} Hz", SensorType.Throughput => "{0:F1} B/s", SensorType.TimeSpan => "{0:g}", SensorType.Timing => "{0:F3} ns", SensorType.Energy => "{0:F0} mWh", SensorType.Noise => "{0:F0} dBA", SensorType.Conductivity => "{0:F1} µS/cm", SensorType.Humidity => "{0:F0} %", _ => string.Empty }; } } }