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