Hardware/Computer.cs
author moel.mich
Sat, 27 Mar 2010 12:57:09 +0000
changeset 83 3fdadd4a830f
parent 64 15181001ee61
child 85 ec4ccaa1210d
permissions -rw-r--r--
Added a dialog for the report filename. Added additional checks to T-Balancer code for cases where the port gets closed. Moved the timer to the Computer class.
     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 Timer timer;
    50 
    51     private List<IGroup> groups = new List<IGroup>();
    52 
    53     private bool open = false;
    54     private bool hddEnabled = false;
    55 
    56     public Computer() { }
    57 
    58     private void Add(IGroup group) {
    59       if (groups.Contains(group))
    60         return;
    61 
    62       groups.Add(group);
    63 
    64       if (HardwareAdded != null)
    65         foreach (IHardware hardware in group.Hardware)
    66           HardwareAdded(hardware);
    67     }
    68 
    69     private void Remove(IGroup group) {
    70       if (!groups.Contains(group))
    71         return;
    72 
    73       groups.Remove(group);
    74 
    75       if (HardwareRemoved != null)
    76         foreach (IHardware hardware in group.Hardware)
    77           HardwareRemoved(hardware);
    78     }
    79 
    80     public void Open() {
    81       if (open)
    82         return;
    83 
    84       Add(new Mainboard.MainboardGroup());
    85       Add(new CPU.CPUGroup());
    86       Add(new ATI.ATIGroup());
    87       Add(new Nvidia.NvidiaGroup());
    88       Add(new TBalancer.TBalancerGroup());
    89 
    90       if (hddEnabled)
    91         Add(new HDD.HDDGroup());
    92 
    93       open = true;
    94 
    95       timer = new Timer(
    96         delegate(Object stateInfo) {
    97           #if !DEBUG
    98           try {
    99           #endif
   100             Update();
   101           #if !DEBUG
   102           } catch (Exception exception) {
   103             Utilities.CrashReport.Save(exception);
   104             throw;
   105           }
   106           #endif
   107         }, null, 1000, 1000);
   108     }
   109 
   110     private void SubHardwareUpdate(IHardware hardware) {
   111       foreach (IHardware subHardware in hardware.SubHardware) {
   112         subHardware.Update();
   113         SubHardwareUpdate(subHardware);
   114       }
   115     }
   116 
   117     private void Update() {
   118       foreach (IGroup group in groups)
   119         foreach (IHardware hardware in group.Hardware) {
   120           hardware.Update();
   121           SubHardwareUpdate(hardware);
   122         }
   123       if (Updated != null)
   124         Updated();
   125     }
   126 
   127     public bool HDDEnabled {
   128       get { return hddEnabled; }
   129       set {
   130         if (open && value && !hddEnabled) {
   131           Add(new HDD.HDDGroup());
   132         } else if (open && !value && hddEnabled) {
   133           List<IGroup> list = new List<IGroup>();
   134           foreach (IGroup group in groups)
   135             if (group is HDD.HDDGroup)
   136               list.Add(group);
   137           foreach (IGroup group in list)
   138             Remove(group);
   139         }
   140         hddEnabled = value;
   141       }
   142     }
   143 
   144     public IHardware[] Hardware {
   145       get {
   146         List<IHardware> list = new List<IHardware>();
   147         foreach (IGroup group in groups)
   148           foreach (IHardware hardware in group.Hardware)
   149             list.Add(hardware);
   150         return list.ToArray();
   151       }
   152     }
   153 
   154     private void NewSection(TextWriter writer) {
   155       for (int i = 0; i < 8; i++)
   156         writer.Write("----------");
   157       writer.WriteLine();
   158       writer.WriteLine();
   159     }
   160 
   161     private void ReportHardwareTree(IHardware hardware, TextWriter w,
   162       string space) {
   163       w.WriteLine("{0}|", space);
   164       w.WriteLine("{0}+-+ {1} ({2})",
   165         space, hardware.Name, hardware.Identifier);
   166       foreach (ISensor sensor in hardware.Sensors) {
   167         w.WriteLine("{0}|   +- {1} : {2} : {3} : {4}",
   168           space, sensor.SensorType, sensor.Index, sensor.Name,
   169             string.Format(CultureInfo.InvariantCulture, "{0} : {1} : {2}",
   170             sensor.Value, sensor.Min, sensor.Max));
   171         foreach (IParameter parameter in sensor.Parameters) {
   172           w.WriteLine("{0}|      +- {1} : {2} : {3}",
   173             space, parameter.Name, parameter.IsDefault,
   174             string.Format(CultureInfo.InvariantCulture, "{0} : {1}",
   175               parameter.DefaultValue, parameter.Value));
   176         }
   177       }
   178       foreach (IHardware subHardware in hardware.SubHardware)
   179         ReportHardwareTree(subHardware, w, "|   ");
   180     }
   181 
   182     private void ReportHardware(IHardware hardware, TextWriter w) {
   183       string hardwareReport = hardware.GetReport();
   184       if (hardwareReport != null && hardwareReport != "") {
   185         NewSection(w);
   186         w.Write(hardwareReport);
   187       }
   188       foreach (IHardware subHardware in hardware.SubHardware)
   189         ReportHardware(subHardware, w);
   190     }
   191 
   192     public string GetReport() {
   193 
   194       using (StringWriter w = new StringWriter()) {
   195 
   196         w.WriteLine();
   197         w.WriteLine("Open Hardware Monitor Report");
   198         w.WriteLine();
   199 
   200         Version version = typeof(Computer).Assembly.GetName().Version;
   201 
   202         NewSection(w);
   203         w.Write("Version: "); w.WriteLine(version.ToString());
   204         w.WriteLine();
   205 
   206         NewSection(w);
   207         foreach (IGroup group in groups) {
   208           foreach (IHardware hardware in group.Hardware)
   209             ReportHardwareTree(hardware, w, "");
   210         }
   211         w.WriteLine();
   212 
   213         foreach (IGroup group in groups) {
   214           string report = group.GetReport();
   215           if (report != null && report != "") {
   216             NewSection(w);
   217             w.Write(report);
   218           }
   219 
   220           IHardware[] hardwareArray = group.Hardware;
   221           foreach (IHardware hardware in hardwareArray)
   222             ReportHardware(hardware, w);
   223 
   224         }
   225         return w.ToString();
   226       }
   227     }
   228 
   229     public void Close() {      
   230       timer.Dispose();
   231       timer = null;
   232 
   233       if (!open)
   234         return;
   235 
   236       foreach (IGroup group in groups)
   237         group.Close();
   238       groups.Clear();
   239 
   240       open = false;
   241     }
   242 
   243     public event UpdateEventHandler Updated;
   244     public event HardwareEventHandler HardwareAdded;
   245     public event HardwareEventHandler HardwareRemoved;
   246 
   247   }
   248 }