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