Hardware/Computer.cs
author moel.mich
Sat, 22 May 2010 15:51:59 +0000
changeset 125 b1278888d9a9
parent 110 411b72b73d8f
child 149 de70c4468b36
permissions -rw-r--r--
A few bug fixes to avoid crashes on Linux systems.
     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     public bool HDDEnabled {
    95       get { return hddEnabled; }
    96       set {
    97         if (open && value && !hddEnabled) {
    98           Add(new HDD.HDDGroup());
    99         } else if (open && !value && hddEnabled) {
   100           List<IGroup> list = new List<IGroup>();
   101           foreach (IGroup group in groups)
   102             if (group is HDD.HDDGroup)
   103               list.Add(group);
   104           foreach (IGroup group in list)
   105             Remove(group);
   106         }
   107         hddEnabled = value;
   108       }
   109     }
   110 
   111     public IHardware[] Hardware {
   112       get {
   113         List<IHardware> list = new List<IHardware>();
   114         foreach (IGroup group in groups)
   115           foreach (IHardware hardware in group.Hardware)
   116             list.Add(hardware);
   117         return list.ToArray();
   118       }
   119     }
   120 
   121     private void NewSection(TextWriter writer) {
   122       for (int i = 0; i < 8; i++)
   123         writer.Write("----------");
   124       writer.WriteLine();
   125       writer.WriteLine();
   126     }
   127 
   128     private void ReportHardwareTree(IHardware hardware, TextWriter w,
   129       string space) {
   130       w.WriteLine("{0}|", space);
   131       w.WriteLine("{0}+-+ {1} ({2})",
   132         space, hardware.Name, hardware.Identifier);
   133       foreach (ISensor sensor in hardware.Sensors) {
   134         w.WriteLine("{0}|   +- {1} : {2} : {3} : {4}",
   135           space, sensor.SensorType, sensor.Index, sensor.Name,
   136             string.Format(CultureInfo.InvariantCulture, "{0} : {1} : {2}",
   137             sensor.Value, sensor.Min, sensor.Max));
   138         foreach (IParameter parameter in sensor.Parameters) {
   139           w.WriteLine("{0}|      +- {1} : {2} : {3}",
   140             space, parameter.Name, parameter.IsDefault,
   141             string.Format(CultureInfo.InvariantCulture, "{0} : {1}",
   142               parameter.DefaultValue, parameter.Value));
   143         }
   144       }
   145       foreach (IHardware subHardware in hardware.SubHardware)
   146         ReportHardwareTree(subHardware, w, "|   ");
   147     }
   148 
   149     private void ReportHardware(IHardware hardware, TextWriter w) {
   150       string hardwareReport = hardware.GetReport();
   151       if (hardwareReport != null && hardwareReport != "") {
   152         NewSection(w);
   153         w.Write(hardwareReport);
   154       }
   155       foreach (IHardware subHardware in hardware.SubHardware)
   156         ReportHardware(subHardware, w);
   157     }
   158 
   159     public string GetReport() {
   160 
   161       using (StringWriter w = new StringWriter()) {
   162 
   163         w.WriteLine();
   164         w.WriteLine("Open Hardware Monitor Report");
   165         w.WriteLine();
   166 
   167         Version version = typeof(Computer).Assembly.GetName().Version;
   168 
   169         NewSection(w);
   170         w.Write("Version: "); w.WriteLine(version.ToString());
   171         w.WriteLine();
   172 
   173         NewSection(w);
   174         w.Write("Common Language Runtime: ");
   175         w.WriteLine(Environment.Version.ToString());
   176         w.Write("Operating System: ");
   177         w.WriteLine(Environment.OSVersion.ToString());
   178         w.Write("Process Type: ");
   179         w.WriteLine(IntPtr.Size == 4 ? "32-Bit" : "64-Bit");
   180         w.WriteLine();
   181 
   182         NewSection(w);
   183         foreach (IGroup group in groups) {
   184           foreach (IHardware hardware in group.Hardware)
   185             ReportHardwareTree(hardware, w, "");
   186         }
   187         w.WriteLine();
   188 
   189         foreach (IGroup group in groups) {
   190           string report = group.GetReport();
   191           if (report != null && report != "") {
   192             NewSection(w);
   193             w.Write(report);
   194           }
   195 
   196           IHardware[] hardwareArray = group.Hardware;
   197           foreach (IHardware hardware in hardwareArray)
   198             ReportHardware(hardware, w);
   199 
   200         }
   201         return w.ToString();
   202       }
   203     }
   204 
   205     public void Close() {      
   206       if (!open)
   207         return;
   208 
   209       foreach (IGroup group in groups)
   210         group.Close();
   211       groups.Clear();
   212 
   213       open = false;
   214     }
   215 
   216     public event HardwareEventHandler HardwareAdded;
   217     public event HardwareEventHandler HardwareRemoved;
   218 
   219     public void Accept(IVisitor visitor) {
   220       visitor.VisitComputer(this);
   221     }
   222 
   223     public void Traverse(IVisitor visitor) {
   224       foreach (IGroup group in groups)
   225         foreach (IHardware hardware in group.Hardware) 
   226           hardware.Accept(visitor);
   227     }
   228   }
   229 }