moel@28: /* moel@28: moel@344: This Source Code Form is subject to the terms of the Mozilla Public moel@344: License, v. 2.0. If a copy of the MPL was not distributed with this moel@344: file, You can obtain one at http://mozilla.org/MPL/2.0/. moel@28: moel@360: Copyright (C) 2009-2012 Michael Möller moel@344: 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 groups = new List(); moel@195: private readonly ISettings settings; moel@28: moel@370: private SMBIOS smbios; moel@370: moel@195: private bool open; moel@360: moel@360: private bool mainboardEnabled; moel@360: private bool cpuEnabled; moel@370: private bool ramEnabled; moel@360: private bool gpuEnabled; moel@360: private bool fanControllerEnabled; moel@370: 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@360: moel@360: group.Close(); moel@360: } moel@360: moel@360: private void RemoveType() where T : IGroup { moel@360: List list = new List(); moel@360: foreach (IGroup group in groups) moel@360: if (group is T) moel@360: list.Add(group); moel@360: foreach (IGroup group in list) moel@360: Remove(group); 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@370: this.smbios = new SMBIOS(); moel@370: moel@236: Ring0.Open(); moel@236: Opcode.Open(); moel@167: moel@360: if (mainboardEnabled) moel@370: Add(new Mainboard.MainboardGroup(smbios, settings)); moel@360: moel@360: if (cpuEnabled) moel@360: Add(new CPU.CPUGroup(settings)); moel@360: moel@370: if (ramEnabled) moel@370: Add(new RAM.RAMGroup(smbios, settings)); moel@370: moel@360: if (gpuEnabled) { moel@360: Add(new ATI.ATIGroup(settings)); moel@360: Add(new Nvidia.NvidiaGroup(settings)); moel@360: } moel@360: moel@360: if (fanControllerEnabled) { moel@360: Add(new TBalancer.TBalancerGroup(settings)); moel@360: Add(new Heatmaster.HeatmasterGroup(settings)); moel@360: } moel@28: moel@83: if (hddEnabled) moel@324: Add(new HDD.HarddriveGroup(settings)); moel@28: moel@28: open = true; moel@28: } moel@360: moel@360: public bool MainboardEnabled { moel@360: get { return mainboardEnabled; } moel@360: moel@360: [SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode = true)] moel@360: set { moel@360: if (open && value != mainboardEnabled) { moel@360: if (value) moel@370: Add(new Mainboard.MainboardGroup(smbios, settings)); moel@360: else moel@360: RemoveType(); moel@360: } moel@360: mainboardEnabled = value; moel@360: } moel@360: } moel@360: moel@360: public bool CPUEnabled { moel@360: get { return cpuEnabled; } moel@360: moel@360: [SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode = true)] moel@360: set { moel@360: if (open && value != cpuEnabled) { moel@360: if (value) moel@360: Add(new CPU.CPUGroup(settings)); moel@360: else moel@360: RemoveType(); moel@360: } moel@360: cpuEnabled = value; moel@360: } moel@360: } moel@360: moel@370: public bool RAMEnabled { moel@370: get { return ramEnabled; } moel@370: moel@370: [SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode = true)] moel@370: set { moel@370: if (open && value != ramEnabled) { moel@370: if (value) moel@370: Add(new RAM.RAMGroup(smbios, settings)); moel@370: else moel@370: RemoveType(); moel@370: } moel@370: ramEnabled = value; moel@370: } moel@370: } moel@370: moel@360: public bool GPUEnabled { moel@360: get { return gpuEnabled; } moel@360: moel@360: [SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode = true)] moel@360: set { moel@360: if (open && value != gpuEnabled) { moel@360: if (value) { moel@360: Add(new ATI.ATIGroup(settings)); moel@360: Add(new Nvidia.NvidiaGroup(settings)); moel@360: } else { moel@360: RemoveType(); moel@360: RemoveType(); moel@360: } moel@360: } moel@360: gpuEnabled = value; moel@360: } moel@360: } moel@360: moel@360: public bool FanControllerEnabled { moel@360: get { return fanControllerEnabled; } moel@360: moel@360: [SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode = true)] moel@360: set { moel@360: if (open && value != fanControllerEnabled) { moel@360: if (value) { moel@360: Add(new TBalancer.TBalancerGroup(settings)); moel@360: Add(new Heatmaster.HeatmasterGroup(settings)); moel@360: } else { moel@360: RemoveType(); moel@360: RemoveType(); moel@360: } moel@360: } moel@360: fanControllerEnabled = value; moel@360: } moel@360: } moel@360: moel@28: public bool HDDEnabled { moel@28: get { return hddEnabled; } moel@167: moel@167: [SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode = true)] moel@28: set { moel@360: if (open && value != hddEnabled) { moel@360: if (value) moel@360: Add(new HDD.HarddriveGroup(settings)); moel@360: else moel@360: RemoveType(); 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@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@360: Remove(group); moel@262: } moel@28: moel@236: Opcode.Close(); moel@236: Ring0.Close(); moel@167: moel@370: this.smbios = null; moel@370: 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: }