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 . 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 groups = new List(); moel@28: moel@28: private bool open = false; moel@28: private bool hddEnabled = false; moel@165: private ISettings settings; moel@28: moel@165: public Computer() { moel@165: this.settings = new Settings(); moel@165: } moel@165: moel@165: public Computer(ISettings settings) { moel@165: if (settings != null) moel@165: this.settings = settings; moel@165: else { moel@165: this.settings = new Settings(); moel@165: } 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@28: public void Open() { moel@28: if (open) moel@28: return; moel@28: moel@165: Add(new Mainboard.MainboardGroup(settings)); moel@165: Add(new CPU.CPUGroup(settings)); moel@165: Add(new ATI.ATIGroup(settings)); moel@165: Add(new Nvidia.NvidiaGroup(settings)); moel@165: Add(new TBalancer.TBalancerGroup(settings)); moel@28: moel@83: if (hddEnabled) moel@165: Add(new HDD.HDDGroup(settings)); moel@28: moel@28: open = true; moel@28: } moel@28: moel@28: public bool HDDEnabled { moel@28: get { return hddEnabled; } 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 list = new List(); 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 list = new List(); 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@149: private 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@149: private void ReportHardwareSensorTree(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@149: ISensor[] sensors = hardware.Sensors; moel@149: Array.Sort(sensors, CompareSensor); moel@149: foreach (ISensor sensor in sensors) { moel@149: w.WriteLine("{0}| +- {1}[{2}] : {3} : {4}", moel@149: space, sensor.SensorType, sensor.Index, moel@64: string.Format(CultureInfo.InvariantCulture, "{0} : {1} : {2}", moel@149: sensor.Value, sensor.Min, sensor.Max), sensor.Name); moel@149: } moel@149: foreach (IHardware subHardware in hardware.SubHardware) moel@149: ReportHardwareSensorTree(subHardware, w, "| "); moel@149: } moel@149: moel@149: private void ReportHardwareParameterTree(IHardware hardware, TextWriter w, moel@149: string space) { moel@149: w.WriteLine("{0}|", space); moel@149: w.WriteLine("{0}+-+ {1} ({2})", moel@149: space, hardware.Name, hardware.Identifier); moel@149: ISensor[] sensors = hardware.Sensors; moel@149: Array.Sort(sensors, CompareSensor); moel@149: foreach (ISensor sensor in sensors) { moel@149: if (sensor.Parameters.Length > 0) { moel@149: w.WriteLine("{0}| +- {1}[{2}] : {3}", moel@149: space, sensor.SensorType, sensor.Index, sensor.Name); moel@149: foreach (IParameter parameter in sensor.Parameters) { moel@149: w.WriteLine("{0}| +- {1} : {2}", moel@149: space, 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@149: ReportHardwareParameterTree(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@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@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@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@110: moel@110: public void Accept(IVisitor visitor) { moel@166: if (visitor != null) moel@166: 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: }