Hardware/Computer.cs
author moel.mich
Sun, 07 Feb 2010 19:53:51 +0000
changeset 31 c4d1fb76a9e1
child 34 dc276daadb2c
permissions -rw-r--r--
Added initial support for W83627HF. Some refactoring for IHardware classes.
     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.Text;
    42 
    43 namespace OpenHardwareMonitor.Hardware {
    44 
    45   public delegate void HardwareEventHandler(IHardware hardware);
    46 
    47   public class Computer {
    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 SMBIOS.SMBIOSGroup());
    83       Add(new LPC.LPCGroup());
    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     public void Update() {
    96       foreach (IGroup group in groups)
    97         foreach (IHardware hardware in group.Hardware)
    98           hardware.Update();
    99     }
   100 
   101     public bool HDDEnabled {
   102       get { return hddEnabled; }
   103       set {
   104         if (open && value && !hddEnabled) {
   105           Add(new HDD.HDDGroup());
   106         } else if (open && !value && hddEnabled) {
   107           List<IGroup> list = new List<IGroup>();
   108           foreach (IGroup group in groups)
   109             if (group is HDD.HDDGroup)
   110               list.Add(group);
   111           foreach (IGroup group in list)
   112             Remove(group);          
   113         }
   114         hddEnabled = value;
   115       }
   116     }
   117 
   118     public IEnumerable<IHardware> Hardware {
   119       get {       
   120         foreach (IGroup group in groups)
   121           foreach (IHardware hardware in group.Hardware)
   122             yield return hardware;
   123       }
   124     }
   125 
   126     private void NewSection(TextWriter writer) {
   127       for (int i = 0; i < 8; i++)
   128         writer.Write("----------");
   129       writer.WriteLine();
   130       writer.WriteLine();
   131     }
   132 
   133     public void SaveReport(Version version) {
   134 
   135       using (TextWriter w =
   136         new StreamWriter("OpenHardwareMonitor.Report.txt")) {
   137 
   138         w.WriteLine();
   139         w.WriteLine("Open Hardware Monitor Report");
   140         w.WriteLine();
   141 
   142         NewSection(w);
   143         w.Write("Version: "); w.WriteLine(version.ToString());
   144         w.WriteLine();
   145 
   146         NewSection(w);
   147         foreach (IGroup group in groups) {
   148           foreach (IHardware hardware in group.Hardware) {
   149             w.WriteLine("|");
   150             w.WriteLine("+-+ {0} ({1})",
   151               new object[] { hardware.Name, hardware.Identifier });
   152             foreach (ISensor sensor in hardware.Sensors) {
   153               w.WriteLine("|   +- {0} : {1} : {2} : {3}",
   154                 new object[] { sensor.SensorType, sensor.Index, sensor.Name, 
   155                   sensor.Value });
   156             }
   157           }
   158         }
   159         w.WriteLine();
   160 
   161         foreach (IGroup group in groups) {
   162           string report = group.GetReport();
   163           if (report != null) {
   164             NewSection(w);
   165             w.Write(report);
   166           }
   167 
   168           IHardware[] hardwareArray = group.Hardware;
   169           foreach (IHardware hardware in hardwareArray) {
   170             string hardwareReport = hardware.GetReport();
   171             if (hardwareReport != null) {
   172               NewSection(w);
   173               w.Write(hardwareReport);
   174             }
   175           }
   176         }
   177       }
   178     }
   179 
   180     public void Close() {
   181       if (!open)
   182         return;
   183 
   184       foreach (IGroup group in groups)
   185         group.Close();
   186       groups.Clear();
   187 
   188       open = false;
   189     }
   190 
   191     public event HardwareEventHandler HardwareAdded;
   192     public event HardwareEventHandler HardwareRemoved;
   193 
   194 
   195 
   196   }
   197 }