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.
     1 /*
     2  
     3   This Source Code Form is subject to the terms of the Mozilla Public
     4   License, v. 2.0. If a copy of the MPL was not distributed with this
     5   file, You can obtain one at http://mozilla.org/MPL/2.0/.
     6  
     7   Copyright (C) 2009-2011 Michael Möller <mmoeller@openhardwaremonitor.org>
     8 	
     9 */
    10 
    11 using System;
    12 using System.Collections.Generic;
    13 using System.Globalization;
    14 using System.IO;
    15 using System.Security.Permissions;
    16 using System.Reflection;
    17 
    18 namespace OpenHardwareMonitor.Hardware {
    19 
    20   public class Computer : IComputer {
    21 
    22     private readonly List<IGroup> groups = new List<IGroup>();
    23     private readonly ISettings settings;
    24 
    25     private bool open;
    26     private bool hddEnabled;    
    27 
    28     public Computer() {
    29       this.settings = new Settings();
    30     }
    31 
    32     public Computer(ISettings settings) {
    33       this.settings = settings ?? new Settings();
    34     }
    35 
    36     private void Add(IGroup group) {
    37       if (groups.Contains(group))
    38         return;
    39 
    40       groups.Add(group);
    41 
    42       if (HardwareAdded != null)
    43         foreach (IHardware hardware in group.Hardware)
    44           HardwareAdded(hardware);
    45     }
    46 
    47     private void Remove(IGroup group) {
    48       if (!groups.Contains(group))
    49         return;
    50 
    51       groups.Remove(group);
    52 
    53       if (HardwareRemoved != null)
    54         foreach (IHardware hardware in group.Hardware)
    55           HardwareRemoved(hardware);
    56     }
    57 
    58     [SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode = true)]
    59     public void Open() {
    60       if (open)
    61         return;
    62 
    63       Ring0.Open();
    64       Opcode.Open();
    65 
    66       Add(new Mainboard.MainboardGroup(settings));
    67       Add(new CPU.CPUGroup(settings));
    68       Add(new ATI.ATIGroup(settings));
    69       Add(new Nvidia.NvidiaGroup(settings));      
    70       Add(new TBalancer.TBalancerGroup(settings));
    71       Add(new Heatmaster.HeatmasterGroup(settings));
    72 
    73       if (hddEnabled)
    74         Add(new HDD.HarddriveGroup(settings));
    75 
    76       open = true;
    77     }
    78     
    79     public bool HDDEnabled {
    80       get { return hddEnabled; }
    81 
    82       [SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode = true)]
    83       set {
    84         if (open && value && !hddEnabled) {
    85           Add(new HDD.HarddriveGroup(settings));
    86         } else if (open && !value && hddEnabled) {
    87           List<IGroup> list = new List<IGroup>();
    88           foreach (IGroup group in groups)
    89             if (group is HDD.HarddriveGroup)
    90               list.Add(group);
    91           foreach (IGroup group in list)
    92             Remove(group);
    93         }
    94         hddEnabled = value;
    95       }
    96     }
    97 
    98     public IHardware[] Hardware {
    99       get {
   100         List<IHardware> list = new List<IHardware>();
   101         foreach (IGroup group in groups)
   102           foreach (IHardware hardware in group.Hardware)
   103             list.Add(hardware);
   104         return list.ToArray();
   105       }
   106     }
   107 
   108     private static void NewSection(TextWriter writer) {
   109       for (int i = 0; i < 8; i++)
   110         writer.Write("----------");
   111       writer.WriteLine();
   112       writer.WriteLine();
   113     }
   114 
   115     private static int CompareSensor(ISensor a, ISensor b) {
   116       int c = a.SensorType.CompareTo(b.SensorType);
   117       if (c == 0)
   118         return a.Index.CompareTo(b.Index);
   119       else
   120         return c;
   121     }
   122 
   123     private static void ReportHardwareSensorTree(
   124       IHardware hardware, TextWriter w, string space) 
   125     {
   126       w.WriteLine("{0}|", space);
   127       w.WriteLine("{0}+- {1} ({2})",
   128         space, hardware.Name, hardware.Identifier);
   129       ISensor[] sensors = hardware.Sensors;
   130       Array.Sort(sensors, CompareSensor);
   131       foreach (ISensor sensor in sensors) {
   132         w.WriteLine("{0}|  +- {1,-14} : {2,8:G6} {3,8:G6} {4,8:G6} ({5})", 
   133           space, sensor.Name, sensor.Value, sensor.Min, sensor.Max, 
   134           sensor.Identifier);
   135       }
   136       foreach (IHardware subHardware in hardware.SubHardware)
   137         ReportHardwareSensorTree(subHardware, w, "|  ");
   138     }
   139 
   140     private static void ReportHardwareParameterTree(
   141       IHardware hardware, TextWriter w, string space) {
   142       w.WriteLine("{0}|", space);
   143       w.WriteLine("{0}+- {1} ({2})",
   144         space, hardware.Name, hardware.Identifier);
   145       ISensor[] sensors = hardware.Sensors;
   146       Array.Sort(sensors, CompareSensor);
   147       foreach (ISensor sensor in sensors) {
   148         string innerSpace = space + "|  ";
   149         if (sensor.Parameters.Length > 0) {
   150           w.WriteLine("{0}|", innerSpace);
   151           w.WriteLine("{0}+- {1} ({2})",
   152             innerSpace, sensor.Name, sensor.Identifier);
   153           foreach (IParameter parameter in sensor.Parameters) {
   154             string innerInnerSpace = innerSpace + "|  ";
   155             w.WriteLine("{0}+- {1} : {2}",
   156               innerInnerSpace, parameter.Name,
   157               string.Format(CultureInfo.InvariantCulture, "{0} : {1}",
   158                 parameter.DefaultValue, parameter.Value));
   159           }
   160         }
   161       }
   162       foreach (IHardware subHardware in hardware.SubHardware)
   163         ReportHardwareParameterTree(subHardware, w, "|  ");
   164     }
   165 
   166     private static void ReportHardware(IHardware hardware, TextWriter w) {
   167       string hardwareReport = hardware.GetReport();
   168       if (!string.IsNullOrEmpty(hardwareReport)) {
   169         NewSection(w);
   170         w.Write(hardwareReport);
   171       }
   172       foreach (IHardware subHardware in hardware.SubHardware)
   173         ReportHardware(subHardware, w);
   174     }
   175 
   176     public string GetReport() {
   177 
   178       using (StringWriter w = new StringWriter(CultureInfo.InvariantCulture)) {
   179 
   180         w.WriteLine();
   181         w.WriteLine("Open Hardware Monitor Report");
   182         w.WriteLine();
   183 
   184         Version version = typeof(Computer).Assembly.GetName().Version;
   185 
   186         NewSection(w);
   187         w.Write("Version: "); w.WriteLine(version.ToString());
   188         w.WriteLine();
   189 
   190         NewSection(w);
   191         w.Write("Common Language Runtime: ");
   192         w.WriteLine(Environment.Version.ToString());
   193         w.Write("Operating System: ");
   194         w.WriteLine(Environment.OSVersion.ToString());
   195         w.Write("Process Type: ");
   196         w.WriteLine(IntPtr.Size == 4 ? "32-Bit" : "64-Bit");
   197         w.WriteLine();
   198 
   199         string r = Ring0.GetReport();
   200         if (r != null) {
   201           NewSection(w);
   202           w.Write(r);
   203           w.WriteLine();
   204         }
   205 
   206         NewSection(w);
   207         w.WriteLine("Sensors");
   208         w.WriteLine();
   209         foreach (IGroup group in groups) {
   210           foreach (IHardware hardware in group.Hardware)
   211             ReportHardwareSensorTree(hardware, w, "");
   212         }
   213         w.WriteLine();
   214 
   215         NewSection(w);
   216         w.WriteLine("Parameters");
   217         w.WriteLine();
   218         foreach (IGroup group in groups) {
   219           foreach (IHardware hardware in group.Hardware)
   220             ReportHardwareParameterTree(hardware, w, "");
   221         }
   222         w.WriteLine();
   223 
   224         foreach (IGroup group in groups) {
   225           string report = group.GetReport();
   226           if (!string.IsNullOrEmpty(report)) {
   227             NewSection(w);
   228             w.Write(report);
   229           }
   230 
   231           IHardware[] hardwareArray = group.Hardware;
   232           foreach (IHardware hardware in hardwareArray)
   233             ReportHardware(hardware, w);
   234 
   235         }
   236         return w.ToString();
   237       }
   238     }
   239 
   240     public void Close() {      
   241       if (!open)
   242         return;
   243 
   244       while (groups.Count > 0) {
   245         IGroup group = groups[groups.Count - 1];
   246         Remove(group);
   247         group.Close(); 
   248       } 
   249 
   250       Opcode.Close();
   251       Ring0.Close();
   252 
   253       open = false;
   254     }
   255 
   256     public event HardwareEventHandler HardwareAdded;
   257     public event HardwareEventHandler HardwareRemoved;
   258 
   259     public void Accept(IVisitor visitor) {
   260       if (visitor == null)
   261         throw new ArgumentNullException("visitor");
   262       visitor.VisitComputer(this);
   263     }
   264 
   265     public void Traverse(IVisitor visitor) {
   266       foreach (IGroup group in groups)
   267         foreach (IHardware hardware in group.Hardware) 
   268           hardware.Accept(visitor);
   269     }
   270 
   271     private class Settings : ISettings {
   272 
   273       public bool Contains(string name) {
   274         return false;
   275       }
   276 
   277       public void SetValue(string name, string value) { }
   278 
   279       public string GetValue(string name, string value) {
   280         return value;
   281       }
   282 
   283       public void Remove(string name) { }
   284     }
   285   }
   286 }