Hardware/Computer.cs
author moel.mich
Sun, 18 Jul 2010 12:38:01 +0000
changeset 156 3e2ab626531c
parent 119 4ca4621553aa
child 165 813d8bc3192f
permissions -rw-r--r--
Replaced all menus with the native implementations MainMenu and ContextMenu. Fixed a bug with the sensor renaming context menu.
     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 int CompareSensor(ISensor a, ISensor b) {
   129       int c = a.SensorType.CompareTo(b.SensorType);
   130       if (c == 0)
   131         return a.Index.CompareTo(b.Index);
   132       else
   133         return c;
   134     }
   135 
   136     private void ReportHardwareSensorTree(IHardware hardware, TextWriter w,
   137       string space) {
   138       w.WriteLine("{0}|", space);
   139       w.WriteLine("{0}+-+ {1} ({2})",
   140         space, hardware.Name, hardware.Identifier);
   141       ISensor[] sensors = hardware.Sensors;
   142       Array.Sort<ISensor>(sensors, CompareSensor);
   143       foreach (ISensor sensor in sensors) {
   144         w.WriteLine("{0}|   +- {1}[{2}] : {3} : {4}",
   145           space, sensor.SensorType, sensor.Index, 
   146             string.Format(CultureInfo.InvariantCulture, "{0} : {1} : {2}",
   147             sensor.Value, sensor.Min, sensor.Max), sensor.Name);
   148       }
   149       foreach (IHardware subHardware in hardware.SubHardware)
   150         ReportHardwareSensorTree(subHardware, w, "|   ");
   151     }
   152 
   153     private void ReportHardwareParameterTree(IHardware hardware, TextWriter w,
   154       string space) {
   155       w.WriteLine("{0}|", space);
   156       w.WriteLine("{0}+-+ {1} ({2})",
   157         space, hardware.Name, hardware.Identifier);
   158       ISensor[] sensors = hardware.Sensors;
   159       Array.Sort<ISensor>(sensors, CompareSensor);
   160       foreach (ISensor sensor in sensors) {
   161         if (sensor.Parameters.Length > 0) {
   162           w.WriteLine("{0}|   +- {1}[{2}] : {3}",
   163             space, sensor.SensorType, sensor.Index, sensor.Name);
   164           foreach (IParameter parameter in sensor.Parameters) {
   165             w.WriteLine("{0}|      +- {1} : {2}",
   166               space, parameter.Name,
   167               string.Format(CultureInfo.InvariantCulture, "{0} : {1}",
   168                 parameter.DefaultValue, parameter.Value));
   169           }
   170         }
   171       }
   172       foreach (IHardware subHardware in hardware.SubHardware)
   173         ReportHardwareParameterTree(subHardware, w, "|   ");
   174     }
   175 
   176     private void ReportHardware(IHardware hardware, TextWriter w) {
   177       string hardwareReport = hardware.GetReport();
   178       if (hardwareReport != null && hardwareReport != "") {
   179         NewSection(w);
   180         w.Write(hardwareReport);
   181       }
   182       foreach (IHardware subHardware in hardware.SubHardware)
   183         ReportHardware(subHardware, w);
   184     }
   185 
   186     public string GetReport() {
   187 
   188       using (StringWriter w = new StringWriter()) {
   189 
   190         w.WriteLine();
   191         w.WriteLine("Open Hardware Monitor Report");
   192         w.WriteLine();
   193 
   194         Version version = typeof(Computer).Assembly.GetName().Version;
   195 
   196         NewSection(w);
   197         w.Write("Version: "); w.WriteLine(version.ToString());
   198         w.WriteLine();
   199 
   200         NewSection(w);
   201         w.Write("Common Language Runtime: ");
   202         w.WriteLine(Environment.Version.ToString());
   203         w.Write("Operating System: ");
   204         w.WriteLine(Environment.OSVersion.ToString());
   205         w.Write("Process Type: ");
   206         w.WriteLine(IntPtr.Size == 4 ? "32-Bit" : "64-Bit");
   207         w.WriteLine();
   208 
   209         NewSection(w);
   210         w.WriteLine("Sensors");
   211         w.WriteLine();
   212         foreach (IGroup group in groups) {
   213           foreach (IHardware hardware in group.Hardware)
   214             ReportHardwareSensorTree(hardware, w, "");
   215         }
   216         w.WriteLine();
   217 
   218         NewSection(w);
   219         w.WriteLine("Parameters");
   220         w.WriteLine();
   221         foreach (IGroup group in groups) {
   222           foreach (IHardware hardware in group.Hardware)
   223             ReportHardwareParameterTree(hardware, w, "");
   224         }
   225         w.WriteLine();
   226 
   227         foreach (IGroup group in groups) {
   228           string report = group.GetReport();
   229           if (report != null && report != "") {
   230             NewSection(w);
   231             w.Write(report);
   232           }
   233 
   234           IHardware[] hardwareArray = group.Hardware;
   235           foreach (IHardware hardware in hardwareArray)
   236             ReportHardware(hardware, w);
   237 
   238         }
   239         return w.ToString();
   240       }
   241     }
   242 
   243     public void Close() {      
   244       if (!open)
   245         return;
   246 
   247       foreach (IGroup group in groups)
   248         group.Close();
   249       groups.Clear();
   250 
   251       open = false;
   252     }
   253 
   254     public event HardwareEventHandler HardwareAdded;
   255     public event HardwareEventHandler HardwareRemoved;
   256 
   257     public void Accept(IVisitor visitor) {
   258       visitor.VisitComputer(this);
   259     }
   260 
   261     public void Traverse(IVisitor visitor) {
   262       foreach (IGroup group in groups)
   263         foreach (IHardware hardware in group.Hardware) 
   264           hardware.Accept(visitor);
   265     }
   266   }
   267 }