Hardware/Computer.cs
author moel.mich
Mon, 02 Jul 2012 21:14:40 +0000
changeset 357 fb8dc26f65a4
parent 324 c6ee430d6995
child 360 c1a4377c11d1
permissions -rw-r--r--
Added mainboard specific configurations for the following Gigabyte mainboards: EX58-UD3R, G41M-Combo, G41MT-S2, G41MT-S2P, GA-MA770T-UD3P, GA-MA785GM-US2H, GA-MA78LM-S2H, GA-MA790X-UD3P, H55-USB3, H55N-USB3, H61M-DS2 REV 1.2, H61M-USB3-B3 REV 2.0, H67A-USB3-B3, P55A-UD3, P67A-UD3-B3, P67A-UD3R-B3, Z68A-D3H-B3, Z68AP-D3, Z68X-UD3H-B3.
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@344
     7
  Copyright (C) 2009-2011 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@195
    26
    private bool hddEnabled;    
moel@28
    27
moel@165
    28
    public Computer() {
moel@165
    29
      this.settings = new Settings();
moel@165
    30
    }
moel@165
    31
moel@165
    32
    public Computer(ISettings settings) {
moel@195
    33
      this.settings = settings ?? new Settings();
moel@165
    34
    }
moel@28
    35
moel@28
    36
    private void Add(IGroup group) {
moel@28
    37
      if (groups.Contains(group))
moel@28
    38
        return;
moel@28
    39
moel@28
    40
      groups.Add(group);
moel@28
    41
moel@28
    42
      if (HardwareAdded != null)
moel@83
    43
        foreach (IHardware hardware in group.Hardware)
moel@28
    44
          HardwareAdded(hardware);
moel@28
    45
    }
moel@28
    46
moel@28
    47
    private void Remove(IGroup group) {
moel@28
    48
      if (!groups.Contains(group))
moel@28
    49
        return;
moel@28
    50
moel@28
    51
      groups.Remove(group);
moel@28
    52
moel@28
    53
      if (HardwareRemoved != null)
moel@83
    54
        foreach (IHardware hardware in group.Hardware)
moel@28
    55
          HardwareRemoved(hardware);
moel@28
    56
    }
moel@28
    57
moel@167
    58
    [SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode = true)]
moel@28
    59
    public void Open() {
moel@28
    60
      if (open)
moel@28
    61
        return;
moel@28
    62
moel@236
    63
      Ring0.Open();
moel@236
    64
      Opcode.Open();
moel@167
    65
moel@165
    66
      Add(new Mainboard.MainboardGroup(settings));
moel@165
    67
      Add(new CPU.CPUGroup(settings));
moel@165
    68
      Add(new ATI.ATIGroup(settings));
moel@171
    69
      Add(new Nvidia.NvidiaGroup(settings));      
moel@165
    70
      Add(new TBalancer.TBalancerGroup(settings));
moel@171
    71
      Add(new Heatmaster.HeatmasterGroup(settings));
moel@28
    72
moel@83
    73
      if (hddEnabled)
moel@324
    74
        Add(new HDD.HarddriveGroup(settings));
moel@28
    75
moel@28
    76
      open = true;
moel@28
    77
    }
moel@167
    78
    
moel@28
    79
    public bool HDDEnabled {
moel@28
    80
      get { return hddEnabled; }
moel@167
    81
moel@167
    82
      [SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode = true)]
moel@28
    83
      set {
moel@28
    84
        if (open && value && !hddEnabled) {
moel@324
    85
          Add(new HDD.HarddriveGroup(settings));
moel@28
    86
        } else if (open && !value && hddEnabled) {
moel@28
    87
          List<IGroup> list = new List<IGroup>();
moel@28
    88
          foreach (IGroup group in groups)
moel@324
    89
            if (group is HDD.HarddriveGroup)
moel@28
    90
              list.Add(group);
moel@28
    91
          foreach (IGroup group in list)
moel@83
    92
            Remove(group);
moel@28
    93
        }
moel@28
    94
        hddEnabled = value;
moel@28
    95
      }
moel@28
    96
    }
moel@28
    97
moel@83
    98
    public IHardware[] Hardware {
moel@83
    99
      get {
moel@83
   100
        List<IHardware> list = new List<IHardware>();
moel@28
   101
        foreach (IGroup group in groups)
moel@28
   102
          foreach (IHardware hardware in group.Hardware)
moel@83
   103
            list.Add(hardware);
moel@83
   104
        return list.ToArray();
moel@28
   105
      }
moel@28
   106
    }
moel@28
   107
moel@167
   108
    private static void NewSection(TextWriter writer) {
moel@28
   109
      for (int i = 0; i < 8; i++)
moel@28
   110
        writer.Write("----------");
moel@28
   111
      writer.WriteLine();
moel@28
   112
      writer.WriteLine();
moel@28
   113
    }
moel@28
   114
moel@195
   115
    private static int CompareSensor(ISensor a, ISensor b) {
moel@149
   116
      int c = a.SensorType.CompareTo(b.SensorType);
moel@149
   117
      if (c == 0)
moel@149
   118
        return a.Index.CompareTo(b.Index);
moel@149
   119
      else
moel@149
   120
        return c;
moel@149
   121
    }
moel@149
   122
moel@195
   123
    private static void ReportHardwareSensorTree(
moel@195
   124
      IHardware hardware, TextWriter w, string space) 
moel@195
   125
    {
moel@64
   126
      w.WriteLine("{0}|", space);
moel@256
   127
      w.WriteLine("{0}+- {1} ({2})",
moel@64
   128
        space, hardware.Name, hardware.Identifier);
moel@149
   129
      ISensor[] sensors = hardware.Sensors;
moel@195
   130
      Array.Sort(sensors, CompareSensor);
moel@149
   131
      foreach (ISensor sensor in sensors) {
moel@256
   132
        w.WriteLine("{0}|  +- {1,-14} : {2,8:G6} {3,8:G6} {4,8:G6} ({5})", 
moel@256
   133
          space, sensor.Name, sensor.Value, sensor.Min, sensor.Max, 
moel@256
   134
          sensor.Identifier);
moel@149
   135
      }
moel@149
   136
      foreach (IHardware subHardware in hardware.SubHardware)
moel@256
   137
        ReportHardwareSensorTree(subHardware, w, "|  ");
moel@149
   138
    }
moel@149
   139
moel@195
   140
    private static void ReportHardwareParameterTree(
moel@256
   141
      IHardware hardware, TextWriter w, string space) {
moel@149
   142
      w.WriteLine("{0}|", space);
moel@256
   143
      w.WriteLine("{0}+- {1} ({2})",
moel@149
   144
        space, hardware.Name, hardware.Identifier);
moel@149
   145
      ISensor[] sensors = hardware.Sensors;
moel@195
   146
      Array.Sort(sensors, CompareSensor);
moel@149
   147
      foreach (ISensor sensor in sensors) {
moel@256
   148
        string innerSpace = space + "|  ";
moel@149
   149
        if (sensor.Parameters.Length > 0) {
moel@256
   150
          w.WriteLine("{0}|", innerSpace);
moel@256
   151
          w.WriteLine("{0}+- {1} ({2})",
moel@256
   152
            innerSpace, sensor.Name, sensor.Identifier);
moel@149
   153
          foreach (IParameter parameter in sensor.Parameters) {
moel@256
   154
            string innerInnerSpace = innerSpace + "|  ";
moel@256
   155
            w.WriteLine("{0}+- {1} : {2}",
moel@256
   156
              innerInnerSpace, parameter.Name,
moel@149
   157
              string.Format(CultureInfo.InvariantCulture, "{0} : {1}",
moel@149
   158
                parameter.DefaultValue, parameter.Value));
moel@149
   159
          }
moel@64
   160
        }
moel@64
   161
      }
moel@64
   162
      foreach (IHardware subHardware in hardware.SubHardware)
moel@256
   163
        ReportHardwareParameterTree(subHardware, w, "|  ");
moel@64
   164
    }
moel@64
   165
moel@195
   166
    private static void ReportHardware(IHardware hardware, TextWriter w) {
moel@64
   167
      string hardwareReport = hardware.GetReport();
moel@167
   168
      if (!string.IsNullOrEmpty(hardwareReport)) {
moel@64
   169
        NewSection(w);
moel@64
   170
        w.Write(hardwareReport);
moel@64
   171
      }
moel@64
   172
      foreach (IHardware subHardware in hardware.SubHardware)
moel@64
   173
        ReportHardware(subHardware, w);
moel@64
   174
    }
moel@64
   175
moel@83
   176
    public string GetReport() {
moel@28
   177
moel@166
   178
      using (StringWriter w = new StringWriter(CultureInfo.InvariantCulture)) {
moel@28
   179
moel@28
   180
        w.WriteLine();
moel@28
   181
        w.WriteLine("Open Hardware Monitor Report");
moel@28
   182
        w.WriteLine();
moel@28
   183
moel@83
   184
        Version version = typeof(Computer).Assembly.GetName().Version;
moel@83
   185
moel@28
   186
        NewSection(w);
moel@28
   187
        w.Write("Version: "); w.WriteLine(version.ToString());
moel@28
   188
        w.WriteLine();
moel@28
   189
moel@28
   190
        NewSection(w);
moel@119
   191
        w.Write("Common Language Runtime: ");
moel@119
   192
        w.WriteLine(Environment.Version.ToString());
moel@119
   193
        w.Write("Operating System: ");
moel@119
   194
        w.WriteLine(Environment.OSVersion.ToString());
moel@119
   195
        w.Write("Process Type: ");
moel@119
   196
        w.WriteLine(IntPtr.Size == 4 ? "32-Bit" : "64-Bit");
moel@119
   197
        w.WriteLine();
moel@119
   198
moel@254
   199
        string r = Ring0.GetReport();
moel@254
   200
        if (r != null) {
moel@254
   201
          NewSection(w);
moel@254
   202
          w.Write(r);
moel@254
   203
          w.WriteLine();
moel@254
   204
        }
moel@254
   205
moel@119
   206
        NewSection(w);
moel@149
   207
        w.WriteLine("Sensors");
moel@149
   208
        w.WriteLine();
moel@28
   209
        foreach (IGroup group in groups) {
moel@64
   210
          foreach (IHardware hardware in group.Hardware)
moel@149
   211
            ReportHardwareSensorTree(hardware, w, "");
moel@149
   212
        }
moel@149
   213
        w.WriteLine();
moel@149
   214
moel@149
   215
        NewSection(w);
moel@149
   216
        w.WriteLine("Parameters");
moel@149
   217
        w.WriteLine();
moel@149
   218
        foreach (IGroup group in groups) {
moel@149
   219
          foreach (IHardware hardware in group.Hardware)
moel@149
   220
            ReportHardwareParameterTree(hardware, w, "");
moel@28
   221
        }
moel@28
   222
        w.WriteLine();
moel@28
   223
moel@28
   224
        foreach (IGroup group in groups) {
moel@28
   225
          string report = group.GetReport();
moel@167
   226
          if (!string.IsNullOrEmpty(report)) {
moel@28
   227
            NewSection(w);
moel@28
   228
            w.Write(report);
moel@28
   229
          }
moel@28
   230
moel@28
   231
          IHardware[] hardwareArray = group.Hardware;
moel@64
   232
          foreach (IHardware hardware in hardwareArray)
moel@64
   233
            ReportHardware(hardware, w);
moel@83
   234
moel@28
   235
        }
moel@83
   236
        return w.ToString();
moel@28
   237
      }
moel@28
   238
    }
moel@28
   239
moel@83
   240
    public void Close() {      
moel@28
   241
      if (!open)
moel@28
   242
        return;
moel@28
   243
moel@262
   244
      while (groups.Count > 0) {
moel@262
   245
        IGroup group = groups[groups.Count - 1];
moel@262
   246
        Remove(group);
moel@262
   247
        group.Close(); 
moel@262
   248
      } 
moel@28
   249
moel@236
   250
      Opcode.Close();
moel@236
   251
      Ring0.Close();
moel@167
   252
moel@28
   253
      open = false;
moel@28
   254
    }
moel@28
   255
moel@28
   256
    public event HardwareEventHandler HardwareAdded;
moel@28
   257
    public event HardwareEventHandler HardwareRemoved;
moel@110
   258
moel@110
   259
    public void Accept(IVisitor visitor) {
moel@167
   260
      if (visitor == null)
moel@167
   261
        throw new ArgumentNullException("visitor");
moel@167
   262
      visitor.VisitComputer(this);
moel@110
   263
    }
moel@110
   264
moel@110
   265
    public void Traverse(IVisitor visitor) {
moel@110
   266
      foreach (IGroup group in groups)
moel@110
   267
        foreach (IHardware hardware in group.Hardware) 
moel@110
   268
          hardware.Accept(visitor);
moel@110
   269
    }
moel@165
   270
moel@165
   271
    private class Settings : ISettings {
moel@165
   272
moel@165
   273
      public bool Contains(string name) {
moel@165
   274
        return false;
moel@165
   275
      }
moel@165
   276
moel@166
   277
      public void SetValue(string name, string value) { }
moel@165
   278
moel@166
   279
      public string GetValue(string name, string value) {
moel@165
   280
        return value;
moel@165
   281
      }
moel@165
   282
moel@165
   283
      public void Remove(string name) { }
moel@165
   284
    }
moel@28
   285
  }
moel@28
   286
}