Hardware/Computer.cs
author moel.mich
Mon, 26 Apr 2010 18:51:22 +0000
changeset 100 25d18bbaa9cf
parent 85 ec4ccaa1210d
child 110 411b72b73d8f
permissions -rw-r--r--
Added a processorIndex to CPU classes for multi CPU support.
     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 
    54     public Computer() { }
    55 
    56     private void Add(IGroup group) {
    57       if (groups.Contains(group))
    58         return;
    59 
    60       groups.Add(group);
    61 
    62       if (HardwareAdded != null)
    63         foreach (IHardware hardware in group.Hardware)
    64           HardwareAdded(hardware);
    65     }
    66 
    67     private void Remove(IGroup group) {
    68       if (!groups.Contains(group))
    69         return;
    70 
    71       groups.Remove(group);
    72 
    73       if (HardwareRemoved != null)
    74         foreach (IHardware hardware in group.Hardware)
    75           HardwareRemoved(hardware);
    76     }
    77 
    78     public void Open() {
    79       if (open)
    80         return;
    81 
    82       Add(new Mainboard.MainboardGroup());
    83       Add(new CPU.CPUGroup());
    84       Add(new ATI.ATIGroup());
    85       Add(new Nvidia.NvidiaGroup());
    86       Add(new TBalancer.TBalancerGroup());
    87 
    88       if (hddEnabled)
    89         Add(new HDD.HDDGroup());
    90 
    91       open = true;
    92     }
    93 
    94     private void SubHardwareUpdate(IHardware hardware) {
    95       foreach (IHardware subHardware in hardware.SubHardware) {
    96         subHardware.Update();
    97         SubHardwareUpdate(subHardware);
    98       }
    99     }
   100 
   101     public void Update() {
   102       foreach (IGroup group in groups)
   103         foreach (IHardware hardware in group.Hardware) {
   104           hardware.Update();
   105           SubHardwareUpdate(hardware);
   106         }
   107     }
   108 
   109     public bool HDDEnabled {
   110       get { return hddEnabled; }
   111       set {
   112         if (open && value && !hddEnabled) {
   113           Add(new HDD.HDDGroup());
   114         } else if (open && !value && hddEnabled) {
   115           List<IGroup> list = new List<IGroup>();
   116           foreach (IGroup group in groups)
   117             if (group is HDD.HDDGroup)
   118               list.Add(group);
   119           foreach (IGroup group in list)
   120             Remove(group);
   121         }
   122         hddEnabled = value;
   123       }
   124     }
   125 
   126     public IHardware[] Hardware {
   127       get {
   128         List<IHardware> list = new List<IHardware>();
   129         foreach (IGroup group in groups)
   130           foreach (IHardware hardware in group.Hardware)
   131             list.Add(hardware);
   132         return list.ToArray();
   133       }
   134     }
   135 
   136     private void NewSection(TextWriter writer) {
   137       for (int i = 0; i < 8; i++)
   138         writer.Write("----------");
   139       writer.WriteLine();
   140       writer.WriteLine();
   141     }
   142 
   143     private void ReportHardwareTree(IHardware hardware, TextWriter w,
   144       string space) {
   145       w.WriteLine("{0}|", space);
   146       w.WriteLine("{0}+-+ {1} ({2})",
   147         space, hardware.Name, hardware.Identifier);
   148       foreach (ISensor sensor in hardware.Sensors) {
   149         w.WriteLine("{0}|   +- {1} : {2} : {3} : {4}",
   150           space, sensor.SensorType, sensor.Index, sensor.Name,
   151             string.Format(CultureInfo.InvariantCulture, "{0} : {1} : {2}",
   152             sensor.Value, sensor.Min, sensor.Max));
   153         foreach (IParameter parameter in sensor.Parameters) {
   154           w.WriteLine("{0}|      +- {1} : {2} : {3}",
   155             space, parameter.Name, parameter.IsDefault,
   156             string.Format(CultureInfo.InvariantCulture, "{0} : {1}",
   157               parameter.DefaultValue, parameter.Value));
   158         }
   159       }
   160       foreach (IHardware subHardware in hardware.SubHardware)
   161         ReportHardwareTree(subHardware, w, "|   ");
   162     }
   163 
   164     private void ReportHardware(IHardware hardware, TextWriter w) {
   165       string hardwareReport = hardware.GetReport();
   166       if (hardwareReport != null && hardwareReport != "") {
   167         NewSection(w);
   168         w.Write(hardwareReport);
   169       }
   170       foreach (IHardware subHardware in hardware.SubHardware)
   171         ReportHardware(subHardware, w);
   172     }
   173 
   174     public string GetReport() {
   175 
   176       using (StringWriter w = new StringWriter()) {
   177 
   178         w.WriteLine();
   179         w.WriteLine("Open Hardware Monitor Report");
   180         w.WriteLine();
   181 
   182         Version version = typeof(Computer).Assembly.GetName().Version;
   183 
   184         NewSection(w);
   185         w.Write("Version: "); w.WriteLine(version.ToString());
   186         w.WriteLine();
   187 
   188         NewSection(w);
   189         foreach (IGroup group in groups) {
   190           foreach (IHardware hardware in group.Hardware)
   191             ReportHardwareTree(hardware, w, "");
   192         }
   193         w.WriteLine();
   194 
   195         foreach (IGroup group in groups) {
   196           string report = group.GetReport();
   197           if (report != null && report != "") {
   198             NewSection(w);
   199             w.Write(report);
   200           }
   201 
   202           IHardware[] hardwareArray = group.Hardware;
   203           foreach (IHardware hardware in hardwareArray)
   204             ReportHardware(hardware, w);
   205 
   206         }
   207         return w.ToString();
   208       }
   209     }
   210 
   211     public void Close() {      
   212       if (!open)
   213         return;
   214 
   215       foreach (IGroup group in groups)
   216         group.Close();
   217       groups.Clear();
   218 
   219       open = false;
   220     }
   221 
   222     public event HardwareEventHandler HardwareAdded;
   223     public event HardwareEventHandler HardwareRemoved;
   224   }
   225 }