// This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. // If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. // Copyright (C) LibreHardwareMonitor and Contributors. // Partial Copyright (C) Michael Möller and Contributors. // All Rights Reserved. using System.Collections.Generic; using LibreHardwareMonitor.Interop; namespace LibreHardwareMonitor.Hardware.Storage; public class SmartAttribute { private readonly RawValueConversion _rawValueConversion; public delegate float RawValueConversion(byte[] rawValue, byte value, IReadOnlyList parameters); /// /// Initializes a new instance of the class. /// /// The SMART id of the attribute. /// The name of the attribute. public SmartAttribute(byte id, string name) : this(id, name, null, null, 0, null) { } /// /// Initializes a new instance of the class. /// /// The SMART id of the attribute. /// The name of the attribute. /// /// A delegate for converting the raw byte /// array into a value (or null to use the attribute value). /// public SmartAttribute(byte id, string name, RawValueConversion rawValueConversion) : this(id, name, rawValueConversion, null, 0, null) { } /// /// Initializes a new instance of the class. /// /// The SMART id of the attribute. /// The name of the attribute. /// /// A delegate for converting the raw byte /// array into a value (or null to use the attribute value). /// /// /// Type of the sensor or null if no sensor is to /// be created. /// /// /// If there exists more than one attribute with /// the same sensor channel and type, then a sensor is created only for the /// first attribute. /// /// /// The name to be used for the sensor, or null if /// no sensor is created. /// /// True to hide the sensor initially. /// /// Description for the parameters of the sensor /// (or null). /// public SmartAttribute(byte id, string name, RawValueConversion rawValueConversion, SensorType? sensorType, int sensorChannel, string sensorName, bool defaultHiddenSensor = false, ParameterDescription[] parameterDescriptions = null) { Id = id; Name = name; _rawValueConversion = rawValueConversion; SensorType = sensorType; SensorChannel = sensorChannel; SensorName = sensorName; DefaultHiddenSensor = defaultHiddenSensor; ParameterDescriptions = parameterDescriptions; } public bool DefaultHiddenSensor { get; } public bool HasRawValueConversion => _rawValueConversion != null; /// /// Gets the SMART identifier. /// public byte Id { get; } public string Name { get; } public ParameterDescription[] ParameterDescriptions { get; } public int SensorChannel { get; } public string SensorName { get; } public SensorType? SensorType { get; } internal float ConvertValue(Kernel32.SMART_ATTRIBUTE value, IReadOnlyList parameters) { if (_rawValueConversion == null) return value.CurrentValue; return _rawValueConversion(value.RawValue, value.CurrentValue, parameters); } }