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