1.1 --- a/Hardware/HDD/HDDGroup.cs Fri Oct 01 19:01:09 2010 +0000
1.2 +++ b/Hardware/HDD/HDDGroup.cs Sat Oct 02 14:39:25 2010 +0000
1.3 @@ -19,7 +19,7 @@
1.4 Portions created by the Initial Developer are Copyright (C) 2009-2010
1.5 the Initial Developer. All Rights Reserved.
1.6
1.7 - Contributor(s):
1.8 + Contributor(s): Paul Werelds
1.9
1.10 Alternatively, the contents of this file may be used under the terms of
1.11 either the GNU General Public License Version 2 or later (the "GPL"), or
1.12 @@ -37,6 +37,7 @@
1.13
1.14 using System;
1.15 using System.Collections.Generic;
1.16 +using System.Text;
1.17
1.18 namespace OpenHardwareMonitor.Hardware.HDD {
1.19 internal class HDDGroup : IGroup {
1.20 @@ -45,59 +46,56 @@
1.21
1.22 private readonly List<HDD> hardware = new List<HDD>();
1.23
1.24 + private readonly Dictionary<string, SMART.DriveAttribute[]> ignoredDrives = new Dictionary<string, SMART.DriveAttribute[]>();
1.25 +
1.26 public HDDGroup(ISettings settings) {
1.27 + int p = (int)Environment.OSVersion.Platform;
1.28 + if (p == 4 || p == 128) return;
1.29
1.30 - int p = (int)Environment.OSVersion.Platform;
1.31 - if ((p != 4) && (p != 128)) {
1.32 - for (int drive = 0; drive < MAX_DRIVES; drive++) {
1.33 - IntPtr handle = SMART.OpenPhysicalDrive(drive);
1.34 + for (int drive = 0; drive < MAX_DRIVES; drive++) {
1.35 + IntPtr handle = SMART.OpenPhysicalDrive(drive);
1.36
1.37 - if (handle == SMART.INVALID_HANDLE_VALUE)
1.38 + if (handle == SMART.INVALID_HANDLE_VALUE)
1.39 + continue;
1.40 +
1.41 + if (!SMART.EnableSmart(handle, drive)) {
1.42 + SMART.CloseHandle(handle);
1.43 + continue;
1.44 + }
1.45 +
1.46 + string name = SMART.ReadName(handle, drive);
1.47 + if (name == null) {
1.48 + SMART.CloseHandle(handle);
1.49 + continue;
1.50 + }
1.51 +
1.52 + SMART.DriveAttribute[] attributes = SMART.ReadSmart(handle, drive);
1.53 +
1.54 + if (attributes != null) {
1.55 + int attribute = -1;
1.56 +
1.57 + int i = 0;
1.58 + foreach (SMART.DriveAttribute attr in attributes) {
1.59 + if (attr.ID == SMART.AttributeID.Temperature
1.60 + || attr.ID == SMART.AttributeID.DriveTemperature
1.61 + || attr.ID == SMART.AttributeID.AirflowTemperature) {
1.62 + attribute = i;
1.63 + break;
1.64 + }
1.65 + i++;
1.66 + }
1.67 +
1.68 + if (attribute >= 0)
1.69 + {
1.70 + hardware.Add(new HDD(name, handle, drive, attribute, settings));
1.71 continue;
1.72 + }
1.73 + }
1.74
1.75 - if (SMART.EnableSmart(handle, drive)) {
1.76 -
1.77 - string name = SMART.ReadName(handle, drive);
1.78 - if (name != null) {
1.79 -
1.80 - SMART.DriveAttribute[] attributes =
1.81 - SMART.ReadSmart(handle, drive);
1.82 -
1.83 - if (attributes == null)
1.84 - continue;
1.85 -
1.86 - int attribute = -1;
1.87 - for (int i = 0; i < attributes.Length; i++)
1.88 - if (attributes[i].ID == SMART.AttributeID.Temperature) {
1.89 - attribute = i;
1.90 - break;
1.91 - }
1.92 - if (attribute == -1)
1.93 - for (int i = 0; i < attributes.Length; i++)
1.94 - if (attributes[i].ID == SMART.AttributeID.DriveTemperature) {
1.95 - attribute = i;
1.96 - break;
1.97 - }
1.98 - if (attribute == -1)
1.99 - for (int i = 0; i < attributes.Length; i++)
1.100 - if (attributes[i].ID == SMART.AttributeID.AirflowTemperature)
1.101 - {
1.102 - attribute = i;
1.103 - break;
1.104 - }
1.105 -
1.106 - if (attribute >= 0) {
1.107 - hardware.Add(new HDD(name, handle, drive, attribute, settings));
1.108 - continue;
1.109 - }
1.110 - }
1.111 - }
1.112 - SMART.CloseHandle(handle);
1.113 - }
1.114 + SMART.CloseHandle(handle);
1.115 }
1.116 }
1.117
1.118 -
1.119 public IHardware[] Hardware {
1.120 get {
1.121 return hardware.ToArray();
1.122 @@ -105,7 +103,63 @@
1.123 }
1.124
1.125 public string GetReport() {
1.126 - return null;
1.127 + int p = (int)Environment.OSVersion.Platform;
1.128 + if (p == 4 || p == 128) return null;
1.129 +
1.130 + StringBuilder r = new StringBuilder();
1.131 +
1.132 + r.AppendLine("S.M.A.R.T Data");
1.133 + r.AppendLine();
1.134 +
1.135 + for (int drive = 0; drive < MAX_DRIVES; drive++) {
1.136 + IntPtr handle = SMART.OpenPhysicalDrive(drive);
1.137 +
1.138 + if (handle == SMART.INVALID_HANDLE_VALUE)
1.139 + continue;
1.140 +
1.141 + if (!SMART.EnableSmart(handle, drive)) {
1.142 + SMART.CloseHandle(handle);
1.143 + continue;
1.144 + }
1.145 +
1.146 + string name = SMART.ReadName(handle, drive);
1.147 + if (name == null) {
1.148 + SMART.CloseHandle(handle);
1.149 + continue;
1.150 + }
1.151 +
1.152 + SMART.DriveAttribute[] attributes = SMART.ReadSmart(handle, drive);
1.153 +
1.154 + if (attributes != null) {
1.155 + r.AppendLine("Drive name: " + name);
1.156 + r.AppendLine();
1.157 + r.AppendFormat(" {0}{1}{2}{3}{4}{5}",
1.158 + ("ID").PadRight(6),
1.159 + ("RawValue").PadRight(20),
1.160 + ("WorstValue").PadRight(12),
1.161 + ("AttrValue").PadRight(12),
1.162 + ("Name"),
1.163 + Environment.NewLine);
1.164 +
1.165 + foreach (SMART.DriveAttribute attr in attributes) {
1.166 + if (attr.ID == 0) continue;
1.167 + string raw = BitConverter.ToString(attr.RawValue);
1.168 + r.AppendFormat(" {0}{1}{2}{3}{4}{5}",
1.169 + attr.ID.ToString("d").PadRight(6),
1.170 + raw.Replace("-", " ").PadRight(20),
1.171 + attr.WorstValue.ToString().PadRight(12),
1.172 + attr.AttrValue.ToString().PadRight(12),
1.173 + attr.ID,
1.174 + Environment.NewLine)
1.175 + ;
1.176 + }
1.177 + r.AppendLine();
1.178 + }
1.179 +
1.180 + SMART.CloseHandle(handle);
1.181 + }
1.182 +
1.183 + return r.ToString();
1.184 }
1.185
1.186 public void Close() {