Hardware/Computer.cs
author moel.mich
Wed, 18 Jul 2012 22:47:30 +0000
changeset 368 1036b453f1f6
parent 344 3145aadca3d2
child 370 8e4dedc41924
permissions -rw-r--r--
Added a missing default case in the mainboard specific configuration selection. The missing default case prevented sensor data from getting displayed on unknown Asus mainboards with NCT6779D super I/O.
moel@28
     1
/*
moel@28
     2
 
moel@344
     3
  This Source Code Form is subject to the terms of the Mozilla Public
moel@344
     4
  License, v. 2.0. If a copy of the MPL was not distributed with this
moel@344
     5
  file, You can obtain one at http://mozilla.org/MPL/2.0/.
moel@28
     6
 
moel@360
     7
  Copyright (C) 2009-2012 Michael Möller <mmoeller@openhardwaremonitor.org>
moel@344
     8
	
moel@28
     9
*/
moel@28
    10
moel@28
    11
using System;
moel@28
    12
using System.Collections.Generic;
moel@182
    13
using System.Globalization;
moel@28
    14
using System.IO;
moel@167
    15
using System.Security.Permissions;
moel@236
    16
using System.Reflection;
moel@167
    17
moel@28
    18
namespace OpenHardwareMonitor.Hardware {
moel@28
    19
moel@83
    20
  public class Computer : IComputer {
moel@28
    21
moel@195
    22
    private readonly List<IGroup> groups = new List<IGroup>();
moel@195
    23
    private readonly ISettings settings;
moel@28
    24
moel@195
    25
    private bool open;
moel@360
    26
moel@360
    27
    private bool mainboardEnabled;
moel@360
    28
    private bool cpuEnabled;
moel@360
    29
    private bool gpuEnabled;
moel@360
    30
    private bool fanControllerEnabled;
moel@360
    31
    private bool hddEnabled;        
moel@28
    32
moel@165
    33
    public Computer() {
moel@165
    34
      this.settings = new Settings();
moel@165
    35
    }
moel@165
    36
moel@165
    37
    public Computer(ISettings settings) {
moel@195
    38
      this.settings = settings ?? new Settings();
moel@165
    39
    }
moel@28
    40
moel@28
    41
    private void Add(IGroup group) {
moel@28
    42
      if (groups.Contains(group))
moel@28
    43
        return;
moel@28
    44
moel@28
    45
      groups.Add(group);
moel@28
    46
moel@28
    47
      if (HardwareAdded != null)
moel@83
    48
        foreach (IHardware hardware in group.Hardware)
moel@28
    49
          HardwareAdded(hardware);
moel@28
    50
    }
moel@28
    51
moel@28
    52
    private void Remove(IGroup group) {
moel@28
    53
      if (!groups.Contains(group))
moel@28
    54
        return;
moel@28
    55
moel@28
    56
      groups.Remove(group);
moel@28
    57
moel@28
    58
      if (HardwareRemoved != null)
moel@83
    59
        foreach (IHardware hardware in group.Hardware)
moel@28
    60
          HardwareRemoved(hardware);
moel@360
    61
moel@360
    62
      group.Close();
moel@360
    63
    }
moel@360
    64
moel@360
    65
    private void RemoveType<T>() where T : IGroup {
moel@360
    66
      List<IGroup> list = new List<IGroup>();
moel@360
    67
      foreach (IGroup group in groups)
moel@360
    68
        if (group is T)
moel@360
    69
          list.Add(group);
moel@360
    70
      foreach (IGroup group in list)
moel@360
    71
        Remove(group);
moel@28
    72
    }
moel@28
    73
moel@167
    74
    [SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode = true)]
moel@28
    75
    public void Open() {
moel@28
    76
      if (open)
moel@28
    77
        return;
moel@28
    78
moel@236
    79
      Ring0.Open();
moel@236
    80
      Opcode.Open();
moel@167
    81
moel@360
    82
      if (mainboardEnabled)
moel@360
    83
        Add(new Mainboard.MainboardGroup(settings));
moel@360
    84
      
moel@360
    85
      if (cpuEnabled)
moel@360
    86
        Add(new CPU.CPUGroup(settings));
moel@360
    87
moel@360
    88
      if (gpuEnabled) {
moel@360
    89
        Add(new ATI.ATIGroup(settings));
moel@360
    90
        Add(new Nvidia.NvidiaGroup(settings));
moel@360
    91
      }
moel@360
    92
moel@360
    93
      if (fanControllerEnabled) {
moel@360
    94
        Add(new TBalancer.TBalancerGroup(settings));
moel@360
    95
        Add(new Heatmaster.HeatmasterGroup(settings));
moel@360
    96
      }
moel@28
    97
moel@83
    98
      if (hddEnabled)
moel@324
    99
        Add(new HDD.HarddriveGroup(settings));
moel@28
   100
moel@28
   101
      open = true;
moel@28
   102
    }
moel@360
   103
moel@360
   104
    public bool MainboardEnabled {
moel@360
   105
      get { return mainboardEnabled; }
moel@360
   106
moel@360
   107
      [SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode = true)]
moel@360
   108
      set {
moel@360
   109
        if (open && value != mainboardEnabled) {
moel@360
   110
          if (value)
moel@360
   111
            Add(new Mainboard.MainboardGroup(settings));
moel@360
   112
          else
moel@360
   113
            RemoveType<Mainboard.MainboardGroup>();
moel@360
   114
        }
moel@360
   115
        mainboardEnabled = value;
moel@360
   116
      }
moel@360
   117
    }
moel@360
   118
moel@360
   119
    public bool CPUEnabled {
moel@360
   120
      get { return cpuEnabled; }
moel@360
   121
moel@360
   122
      [SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode = true)]
moel@360
   123
      set {
moel@360
   124
        if (open && value != cpuEnabled) {
moel@360
   125
          if (value)
moel@360
   126
            Add(new CPU.CPUGroup(settings));
moel@360
   127
          else
moel@360
   128
            RemoveType<CPU.CPUGroup>();
moel@360
   129
        }
moel@360
   130
        cpuEnabled = value;
moel@360
   131
      }
moel@360
   132
    }
moel@360
   133
moel@360
   134
    public bool GPUEnabled {
moel@360
   135
      get { return gpuEnabled; }
moel@360
   136
moel@360
   137
      [SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode = true)]
moel@360
   138
      set {
moel@360
   139
        if (open && value != gpuEnabled) {
moel@360
   140
          if (value) {
moel@360
   141
            Add(new ATI.ATIGroup(settings));
moel@360
   142
            Add(new Nvidia.NvidiaGroup(settings));
moel@360
   143
          } else {
moel@360
   144
            RemoveType<ATI.ATIGroup>();
moel@360
   145
            RemoveType<Nvidia.NvidiaGroup>();
moel@360
   146
          }
moel@360
   147
        }
moel@360
   148
        gpuEnabled = value;
moel@360
   149
      }
moel@360
   150
    }
moel@360
   151
moel@360
   152
    public bool FanControllerEnabled {
moel@360
   153
      get { return fanControllerEnabled; }
moel@360
   154
moel@360
   155
      [SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode = true)]
moel@360
   156
      set {
moel@360
   157
        if (open && value != fanControllerEnabled) {
moel@360
   158
          if (value) {
moel@360
   159
            Add(new TBalancer.TBalancerGroup(settings));
moel@360
   160
            Add(new Heatmaster.HeatmasterGroup(settings));
moel@360
   161
          } else {
moel@360
   162
            RemoveType<TBalancer.TBalancerGroup>();
moel@360
   163
            RemoveType<Heatmaster.HeatmasterGroup>();
moel@360
   164
          }
moel@360
   165
        }
moel@360
   166
        fanControllerEnabled = value;
moel@360
   167
      }
moel@360
   168
    }
moel@360
   169
moel@28
   170
    public bool HDDEnabled {
moel@28
   171
      get { return hddEnabled; }
moel@167
   172
moel@167
   173
      [SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode = true)]
moel@28
   174
      set {
moel@360
   175
        if (open && value != hddEnabled) {
moel@360
   176
          if (value)
moel@360
   177
            Add(new HDD.HarddriveGroup(settings));
moel@360
   178
          else
moel@360
   179
            RemoveType<HDD.HarddriveGroup>();
moel@28
   180
        }
moel@28
   181
        hddEnabled = value;
moel@28
   182
      }
moel@28
   183
    }
moel@28
   184
moel@83
   185
    public IHardware[] Hardware {
moel@83
   186
      get {
moel@83
   187
        List<IHardware> list = new List<IHardware>();
moel@28
   188
        foreach (IGroup group in groups)
moel@28
   189
          foreach (IHardware hardware in group.Hardware)
moel@83
   190
            list.Add(hardware);
moel@83
   191
        return list.ToArray();
moel@28
   192
      }
moel@28
   193
    }
moel@28
   194
moel@167
   195
    private static void NewSection(TextWriter writer) {
moel@28
   196
      for (int i = 0; i < 8; i++)
moel@28
   197
        writer.Write("----------");
moel@28
   198
      writer.WriteLine();
moel@28
   199
      writer.WriteLine();
moel@28
   200
    }
moel@28
   201
moel@195
   202
    private static int CompareSensor(ISensor a, ISensor b) {
moel@149
   203
      int c = a.SensorType.CompareTo(b.SensorType);
moel@149
   204
      if (c == 0)
moel@149
   205
        return a.Index.CompareTo(b.Index);
moel@149
   206
      else
moel@149
   207
        return c;
moel@149
   208
    }
moel@149
   209
moel@195
   210
    private static void ReportHardwareSensorTree(
moel@195
   211
      IHardware hardware, TextWriter w, string space) 
moel@195
   212
    {
moel@64
   213
      w.WriteLine("{0}|", space);
moel@256
   214
      w.WriteLine("{0}+- {1} ({2})",
moel@64
   215
        space, hardware.Name, hardware.Identifier);
moel@149
   216
      ISensor[] sensors = hardware.Sensors;
moel@195
   217
      Array.Sort(sensors, CompareSensor);
moel@149
   218
      foreach (ISensor sensor in sensors) {
moel@256
   219
        w.WriteLine("{0}|  +- {1,-14} : {2,8:G6} {3,8:G6} {4,8:G6} ({5})", 
moel@256
   220
          space, sensor.Name, sensor.Value, sensor.Min, sensor.Max, 
moel@256
   221
          sensor.Identifier);
moel@149
   222
      }
moel@149
   223
      foreach (IHardware subHardware in hardware.SubHardware)
moel@256
   224
        ReportHardwareSensorTree(subHardware, w, "|  ");
moel@149
   225
    }
moel@149
   226
moel@195
   227
    private static void ReportHardwareParameterTree(
moel@256
   228
      IHardware hardware, TextWriter w, string space) {
moel@149
   229
      w.WriteLine("{0}|", space);
moel@256
   230
      w.WriteLine("{0}+- {1} ({2})",
moel@149
   231
        space, hardware.Name, hardware.Identifier);
moel@149
   232
      ISensor[] sensors = hardware.Sensors;
moel@195
   233
      Array.Sort(sensors, CompareSensor);
moel@149
   234
      foreach (ISensor sensor in sensors) {
moel@256
   235
        string innerSpace = space + "|  ";
moel@149
   236
        if (sensor.Parameters.Length > 0) {
moel@256
   237
          w.WriteLine("{0}|", innerSpace);
moel@256
   238
          w.WriteLine("{0}+- {1} ({2})",
moel@256
   239
            innerSpace, sensor.Name, sensor.Identifier);
moel@149
   240
          foreach (IParameter parameter in sensor.Parameters) {
moel@256
   241
            string innerInnerSpace = innerSpace + "|  ";
moel@256
   242
            w.WriteLine("{0}+- {1} : {2}",
moel@256
   243
              innerInnerSpace, parameter.Name,
moel@149
   244
              string.Format(CultureInfo.InvariantCulture, "{0} : {1}",
moel@149
   245
                parameter.DefaultValue, parameter.Value));
moel@149
   246
          }
moel@64
   247
        }
moel@64
   248
      }
moel@64
   249
      foreach (IHardware subHardware in hardware.SubHardware)
moel@256
   250
        ReportHardwareParameterTree(subHardware, w, "|  ");
moel@64
   251
    }
moel@64
   252
moel@195
   253
    private static void ReportHardware(IHardware hardware, TextWriter w) {
moel@64
   254
      string hardwareReport = hardware.GetReport();
moel@167
   255
      if (!string.IsNullOrEmpty(hardwareReport)) {
moel@64
   256
        NewSection(w);
moel@64
   257
        w.Write(hardwareReport);
moel@64
   258
      }
moel@64
   259
      foreach (IHardware subHardware in hardware.SubHardware)
moel@64
   260
        ReportHardware(subHardware, w);
moel@64
   261
    }
moel@64
   262
moel@83
   263
    public string GetReport() {
moel@28
   264
moel@166
   265
      using (StringWriter w = new StringWriter(CultureInfo.InvariantCulture)) {
moel@28
   266
moel@28
   267
        w.WriteLine();
moel@28
   268
        w.WriteLine("Open Hardware Monitor Report");
moel@28
   269
        w.WriteLine();
moel@28
   270
moel@83
   271
        Version version = typeof(Computer).Assembly.GetName().Version;
moel@83
   272
moel@28
   273
        NewSection(w);
moel@28
   274
        w.Write("Version: "); w.WriteLine(version.ToString());
moel@28
   275
        w.WriteLine();
moel@28
   276
moel@28
   277
        NewSection(w);
moel@119
   278
        w.Write("Common Language Runtime: ");
moel@119
   279
        w.WriteLine(Environment.Version.ToString());
moel@119
   280
        w.Write("Operating System: ");
moel@119
   281
        w.WriteLine(Environment.OSVersion.ToString());
moel@119
   282
        w.Write("Process Type: ");
moel@119
   283
        w.WriteLine(IntPtr.Size == 4 ? "32-Bit" : "64-Bit");
moel@119
   284
        w.WriteLine();
moel@119
   285
moel@254
   286
        string r = Ring0.GetReport();
moel@254
   287
        if (r != null) {
moel@254
   288
          NewSection(w);
moel@254
   289
          w.Write(r);
moel@254
   290
          w.WriteLine();
moel@254
   291
        }
moel@254
   292
moel@119
   293
        NewSection(w);
moel@149
   294
        w.WriteLine("Sensors");
moel@149
   295
        w.WriteLine();
moel@28
   296
        foreach (IGroup group in groups) {
moel@64
   297
          foreach (IHardware hardware in group.Hardware)
moel@149
   298
            ReportHardwareSensorTree(hardware, w, "");
moel@149
   299
        }
moel@149
   300
        w.WriteLine();
moel@149
   301
moel@149
   302
        NewSection(w);
moel@149
   303
        w.WriteLine("Parameters");
moel@149
   304
        w.WriteLine();
moel@149
   305
        foreach (IGroup group in groups) {
moel@149
   306
          foreach (IHardware hardware in group.Hardware)
moel@149
   307
            ReportHardwareParameterTree(hardware, w, "");
moel@28
   308
        }
moel@28
   309
        w.WriteLine();
moel@28
   310
moel@28
   311
        foreach (IGroup group in groups) {
moel@28
   312
          string report = group.GetReport();
moel@167
   313
          if (!string.IsNullOrEmpty(report)) {
moel@28
   314
            NewSection(w);
moel@28
   315
            w.Write(report);
moel@28
   316
          }
moel@28
   317
moel@28
   318
          IHardware[] hardwareArray = group.Hardware;
moel@64
   319
          foreach (IHardware hardware in hardwareArray)
moel@64
   320
            ReportHardware(hardware, w);
moel@83
   321
moel@28
   322
        }
moel@83
   323
        return w.ToString();
moel@28
   324
      }
moel@28
   325
    }
moel@28
   326
moel@83
   327
    public void Close() {      
moel@28
   328
      if (!open)
moel@28
   329
        return;
moel@28
   330
moel@262
   331
      while (groups.Count > 0) {
moel@262
   332
        IGroup group = groups[groups.Count - 1];
moel@360
   333
        Remove(group);         
moel@262
   334
      } 
moel@28
   335
moel@236
   336
      Opcode.Close();
moel@236
   337
      Ring0.Close();
moel@167
   338
moel@28
   339
      open = false;
moel@28
   340
    }
moel@28
   341
moel@28
   342
    public event HardwareEventHandler HardwareAdded;
moel@28
   343
    public event HardwareEventHandler HardwareRemoved;
moel@110
   344
moel@110
   345
    public void Accept(IVisitor visitor) {
moel@167
   346
      if (visitor == null)
moel@167
   347
        throw new ArgumentNullException("visitor");
moel@167
   348
      visitor.VisitComputer(this);
moel@110
   349
    }
moel@110
   350
moel@110
   351
    public void Traverse(IVisitor visitor) {
moel@110
   352
      foreach (IGroup group in groups)
moel@110
   353
        foreach (IHardware hardware in group.Hardware) 
moel@110
   354
          hardware.Accept(visitor);
moel@110
   355
    }
moel@165
   356
moel@165
   357
    private class Settings : ISettings {
moel@165
   358
moel@165
   359
      public bool Contains(string name) {
moel@165
   360
        return false;
moel@165
   361
      }
moel@165
   362
moel@166
   363
      public void SetValue(string name, string value) { }
moel@165
   364
moel@166
   365
      public string GetValue(string name, string value) {
moel@165
   366
        return value;
moel@165
   367
      }
moel@165
   368
moel@165
   369
      public void Remove(string name) { }
moel@165
   370
    }
moel@28
   371
  }
moel@28
   372
}