moel@28: /*
moel@28:   
moel@28:   Version: MPL 1.1/GPL 2.0/LGPL 2.1
moel@28: 
moel@28:   The contents of this file are subject to the Mozilla Public License Version
moel@28:   1.1 (the "License"); you may not use this file except in compliance with
moel@28:   the License. You may obtain a copy of the License at
moel@28:  
moel@28:   http://www.mozilla.org/MPL/
moel@28: 
moel@28:   Software distributed under the License is distributed on an "AS IS" basis,
moel@28:   WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
moel@28:   for the specific language governing rights and limitations under the License.
moel@28: 
moel@28:   The Original Code is the Open Hardware Monitor code.
moel@28: 
moel@28:   The Initial Developer of the Original Code is 
moel@28:   Michael Möller <m.moeller@gmx.ch>.
moel@28:   Portions created by the Initial Developer are Copyright (C) 2009-2010
moel@28:   the Initial Developer. All Rights Reserved.
moel@28: 
moel@28:   Contributor(s):
moel@28: 
moel@28:   Alternatively, the contents of this file may be used under the terms of
moel@28:   either the GNU General Public License Version 2 or later (the "GPL"), or
moel@28:   the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
moel@28:   in which case the provisions of the GPL or the LGPL are applicable instead
moel@28:   of those above. If you wish to allow use of your version of this file only
moel@28:   under the terms of either the GPL or the LGPL, and not to allow others to
moel@28:   use your version of this file under the terms of the MPL, indicate your
moel@28:   decision by deleting the provisions above and replace them with the notice
moel@28:   and other provisions required by the GPL or the LGPL. If you do not delete
moel@28:   the provisions above, a recipient may use your version of this file under
moel@28:   the terms of any one of the MPL, the GPL or the LGPL.
moel@28:  
moel@28: */
moel@28: 
moel@28: using System;
moel@28: using System.Collections.Generic;
moel@182: using System.Globalization;
moel@28: using System.IO;
moel@167: using System.Security.Permissions;
moel@236: using System.Reflection;
moel@167: 
moel@28: namespace OpenHardwareMonitor.Hardware {
moel@28: 
moel@83:   public class Computer : IComputer {
moel@28: 
moel@195:     private readonly List<IGroup> groups = new List<IGroup>();
moel@195:     private readonly ISettings settings;
moel@28: 
moel@195:     private bool open;
moel@195:     private bool hddEnabled;    
moel@28: 
moel@165:     public Computer() {
moel@165:       this.settings = new Settings();
moel@165:     }
moel@165: 
moel@165:     public Computer(ISettings settings) {
moel@195:       this.settings = settings ?? new Settings();
moel@165:     }
moel@28: 
moel@28:     private void Add(IGroup group) {
moel@28:       if (groups.Contains(group))
moel@28:         return;
moel@28: 
moel@28:       groups.Add(group);
moel@28: 
moel@28:       if (HardwareAdded != null)
moel@83:         foreach (IHardware hardware in group.Hardware)
moel@28:           HardwareAdded(hardware);
moel@28:     }
moel@28: 
moel@28:     private void Remove(IGroup group) {
moel@28:       if (!groups.Contains(group))
moel@28:         return;
moel@28: 
moel@28:       groups.Remove(group);
moel@28: 
moel@28:       if (HardwareRemoved != null)
moel@83:         foreach (IHardware hardware in group.Hardware)
moel@28:           HardwareRemoved(hardware);
moel@28:     }
moel@28: 
moel@167:     [SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode = true)]
moel@28:     public void Open() {
moel@28:       if (open)
moel@28:         return;
moel@28: 
moel@236:       Ring0.Open();
moel@236:       Opcode.Open();
moel@167: 
moel@165:       Add(new Mainboard.MainboardGroup(settings));
moel@165:       Add(new CPU.CPUGroup(settings));
moel@165:       Add(new ATI.ATIGroup(settings));
moel@171:       Add(new Nvidia.NvidiaGroup(settings));      
moel@165:       Add(new TBalancer.TBalancerGroup(settings));
moel@171:       Add(new Heatmaster.HeatmasterGroup(settings));
moel@28: 
moel@83:       if (hddEnabled)
moel@165:         Add(new HDD.HDDGroup(settings));
moel@28: 
moel@28:       open = true;
moel@28:     }
moel@167:     
moel@28:     public bool HDDEnabled {
moel@28:       get { return hddEnabled; }
moel@167: 
moel@167:       [SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode = true)]
moel@28:       set {
moel@28:         if (open && value && !hddEnabled) {
moel@165:           Add(new HDD.HDDGroup(settings));
moel@28:         } else if (open && !value && hddEnabled) {
moel@28:           List<IGroup> list = new List<IGroup>();
moel@28:           foreach (IGroup group in groups)
moel@28:             if (group is HDD.HDDGroup)
moel@28:               list.Add(group);
moel@28:           foreach (IGroup group in list)
moel@83:             Remove(group);
moel@28:         }
moel@28:         hddEnabled = value;
moel@28:       }
moel@28:     }
moel@28: 
moel@83:     public IHardware[] Hardware {
moel@83:       get {
moel@83:         List<IHardware> list = new List<IHardware>();
moel@28:         foreach (IGroup group in groups)
moel@28:           foreach (IHardware hardware in group.Hardware)
moel@83:             list.Add(hardware);
moel@83:         return list.ToArray();
moel@28:       }
moel@28:     }
moel@28: 
moel@167:     private static void NewSection(TextWriter writer) {
moel@28:       for (int i = 0; i < 8; i++)
moel@28:         writer.Write("----------");
moel@28:       writer.WriteLine();
moel@28:       writer.WriteLine();
moel@28:     }
moel@28: 
moel@195:     private static int CompareSensor(ISensor a, ISensor b) {
moel@149:       int c = a.SensorType.CompareTo(b.SensorType);
moel@149:       if (c == 0)
moel@149:         return a.Index.CompareTo(b.Index);
moel@149:       else
moel@149:         return c;
moel@149:     }
moel@149: 
moel@195:     private static void ReportHardwareSensorTree(
moel@195:       IHardware hardware, TextWriter w, string space) 
moel@195:     {
moel@64:       w.WriteLine("{0}|", space);
moel@256:       w.WriteLine("{0}+- {1} ({2})",
moel@64:         space, hardware.Name, hardware.Identifier);
moel@149:       ISensor[] sensors = hardware.Sensors;
moel@195:       Array.Sort(sensors, CompareSensor);
moel@149:       foreach (ISensor sensor in sensors) {
moel@256:         w.WriteLine("{0}|  +- {1,-14} : {2,8:G6} {3,8:G6} {4,8:G6} ({5})", 
moel@256:           space, sensor.Name, sensor.Value, sensor.Min, sensor.Max, 
moel@256:           sensor.Identifier);
moel@149:       }
moel@149:       foreach (IHardware subHardware in hardware.SubHardware)
moel@256:         ReportHardwareSensorTree(subHardware, w, "|  ");
moel@149:     }
moel@149: 
moel@195:     private static void ReportHardwareParameterTree(
moel@256:       IHardware hardware, TextWriter w, string space) {
moel@149:       w.WriteLine("{0}|", space);
moel@256:       w.WriteLine("{0}+- {1} ({2})",
moel@149:         space, hardware.Name, hardware.Identifier);
moel@149:       ISensor[] sensors = hardware.Sensors;
moel@195:       Array.Sort(sensors, CompareSensor);
moel@149:       foreach (ISensor sensor in sensors) {
moel@256:         string innerSpace = space + "|  ";
moel@149:         if (sensor.Parameters.Length > 0) {
moel@256:           w.WriteLine("{0}|", innerSpace);
moel@256:           w.WriteLine("{0}+- {1} ({2})",
moel@256:             innerSpace, sensor.Name, sensor.Identifier);
moel@149:           foreach (IParameter parameter in sensor.Parameters) {
moel@256:             string innerInnerSpace = innerSpace + "|  ";
moel@256:             w.WriteLine("{0}+- {1} : {2}",
moel@256:               innerInnerSpace, parameter.Name,
moel@149:               string.Format(CultureInfo.InvariantCulture, "{0} : {1}",
moel@149:                 parameter.DefaultValue, parameter.Value));
moel@149:           }
moel@64:         }
moel@64:       }
moel@64:       foreach (IHardware subHardware in hardware.SubHardware)
moel@256:         ReportHardwareParameterTree(subHardware, w, "|  ");
moel@64:     }
moel@64: 
moel@195:     private static void ReportHardware(IHardware hardware, TextWriter w) {
moel@64:       string hardwareReport = hardware.GetReport();
moel@167:       if (!string.IsNullOrEmpty(hardwareReport)) {
moel@64:         NewSection(w);
moel@64:         w.Write(hardwareReport);
moel@64:       }
moel@64:       foreach (IHardware subHardware in hardware.SubHardware)
moel@64:         ReportHardware(subHardware, w);
moel@64:     }
moel@64: 
moel@83:     public string GetReport() {
moel@28: 
moel@166:       using (StringWriter w = new StringWriter(CultureInfo.InvariantCulture)) {
moel@28: 
moel@28:         w.WriteLine();
moel@28:         w.WriteLine("Open Hardware Monitor Report");
moel@28:         w.WriteLine();
moel@28: 
moel@83:         Version version = typeof(Computer).Assembly.GetName().Version;
moel@83: 
moel@28:         NewSection(w);
moel@28:         w.Write("Version: "); w.WriteLine(version.ToString());
moel@28:         w.WriteLine();
moel@28: 
moel@28:         NewSection(w);
moel@119:         w.Write("Common Language Runtime: ");
moel@119:         w.WriteLine(Environment.Version.ToString());
moel@119:         w.Write("Operating System: ");
moel@119:         w.WriteLine(Environment.OSVersion.ToString());
moel@119:         w.Write("Process Type: ");
moel@119:         w.WriteLine(IntPtr.Size == 4 ? "32-Bit" : "64-Bit");
moel@119:         w.WriteLine();
moel@119: 
moel@254:         string r = Ring0.GetReport();
moel@254:         if (r != null) {
moel@254:           NewSection(w);
moel@254:           w.Write(r);
moel@254:           w.WriteLine();
moel@254:         }
moel@254: 
moel@119:         NewSection(w);
moel@149:         w.WriteLine("Sensors");
moel@149:         w.WriteLine();
moel@28:         foreach (IGroup group in groups) {
moel@64:           foreach (IHardware hardware in group.Hardware)
moel@149:             ReportHardwareSensorTree(hardware, w, "");
moel@149:         }
moel@149:         w.WriteLine();
moel@149: 
moel@149:         NewSection(w);
moel@149:         w.WriteLine("Parameters");
moel@149:         w.WriteLine();
moel@149:         foreach (IGroup group in groups) {
moel@149:           foreach (IHardware hardware in group.Hardware)
moel@149:             ReportHardwareParameterTree(hardware, w, "");
moel@28:         }
moel@28:         w.WriteLine();
moel@28: 
moel@28:         foreach (IGroup group in groups) {
moel@28:           string report = group.GetReport();
moel@167:           if (!string.IsNullOrEmpty(report)) {
moel@28:             NewSection(w);
moel@28:             w.Write(report);
moel@28:           }
moel@28: 
moel@28:           IHardware[] hardwareArray = group.Hardware;
moel@64:           foreach (IHardware hardware in hardwareArray)
moel@64:             ReportHardware(hardware, w);
moel@83: 
moel@28:         }
moel@83:         return w.ToString();
moel@28:       }
moel@28:     }
moel@28: 
moel@83:     public void Close() {      
moel@28:       if (!open)
moel@28:         return;
moel@28: 
moel@262:       while (groups.Count > 0) {
moel@262:         IGroup group = groups[groups.Count - 1];
moel@262:         Remove(group);
moel@262:         group.Close(); 
moel@262:       } 
moel@28: 
moel@236:       Opcode.Close();
moel@236:       Ring0.Close();
moel@167: 
moel@28:       open = false;
moel@28:     }
moel@28: 
moel@28:     public event HardwareEventHandler HardwareAdded;
moel@28:     public event HardwareEventHandler HardwareRemoved;
moel@110: 
moel@110:     public void Accept(IVisitor visitor) {
moel@167:       if (visitor == null)
moel@167:         throw new ArgumentNullException("visitor");
moel@167:       visitor.VisitComputer(this);
moel@110:     }
moel@110: 
moel@110:     public void Traverse(IVisitor visitor) {
moel@110:       foreach (IGroup group in groups)
moel@110:         foreach (IHardware hardware in group.Hardware) 
moel@110:           hardware.Accept(visitor);
moel@110:     }
moel@165: 
moel@165:     private class Settings : ISettings {
moel@165: 
moel@165:       public bool Contains(string name) {
moel@165:         return false;
moel@165:       }
moel@165: 
moel@166:       public void SetValue(string name, string value) { }
moel@165: 
moel@166:       public string GetValue(string name, string value) {
moel@165:         return value;
moel@165:       }
moel@165: 
moel@165:       public void Remove(string name) { }
moel@165:     }
moel@28:   }
moel@28: }