Hardware/Computer.cs
author moel.mich
Mon, 08 Mar 2010 20:05:51 +0000
changeset 77 c71d4cd5f29c
parent 63 1a7c13ac7348
child 83 3fdadd4a830f
permissions -rw-r--r--
Fixed Issue 12.
     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 Mainboard.MainboardGroup());
    84       Add(new CPU.CPUGroup());
    85       Add(new ATI.ATIGroup());
    86       Add(new Nvidia.NvidiaGroup());
    87       Add(new TBalancer.TBalancerGroup());
    88 
    89       if (hddEnabled)        
    90         Add(new HDD.HDDGroup());
    91 
    92       open = true;
    93     }
    94 
    95     private void SubHardwareUpdate(IHardware hardware) {
    96       foreach (IHardware subHardware in hardware.SubHardware) {
    97         subHardware.Update();
    98         SubHardwareUpdate(subHardware);
    99       }
   100     }
   101 
   102     public void Update() {
   103       foreach (IGroup group in groups)
   104         foreach (IHardware hardware in group.Hardware) {
   105           hardware.Update();
   106           SubHardwareUpdate(hardware);
   107         }
   108     }
   109 
   110     public bool HDDEnabled {
   111       get { return hddEnabled; }
   112       set {
   113         if (open && value && !hddEnabled) {
   114           Add(new HDD.HDDGroup());
   115         } else if (open && !value && hddEnabled) {
   116           List<IGroup> list = new List<IGroup>();
   117           foreach (IGroup group in groups)
   118             if (group is HDD.HDDGroup)
   119               list.Add(group);
   120           foreach (IGroup group in list)
   121             Remove(group);          
   122         }
   123         hddEnabled = value;
   124       }
   125     }
   126 
   127     public IEnumerable<IHardware> Hardware {
   128       get {       
   129         foreach (IGroup group in groups)
   130           foreach (IHardware hardware in group.Hardware)
   131             yield return hardware;
   132       }
   133     }
   134 
   135     private void NewSection(TextWriter writer) {
   136       for (int i = 0; i < 8; i++)
   137         writer.Write("----------");
   138       writer.WriteLine();
   139       writer.WriteLine();
   140     }
   141 
   142     private void ReportHardwareTree(IHardware hardware, TextWriter w, 
   143       string space) 
   144     {
   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 void SaveReport(Version version) {
   175 
   176       using (TextWriter w =
   177         new StreamWriter("OpenHardwareMonitor.Report.txt")) {        
   178 
   179         w.WriteLine();
   180         w.WriteLine("Open Hardware Monitor Report");
   181         w.WriteLine();
   182 
   183         NewSection(w);
   184         w.Write("Version: "); w.WriteLine(version.ToString());
   185         w.WriteLine();
   186 
   187         NewSection(w);
   188         foreach (IGroup group in groups) {
   189           foreach (IHardware hardware in group.Hardware)
   190             ReportHardwareTree(hardware, w, "");          
   191         }
   192         w.WriteLine();
   193 
   194         foreach (IGroup group in groups) {
   195           string report = group.GetReport();
   196           if (report != null && report != "") {
   197             NewSection(w);
   198             w.Write(report);
   199           }
   200 
   201           IHardware[] hardwareArray = group.Hardware;
   202           foreach (IHardware hardware in hardwareArray)
   203             ReportHardware(hardware, w);
   204           
   205         }
   206       }
   207     }
   208 
   209     public void Close() {
   210       if (!open)
   211         return;
   212 
   213       foreach (IGroup group in groups)
   214         group.Close();
   215       groups.Clear();
   216 
   217       open = false;
   218     }
   219 
   220     public event HardwareEventHandler HardwareAdded;
   221     public event HardwareEventHandler HardwareRemoved;
   222 
   223   }
   224 }