Hardware/Computer.cs
author moel.mich
Thu, 12 Aug 2010 20:53:27 +0000
changeset 166 fa9dfbfc4145
parent 165 813d8bc3192f
child 167 b7cc9d09aefe
permissions -rw-r--r--
Changed the project files to Visual Studio 2010. Fixed some Code Analysis warnings.
     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 using System.Threading;
    44 
    45 namespace OpenHardwareMonitor.Hardware {
    46 
    47   public class Computer : IComputer {
    48 
    49     private List<IGroup> groups = new List<IGroup>();
    50 
    51     private bool open = false;
    52     private bool hddEnabled = false;
    53     private ISettings settings;
    54 
    55     public Computer() {
    56       this.settings = new Settings();
    57     }
    58 
    59     public Computer(ISettings settings) {
    60       if (settings != null)
    61         this.settings = settings;
    62       else {
    63         this.settings = new Settings();
    64       }
    65     }
    66 
    67     private void Add(IGroup group) {
    68       if (groups.Contains(group))
    69         return;
    70 
    71       groups.Add(group);
    72 
    73       if (HardwareAdded != null)
    74         foreach (IHardware hardware in group.Hardware)
    75           HardwareAdded(hardware);
    76     }
    77 
    78     private void Remove(IGroup group) {
    79       if (!groups.Contains(group))
    80         return;
    81 
    82       groups.Remove(group);
    83 
    84       if (HardwareRemoved != null)
    85         foreach (IHardware hardware in group.Hardware)
    86           HardwareRemoved(hardware);
    87     }
    88 
    89     public void Open() {
    90       if (open)
    91         return;
    92 
    93       Add(new Mainboard.MainboardGroup(settings));
    94       Add(new CPU.CPUGroup(settings));
    95       Add(new ATI.ATIGroup(settings));
    96       Add(new Nvidia.NvidiaGroup(settings));
    97       Add(new TBalancer.TBalancerGroup(settings));
    98 
    99       if (hddEnabled)
   100         Add(new HDD.HDDGroup(settings));
   101 
   102       open = true;
   103     }
   104 
   105     public bool HDDEnabled {
   106       get { return hddEnabled; }
   107       set {
   108         if (open && value && !hddEnabled) {
   109           Add(new HDD.HDDGroup(settings));
   110         } else if (open && !value && hddEnabled) {
   111           List<IGroup> list = new List<IGroup>();
   112           foreach (IGroup group in groups)
   113             if (group is HDD.HDDGroup)
   114               list.Add(group);
   115           foreach (IGroup group in list)
   116             Remove(group);
   117         }
   118         hddEnabled = value;
   119       }
   120     }
   121 
   122     public IHardware[] Hardware {
   123       get {
   124         List<IHardware> list = new List<IHardware>();
   125         foreach (IGroup group in groups)
   126           foreach (IHardware hardware in group.Hardware)
   127             list.Add(hardware);
   128         return list.ToArray();
   129       }
   130     }
   131 
   132     private void NewSection(TextWriter writer) {
   133       for (int i = 0; i < 8; i++)
   134         writer.Write("----------");
   135       writer.WriteLine();
   136       writer.WriteLine();
   137     }
   138 
   139     private int CompareSensor(ISensor a, ISensor b) {
   140       int c = a.SensorType.CompareTo(b.SensorType);
   141       if (c == 0)
   142         return a.Index.CompareTo(b.Index);
   143       else
   144         return c;
   145     }
   146 
   147     private void ReportHardwareSensorTree(IHardware hardware, TextWriter w,
   148       string space) {
   149       w.WriteLine("{0}|", space);
   150       w.WriteLine("{0}+-+ {1} ({2})",
   151         space, hardware.Name, hardware.Identifier);
   152       ISensor[] sensors = hardware.Sensors;
   153       Array.Sort<ISensor>(sensors, CompareSensor);
   154       foreach (ISensor sensor in sensors) {
   155         w.WriteLine("{0}|   +- {1}[{2}] : {3} : {4}",
   156           space, sensor.SensorType, sensor.Index, 
   157             string.Format(CultureInfo.InvariantCulture, "{0} : {1} : {2}",
   158             sensor.Value, sensor.Min, sensor.Max), sensor.Name);
   159       }
   160       foreach (IHardware subHardware in hardware.SubHardware)
   161         ReportHardwareSensorTree(subHardware, w, "|   ");
   162     }
   163 
   164     private void ReportHardwareParameterTree(IHardware hardware, TextWriter w,
   165       string space) {
   166       w.WriteLine("{0}|", space);
   167       w.WriteLine("{0}+-+ {1} ({2})",
   168         space, hardware.Name, hardware.Identifier);
   169       ISensor[] sensors = hardware.Sensors;
   170       Array.Sort<ISensor>(sensors, CompareSensor);
   171       foreach (ISensor sensor in sensors) {
   172         if (sensor.Parameters.Length > 0) {
   173           w.WriteLine("{0}|   +- {1}[{2}] : {3}",
   174             space, sensor.SensorType, sensor.Index, sensor.Name);
   175           foreach (IParameter parameter in sensor.Parameters) {
   176             w.WriteLine("{0}|      +- {1} : {2}",
   177               space, parameter.Name,
   178               string.Format(CultureInfo.InvariantCulture, "{0} : {1}",
   179                 parameter.DefaultValue, parameter.Value));
   180           }
   181         }
   182       }
   183       foreach (IHardware subHardware in hardware.SubHardware)
   184         ReportHardwareParameterTree(subHardware, w, "|   ");
   185     }
   186 
   187     private void ReportHardware(IHardware hardware, TextWriter w) {
   188       string hardwareReport = hardware.GetReport();
   189       if (hardwareReport != null && hardwareReport != "") {
   190         NewSection(w);
   191         w.Write(hardwareReport);
   192       }
   193       foreach (IHardware subHardware in hardware.SubHardware)
   194         ReportHardware(subHardware, w);
   195     }
   196 
   197     public string GetReport() {
   198 
   199       using (StringWriter w = new StringWriter(CultureInfo.InvariantCulture)) {
   200 
   201         w.WriteLine();
   202         w.WriteLine("Open Hardware Monitor Report");
   203         w.WriteLine();
   204 
   205         Version version = typeof(Computer).Assembly.GetName().Version;
   206 
   207         NewSection(w);
   208         w.Write("Version: "); w.WriteLine(version.ToString());
   209         w.WriteLine();
   210 
   211         NewSection(w);
   212         w.Write("Common Language Runtime: ");
   213         w.WriteLine(Environment.Version.ToString());
   214         w.Write("Operating System: ");
   215         w.WriteLine(Environment.OSVersion.ToString());
   216         w.Write("Process Type: ");
   217         w.WriteLine(IntPtr.Size == 4 ? "32-Bit" : "64-Bit");
   218         w.WriteLine();
   219 
   220         NewSection(w);
   221         w.WriteLine("Sensors");
   222         w.WriteLine();
   223         foreach (IGroup group in groups) {
   224           foreach (IHardware hardware in group.Hardware)
   225             ReportHardwareSensorTree(hardware, w, "");
   226         }
   227         w.WriteLine();
   228 
   229         NewSection(w);
   230         w.WriteLine("Parameters");
   231         w.WriteLine();
   232         foreach (IGroup group in groups) {
   233           foreach (IHardware hardware in group.Hardware)
   234             ReportHardwareParameterTree(hardware, w, "");
   235         }
   236         w.WriteLine();
   237 
   238         foreach (IGroup group in groups) {
   239           string report = group.GetReport();
   240           if (report != null && report != "") {
   241             NewSection(w);
   242             w.Write(report);
   243           }
   244 
   245           IHardware[] hardwareArray = group.Hardware;
   246           foreach (IHardware hardware in hardwareArray)
   247             ReportHardware(hardware, w);
   248 
   249         }
   250         return w.ToString();
   251       }
   252     }
   253 
   254     public void Close() {      
   255       if (!open)
   256         return;
   257 
   258       foreach (IGroup group in groups)
   259         group.Close();
   260       groups.Clear();
   261 
   262       open = false;
   263     }
   264 
   265     public event HardwareEventHandler HardwareAdded;
   266     public event HardwareEventHandler HardwareRemoved;
   267 
   268     public void Accept(IVisitor visitor) {
   269       if (visitor != null)
   270         visitor.VisitComputer(this);
   271     }
   272 
   273     public void Traverse(IVisitor visitor) {
   274       foreach (IGroup group in groups)
   275         foreach (IHardware hardware in group.Hardware) 
   276           hardware.Accept(visitor);
   277     }
   278 
   279     private class Settings : ISettings {
   280 
   281       public bool Contains(string name) {
   282         return false;
   283       }
   284 
   285       public void SetValue(string name, string value) { }
   286 
   287       public string GetValue(string name, string value) {
   288         return value;
   289       }
   290 
   291       public void Remove(string name) { }
   292     }
   293   }
   294 }