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@28: using System.IO;
moel@34: using System.Globalization;
moel@28: using System.Text;
moel@83: using System.Threading;
moel@28: 
moel@28: namespace OpenHardwareMonitor.Hardware {
moel@28: 
moel@83:   public class Computer : IComputer {
moel@28: 
moel@28:     private List<IGroup> groups = new List<IGroup>();
moel@28: 
moel@28:     private bool open = false;
moel@28:     private bool hddEnabled = false;
moel@28: 
moel@28:     public Computer() { }
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@28:     public void Open() {
moel@28:       if (open)
moel@28:         return;
moel@28: 
moel@64:       Add(new Mainboard.MainboardGroup());
moel@28:       Add(new CPU.CPUGroup());
moel@28:       Add(new ATI.ATIGroup());
moel@28:       Add(new Nvidia.NvidiaGroup());
moel@28:       Add(new TBalancer.TBalancerGroup());
moel@28: 
moel@83:       if (hddEnabled)
moel@28:         Add(new HDD.HDDGroup());
moel@28: 
moel@28:       open = true;
moel@28:     }
moel@28: 
moel@64:     private void SubHardwareUpdate(IHardware hardware) {
moel@64:       foreach (IHardware subHardware in hardware.SubHardware) {
moel@64:         subHardware.Update();
moel@64:         SubHardwareUpdate(subHardware);
moel@64:       }
moel@64:     }
moel@64: 
moel@86:     public void Update() {
moel@28:       foreach (IGroup group in groups)
moel@64:         foreach (IHardware hardware in group.Hardware) {
moel@28:           hardware.Update();
moel@64:           SubHardwareUpdate(hardware);
moel@64:         }
moel@28:     }
moel@28: 
moel@28:     public bool HDDEnabled {
moel@28:       get { return hddEnabled; }
moel@28:       set {
moel@28:         if (open && value && !hddEnabled) {
moel@28:           Add(new HDD.HDDGroup());
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@28:     private 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@83:     private void ReportHardwareTree(IHardware hardware, TextWriter w,
moel@83:       string space) {
moel@64:       w.WriteLine("{0}|", space);
moel@83:       w.WriteLine("{0}+-+ {1} ({2})",
moel@64:         space, hardware.Name, hardware.Identifier);
moel@64:       foreach (ISensor sensor in hardware.Sensors) {
moel@64:         w.WriteLine("{0}|   +- {1} : {2} : {3} : {4}",
moel@64:           space, sensor.SensorType, sensor.Index, sensor.Name,
moel@64:             string.Format(CultureInfo.InvariantCulture, "{0} : {1} : {2}",
moel@64:             sensor.Value, sensor.Min, sensor.Max));
moel@64:         foreach (IParameter parameter in sensor.Parameters) {
moel@64:           w.WriteLine("{0}|      +- {1} : {2} : {3}",
moel@64:             space, parameter.Name, parameter.IsDefault,
moel@64:             string.Format(CultureInfo.InvariantCulture, "{0} : {1}",
moel@64:               parameter.DefaultValue, parameter.Value));
moel@64:         }
moel@64:       }
moel@64:       foreach (IHardware subHardware in hardware.SubHardware)
moel@64:         ReportHardwareTree(subHardware, w, "|   ");
moel@64:     }
moel@64: 
moel@64:     private void ReportHardware(IHardware hardware, TextWriter w) {
moel@64:       string hardwareReport = hardware.GetReport();
moel@64:       if (hardwareReport != null && 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@83:       using (StringWriter w = new StringWriter()) {
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@28:         foreach (IGroup group in groups) {
moel@64:           foreach (IHardware hardware in group.Hardware)
moel@83:             ReportHardwareTree(hardware, w, "");
moel@28:         }
moel@28:         w.WriteLine();
moel@28: 
moel@28:         foreach (IGroup group in groups) {
moel@28:           string report = group.GetReport();
moel@47:           if (report != null && 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@28:       foreach (IGroup group in groups)
moel@28:         group.Close();
moel@28:       groups.Clear();
moel@28: 
moel@28:       open = false;
moel@28:     }
moel@28: 
moel@28:     public event HardwareEventHandler HardwareAdded;
moel@28:     public event HardwareEventHandler HardwareRemoved;
moel@28:   }
moel@28: }