All checks were successful
Deploy to Gitea Releases / deploy-to-gitea-releases (push) Successful in 2m34s
- Pad labels
134 lines
3.9 KiB
C#
134 lines
3.9 KiB
C#
using HardwareMonitorStatusWindow.Service;
|
|
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel;
|
|
using System.Linq;
|
|
using System.Runtime.CompilerServices;
|
|
using System.Text.Json.Serialization;
|
|
|
|
namespace HardwareMonitorStatusWindow.StatusWindow;
|
|
|
|
public class SensorEntry : INotifyDataErrorInfo, INotifyPropertyChanged
|
|
{
|
|
private readonly DataErrorDictionary _dataErrorDictionary = new();
|
|
|
|
public SensorEntry()
|
|
{
|
|
_dataErrorDictionary.ErrorsChanged += DataErrorDictionaryErrorsChanged;
|
|
}
|
|
|
|
public string Label
|
|
{
|
|
get;
|
|
set
|
|
{
|
|
if (!ValidateLabel(value))
|
|
return;
|
|
|
|
SetField(ref field, value);
|
|
}
|
|
} = string.Empty;
|
|
|
|
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)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(propertyName))
|
|
throw new InvalidOperationException();
|
|
|
|
return _dataErrorDictionary.GetErrors(propertyName) ?? Enumerable.Empty<string>();
|
|
}
|
|
|
|
public event EventHandler<DataErrorsChangedEventArgs>? 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;
|
|
|
|
private void OnPropertyChanged([CallerMemberName] string? propertyName = null)
|
|
{
|
|
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
|
|
}
|
|
|
|
private void SetField<T>(ref T field, T value, [CallerMemberName] string? propertyName = null)
|
|
{
|
|
if (EqualityComparer<T>.Default.Equals(field, value)) return;
|
|
field = value;
|
|
OnPropertyChanged(propertyName);
|
|
}
|
|
|
|
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
|
|
};
|
|
}
|
|
}
|
|
} |