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