Changed a few types to internal (instead of public) in the Open Hardware Monitor library.
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-2012 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;
25 private SMBIOS smbios;
29 private bool mainboardEnabled;
30 private bool cpuEnabled;
31 private bool ramEnabled;
32 private bool gpuEnabled;
33 private bool fanControllerEnabled;
34 private bool hddEnabled;
37 this.settings = new Settings();
40 public Computer(ISettings settings) {
41 this.settings = settings ?? new Settings();
44 private void Add(IGroup group) {
45 if (groups.Contains(group))
50 if (HardwareAdded != null)
51 foreach (IHardware hardware in group.Hardware)
52 HardwareAdded(hardware);
55 private void Remove(IGroup group) {
56 if (!groups.Contains(group))
61 if (HardwareRemoved != null)
62 foreach (IHardware hardware in group.Hardware)
63 HardwareRemoved(hardware);
68 private void RemoveType<T>() where T : IGroup {
69 List<IGroup> list = new List<IGroup>();
70 foreach (IGroup group in groups)
73 foreach (IGroup group in list)
77 [SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode = true)]
82 this.smbios = new SMBIOS();
88 Add(new Mainboard.MainboardGroup(smbios, settings));
91 Add(new CPU.CPUGroup(settings));
94 Add(new RAM.RAMGroup(smbios, settings));
97 Add(new ATI.ATIGroup(settings));
98 Add(new Nvidia.NvidiaGroup(settings));
101 if (fanControllerEnabled) {
102 Add(new TBalancer.TBalancerGroup(settings));
103 Add(new Heatmaster.HeatmasterGroup(settings));
107 Add(new HDD.HarddriveGroup(settings));
112 public bool MainboardEnabled {
113 get { return mainboardEnabled; }
115 [SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode = true)]
117 if (open && value != mainboardEnabled) {
119 Add(new Mainboard.MainboardGroup(smbios, settings));
121 RemoveType<Mainboard.MainboardGroup>();
123 mainboardEnabled = value;
127 public bool CPUEnabled {
128 get { return cpuEnabled; }
130 [SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode = true)]
132 if (open && value != cpuEnabled) {
134 Add(new CPU.CPUGroup(settings));
136 RemoveType<CPU.CPUGroup>();
142 public bool RAMEnabled {
143 get { return ramEnabled; }
145 [SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode = true)]
147 if (open && value != ramEnabled) {
149 Add(new RAM.RAMGroup(smbios, settings));
151 RemoveType<RAM.RAMGroup>();
157 public bool GPUEnabled {
158 get { return gpuEnabled; }
160 [SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode = true)]
162 if (open && value != gpuEnabled) {
164 Add(new ATI.ATIGroup(settings));
165 Add(new Nvidia.NvidiaGroup(settings));
167 RemoveType<ATI.ATIGroup>();
168 RemoveType<Nvidia.NvidiaGroup>();
175 public bool FanControllerEnabled {
176 get { return fanControllerEnabled; }
178 [SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode = true)]
180 if (open && value != fanControllerEnabled) {
182 Add(new TBalancer.TBalancerGroup(settings));
183 Add(new Heatmaster.HeatmasterGroup(settings));
185 RemoveType<TBalancer.TBalancerGroup>();
186 RemoveType<Heatmaster.HeatmasterGroup>();
189 fanControllerEnabled = value;
193 public bool HDDEnabled {
194 get { return hddEnabled; }
196 [SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode = true)]
198 if (open && value != hddEnabled) {
200 Add(new HDD.HarddriveGroup(settings));
202 RemoveType<HDD.HarddriveGroup>();
208 public IHardware[] Hardware {
210 List<IHardware> list = new List<IHardware>();
211 foreach (IGroup group in groups)
212 foreach (IHardware hardware in group.Hardware)
214 return list.ToArray();
218 private static void NewSection(TextWriter writer) {
219 for (int i = 0; i < 8; i++)
220 writer.Write("----------");
225 private static int CompareSensor(ISensor a, ISensor b) {
226 int c = a.SensorType.CompareTo(b.SensorType);
228 return a.Index.CompareTo(b.Index);
233 private static void ReportHardwareSensorTree(
234 IHardware hardware, TextWriter w, string space)
236 w.WriteLine("{0}|", space);
237 w.WriteLine("{0}+- {1} ({2})",
238 space, hardware.Name, hardware.Identifier);
239 ISensor[] sensors = hardware.Sensors;
240 Array.Sort(sensors, CompareSensor);
241 foreach (ISensor sensor in sensors) {
242 w.WriteLine("{0}| +- {1,-14} : {2,8:G6} {3,8:G6} {4,8:G6} ({5})",
243 space, sensor.Name, sensor.Value, sensor.Min, sensor.Max,
246 foreach (IHardware subHardware in hardware.SubHardware)
247 ReportHardwareSensorTree(subHardware, w, "| ");
250 private static void ReportHardwareParameterTree(
251 IHardware hardware, TextWriter w, string space) {
252 w.WriteLine("{0}|", space);
253 w.WriteLine("{0}+- {1} ({2})",
254 space, hardware.Name, hardware.Identifier);
255 ISensor[] sensors = hardware.Sensors;
256 Array.Sort(sensors, CompareSensor);
257 foreach (ISensor sensor in sensors) {
258 string innerSpace = space + "| ";
259 if (sensor.Parameters.Length > 0) {
260 w.WriteLine("{0}|", innerSpace);
261 w.WriteLine("{0}+- {1} ({2})",
262 innerSpace, sensor.Name, sensor.Identifier);
263 foreach (IParameter parameter in sensor.Parameters) {
264 string innerInnerSpace = innerSpace + "| ";
265 w.WriteLine("{0}+- {1} : {2}",
266 innerInnerSpace, parameter.Name,
267 string.Format(CultureInfo.InvariantCulture, "{0} : {1}",
268 parameter.DefaultValue, parameter.Value));
272 foreach (IHardware subHardware in hardware.SubHardware)
273 ReportHardwareParameterTree(subHardware, w, "| ");
276 private static void ReportHardware(IHardware hardware, TextWriter w) {
277 string hardwareReport = hardware.GetReport();
278 if (!string.IsNullOrEmpty(hardwareReport)) {
280 w.Write(hardwareReport);
282 foreach (IHardware subHardware in hardware.SubHardware)
283 ReportHardware(subHardware, w);
286 public string GetReport() {
288 using (StringWriter w = new StringWriter(CultureInfo.InvariantCulture)) {
291 w.WriteLine("Open Hardware Monitor Report");
294 Version version = typeof(Computer).Assembly.GetName().Version;
297 w.Write("Version: "); w.WriteLine(version.ToString());
301 w.Write("Common Language Runtime: ");
302 w.WriteLine(Environment.Version.ToString());
303 w.Write("Operating System: ");
304 w.WriteLine(Environment.OSVersion.ToString());
305 w.Write("Process Type: ");
306 w.WriteLine(IntPtr.Size == 4 ? "32-Bit" : "64-Bit");
309 string r = Ring0.GetReport();
317 w.WriteLine("Sensors");
319 foreach (IGroup group in groups) {
320 foreach (IHardware hardware in group.Hardware)
321 ReportHardwareSensorTree(hardware, w, "");
326 w.WriteLine("Parameters");
328 foreach (IGroup group in groups) {
329 foreach (IHardware hardware in group.Hardware)
330 ReportHardwareParameterTree(hardware, w, "");
334 foreach (IGroup group in groups) {
335 string report = group.GetReport();
336 if (!string.IsNullOrEmpty(report)) {
341 IHardware[] hardwareArray = group.Hardware;
342 foreach (IHardware hardware in hardwareArray)
343 ReportHardware(hardware, w);
350 public void Close() {
354 while (groups.Count > 0) {
355 IGroup group = groups[groups.Count - 1];
367 public event HardwareEventHandler HardwareAdded;
368 public event HardwareEventHandler HardwareRemoved;
370 public void Accept(IVisitor visitor) {
372 throw new ArgumentNullException("visitor");
373 visitor.VisitComputer(this);
376 public void Traverse(IVisitor visitor) {
377 foreach (IGroup group in groups)
378 foreach (IHardware hardware in group.Hardware)
379 hardware.Accept(visitor);
382 private class Settings : ISettings {
384 public bool Contains(string name) {
388 public void SetValue(string name, string value) { }
390 public string GetValue(string name, string value) {
394 public void Remove(string name) { }