Hardware/HDD/SmartAttribute.cs
author moel.mich
Thu, 26 Jul 2012 06:50:56 +0000
changeset 376 df64b56ece65
parent 344 3145aadca3d2
permissions -rw-r--r--
Fixed an error where loading config files from previous versions would crash the application.
     1 /*
     2  
     3   This Source Code Form is subject to the terms of the Mozilla Public
     4   License, v. 2.0. If a copy of the MPL was not distributed with this
     5   file, You can obtain one at http://mozilla.org/MPL/2.0/.
     6  
     7   Copyright (C) 2011-2012 Michael Möller <mmoeller@openhardwaremonitor.org>
     8   Copyright (C) 2011 Roland Reinl <roland-reinl@gmx.de>
     9 	
    10 */
    11 
    12 using System;
    13 using System.Collections.Generic;
    14 using OpenHardwareMonitor.Collections;
    15 
    16 namespace OpenHardwareMonitor.Hardware.HDD {
    17   internal class SmartAttribute {
    18 
    19     private RawValueConversion rawValueConversion;
    20 
    21     /// <summary>
    22     /// Initializes a new instance of the <see cref="SmartAttribute"/> class.
    23     /// </summary>
    24     /// <param name="identifier">The SMART identifier of the attribute.</param>
    25     /// <param name="name">The name of the attribute.</param>
    26     public SmartAttribute(byte identifier, string name) : 
    27       this(identifier, name, null, null, 0) { }
    28 
    29     /// <summary>
    30     /// Initializes a new instance of the <see cref="SmartAttribute"/> class.
    31     /// </summary>
    32     /// <param name="identifier">The SMART identifier of the attribute.</param>
    33     /// <param name="name">The name of the attribute.</param>
    34     /// <param name="rawValueConversion">A delegate for converting the raw byte 
    35     /// array into a value (or null to use the attribute value).</param>
    36     public SmartAttribute(byte identifier, string name,
    37       RawValueConversion rawValueConversion) :
    38       this(identifier, name, rawValueConversion, null, 0) { }
    39 
    40     /// <summary>
    41     /// Initializes a new instance of the <see cref="SmartAttribute"/> class.
    42     /// </summary>
    43     /// <param name="identifier">The SMART identifier of the attribute.</param>
    44     /// <param name="name">The name of the attribute.</param>
    45     /// <param name="rawValueConversion">A delegate for converting the raw byte 
    46     /// array into a value (or null to use the attribute value).</param>
    47     /// <param name="sensorType">Type of the sensor or null if no sensor is to 
    48     /// be created.</param>
    49     /// <param name="sensorChannel">If there exists more than one attribute with 
    50     /// the same sensor channel and type, then a sensor is created only for the  
    51     /// first attribute.</param>
    52     /// <param name="defaultHiddenSensor">True to hide the sensor initially.</param>
    53     /// <param name="parameterDescriptions">Description for the parameters of the sensor 
    54     /// (or null).</param>
    55     public SmartAttribute(byte identifier, string name,
    56       RawValueConversion rawValueConversion, SensorType? sensorType, 
    57       int sensorChannel, bool defaultHiddenSensor = false,
    58       ParameterDescription[] parameterDescriptions = null) 
    59     {
    60       this.Identifier = identifier;
    61       this.Name = name;
    62       this.rawValueConversion = rawValueConversion;
    63       this.SensorType = sensorType;
    64       this.SensorChannel = sensorChannel;
    65       this.DefaultHiddenSensor = defaultHiddenSensor;
    66       this.ParameterDescriptions = parameterDescriptions;
    67     }
    68 
    69     /// <summary>
    70     /// Gets the SMART identifier.
    71     /// </summary>
    72     public byte Identifier { get; private set; }
    73 
    74     public string Name { get; private set; }
    75 
    76     public SensorType? SensorType { get; private set; }
    77 
    78     public int SensorChannel { get; private set; }
    79 
    80     public bool DefaultHiddenSensor { get; private set; }
    81 
    82     public ParameterDescription[] ParameterDescriptions { get; private set; }
    83 
    84     public bool HasRawValueConversion {
    85       get {
    86         return rawValueConversion != null;
    87       }
    88     }
    89 
    90     public float ConvertValue(DriveAttributeValue value, 
    91       IReadOnlyArray<IParameter> parameters) 
    92     {
    93       if (rawValueConversion == null) {
    94         return value.AttrValue;
    95       } else {
    96         return rawValueConversion(value.RawValue, value.AttrValue, parameters);
    97       }
    98     }
    99 
   100     public delegate float RawValueConversion(byte[] rawValue, byte value,
   101       IReadOnlyArray<IParameter> parameters);
   102   }
   103 }