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