Hardware/Computer.cs
author moel.mich
Mon, 01 Mar 2010 22:12:29 +0000
changeset 67 bb5696abab23
parent 63 1a7c13ac7348
child 83 3fdadd4a830f
permissions -rw-r--r--
Added support for Winbond W83687THF and improved report output for LPC detection.
moel@28
     1
/*
moel@28
     2
  
moel@28
     3
  Version: MPL 1.1/GPL 2.0/LGPL 2.1
moel@28
     4
moel@28
     5
  The contents of this file are subject to the Mozilla Public License Version
moel@28
     6
  1.1 (the "License"); you may not use this file except in compliance with
moel@28
     7
  the License. You may obtain a copy of the License at
moel@28
     8
 
moel@28
     9
  http://www.mozilla.org/MPL/
moel@28
    10
moel@28
    11
  Software distributed under the License is distributed on an "AS IS" basis,
moel@28
    12
  WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
moel@28
    13
  for the specific language governing rights and limitations under the License.
moel@28
    14
moel@28
    15
  The Original Code is the Open Hardware Monitor code.
moel@28
    16
moel@28
    17
  The Initial Developer of the Original Code is 
moel@28
    18
  Michael Möller <m.moeller@gmx.ch>.
moel@28
    19
  Portions created by the Initial Developer are Copyright (C) 2009-2010
moel@28
    20
  the Initial Developer. All Rights Reserved.
moel@28
    21
moel@28
    22
  Contributor(s):
moel@28
    23
moel@28
    24
  Alternatively, the contents of this file may be used under the terms of
moel@28
    25
  either the GNU General Public License Version 2 or later (the "GPL"), or
moel@28
    26
  the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
moel@28
    27
  in which case the provisions of the GPL or the LGPL are applicable instead
moel@28
    28
  of those above. If you wish to allow use of your version of this file only
moel@28
    29
  under the terms of either the GPL or the LGPL, and not to allow others to
moel@28
    30
  use your version of this file under the terms of the MPL, indicate your
moel@28
    31
  decision by deleting the provisions above and replace them with the notice
moel@28
    32
  and other provisions required by the GPL or the LGPL. If you do not delete
moel@28
    33
  the provisions above, a recipient may use your version of this file under
moel@28
    34
  the terms of any one of the MPL, the GPL or the LGPL.
moel@28
    35
 
moel@28
    36
*/
moel@28
    37
moel@28
    38
using System;
moel@28
    39
using System.Collections.Generic;
moel@28
    40
using System.IO;
moel@34
    41
using System.Globalization;
moel@28
    42
using System.Text;
moel@28
    43
moel@28
    44
namespace OpenHardwareMonitor.Hardware {
moel@28
    45
moel@28
    46
  public delegate void HardwareEventHandler(IHardware hardware);
moel@28
    47
moel@28
    48
  public class Computer {
moel@28
    49
moel@28
    50
    private List<IGroup> groups = new List<IGroup>();
moel@28
    51
moel@28
    52
    private bool open = false;
moel@28
    53
    private bool hddEnabled = false;
moel@28
    54
moel@28
    55
    public Computer() { }
moel@28
    56
moel@28
    57
    private void Add(IGroup group) {
moel@28
    58
      if (groups.Contains(group))
moel@28
    59
        return;
moel@28
    60
moel@28
    61
      groups.Add(group);
moel@28
    62
moel@28
    63
      if (HardwareAdded != null)
moel@64
    64
        foreach (IHardware hardware in group.Hardware) 
moel@28
    65
          HardwareAdded(hardware);
moel@28
    66
    }
moel@28
    67
moel@28
    68
    private void Remove(IGroup group) {
moel@28
    69
      if (!groups.Contains(group))
moel@28
    70
        return;
moel@28
    71
moel@28
    72
      groups.Remove(group);
moel@28
    73
moel@28
    74
      if (HardwareRemoved != null)
moel@64
    75
        foreach (IHardware hardware in group.Hardware) 
moel@28
    76
          HardwareRemoved(hardware);
moel@28
    77
    }
moel@28
    78
moel@28
    79
    public void Open() {
moel@28
    80
      if (open)
moel@28
    81
        return;
moel@28
    82
moel@64
    83
      Add(new Mainboard.MainboardGroup());
moel@28
    84
      Add(new CPU.CPUGroup());
moel@28
    85
      Add(new ATI.ATIGroup());
moel@28
    86
      Add(new Nvidia.NvidiaGroup());
moel@28
    87
      Add(new TBalancer.TBalancerGroup());
moel@28
    88
moel@28
    89
      if (hddEnabled)        
moel@28
    90
        Add(new HDD.HDDGroup());
moel@28
    91
moel@28
    92
      open = true;
moel@28
    93
    }
moel@28
    94
moel@64
    95
    private void SubHardwareUpdate(IHardware hardware) {
moel@64
    96
      foreach (IHardware subHardware in hardware.SubHardware) {
moel@64
    97
        subHardware.Update();
moel@64
    98
        SubHardwareUpdate(subHardware);
moel@64
    99
      }
moel@64
   100
    }
moel@64
   101
moel@28
   102
    public void Update() {
moel@28
   103
      foreach (IGroup group in groups)
moel@64
   104
        foreach (IHardware hardware in group.Hardware) {
moel@28
   105
          hardware.Update();
moel@64
   106
          SubHardwareUpdate(hardware);
moel@64
   107
        }
moel@28
   108
    }
moel@28
   109
moel@28
   110
    public bool HDDEnabled {
moel@28
   111
      get { return hddEnabled; }
moel@28
   112
      set {
moel@28
   113
        if (open && value && !hddEnabled) {
moel@28
   114
          Add(new HDD.HDDGroup());
moel@28
   115
        } else if (open && !value && hddEnabled) {
moel@28
   116
          List<IGroup> list = new List<IGroup>();
moel@28
   117
          foreach (IGroup group in groups)
moel@28
   118
            if (group is HDD.HDDGroup)
moel@28
   119
              list.Add(group);
moel@28
   120
          foreach (IGroup group in list)
moel@28
   121
            Remove(group);          
moel@28
   122
        }
moel@28
   123
        hddEnabled = value;
moel@28
   124
      }
moel@28
   125
    }
moel@28
   126
moel@28
   127
    public IEnumerable<IHardware> Hardware {
moel@28
   128
      get {       
moel@28
   129
        foreach (IGroup group in groups)
moel@28
   130
          foreach (IHardware hardware in group.Hardware)
moel@28
   131
            yield return hardware;
moel@28
   132
      }
moel@28
   133
    }
moel@28
   134
moel@28
   135
    private void NewSection(TextWriter writer) {
moel@28
   136
      for (int i = 0; i < 8; i++)
moel@28
   137
        writer.Write("----------");
moel@28
   138
      writer.WriteLine();
moel@28
   139
      writer.WriteLine();
moel@28
   140
    }
moel@28
   141
moel@64
   142
    private void ReportHardwareTree(IHardware hardware, TextWriter w, 
moel@64
   143
      string space) 
moel@64
   144
    {
moel@64
   145
      w.WriteLine("{0}|", space);
moel@64
   146
      w.WriteLine("{0}+-+ {1} ({2})", 
moel@64
   147
        space, hardware.Name, hardware.Identifier);
moel@64
   148
      foreach (ISensor sensor in hardware.Sensors) {
moel@64
   149
        w.WriteLine("{0}|   +- {1} : {2} : {3} : {4}",
moel@64
   150
          space, sensor.SensorType, sensor.Index, sensor.Name,
moel@64
   151
            string.Format(CultureInfo.InvariantCulture, "{0} : {1} : {2}",
moel@64
   152
            sensor.Value, sensor.Min, sensor.Max));
moel@64
   153
        foreach (IParameter parameter in sensor.Parameters) {
moel@64
   154
          w.WriteLine("{0}|      +- {1} : {2} : {3}",
moel@64
   155
            space, parameter.Name, parameter.IsDefault,
moel@64
   156
            string.Format(CultureInfo.InvariantCulture, "{0} : {1}",
moel@64
   157
              parameter.DefaultValue, parameter.Value));
moel@64
   158
        }
moel@64
   159
      }
moel@64
   160
      foreach (IHardware subHardware in hardware.SubHardware)
moel@64
   161
        ReportHardwareTree(subHardware, w, "|   ");
moel@64
   162
    }
moel@64
   163
moel@64
   164
    private void ReportHardware(IHardware hardware, TextWriter w) {
moel@64
   165
      string hardwareReport = hardware.GetReport();
moel@64
   166
      if (hardwareReport != null && hardwareReport != "") {
moel@64
   167
        NewSection(w);
moel@64
   168
        w.Write(hardwareReport);
moel@64
   169
      }
moel@64
   170
      foreach (IHardware subHardware in hardware.SubHardware)
moel@64
   171
        ReportHardware(subHardware, w);
moel@64
   172
    }
moel@64
   173
moel@28
   174
    public void SaveReport(Version version) {
moel@28
   175
moel@28
   176
      using (TextWriter w =
moel@34
   177
        new StreamWriter("OpenHardwareMonitor.Report.txt")) {        
moel@28
   178
moel@28
   179
        w.WriteLine();
moel@28
   180
        w.WriteLine("Open Hardware Monitor Report");
moel@28
   181
        w.WriteLine();
moel@28
   182
moel@28
   183
        NewSection(w);
moel@28
   184
        w.Write("Version: "); w.WriteLine(version.ToString());
moel@28
   185
        w.WriteLine();
moel@28
   186
moel@28
   187
        NewSection(w);
moel@28
   188
        foreach (IGroup group in groups) {
moel@64
   189
          foreach (IHardware hardware in group.Hardware)
moel@64
   190
            ReportHardwareTree(hardware, w, "");          
moel@28
   191
        }
moel@28
   192
        w.WriteLine();
moel@28
   193
moel@28
   194
        foreach (IGroup group in groups) {
moel@28
   195
          string report = group.GetReport();
moel@47
   196
          if (report != null && report != "") {
moel@28
   197
            NewSection(w);
moel@28
   198
            w.Write(report);
moel@28
   199
          }
moel@28
   200
moel@28
   201
          IHardware[] hardwareArray = group.Hardware;
moel@64
   202
          foreach (IHardware hardware in hardwareArray)
moel@64
   203
            ReportHardware(hardware, w);
moel@64
   204
          
moel@28
   205
        }
moel@28
   206
      }
moel@28
   207
    }
moel@28
   208
moel@28
   209
    public void Close() {
moel@28
   210
      if (!open)
moel@28
   211
        return;
moel@28
   212
moel@28
   213
      foreach (IGroup group in groups)
moel@28
   214
        group.Close();
moel@28
   215
      groups.Clear();
moel@28
   216
moel@28
   217
      open = false;
moel@28
   218
    }
moel@28
   219
moel@28
   220
    public event HardwareEventHandler HardwareAdded;
moel@28
   221
    public event HardwareEventHandler HardwareRemoved;
moel@28
   222
moel@28
   223
  }
moel@28
   224
}