Hardware/Computer.cs
author moel.mich
Sun, 17 Oct 2010 16:04:19 +0000
changeset 228 458a6c3de579
parent 182 4801e9eaf979
child 236 763675f19ff4
permissions -rw-r--r--
Added code to read the additional fans on the ASRock P55 Deluxe mainboard via special GPIO switching.
     1 /*
     2   
     3   Version: MPL 1.1/GPL 2.0/LGPL 2.1
     4 
     5   The contents of this file are subject to the Mozilla Public License Version
     6   1.1 (the "License"); you may not use this file except in compliance with
     7   the License. You may obtain a copy of the License at
     8  
     9   http://www.mozilla.org/MPL/
    10 
    11   Software distributed under the License is distributed on an "AS IS" basis,
    12   WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
    13   for the specific language governing rights and limitations under the License.
    14 
    15   The Original Code is the Open Hardware Monitor code.
    16 
    17   The Initial Developer of the Original Code is 
    18   Michael Möller <m.moeller@gmx.ch>.
    19   Portions created by the Initial Developer are Copyright (C) 2009-2010
    20   the Initial Developer. All Rights Reserved.
    21 
    22   Contributor(s):
    23 
    24   Alternatively, the contents of this file may be used under the terms of
    25   either the GNU General Public License Version 2 or later (the "GPL"), or
    26   the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
    27   in which case the provisions of the GPL or the LGPL are applicable instead
    28   of those above. If you wish to allow use of your version of this file only
    29   under the terms of either the GPL or the LGPL, and not to allow others to
    30   use your version of this file under the terms of the MPL, indicate your
    31   decision by deleting the provisions above and replace them with the notice
    32   and other provisions required by the GPL or the LGPL. If you do not delete
    33   the provisions above, a recipient may use your version of this file under
    34   the terms of any one of the MPL, the GPL or the LGPL.
    35  
    36 */
    37 
    38 using System;
    39 using System.Collections.Generic;
    40 using System.Globalization;
    41 using System.IO;
    42 using System.Security.Permissions;
    43 
    44 namespace OpenHardwareMonitor.Hardware {
    45 
    46   public class Computer : IComputer {
    47 
    48     private readonly List<IGroup> groups = new List<IGroup>();
    49     private readonly ISettings settings;
    50 
    51     private bool open;
    52     private bool hddEnabled;    
    53 
    54     public Computer() {
    55       this.settings = new Settings();
    56     }
    57 
    58     public Computer(ISettings settings) {
    59       this.settings = settings ?? new Settings();
    60     }
    61 
    62     private void Add(IGroup group) {
    63       if (groups.Contains(group))
    64         return;
    65 
    66       groups.Add(group);
    67 
    68       if (HardwareAdded != null)
    69         foreach (IHardware hardware in group.Hardware)
    70           HardwareAdded(hardware);
    71     }
    72 
    73     private void Remove(IGroup group) {
    74       if (!groups.Contains(group))
    75         return;
    76 
    77       groups.Remove(group);
    78 
    79       if (HardwareRemoved != null)
    80         foreach (IHardware hardware in group.Hardware)
    81           HardwareRemoved(hardware);
    82     }
    83 
    84     [SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode = true)]
    85     public void Open() {
    86       if (open)
    87         return;
    88 
    89       WinRing0.Open();
    90 
    91       Add(new Mainboard.MainboardGroup(settings));
    92       Add(new CPU.CPUGroup(settings));
    93       Add(new ATI.ATIGroup(settings));
    94       Add(new Nvidia.NvidiaGroup(settings));      
    95       Add(new TBalancer.TBalancerGroup(settings));
    96       Add(new Heatmaster.HeatmasterGroup(settings));
    97 
    98       if (hddEnabled)
    99         Add(new HDD.HDDGroup(settings));
   100 
   101       open = true;
   102     }
   103     
   104     public bool HDDEnabled {
   105       get { return hddEnabled; }
   106 
   107       [SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode = true)]
   108       set {
   109         if (open && value && !hddEnabled) {
   110           Add(new HDD.HDDGroup(settings));
   111         } else if (open && !value && hddEnabled) {
   112           List<IGroup> list = new List<IGroup>();
   113           foreach (IGroup group in groups)
   114             if (group is HDD.HDDGroup)
   115               list.Add(group);
   116           foreach (IGroup group in list)
   117             Remove(group);
   118         }
   119         hddEnabled = value;
   120       }
   121     }
   122 
   123     public IHardware[] Hardware {
   124       get {
   125         List<IHardware> list = new List<IHardware>();
   126         foreach (IGroup group in groups)
   127           foreach (IHardware hardware in group.Hardware)
   128             list.Add(hardware);
   129         return list.ToArray();
   130       }
   131     }
   132 
   133     private static void NewSection(TextWriter writer) {
   134       for (int i = 0; i < 8; i++)
   135         writer.Write("----------");
   136       writer.WriteLine();
   137       writer.WriteLine();
   138     }
   139 
   140     private static int CompareSensor(ISensor a, ISensor b) {
   141       int c = a.SensorType.CompareTo(b.SensorType);
   142       if (c == 0)
   143         return a.Index.CompareTo(b.Index);
   144       else
   145         return c;
   146     }
   147 
   148     private static void ReportHardwareSensorTree(
   149       IHardware hardware, TextWriter w, string space) 
   150     {
   151       w.WriteLine("{0}|", space);
   152       w.WriteLine("{0}+-+ {1} ({2})",
   153         space, hardware.Name, hardware.Identifier);
   154       ISensor[] sensors = hardware.Sensors;
   155       Array.Sort(sensors, CompareSensor);
   156       foreach (ISensor sensor in sensors) {
   157         w.WriteLine("{0}|   +- {1}[{2}] : {3} : {4}",
   158           space, sensor.SensorType, sensor.Index, 
   159             string.Format(CultureInfo.InvariantCulture, "{0} : {1} : {2}",
   160             sensor.Value, sensor.Min, sensor.Max), sensor.Name);
   161       }
   162       foreach (IHardware subHardware in hardware.SubHardware)
   163         ReportHardwareSensorTree(subHardware, w, "|   ");
   164     }
   165 
   166     private static void ReportHardwareParameterTree(
   167       IHardware hardware, TextWriter w, string space) 
   168     {
   169       w.WriteLine("{0}|", space);
   170       w.WriteLine("{0}+-+ {1} ({2})",
   171         space, hardware.Name, hardware.Identifier);
   172       ISensor[] sensors = hardware.Sensors;
   173       Array.Sort(sensors, CompareSensor);
   174       foreach (ISensor sensor in sensors) {
   175         if (sensor.Parameters.Length > 0) {
   176           w.WriteLine("{0}|   +- {1}[{2}] : {3}",
   177             space, sensor.SensorType, sensor.Index, sensor.Name);
   178           foreach (IParameter parameter in sensor.Parameters) {
   179             w.WriteLine("{0}|      +- {1} : {2}",
   180               space, parameter.Name,
   181               string.Format(CultureInfo.InvariantCulture, "{0} : {1}",
   182                 parameter.DefaultValue, parameter.Value));
   183           }
   184         }
   185       }
   186       foreach (IHardware subHardware in hardware.SubHardware)
   187         ReportHardwareParameterTree(subHardware, w, "|   ");
   188     }
   189 
   190     private static void ReportHardware(IHardware hardware, TextWriter w) {
   191       string hardwareReport = hardware.GetReport();
   192       if (!string.IsNullOrEmpty(hardwareReport)) {
   193         NewSection(w);
   194         w.Write(hardwareReport);
   195       }
   196       foreach (IHardware subHardware in hardware.SubHardware)
   197         ReportHardware(subHardware, w);
   198     }
   199 
   200     public string GetReport() {
   201 
   202       using (StringWriter w = new StringWriter(CultureInfo.InvariantCulture)) {
   203 
   204         w.WriteLine();
   205         w.WriteLine("Open Hardware Monitor Report");
   206         w.WriteLine();
   207 
   208         Version version = typeof(Computer).Assembly.GetName().Version;
   209 
   210         NewSection(w);
   211         w.Write("Version: "); w.WriteLine(version.ToString());
   212         w.WriteLine();
   213 
   214         NewSection(w);
   215         w.Write("Common Language Runtime: ");
   216         w.WriteLine(Environment.Version.ToString());
   217         w.Write("Operating System: ");
   218         w.WriteLine(Environment.OSVersion.ToString());
   219         w.Write("Process Type: ");
   220         w.WriteLine(IntPtr.Size == 4 ? "32-Bit" : "64-Bit");
   221         w.WriteLine();
   222 
   223         NewSection(w);
   224         w.WriteLine("Sensors");
   225         w.WriteLine();
   226         foreach (IGroup group in groups) {
   227           foreach (IHardware hardware in group.Hardware)
   228             ReportHardwareSensorTree(hardware, w, "");
   229         }
   230         w.WriteLine();
   231 
   232         NewSection(w);
   233         w.WriteLine("Parameters");
   234         w.WriteLine();
   235         foreach (IGroup group in groups) {
   236           foreach (IHardware hardware in group.Hardware)
   237             ReportHardwareParameterTree(hardware, w, "");
   238         }
   239         w.WriteLine();
   240 
   241         foreach (IGroup group in groups) {
   242           string report = group.GetReport();
   243           if (!string.IsNullOrEmpty(report)) {
   244             NewSection(w);
   245             w.Write(report);
   246           }
   247 
   248           IHardware[] hardwareArray = group.Hardware;
   249           foreach (IHardware hardware in hardwareArray)
   250             ReportHardware(hardware, w);
   251 
   252         }
   253         return w.ToString();
   254       }
   255     }
   256 
   257     public void Close() {      
   258       if (!open)
   259         return;
   260 
   261       foreach (IGroup group in groups)
   262         group.Close();
   263       groups.Clear();
   264 
   265       WinRing0.Close();
   266 
   267       open = false;
   268     }
   269 
   270     public event HardwareEventHandler HardwareAdded;
   271     public event HardwareEventHandler HardwareRemoved;
   272 
   273     public void Accept(IVisitor visitor) {
   274       if (visitor == null)
   275         throw new ArgumentNullException("visitor");
   276       visitor.VisitComputer(this);
   277     }
   278 
   279     public void Traverse(IVisitor visitor) {
   280       foreach (IGroup group in groups)
   281         foreach (IHardware hardware in group.Hardware) 
   282           hardware.Accept(visitor);
   283     }
   284 
   285     private class Settings : ISettings {
   286 
   287       public bool Contains(string name) {
   288         return false;
   289       }
   290 
   291       public void SetValue(string name, string value) { }
   292 
   293       public string GetValue(string name, string value) {
   294         return value;
   295       }
   296 
   297       public void Remove(string name) { }
   298     }
   299   }
   300 }