Hardware/Computer.cs
author moel.mich
Sat, 27 Feb 2010 15:55:17 +0000
changeset 63 1a7c13ac7348
parent 47 1d02fde1bb23
child 64 15181001ee61
permissions -rw-r--r--
Added support for sensor parameters. Fixed Core and Thread count detection for Intel Core i7 CPUs with disabled HyperThreading.
     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.IO;
    41 using System.Globalization;
    42 using System.Text;
    43 
    44 namespace OpenHardwareMonitor.Hardware {
    45 
    46   public delegate void HardwareEventHandler(IHardware hardware);
    47 
    48   public class Computer {
    49 
    50     private List<IGroup> groups = new List<IGroup>();
    51 
    52     private bool open = false;
    53     private bool hddEnabled = false;
    54 
    55     public Computer() { }
    56 
    57     private void Add(IGroup group) {
    58       if (groups.Contains(group))
    59         return;
    60 
    61       groups.Add(group);
    62 
    63       if (HardwareAdded != null)
    64         foreach (IHardware hardware in group.Hardware)
    65           HardwareAdded(hardware);
    66     }
    67 
    68     private void Remove(IGroup group) {
    69       if (!groups.Contains(group))
    70         return;
    71 
    72       groups.Remove(group);
    73 
    74       if (HardwareRemoved != null)
    75         foreach (IHardware hardware in group.Hardware)
    76           HardwareRemoved(hardware);
    77     }
    78 
    79     public void Open() {
    80       if (open)
    81         return;
    82 
    83       Add(new SMBIOS.SMBIOSGroup());
    84       Add(new LPC.LPCGroup());
    85       Add(new CPU.CPUGroup());
    86       Add(new ATI.ATIGroup());
    87       Add(new Nvidia.NvidiaGroup());
    88       Add(new TBalancer.TBalancerGroup());
    89 
    90       if (hddEnabled)        
    91         Add(new HDD.HDDGroup());
    92 
    93       open = true;
    94     }
    95 
    96     public void Update() {
    97       foreach (IGroup group in groups)
    98         foreach (IHardware hardware in group.Hardware)
    99           hardware.Update();
   100     }
   101 
   102     public bool HDDEnabled {
   103       get { return hddEnabled; }
   104       set {
   105         if (open && value && !hddEnabled) {
   106           Add(new HDD.HDDGroup());
   107         } else if (open && !value && hddEnabled) {
   108           List<IGroup> list = new List<IGroup>();
   109           foreach (IGroup group in groups)
   110             if (group is HDD.HDDGroup)
   111               list.Add(group);
   112           foreach (IGroup group in list)
   113             Remove(group);          
   114         }
   115         hddEnabled = value;
   116       }
   117     }
   118 
   119     public IEnumerable<IHardware> Hardware {
   120       get {       
   121         foreach (IGroup group in groups)
   122           foreach (IHardware hardware in group.Hardware)
   123             yield return hardware;
   124       }
   125     }
   126 
   127     private void NewSection(TextWriter writer) {
   128       for (int i = 0; i < 8; i++)
   129         writer.Write("----------");
   130       writer.WriteLine();
   131       writer.WriteLine();
   132     }
   133 
   134     public void SaveReport(Version version) {
   135 
   136       using (TextWriter w =
   137         new StreamWriter("OpenHardwareMonitor.Report.txt")) {        
   138 
   139         w.WriteLine();
   140         w.WriteLine("Open Hardware Monitor Report");
   141         w.WriteLine();
   142 
   143         NewSection(w);
   144         w.Write("Version: "); w.WriteLine(version.ToString());
   145         w.WriteLine();
   146 
   147         NewSection(w);
   148         foreach (IGroup group in groups) {
   149           foreach (IHardware hardware in group.Hardware) {
   150             w.WriteLine("|");
   151             w.WriteLine("+-+ {0} ({1})",
   152               new object[] { hardware.Name, hardware.Identifier });
   153             foreach (ISensor sensor in hardware.Sensors) {
   154               w.WriteLine("|   +- {0} : {1} : {2} : {3}",
   155                 sensor.SensorType, sensor.Index, sensor.Name, 
   156                   string.Format(CultureInfo.InvariantCulture, "{0} : {1} : {2}", 
   157                   sensor.Value, sensor.Min, sensor.Max) );
   158               foreach (IParameter parameter in sensor.Parameters) {
   159                 w.WriteLine("|      +- {0} : {1} : {2}",
   160                   parameter.Name, parameter.IsDefault,   
   161                   string.Format(CultureInfo.InvariantCulture, "{0} : {1}", 
   162                     parameter.DefaultValue, parameter.Value) );
   163               }
   164             }
   165           }
   166         }
   167         w.WriteLine();
   168 
   169         foreach (IGroup group in groups) {
   170           string report = group.GetReport();
   171           if (report != null && report != "") {
   172             NewSection(w);
   173             w.Write(report);
   174           }
   175 
   176           IHardware[] hardwareArray = group.Hardware;
   177           foreach (IHardware hardware in hardwareArray) {
   178             string hardwareReport = hardware.GetReport();
   179             if (hardwareReport != null && hardwareReport != "") {
   180               NewSection(w);
   181               w.Write(hardwareReport);
   182             }
   183           }
   184         }
   185       }
   186     }
   187 
   188     public void Close() {
   189       if (!open)
   190         return;
   191 
   192       foreach (IGroup group in groups)
   193         group.Close();
   194       groups.Clear();
   195 
   196       open = false;
   197     }
   198 
   199     public event HardwareEventHandler HardwareAdded;
   200     public event HardwareEventHandler HardwareRemoved;
   201 
   202 
   203 
   204   }
   205 }