Changed the license to the Mozilla Public License 2.0 and update the licensing information.
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/.
7 Copyright (C) 2009-2011 Michael Möller <mmoeller@openhardwaremonitor.org>
12 using System.Collections.Generic;
13 using System.Globalization;
15 using System.Security.Permissions;
16 using System.Reflection;
18 namespace OpenHardwareMonitor.Hardware {
20 public class Computer : IComputer {
22 private readonly List<IGroup> groups = new List<IGroup>();
23 private readonly ISettings settings;
26 private bool hddEnabled;
29 this.settings = new Settings();
32 public Computer(ISettings settings) {
33 this.settings = settings ?? new Settings();
36 private void Add(IGroup group) {
37 if (groups.Contains(group))
42 if (HardwareAdded != null)
43 foreach (IHardware hardware in group.Hardware)
44 HardwareAdded(hardware);
47 private void Remove(IGroup group) {
48 if (!groups.Contains(group))
53 if (HardwareRemoved != null)
54 foreach (IHardware hardware in group.Hardware)
55 HardwareRemoved(hardware);
58 [SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode = true)]
66 Add(new Mainboard.MainboardGroup(settings));
67 Add(new CPU.CPUGroup(settings));
68 Add(new ATI.ATIGroup(settings));
69 Add(new Nvidia.NvidiaGroup(settings));
70 Add(new TBalancer.TBalancerGroup(settings));
71 Add(new Heatmaster.HeatmasterGroup(settings));
74 Add(new HDD.HarddriveGroup(settings));
79 public bool HDDEnabled {
80 get { return hddEnabled; }
82 [SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode = true)]
84 if (open && value && !hddEnabled) {
85 Add(new HDD.HarddriveGroup(settings));
86 } else if (open && !value && hddEnabled) {
87 List<IGroup> list = new List<IGroup>();
88 foreach (IGroup group in groups)
89 if (group is HDD.HarddriveGroup)
91 foreach (IGroup group in list)
98 public IHardware[] Hardware {
100 List<IHardware> list = new List<IHardware>();
101 foreach (IGroup group in groups)
102 foreach (IHardware hardware in group.Hardware)
104 return list.ToArray();
108 private static void NewSection(TextWriter writer) {
109 for (int i = 0; i < 8; i++)
110 writer.Write("----------");
115 private static int CompareSensor(ISensor a, ISensor b) {
116 int c = a.SensorType.CompareTo(b.SensorType);
118 return a.Index.CompareTo(b.Index);
123 private static void ReportHardwareSensorTree(
124 IHardware hardware, TextWriter w, string space)
126 w.WriteLine("{0}|", space);
127 w.WriteLine("{0}+- {1} ({2})",
128 space, hardware.Name, hardware.Identifier);
129 ISensor[] sensors = hardware.Sensors;
130 Array.Sort(sensors, CompareSensor);
131 foreach (ISensor sensor in sensors) {
132 w.WriteLine("{0}| +- {1,-14} : {2,8:G6} {3,8:G6} {4,8:G6} ({5})",
133 space, sensor.Name, sensor.Value, sensor.Min, sensor.Max,
136 foreach (IHardware subHardware in hardware.SubHardware)
137 ReportHardwareSensorTree(subHardware, w, "| ");
140 private static void ReportHardwareParameterTree(
141 IHardware hardware, TextWriter w, string space) {
142 w.WriteLine("{0}|", space);
143 w.WriteLine("{0}+- {1} ({2})",
144 space, hardware.Name, hardware.Identifier);
145 ISensor[] sensors = hardware.Sensors;
146 Array.Sort(sensors, CompareSensor);
147 foreach (ISensor sensor in sensors) {
148 string innerSpace = space + "| ";
149 if (sensor.Parameters.Length > 0) {
150 w.WriteLine("{0}|", innerSpace);
151 w.WriteLine("{0}+- {1} ({2})",
152 innerSpace, sensor.Name, sensor.Identifier);
153 foreach (IParameter parameter in sensor.Parameters) {
154 string innerInnerSpace = innerSpace + "| ";
155 w.WriteLine("{0}+- {1} : {2}",
156 innerInnerSpace, parameter.Name,
157 string.Format(CultureInfo.InvariantCulture, "{0} : {1}",
158 parameter.DefaultValue, parameter.Value));
162 foreach (IHardware subHardware in hardware.SubHardware)
163 ReportHardwareParameterTree(subHardware, w, "| ");
166 private static void ReportHardware(IHardware hardware, TextWriter w) {
167 string hardwareReport = hardware.GetReport();
168 if (!string.IsNullOrEmpty(hardwareReport)) {
170 w.Write(hardwareReport);
172 foreach (IHardware subHardware in hardware.SubHardware)
173 ReportHardware(subHardware, w);
176 public string GetReport() {
178 using (StringWriter w = new StringWriter(CultureInfo.InvariantCulture)) {
181 w.WriteLine("Open Hardware Monitor Report");
184 Version version = typeof(Computer).Assembly.GetName().Version;
187 w.Write("Version: "); w.WriteLine(version.ToString());
191 w.Write("Common Language Runtime: ");
192 w.WriteLine(Environment.Version.ToString());
193 w.Write("Operating System: ");
194 w.WriteLine(Environment.OSVersion.ToString());
195 w.Write("Process Type: ");
196 w.WriteLine(IntPtr.Size == 4 ? "32-Bit" : "64-Bit");
199 string r = Ring0.GetReport();
207 w.WriteLine("Sensors");
209 foreach (IGroup group in groups) {
210 foreach (IHardware hardware in group.Hardware)
211 ReportHardwareSensorTree(hardware, w, "");
216 w.WriteLine("Parameters");
218 foreach (IGroup group in groups) {
219 foreach (IHardware hardware in group.Hardware)
220 ReportHardwareParameterTree(hardware, w, "");
224 foreach (IGroup group in groups) {
225 string report = group.GetReport();
226 if (!string.IsNullOrEmpty(report)) {
231 IHardware[] hardwareArray = group.Hardware;
232 foreach (IHardware hardware in hardwareArray)
233 ReportHardware(hardware, w);
240 public void Close() {
244 while (groups.Count > 0) {
245 IGroup group = groups[groups.Count - 1];
256 public event HardwareEventHandler HardwareAdded;
257 public event HardwareEventHandler HardwareRemoved;
259 public void Accept(IVisitor visitor) {
261 throw new ArgumentNullException("visitor");
262 visitor.VisitComputer(this);
265 public void Traverse(IVisitor visitor) {
266 foreach (IGroup group in groups)
267 foreach (IHardware hardware in group.Hardware)
268 hardware.Accept(visitor);
271 private class Settings : ISettings {
273 public bool Contains(string name) {
277 public void SetValue(string name, string value) { }
279 public string GetValue(string name, string value) {
283 public void Remove(string name) { }