Hardware/Mainboard/Mainboard.cs
author moel.mich
Mon, 23 Jul 2012 21:54:35 +0000
changeset 370 8e4dedc41924
parent 344 3145aadca3d2
permissions -rw-r--r--
Added a RAM hardware and sensor, fixed Issue 115.
     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-2012 Michael Möller <mmoeller@openhardwaremonitor.org>
     8 	
     9 */
    10 
    11 using System;
    12 using System.Text;
    13 using OpenHardwareMonitor.Hardware.LPC;
    14 
    15 namespace OpenHardwareMonitor.Hardware.Mainboard {
    16   internal class Mainboard : IHardware {
    17     private readonly SMBIOS smbios;
    18     private readonly string name;
    19     private string customName;
    20     private readonly ISettings settings;
    21     private readonly LPCIO lpcio;
    22     private readonly LMSensors lmSensors;
    23     private readonly Hardware[] superIOHardware;
    24 
    25     public Mainboard(SMBIOS smbios, ISettings settings) {
    26       this.settings = settings;
    27       this.smbios = smbios;
    28 
    29       Manufacturer manufacturer = smbios.Board == null ? Manufacturer.Unknown :
    30         Identification.GetManufacturer(smbios.Board.ManufacturerName);
    31 
    32       Model model = smbios.Board == null ? Model.Unknown : 
    33         Identification.GetModel(smbios.Board.ProductName);
    34 
    35       if (smbios.Board != null) {
    36         if (!string.IsNullOrEmpty(smbios.Board.ProductName)) {
    37           if (manufacturer == Manufacturer.Unknown)
    38             this.name = smbios.Board.ProductName;
    39           else
    40             this.name = manufacturer + " " +
    41               smbios.Board.ProductName;
    42         } else {
    43           this.name = manufacturer.ToString();
    44         }
    45       } else {
    46         this.name = Manufacturer.Unknown.ToString();
    47       }
    48 
    49       this.customName = settings.GetValue(
    50         new Identifier(Identifier, "name").ToString(), name);
    51 
    52       ISuperIO[] superIO;
    53       int p = (int)Environment.OSVersion.Platform;
    54       if ((p == 4) || (p == 128)) {
    55         this.lmSensors = new LMSensors();
    56         superIO = lmSensors.SuperIO;
    57       } else {
    58         this.lpcio = new LPCIO();       
    59         superIO = lpcio.SuperIO;
    60       }
    61       
    62       superIOHardware = new Hardware[superIO.Length];
    63       for (int i = 0; i < superIO.Length; i++)
    64         superIOHardware[i] = new SuperIOHardware(this, superIO[i],
    65           manufacturer, model, settings);
    66     }
    67 
    68     public string Name {
    69       get {
    70         return customName;
    71       }
    72       set {
    73         if (!string.IsNullOrEmpty(value))
    74           customName = value;
    75         else
    76           customName = name;
    77         settings.SetValue(new Identifier(Identifier, "name").ToString(),
    78           customName);
    79       }
    80     }
    81 
    82     public Identifier Identifier {
    83       get { return new Identifier("mainboard"); }
    84     }
    85 
    86     public HardwareType HardwareType {
    87       get { return HardwareType.Mainboard; }
    88     }
    89 
    90     public virtual IHardware Parent {
    91       get { return null; }
    92     }
    93 
    94     public string GetReport() {
    95       StringBuilder r = new StringBuilder(); 
    96 
    97       r.AppendLine("Mainboard");
    98       r.AppendLine();           
    99       r.Append(smbios.GetReport());
   100 
   101       if (lpcio != null)
   102         r.Append(lpcio.GetReport());
   103 
   104       byte[] table = 
   105         FirmwareTable.GetTable(FirmwareTable.Provider.ACPI, "TAMG");
   106       if (table != null) {
   107         GigabyteTAMG tamg = new GigabyteTAMG(table);
   108         r.Append(tamg.GetReport());
   109       }
   110 
   111       return r.ToString();
   112     }
   113 
   114     public void Update() { }
   115 
   116     public void Close() {
   117       if (lmSensors != null)
   118         lmSensors.Close();
   119       foreach (Hardware hardware in superIOHardware)
   120         hardware.Close();
   121     }
   122 
   123     public IHardware[] SubHardware {
   124       get { return superIOHardware; }
   125     }
   126 
   127     public ISensor[] Sensors {
   128       get { return new ISensor[0]; }
   129     }
   130 
   131     #pragma warning disable 67
   132     public event SensorEventHandler SensorAdded;
   133     public event SensorEventHandler SensorRemoved;
   134     #pragma warning restore 67
   135 
   136     public void Accept(IVisitor visitor) {
   137       if (visitor == null)
   138         throw new ArgumentNullException("visitor");
   139       visitor.VisitHardware(this);
   140     }
   141 
   142     public void Traverse(IVisitor visitor) {
   143       foreach (IHardware hardware in superIOHardware)
   144         hardware.Accept(visitor);     
   145     }
   146   }
   147 }