Hardware/HDD/HDDGroup.cs
author moel.mich
Sat, 02 Oct 2010 18:15:46 +0000
changeset 206 1fa8eddc24a7
parent 204 59278dadc5c0
child 218 194186efdde9
permissions -rw-r--r--
Replaced HttpUtility.UrlEncode with Uri.EscapeDataString and deleted the reference to the System.Web assembly. The System.Web assembly seems to be missing on some .NET 4.0 installations (and the overhead of using it is a bit large, just for the UrlEncode method).
moel@1
     1
/*
moel@1
     2
  
moel@1
     3
  Version: MPL 1.1/GPL 2.0/LGPL 2.1
moel@1
     4
moel@1
     5
  The contents of this file are subject to the Mozilla Public License Version
moel@1
     6
  1.1 (the "License"); you may not use this file except in compliance with
moel@1
     7
  the License. You may obtain a copy of the License at
moel@1
     8
 
moel@1
     9
  http://www.mozilla.org/MPL/
moel@1
    10
moel@1
    11
  Software distributed under the License is distributed on an "AS IS" basis,
moel@1
    12
  WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
moel@1
    13
  for the specific language governing rights and limitations under the License.
moel@1
    14
moel@1
    15
  The Original Code is the Open Hardware Monitor code.
moel@1
    16
moel@1
    17
  The Initial Developer of the Original Code is 
moel@1
    18
  Michael Möller <m.moeller@gmx.ch>.
moel@1
    19
  Portions created by the Initial Developer are Copyright (C) 2009-2010
moel@1
    20
  the Initial Developer. All Rights Reserved.
moel@1
    21
paulwerelds@204
    22
  Contributor(s): Paul Werelds
moel@1
    23
moel@1
    24
  Alternatively, the contents of this file may be used under the terms of
moel@1
    25
  either the GNU General Public License Version 2 or later (the "GPL"), or
moel@1
    26
  the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
moel@1
    27
  in which case the provisions of the GPL or the LGPL are applicable instead
moel@1
    28
  of those above. If you wish to allow use of your version of this file only
moel@1
    29
  under the terms of either the GPL or the LGPL, and not to allow others to
moel@1
    30
  use your version of this file under the terms of the MPL, indicate your
moel@1
    31
  decision by deleting the provisions above and replace them with the notice
moel@1
    32
  and other provisions required by the GPL or the LGPL. If you do not delete
moel@1
    33
  the provisions above, a recipient may use your version of this file under
moel@1
    34
  the terms of any one of the MPL, the GPL or the LGPL.
moel@1
    35
 
moel@1
    36
*/
moel@1
    37
moel@1
    38
using System;
moel@1
    39
using System.Collections.Generic;
moel@205
    40
using System.Globalization;
paulwerelds@204
    41
using System.Text;
moel@1
    42
moel@1
    43
namespace OpenHardwareMonitor.Hardware.HDD {
moel@165
    44
  internal class HDDGroup : IGroup {
moel@1
    45
moel@1
    46
    private const int MAX_DRIVES = 32;
moel@1
    47
moel@195
    48
    private readonly List<HDD> hardware = new List<HDD>();
moel@1
    49
moel@165
    50
    public HDDGroup(ISettings settings) {
paulwerelds@204
    51
      int p = (int)Environment.OSVersion.Platform;
paulwerelds@204
    52
      if (p == 4 || p == 128) return;
moel@1
    53
paulwerelds@204
    54
      for (int drive = 0; drive < MAX_DRIVES; drive++) {
paulwerelds@204
    55
        IntPtr handle = SMART.OpenPhysicalDrive(drive);
moel@1
    56
paulwerelds@204
    57
        if (handle == SMART.INVALID_HANDLE_VALUE)
paulwerelds@204
    58
          continue;
paulwerelds@204
    59
paulwerelds@204
    60
        if (!SMART.EnableSmart(handle, drive)) {
paulwerelds@204
    61
          SMART.CloseHandle(handle);
paulwerelds@204
    62
          continue;
paulwerelds@204
    63
        }
paulwerelds@204
    64
paulwerelds@204
    65
        string name = SMART.ReadName(handle, drive);
paulwerelds@204
    66
        if (name == null) {
paulwerelds@204
    67
          SMART.CloseHandle(handle);
paulwerelds@204
    68
          continue;
paulwerelds@204
    69
        }
paulwerelds@204
    70
paulwerelds@204
    71
        SMART.DriveAttribute[] attributes = SMART.ReadSmart(handle, drive);
moel@205
    72
        if (attributes == null) {
moel@205
    73
          SMART.CloseHandle(handle);
moel@205
    74
          continue;
moel@205
    75
        }
paulwerelds@204
    76
moel@205
    77
        int attribute = -1;
paulwerelds@204
    78
moel@205
    79
        // search for the Temperature attribute
moel@205
    80
        for (int i = 0; i < attributes.Length; i++)
moel@205
    81
          if (attributes[i].ID == SMART.AttributeID.Temperature) {
moel@205
    82
            attribute = i;
moel@205
    83
            break;
moel@205
    84
          }
moel@205
    85
moel@205
    86
        // if no temperature attribute is found, search for DriveTemperature
moel@205
    87
        if (attribute == -1)
moel@205
    88
          for (int i = 0; i < attributes.Length; i++)
moel@205
    89
            if (attributes[i].ID == SMART.AttributeID.DriveTemperature) {
paulwerelds@204
    90
              attribute = i;
paulwerelds@204
    91
              break;
paulwerelds@204
    92
            }
paulwerelds@204
    93
moel@205
    94
        // if no temperature attribute is found, search for AirflowTemperature
moel@205
    95
        if (attribute == -1)
moel@205
    96
          for (int i = 0; i < attributes.Length; i++)
moel@205
    97
            if (attributes[i].ID == SMART.AttributeID.AirflowTemperature) {
moel@205
    98
              attribute = i;
moel@205
    99
              break;
moel@205
   100
            }
moel@205
   101
moel@205
   102
        if (attribute >= 0) {
moel@205
   103
          hardware.Add(new HDD(name, handle, drive, attribute, settings));
moel@205
   104
          continue;
paulwerelds@204
   105
        }
moel@1
   106
paulwerelds@204
   107
        SMART.CloseHandle(handle);
moel@1
   108
      }
moel@1
   109
    }
moel@1
   110
moel@1
   111
    public IHardware[] Hardware {
moel@1
   112
      get {
moel@1
   113
        return hardware.ToArray();
moel@1
   114
      }
moel@1
   115
    }
moel@1
   116
moel@1
   117
    public string GetReport() {
paulwerelds@204
   118
      int p = (int)Environment.OSVersion.Platform;
paulwerelds@204
   119
      if (p == 4 || p == 128) return null;
paulwerelds@204
   120
paulwerelds@204
   121
      StringBuilder r = new StringBuilder();
paulwerelds@204
   122
paulwerelds@204
   123
      r.AppendLine("S.M.A.R.T Data");
paulwerelds@204
   124
      r.AppendLine();
paulwerelds@204
   125
paulwerelds@204
   126
      for (int drive = 0; drive < MAX_DRIVES; drive++) {
paulwerelds@204
   127
        IntPtr handle = SMART.OpenPhysicalDrive(drive);
paulwerelds@204
   128
paulwerelds@204
   129
        if (handle == SMART.INVALID_HANDLE_VALUE)
paulwerelds@204
   130
          continue;
paulwerelds@204
   131
paulwerelds@204
   132
        if (!SMART.EnableSmart(handle, drive)) {
paulwerelds@204
   133
          SMART.CloseHandle(handle);
paulwerelds@204
   134
          continue;
paulwerelds@204
   135
        }
paulwerelds@204
   136
paulwerelds@204
   137
        string name = SMART.ReadName(handle, drive);
paulwerelds@204
   138
        if (name == null) {
paulwerelds@204
   139
          SMART.CloseHandle(handle);
paulwerelds@204
   140
          continue;
paulwerelds@204
   141
        }
paulwerelds@204
   142
paulwerelds@204
   143
        SMART.DriveAttribute[] attributes = SMART.ReadSmart(handle, drive);
paulwerelds@204
   144
paulwerelds@204
   145
        if (attributes != null) {
paulwerelds@204
   146
          r.AppendLine("Drive name: " + name);
paulwerelds@204
   147
          r.AppendLine();
moel@205
   148
          r.AppendFormat(CultureInfo.InvariantCulture, " {0}{1}{2}{3}{4}{5}",
moel@205
   149
            ("ID").PadRight(6),
moel@205
   150
            ("RawValue").PadRight(20),
moel@205
   151
            ("WorstValue").PadRight(12),
moel@205
   152
            ("AttrValue").PadRight(12),
moel@205
   153
            ("Name"),
moel@205
   154
            Environment.NewLine);
paulwerelds@204
   155
moel@205
   156
          foreach (SMART.DriveAttribute a in attributes) {
moel@205
   157
            if (a.ID == 0) continue;
moel@205
   158
            string raw = BitConverter.ToString(a.RawValue);
moel@205
   159
            r.AppendFormat(CultureInfo.InvariantCulture, " {0}{1}{2}{3}{4}{5}",
moel@205
   160
              a.ID.ToString("d").PadRight(6), 
moel@205
   161
              raw.Replace("-", " ").PadRight(20),
moel@205
   162
              a.WorstValue.ToString(CultureInfo.InvariantCulture).PadRight(12),
moel@205
   163
              a.AttrValue.ToString(CultureInfo.InvariantCulture).PadRight(12),
moel@205
   164
              a.ID,
moel@205
   165
              Environment.NewLine);
paulwerelds@204
   166
          }
paulwerelds@204
   167
          r.AppendLine();
paulwerelds@204
   168
        }
paulwerelds@204
   169
paulwerelds@204
   170
        SMART.CloseHandle(handle);
paulwerelds@204
   171
      }
paulwerelds@204
   172
paulwerelds@204
   173
      return r.ToString();
moel@1
   174
    }
moel@1
   175
moel@1
   176
    public void Close() {
moel@1
   177
      foreach (HDD hdd in hardware) 
moel@1
   178
        hdd.Close();
moel@1
   179
    }
moel@1
   180
  }
moel@1
   181
}