Hardware/Computer.cs
author sl
Sun, 03 Feb 2013 18:01:50 +0100
changeset 391 ca4c0e7ae75d
parent 360 c1a4377c11d1
permissions -rw-r--r--
Converted project to VisualStudio 2012.
Adding SoundGraphDisplay and SensorFrontView classes.
They were respectively based on SystemTray and SensorNotifyIcon.
SoundGraphDisplay is now able to load iMONDisplay.dll providing it lives on your PATH.
Adding option to sensor context menu for adding it into FrontView.
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@370
    25
    private SMBIOS smbios;
moel@370
    26
moel@195
    27
    private bool open;
moel@360
    28
moel@360
    29
    private bool mainboardEnabled;
moel@360
    30
    private bool cpuEnabled;
moel@370
    31
    private bool ramEnabled;
moel@360
    32
    private bool gpuEnabled;
moel@360
    33
    private bool fanControllerEnabled;
moel@370
    34
    private bool hddEnabled;    
moel@28
    35
moel@165
    36
    public Computer() {
moel@165
    37
      this.settings = new Settings();
moel@165
    38
    }
moel@165
    39
moel@165
    40
    public Computer(ISettings settings) {
moel@195
    41
      this.settings = settings ?? new Settings();
moel@165
    42
    }
moel@28
    43
moel@28
    44
    private void Add(IGroup group) {
moel@28
    45
      if (groups.Contains(group))
moel@28
    46
        return;
moel@28
    47
moel@28
    48
      groups.Add(group);
moel@28
    49
moel@28
    50
      if (HardwareAdded != null)
moel@83
    51
        foreach (IHardware hardware in group.Hardware)
moel@28
    52
          HardwareAdded(hardware);
moel@28
    53
    }
moel@28
    54
moel@28
    55
    private void Remove(IGroup group) {
moel@28
    56
      if (!groups.Contains(group))
moel@28
    57
        return;
moel@28
    58
moel@28
    59
      groups.Remove(group);
moel@28
    60
moel@28
    61
      if (HardwareRemoved != null)
moel@83
    62
        foreach (IHardware hardware in group.Hardware)
moel@28
    63
          HardwareRemoved(hardware);
moel@360
    64
moel@360
    65
      group.Close();
moel@360
    66
    }
moel@360
    67
moel@360
    68
    private void RemoveType<T>() where T : IGroup {
moel@360
    69
      List<IGroup> list = new List<IGroup>();
moel@360
    70
      foreach (IGroup group in groups)
moel@360
    71
        if (group is T)
moel@360
    72
          list.Add(group);
moel@360
    73
      foreach (IGroup group in list)
moel@360
    74
        Remove(group);
moel@28
    75
    }
moel@28
    76
moel@167
    77
    [SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode = true)]
moel@28
    78
    public void Open() {
moel@28
    79
      if (open)
moel@28
    80
        return;
moel@28
    81
moel@370
    82
      this.smbios = new SMBIOS();
moel@370
    83
moel@236
    84
      Ring0.Open();
moel@236
    85
      Opcode.Open();
moel@167
    86
moel@360
    87
      if (mainboardEnabled)
moel@370
    88
        Add(new Mainboard.MainboardGroup(smbios, settings));
moel@360
    89
      
moel@360
    90
      if (cpuEnabled)
moel@360
    91
        Add(new CPU.CPUGroup(settings));
moel@360
    92
moel@370
    93
      if (ramEnabled)
moel@370
    94
        Add(new RAM.RAMGroup(smbios, settings));
moel@370
    95
moel@360
    96
      if (gpuEnabled) {
moel@360
    97
        Add(new ATI.ATIGroup(settings));
moel@360
    98
        Add(new Nvidia.NvidiaGroup(settings));
moel@360
    99
      }
moel@360
   100
moel@360
   101
      if (fanControllerEnabled) {
moel@360
   102
        Add(new TBalancer.TBalancerGroup(settings));
moel@360
   103
        Add(new Heatmaster.HeatmasterGroup(settings));
moel@360
   104
      }
moel@28
   105
moel@83
   106
      if (hddEnabled)
moel@324
   107
        Add(new HDD.HarddriveGroup(settings));
moel@28
   108
moel@28
   109
      open = true;
moel@28
   110
    }
moel@360
   111
moel@360
   112
    public bool MainboardEnabled {
moel@360
   113
      get { return mainboardEnabled; }
moel@360
   114
moel@360
   115
      [SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode = true)]
moel@360
   116
      set {
moel@360
   117
        if (open && value != mainboardEnabled) {
moel@360
   118
          if (value)
moel@370
   119
            Add(new Mainboard.MainboardGroup(smbios, settings));
moel@360
   120
          else
moel@360
   121
            RemoveType<Mainboard.MainboardGroup>();
moel@360
   122
        }
moel@360
   123
        mainboardEnabled = value;
moel@360
   124
      }
moel@360
   125
    }
moel@360
   126
moel@360
   127
    public bool CPUEnabled {
moel@360
   128
      get { return cpuEnabled; }
moel@360
   129
moel@360
   130
      [SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode = true)]
moel@360
   131
      set {
moel@360
   132
        if (open && value != cpuEnabled) {
moel@360
   133
          if (value)
moel@360
   134
            Add(new CPU.CPUGroup(settings));
moel@360
   135
          else
moel@360
   136
            RemoveType<CPU.CPUGroup>();
moel@360
   137
        }
moel@360
   138
        cpuEnabled = value;
moel@360
   139
      }
moel@360
   140
    }
moel@360
   141
moel@370
   142
    public bool RAMEnabled {
moel@370
   143
      get { return ramEnabled; }
moel@370
   144
moel@370
   145
      [SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode = true)]
moel@370
   146
      set {
moel@370
   147
        if (open && value != ramEnabled) {
moel@370
   148
          if (value)
moel@370
   149
            Add(new RAM.RAMGroup(smbios, settings));
moel@370
   150
          else
moel@370
   151
            RemoveType<RAM.RAMGroup>();
moel@370
   152
        }
moel@370
   153
        ramEnabled = value;
moel@370
   154
      }
moel@370
   155
    }    
moel@370
   156
moel@360
   157
    public bool GPUEnabled {
moel@360
   158
      get { return gpuEnabled; }
moel@360
   159
moel@360
   160
      [SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode = true)]
moel@360
   161
      set {
moel@360
   162
        if (open && value != gpuEnabled) {
moel@360
   163
          if (value) {
moel@360
   164
            Add(new ATI.ATIGroup(settings));
moel@360
   165
            Add(new Nvidia.NvidiaGroup(settings));
moel@360
   166
          } else {
moel@360
   167
            RemoveType<ATI.ATIGroup>();
moel@360
   168
            RemoveType<Nvidia.NvidiaGroup>();
moel@360
   169
          }
moel@360
   170
        }
moel@360
   171
        gpuEnabled = value;
moel@360
   172
      }
moel@360
   173
    }
moel@360
   174
moel@360
   175
    public bool FanControllerEnabled {
moel@360
   176
      get { return fanControllerEnabled; }
moel@360
   177
moel@360
   178
      [SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode = true)]
moel@360
   179
      set {
moel@360
   180
        if (open && value != fanControllerEnabled) {
moel@360
   181
          if (value) {
moel@360
   182
            Add(new TBalancer.TBalancerGroup(settings));
moel@360
   183
            Add(new Heatmaster.HeatmasterGroup(settings));
moel@360
   184
          } else {
moel@360
   185
            RemoveType<TBalancer.TBalancerGroup>();
moel@360
   186
            RemoveType<Heatmaster.HeatmasterGroup>();
moel@360
   187
          }
moel@360
   188
        }
moel@360
   189
        fanControllerEnabled = value;
moel@360
   190
      }
moel@360
   191
    }
moel@360
   192
moel@28
   193
    public bool HDDEnabled {
moel@28
   194
      get { return hddEnabled; }
moel@167
   195
moel@167
   196
      [SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode = true)]
moel@28
   197
      set {
moel@360
   198
        if (open && value != hddEnabled) {
moel@360
   199
          if (value)
moel@360
   200
            Add(new HDD.HarddriveGroup(settings));
moel@360
   201
          else
moel@360
   202
            RemoveType<HDD.HarddriveGroup>();
moel@28
   203
        }
moel@28
   204
        hddEnabled = value;
moel@28
   205
      }
moel@28
   206
    }
moel@28
   207
moel@83
   208
    public IHardware[] Hardware {
moel@83
   209
      get {
moel@83
   210
        List<IHardware> list = new List<IHardware>();
moel@28
   211
        foreach (IGroup group in groups)
moel@28
   212
          foreach (IHardware hardware in group.Hardware)
moel@83
   213
            list.Add(hardware);
moel@83
   214
        return list.ToArray();
moel@28
   215
      }
moel@28
   216
    }
moel@28
   217
moel@167
   218
    private static void NewSection(TextWriter writer) {
moel@28
   219
      for (int i = 0; i < 8; i++)
moel@28
   220
        writer.Write("----------");
moel@28
   221
      writer.WriteLine();
moel@28
   222
      writer.WriteLine();
moel@28
   223
    }
moel@28
   224
moel@195
   225
    private static int CompareSensor(ISensor a, ISensor b) {
moel@149
   226
      int c = a.SensorType.CompareTo(b.SensorType);
moel@149
   227
      if (c == 0)
moel@149
   228
        return a.Index.CompareTo(b.Index);
moel@149
   229
      else
moel@149
   230
        return c;
moel@149
   231
    }
moel@149
   232
moel@195
   233
    private static void ReportHardwareSensorTree(
moel@195
   234
      IHardware hardware, TextWriter w, string space) 
moel@195
   235
    {
moel@64
   236
      w.WriteLine("{0}|", space);
moel@256
   237
      w.WriteLine("{0}+- {1} ({2})",
moel@64
   238
        space, hardware.Name, hardware.Identifier);
moel@149
   239
      ISensor[] sensors = hardware.Sensors;
moel@195
   240
      Array.Sort(sensors, CompareSensor);
moel@149
   241
      foreach (ISensor sensor in sensors) {
moel@256
   242
        w.WriteLine("{0}|  +- {1,-14} : {2,8:G6} {3,8:G6} {4,8:G6} ({5})", 
moel@256
   243
          space, sensor.Name, sensor.Value, sensor.Min, sensor.Max, 
moel@256
   244
          sensor.Identifier);
moel@149
   245
      }
moel@149
   246
      foreach (IHardware subHardware in hardware.SubHardware)
moel@256
   247
        ReportHardwareSensorTree(subHardware, w, "|  ");
moel@149
   248
    }
moel@149
   249
moel@195
   250
    private static void ReportHardwareParameterTree(
moel@256
   251
      IHardware hardware, TextWriter w, string space) {
moel@149
   252
      w.WriteLine("{0}|", space);
moel@256
   253
      w.WriteLine("{0}+- {1} ({2})",
moel@149
   254
        space, hardware.Name, hardware.Identifier);
moel@149
   255
      ISensor[] sensors = hardware.Sensors;
moel@195
   256
      Array.Sort(sensors, CompareSensor);
moel@149
   257
      foreach (ISensor sensor in sensors) {
moel@256
   258
        string innerSpace = space + "|  ";
moel@149
   259
        if (sensor.Parameters.Length > 0) {
moel@256
   260
          w.WriteLine("{0}|", innerSpace);
moel@256
   261
          w.WriteLine("{0}+- {1} ({2})",
moel@256
   262
            innerSpace, sensor.Name, sensor.Identifier);
moel@149
   263
          foreach (IParameter parameter in sensor.Parameters) {
moel@256
   264
            string innerInnerSpace = innerSpace + "|  ";
moel@256
   265
            w.WriteLine("{0}+- {1} : {2}",
moel@256
   266
              innerInnerSpace, parameter.Name,
moel@149
   267
              string.Format(CultureInfo.InvariantCulture, "{0} : {1}",
moel@149
   268
                parameter.DefaultValue, parameter.Value));
moel@149
   269
          }
moel@64
   270
        }
moel@64
   271
      }
moel@64
   272
      foreach (IHardware subHardware in hardware.SubHardware)
moel@256
   273
        ReportHardwareParameterTree(subHardware, w, "|  ");
moel@64
   274
    }
moel@64
   275
moel@195
   276
    private static void ReportHardware(IHardware hardware, TextWriter w) {
moel@64
   277
      string hardwareReport = hardware.GetReport();
moel@167
   278
      if (!string.IsNullOrEmpty(hardwareReport)) {
moel@64
   279
        NewSection(w);
moel@64
   280
        w.Write(hardwareReport);
moel@64
   281
      }
moel@64
   282
      foreach (IHardware subHardware in hardware.SubHardware)
moel@64
   283
        ReportHardware(subHardware, w);
moel@64
   284
    }
moel@64
   285
moel@83
   286
    public string GetReport() {
moel@28
   287
moel@166
   288
      using (StringWriter w = new StringWriter(CultureInfo.InvariantCulture)) {
moel@28
   289
moel@28
   290
        w.WriteLine();
moel@28
   291
        w.WriteLine("Open Hardware Monitor Report");
moel@28
   292
        w.WriteLine();
moel@28
   293
moel@83
   294
        Version version = typeof(Computer).Assembly.GetName().Version;
moel@83
   295
moel@28
   296
        NewSection(w);
moel@28
   297
        w.Write("Version: "); w.WriteLine(version.ToString());
moel@28
   298
        w.WriteLine();
moel@28
   299
moel@28
   300
        NewSection(w);
moel@119
   301
        w.Write("Common Language Runtime: ");
moel@119
   302
        w.WriteLine(Environment.Version.ToString());
moel@119
   303
        w.Write("Operating System: ");
moel@119
   304
        w.WriteLine(Environment.OSVersion.ToString());
moel@119
   305
        w.Write("Process Type: ");
moel@119
   306
        w.WriteLine(IntPtr.Size == 4 ? "32-Bit" : "64-Bit");
moel@119
   307
        w.WriteLine();
moel@119
   308
moel@254
   309
        string r = Ring0.GetReport();
moel@254
   310
        if (r != null) {
moel@254
   311
          NewSection(w);
moel@254
   312
          w.Write(r);
moel@254
   313
          w.WriteLine();
moel@254
   314
        }
moel@254
   315
moel@119
   316
        NewSection(w);
moel@149
   317
        w.WriteLine("Sensors");
moel@149
   318
        w.WriteLine();
moel@28
   319
        foreach (IGroup group in groups) {
moel@64
   320
          foreach (IHardware hardware in group.Hardware)
moel@149
   321
            ReportHardwareSensorTree(hardware, w, "");
moel@149
   322
        }
moel@149
   323
        w.WriteLine();
moel@149
   324
moel@149
   325
        NewSection(w);
moel@149
   326
        w.WriteLine("Parameters");
moel@149
   327
        w.WriteLine();
moel@149
   328
        foreach (IGroup group in groups) {
moel@149
   329
          foreach (IHardware hardware in group.Hardware)
moel@149
   330
            ReportHardwareParameterTree(hardware, w, "");
moel@28
   331
        }
moel@28
   332
        w.WriteLine();
moel@28
   333
moel@28
   334
        foreach (IGroup group in groups) {
moel@28
   335
          string report = group.GetReport();
moel@167
   336
          if (!string.IsNullOrEmpty(report)) {
moel@28
   337
            NewSection(w);
moel@28
   338
            w.Write(report);
moel@28
   339
          }
moel@28
   340
moel@28
   341
          IHardware[] hardwareArray = group.Hardware;
moel@64
   342
          foreach (IHardware hardware in hardwareArray)
moel@64
   343
            ReportHardware(hardware, w);
moel@83
   344
moel@28
   345
        }
moel@83
   346
        return w.ToString();
moel@28
   347
      }
moel@28
   348
    }
moel@28
   349
moel@83
   350
    public void Close() {      
moel@28
   351
      if (!open)
moel@28
   352
        return;
moel@28
   353
moel@262
   354
      while (groups.Count > 0) {
moel@262
   355
        IGroup group = groups[groups.Count - 1];
moel@360
   356
        Remove(group);         
moel@262
   357
      } 
moel@28
   358
moel@236
   359
      Opcode.Close();
moel@236
   360
      Ring0.Close();
moel@167
   361
moel@370
   362
      this.smbios = null;
moel@370
   363
moel@28
   364
      open = false;
moel@28
   365
    }
moel@28
   366
moel@28
   367
    public event HardwareEventHandler HardwareAdded;
moel@28
   368
    public event HardwareEventHandler HardwareRemoved;
moel@110
   369
moel@110
   370
    public void Accept(IVisitor visitor) {
moel@167
   371
      if (visitor == null)
moel@167
   372
        throw new ArgumentNullException("visitor");
moel@167
   373
      visitor.VisitComputer(this);
moel@110
   374
    }
moel@110
   375
moel@110
   376
    public void Traverse(IVisitor visitor) {
moel@110
   377
      foreach (IGroup group in groups)
moel@110
   378
        foreach (IHardware hardware in group.Hardware) 
moel@110
   379
          hardware.Accept(visitor);
moel@110
   380
    }
moel@165
   381
moel@165
   382
    private class Settings : ISettings {
moel@165
   383
moel@165
   384
      public bool Contains(string name) {
moel@165
   385
        return false;
moel@165
   386
      }
moel@165
   387
moel@166
   388
      public void SetValue(string name, string value) { }
moel@165
   389
moel@166
   390
      public string GetValue(string name, string value) {
moel@165
   391
        return value;
moel@165
   392
      }
moel@165
   393
moel@165
   394
      public void Remove(string name) { }
moel@165
   395
    }
moel@28
   396
  }
moel@28
   397
}