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