Added used space load sensors for hard drives.
authormoel.mich
Sun, 22 Jul 2012 18:07:11 +0000
changeset 3695077ed7ddca8
parent 368 1036b453f1f6
child 370 8e4dedc41924
Added used space load sensors for hard drives.
Hardware/HDD/AbstractHarddrive.cs
Hardware/HDD/DebugSmart.cs
Hardware/HDD/ISmart.cs
Hardware/HDD/WindowsSmart.cs
Properties/AssemblyVersion.cs
     1.1 --- a/Hardware/HDD/AbstractHarddrive.cs	Wed Jul 18 22:47:30 2012 +0000
     1.2 +++ b/Hardware/HDD/AbstractHarddrive.cs	Sun Jul 22 18:07:11 2012 +0000
     1.3 @@ -13,6 +13,7 @@
     1.4  using System;
     1.5  using System.Collections.Generic;
     1.6  using System.Globalization;
     1.7 +using System.IO;
     1.8  using System.Text;
     1.9  using OpenHardwareMonitor.Collections;
    1.10  
    1.11 @@ -42,6 +43,9 @@
    1.12      private IList<SmartAttribute> smartAttributes;
    1.13      private IDictionary<SmartAttribute, Sensor> sensors;
    1.14  
    1.15 +    private DriveInfo[] driveInfos;
    1.16 +    private Sensor usageSensor;
    1.17 +
    1.18      protected AbstractHarddrive(ISmart smart, string name, 
    1.19        string firmwareRevision, int index, 
    1.20        IEnumerable<SmartAttribute> smartAttributes, ISettings settings) 
    1.21 @@ -59,6 +63,17 @@
    1.22  
    1.23        this.smartAttributes = new List<SmartAttribute>(smartAttributes);
    1.24  
    1.25 +      string[] logicalDrives = smart.GetLogicalDrives(index);
    1.26 +      List<DriveInfo> driveInfoList = new List<DriveInfo>(logicalDrives.Length);
    1.27 +      foreach (string logicalDrive in logicalDrives) {
    1.28 +        try {
    1.29 +          DriveInfo di = new DriveInfo(logicalDrive);
    1.30 +          if (di.TotalSize > 0) 
    1.31 +            driveInfoList.Add(new DriveInfo(logicalDrive));
    1.32 +        } catch (ArgumentException) { } catch (IOException) { }
    1.33 +      }
    1.34 +      driveInfos = driveInfoList.ToArray();
    1.35 +
    1.36        CreateSensors();
    1.37      }
    1.38  
    1.39 @@ -83,7 +98,7 @@
    1.40        smart.CloseHandle(deviceHandle);
    1.41  
    1.42        if (!nameValid || string.IsNullOrEmpty(name)) 
    1.43 -        return null;
    1.44 +        return null;      
    1.45  
    1.46        foreach (Type type in hddTypes) {
    1.47          // get the array of name prefixes for the current type
    1.48 @@ -161,6 +176,12 @@
    1.49            sensorTypeAndChannels.Add(pair);
    1.50          }     
    1.51        }
    1.52 +
    1.53 +      if (driveInfos.Length > 0) {
    1.54 +        usageSensor = 
    1.55 +          new Sensor("Used Space", 0, SensorType.Load, this, settings);
    1.56 +        ActivateSensor(usageSensor);
    1.57 +      }
    1.58      }
    1.59  
    1.60      public override HardwareType HardwareType {
    1.61 @@ -184,6 +205,16 @@
    1.62          }
    1.63  
    1.64          UpdateAdditionalSensors(values);
    1.65 +
    1.66 +        if (usageSensor != null) {
    1.67 +          long totalSize = 0;
    1.68 +          long totalFreeSpace = 0;
    1.69 +          for (int i = 0; i < driveInfos.Length; i++) {
    1.70 +            totalSize += driveInfos[i].TotalSize;
    1.71 +            totalFreeSpace += driveInfos[i].TotalFreeSpace;
    1.72 +          }
    1.73 +          usageSensor.Value = 100.0f - (100.0f * totalFreeSpace) / totalSize;
    1.74 +        }
    1.75        }
    1.76  
    1.77        count++; 
    1.78 @@ -201,7 +232,7 @@
    1.79          r.AppendLine();
    1.80          r.AppendLine("Drive name: " + name);
    1.81          r.AppendLine("Firmware version: " + firmwareRevision);
    1.82 -        r.AppendLine();
    1.83 +        r.AppendLine();    
    1.84          r.AppendFormat(CultureInfo.InvariantCulture, 
    1.85            " {0}{1}{2}{3}{4}{5}{6}{7}",
    1.86            ("ID").PadRight(3),
    1.87 @@ -253,6 +284,14 @@
    1.88          r.AppendLine();
    1.89        }
    1.90  
    1.91 +      foreach (DriveInfo di in driveInfos) {
    1.92 +        r.AppendLine("Logical drive name: " + di.Name);
    1.93 +        r.AppendLine("Format: " + di.DriveFormat);
    1.94 +        r.AppendLine("Total size: " + di.TotalSize);
    1.95 +        r.AppendLine("Total free space: " + di.TotalFreeSpace);
    1.96 +        r.AppendLine();
    1.97 +      }
    1.98 +
    1.99        return r.ToString();
   1.100      }
   1.101  
     2.1 --- a/Hardware/HDD/DebugSmart.cs	Wed Jul 18 22:47:30 2012 +0000
     2.2 +++ b/Hardware/HDD/DebugSmart.cs	Sun Jul 22 18:07:11 2012 +0000
     2.3 @@ -412,6 +412,10 @@
     2.4      }
     2.5  
     2.6      public IntPtr InvalidHandle { get { return (IntPtr)(-1); } }
     2.7 +
     2.8 +    public string[] GetLogicalDrives(int driveIndex) {
     2.9 +      return new string[0];
    2.10 +    }
    2.11    }
    2.12  
    2.13  #endif
     3.1 --- a/Hardware/HDD/ISmart.cs	Wed Jul 18 22:47:30 2012 +0000
     3.2 +++ b/Hardware/HDD/ISmart.cs	Sun Jul 22 18:07:11 2012 +0000
     3.3 @@ -30,5 +30,7 @@
     3.4      void CloseHandle(IntPtr handle);
     3.5  
     3.6      IntPtr InvalidHandle { get; }
     3.7 +
     3.8 +    string[] GetLogicalDrives(int driveIndex);
     3.9    }
    3.10  }
     4.1 --- a/Hardware/HDD/WindowsSmart.cs	Wed Jul 18 22:47:30 2012 +0000
     4.2 +++ b/Hardware/HDD/WindowsSmart.cs	Sun Jul 22 18:07:11 2012 +0000
     4.3 @@ -12,6 +12,7 @@
     4.4  
     4.5  using System;
     4.6  using System.Collections.Generic;
     4.7 +using System.Management;
     4.8  using System.Runtime.InteropServices;
     4.9  
    4.10  namespace OpenHardwareMonitor.Hardware.HDD {
    4.11 @@ -341,6 +342,23 @@
    4.12        NativeMethods.CloseHandle(handle);
    4.13      }
    4.14  
    4.15 +    public string[] GetLogicalDrives(int driveIndex) {
    4.16 +      List<string> list = new List<string>();
    4.17 +      try {
    4.18 +        using (ManagementObjectSearcher s = new ManagementObjectSearcher(
    4.19 +            "root\\CIMV2",
    4.20 +            "SELECT * FROM Win32_DiskPartition " +
    4.21 +            "WHERE DiskIndex = " + driveIndex))
    4.22 +        using (ManagementObjectCollection dpc = s.Get())
    4.23 +        foreach (ManagementObject dp in dpc) 
    4.24 +          using (ManagementObjectCollection ldc = 
    4.25 +            dp.GetRelated("Win32_LogicalDisk"))
    4.26 +          foreach (ManagementBaseObject ld in ldc) 
    4.27 +            list.Add(((string)ld["Name"]).TrimEnd(':')); 
    4.28 +      } catch { }
    4.29 +      return list.ToArray();
    4.30 +    }
    4.31 +
    4.32      protected static class NativeMethods {
    4.33        private const string KERNEL = "kernel32.dll";
    4.34  
     5.1 --- a/Properties/AssemblyVersion.cs	Wed Jul 18 22:47:30 2012 +0000
     5.2 +++ b/Properties/AssemblyVersion.cs	Sun Jul 22 18:07:11 2012 +0000
     5.3 @@ -10,5 +10,5 @@
     5.4  
     5.5  using System.Reflection;
     5.6  
     5.7 -[assembly: AssemblyVersion("0.4.0.20")]
     5.8 -[assembly: AssemblyInformationalVersion("0.4.0.20 Alpha")]
     5.9 \ No newline at end of file
    5.10 +[assembly: AssemblyVersion("0.4.0.21")]
    5.11 +[assembly: AssemblyInformationalVersion("0.4.0.21 Alpha")]
    5.12 \ No newline at end of file